+ vector_sort (ivs, Interval::left_less);
+
+ Interval_set ret;
+
+ if (ivs.empty ())
+ return ret;
+
+ ret.intervals_.push_back (ivs.front ());
+
+ // Go over the intervals from left to right. If the current interval
+ // overlaps with the last one, merge them. Otherwise, append the
+ // current interval to the list.
+ for (vsize i = 1; i < ivs.size (); ++i)
+ {
+ Interval iv = ivs[i];
+ Interval &last = ret.intervals_.back ();
+
+ if (last[RIGHT] >= iv[LEFT])
+ // overlapping intervals: merge them
+ last[RIGHT] = max (last[RIGHT], iv[RIGHT]);
+ else if (!iv.is_empty ())
+ ret.intervals_.push_back (iv);
+ }
+
+ return ret;
+}
+
+// Returns an iterator pointing to the first interval whose left
+// endpoint is at least x. That interval may or may not contain x.
+vector<Interval>::const_iterator
+Interval_set::upper_bound (Real x) const
+{
+ Interval xx (x, x);
+ return std::upper_bound (intervals_.begin (), intervals_.end (), xx, Interval::left_less);