}
/**
- * TODO: Think of a better than O(n^2) implementation.
- * Possibly move to cache result
+ *
*/
std::vector<Interval> findIntervalsAtPoints(const vector<Interval>& intervals){
- vector<Interval> points;
- points.push_back(Interval(intervals.size(),intervals.size())); //Dummy interval to match point and index
- for (size_t p = 1; p <= intervals.size(); ++p){
- size_t f = 0;
- size_t s = 0;
- bool found = false;
- size_t n = 0;
- for (vector<Interval>::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){
- if (i->first == p){
- if (!found){
- f = n;
- found = true;
- }else{
- s = n;
- break;
- }
- }
- if (i->second == p){
- if (!found){
- f = n;
- found = true;
- }else{
- s = n;
- break;
- }
- }
- }
- points.push_back(Interval(f,s));
+ size_t max = intervals.size()+1;
+ vector<Interval> points(max,Interval(max,max));
+ size_t n = 0;
+ for (vector<Interval>::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){
+ if (points[i->first].first == max){
+ points[i->first].first = n;
+ }else
+ points[i->first].second = n;
+
+ if (points[i->second].first == max){
+ points[i->second].first = n;
+ }else
+ points[i->second].second = n;
}
return points;