ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rangemap.h
Go to the documentation of this file.
1 #ifndef ROSE_RANGEMAP_H
2 #define ROSE_RANGEMAP_H
3 
4 /* This file defines four main template classes:
5  * - RangeMap: Time and space efficient storage of non-overlapping Range objects each associated with an arbitrary value.
6  * - Range: Contiguous set of integers defined by the starting value and size.
7  * - RangeMapVoid: Void value for RangeMap classes that don't store any useful value (just the ranges themselves).
8  * - RangeMapValue: Class for storing simple values in a RangeMap.
9  */
10 
11 
12 /* There is no need to include "sage3basic.h"; this file defines all it needs. */
13 
14 #ifndef __STDC_FORMAT_MACROS
15 #define __STDC_FORMAT_MACROS
16 #endif
17 #include <inttypes.h>
18 
19 #include <cassert>
20 #include <cmath>
21 #include <cstdlib>
22 #include <iostream>
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
28 /* Define this if you want the class to do fairly extensive testing of the consistency of the map after every operation. Note
29  * that this substantially increases execution time. The NDEBUG preprocessor symbol must not be defined, or else the check()
30  * method won't really do anything. */
31 //#define RANGEMAP_CHECK
32 
33 /******************************************************************************************************************************
34  * Range<>
35  ******************************************************************************************************************************/
36 
45 template<class T>
46 class Range {
47 public:
48  typedef T Value;
49  typedef std::pair<Range, Range> Pair;
51 protected:
55 public:
59  : r_first(1), r_last(0) {} // see clear()
60 
62  explicit Range(const Value &first)
63  : r_first(first), r_last(first) {}
64 
69  Range(const Value &first, const Value &size)
70  : r_first(first), r_last(first+size-1) {
71  if (0==size) {
72  r_last = first; // or else clear() is a no-op leaving invalid values
73  clear();
74  } else {
75  assert(!empty()); // checks for overflow
76  }
77  }
78 
80  template<class Other>
81  explicit Range(const Other &other)
82  : r_first(other.relaxed_first()), r_last(other.relaxed_last()) {}
83 
88  static Range inin(const Value &v1, const Value &v2) {
89  assert(v1<=v2);
90  Range retval;
91  retval.first(v1);
92  retval.last(v2);
93  return retval;
94  }
95 
99  void first(const Value &first) {
100  r_first = first;
101  }
102  const Value first() const {
103  assert(!empty());
104  return r_first;
105  }
106  const Value relaxed_first() const {
107  if (!empty())
108  return first();
109  if (1==r_first-r_last)
110  return r_first-1;
111  return r_first;
112  }
118  void last(const Value &last) {
119  r_last = last;
120  }
121  const Value last() const {
122  assert(!empty());
123  return r_last;
124  }
125  const Value relaxed_last() const {
126  return empty() ? relaxed_first() : last();
127  }
138  Value size() const {
139  if (empty())
140  return 0;
141  return r_last + 1 - r_first;
142  }
143 
153  void resize(const Value &new_size) {
154  assert(!empty());
155  if (0==new_size) {
156  clear();
157  } else {
158  r_last = r_first + new_size - 1;
159  assert(!empty()); // this one is to check for overflow of r_last
160  }
161  }
162  void relaxed_resize(const Value &new_size) {
163  if (0==new_size) {
164  clear();
165  } else {
167  r_last = r_first + new_size - 1;
168  assert(!empty()); // this one is to check for overflow of r_last
169  }
170  }
175  Pair split_range_at(const Value &at) const {
176  assert(!empty());
177  assert(at>first() && at<=last()); // neither half can be empty
178  Range left(first(), at-first());
179  Range right(at, last()+1-at);
180  return std::make_pair(left, right);
181  }
182 
186  Range join(const Range &right) const {
187  if (empty())
188  return right;
189  if (right.empty())
190  return *this;
191  assert(abuts_lt(right));
192  return Range::inin(first(), right.last());
193  }
194 
209  std::vector<Range> erase(const Range &to_erase) const {
210  std::vector<Range> retval;
211  if (to_erase.empty() || distinct(to_erase)) {
212  retval.push_back(*this);
213  } else if (contained_in(to_erase)) {
214  // void
215  } else {
216  if (begins_before(to_erase))
217  retval.push_back(Range::inin(first(), to_erase.first()-1));
218  if (ends_after(to_erase))
219  retval.push_back(Range::inin(to_erase.last()+1, last()));
220  }
221  return retval;
222  }
223 
225  Range intersect(const Range &other) const {
226  if (empty() || contains(other))
227  return other;
228  if (other.empty() || other.contains(*this))
229  return *this;
230  if (!overlaps(other))
231  return Range();
232  return Range::inin(
233  std::max(first(), other.first()),
234  std::min(last(), other.last()));
235  }
236 
240  bool empty() const {
241  return r_last<r_first; // can't use first() or last() here because they're not valid for empty ranges.
242  }
243 
246  void clear() {
247  if (!empty()) {
248  if (r_first<maximum()) {
249  r_first = r_first + 1;
250  r_last = r_first - 1;
251  } else {
252  r_last = r_first - 2;
253  }
254  assert(empty());
255  }
256  }
257 
261  bool begins_with(const Range &x) const {
262  if (empty() || x.empty())
263  return false;
264  return first() == x.first();
265  }
266 
270  bool ends_with(const Range &x) const {
271  if (empty() || x.empty())
272  return false;
273  return last() == x.last();
274  }
275 
279  bool begins_after(const Range &x, bool strict=true) const {
280  if (empty() || x.empty())
281  return false;
282  return strict ? first() > x.first() : first() >= x.first();
283  }
284 
288  bool begins_before(const Range &x, bool strict=true) const {
289  if (empty() || x.empty())
290  return false;
291  return strict ? first() < x.first() : first() <= x.first();
292  }
293 
297  bool ends_after(const Range &x, bool strict=true) const {
298  if (empty() || x.empty())
299  return false;
300  return strict ? last() > x.last() : last() >= x.last();
301  }
302 
306  bool ends_before(const Range &x, bool strict=true) const {
307  if (empty() || x.empty())
308  return false;
309  return strict ? last() < x.last() : last() <= x.last();
310  }
311 
317  bool contains(const Range &x, bool strict=false) const {
318  if (empty() || x.empty())
319  return false;
320  return strict ? x.first()>first() && x.last()<last() : x.first()>=first() && x.last()<=last();
321  }
322 
328  bool contained_in(const Range &x, bool strict=false) const {
329  if (empty() || x.empty())
330  return false;
331  return strict ? first()>x.first() && last()<x.last() : first()>=x.first() && last()<=x.last();
332  }
333 
337  bool congruent(const Range &x) const {
338  if (empty() || x.empty())
339  return empty() && x.empty();
340  return first()==x.first() && last()==x.last();
341  }
342 
347  bool left_of(const Range &x) const {
348  if (empty() || x.empty())
349  return false;
350  return last() < x.first();
351  }
352 
357  bool right_of(const Range &x) const {
358  if (empty() || x.empty())
359  return false;
360  return first() > x.last();
361  }
362 
366  bool overlaps(const Range &x) const {
367  if (empty() || x.empty())
368  return false;
369  return !left_of(x) && !right_of(x);
370  }
371 
376  bool distinct(const Range &x) const {
377  if (empty() || x.empty())
378  return true;
379  return !overlaps(x);
380  }
381 
386  bool abuts_lt(const Range &x) const {
387  if (empty() || x.empty())
388  return false;
389  return last()+1 == x.first();
390  }
391 
396  bool abuts_gt(const Range &x) const {
397  if (empty() || x.empty())
398  return false;
399  return first() == x.last()+1;
400  }
401 
403  static Value minimum() {
404  return 0; // FIXME
405  }
406 
408  static Value maximum() {
409  return (Value)(-1); // FIXME
410  }
411 
413  static Range all() {
414  return Range::inin(minimum(), maximum());
415  }
416 
417  bool operator==(const Range &x) const {
418  return congruent(x);
419  }
420  bool operator!=(const Range &x) const {
421  return !congruent(x);
422  }
423 
424  void print(std::ostream &o) const {
425  if (empty()) {
426  o <<"<empty>";
427  } else if (first()==last()) {
428  o <<first();
429  } else {
430  o <<first() <<".." <<last();
431  }
432  }
433 
434  friend std::ostream& operator<<(std::ostream &o, const Range &x) {
435  x.print(o);
436  return o;
437  }
438 };
439 
440 /******************************************************************************************************************************
441  * Specializations for Range<double>
442  ******************************************************************************************************************************/
443 
444 template<>
446 
447 template<>
448 bool
449 Range<double>::empty() const;
450 
451 template<>
452 void
454 
455 template<>
456 const double
458 
459 template<>
460 double
461 Range<double>::size() const;
462 
463 template<>
464 void
465 Range<double>::resize(const double &new_size);
466 
467 template<>
468 void
469 Range<double>::relaxed_resize(const double &new_size);
470 
471 template<>
473 Range<double>::split_range_at(const double &at) const;
474 
475 template<>
476 double
478 
479 template<>
480 double
482 
483 /******************************************************************************************************************************
484  * Specializations for Range<float>
485  ******************************************************************************************************************************/
486 
487 template<>
489 
490 template<>
491 bool
492 Range<float>::empty() const;
493 
494 template<>
495 void
497 
498 template<>
499 const float
501 
502 template<>
503 float
504 Range<float>::size() const;
505 
506 template<>
507 void
508 Range<float>::resize(const float &new_size);
509 
510 template<>
511 void
512 Range<float>::relaxed_resize(const float &new_size);
513 
514 template<>
516 Range<float>::split_range_at(const float &at) const;
517 
518 template<>
519 float
521 
522 template<>
523 float
525 
526 /******************************************************************************************************************************
527  * RangeMap void values
528  ******************************************************************************************************************************/
529 
532 template<class R>
534 public:
535  typedef R Range;
536 
538 
539  template<class Other>
540  explicit RangeMapVoid(const Other&) {}
541 
544  void removing(const Range &my_range) {
545  assert(!my_range.empty());
546  }
547 
551  void truncate(const Range &my_range, const typename Range::Value &new_end) {
552  assert(new_end>my_range.first() && new_end<=my_range.last());
553  }
554 
569  bool merge(const Range &my_range, const Range &other_range, const RangeMapVoid &other_value) {
570  assert(!my_range.empty() && !other_range.empty());
571  return true;
572  }
573 
578  RangeMapVoid split(const Range &my_range, const typename Range::Value &new_end) {
579  assert(my_range.contains(Range(new_end)));
580  return RangeMapVoid();
581  }
582 
583  void print(std::ostream &o) const {}
584  friend std::ostream& operator<<(std::ostream &o, const RangeMapVoid &x) {
585  x.print(o);
586  return o;
587  }
588 };
589 
590 /******************************************************************************************************************************
591  * RangeMap numeric values
592  ******************************************************************************************************************************/
593 
596 template<class R, class T>
597 class RangeMapNumeric /*final*/ {
598 public:
599  typedef R Range;
600  typedef T Value;
601 
604 
606  RangeMapNumeric(Value v): value(v) {} // implicit
607 
610  void set(Value v) /*final*/ {
611  value = v;
612  }
613  virtual Value get() const /*final*/ {
614  return value;
615  }
620  void removing(const Range &my_range) /*final*/ {
621  assert(!my_range.empty());
622  }
623 
625  void truncate(const Range &my_range, const typename Range::Value &new_end) /*final*/ {
626  assert(new_end>my_range.first() && new_end<=my_range.last());
627  }
628 
630  bool merge(const Range &my_range, const Range &other_range, RangeMapNumeric other_value) /*final*/ {
631  assert(!my_range.empty() && !other_range.empty());
632  return get()==other_value.get();
633  }
634 
636  RangeMapNumeric split(const Range &my_range, typename Range::Value new_end) /*final*/ {
637  assert(my_range.contains(Range(new_end)));
638  return *this;
639  }
640 
643  void print(std::ostream &o) const /*final*/ {
644  o <<value;
645  }
646  friend std::ostream& operator<<(std::ostream &o, const RangeMapNumeric &x) {
647  x.print(o);
648  return o;
649  }
652 private:
654 };
655 
656 /******************************************************************************************************************************
657  * RangeMap simple values
658  ******************************************************************************************************************************/
659 
663 template<class R, class T>
665 public:
666  typedef R Range;
667  typedef T Value;
668 
671 
673  RangeMapValue(const Value &v) { // implicit
674  value = v;
675  }
676 
677  /* This class often serves as a base class, so we have some virtual methods. */
678  virtual ~RangeMapValue() {}
679 
680 
683  virtual void set(const Value &v) {
684  value = v;
685  }
686  virtual Value get() const {
687  return value;
688  }
693  virtual void removing(const Range &my_range) {
694  assert(!my_range.empty());
695  }
696 
698  virtual void truncate(const Range &my_range, const typename Range::Value &new_end) {
699  assert(new_end>my_range.first() && new_end<=my_range.last());
700  }
701 
703  bool merge(const Range &my_range, const Range &other_range, const RangeMapValue &other_value) {
704  assert(!my_range.empty() && !other_range.empty());
705  return get()==other_value.get();
706  }
707 
708 #if 0 /* Must be implemented in the subclass due to return type. */
709 
710  RangeMapValue split(const Range &my_range, const typename Range::Value &new_end) {
711  assert(my_range.contains(Range(new_end)));
712  return *this;
713  }
714 #endif
715 
718  virtual void print(std::ostream &o) const {
719  o <<value;
720  }
721  friend std::ostream& operator<<(std::ostream &o, const RangeMapValue &x) {
722  x.print(o);
723  return o;
724  }
727 protected:
729 };
730 
731 /******************************************************************************************************************************
732  * RangeMap<>
733  ******************************************************************************************************************************/
734 
837 template<class R, class T=RangeMapVoid<R> >
838 class RangeMap {
839 public:
840  typedef R Range;
841  typedef T Value;
843 protected:
844  /* The keys of the underlying map are sorted by their last value rather than beginning value. This allows us to use the
845  * map's lower_bound() method to find the range to which an address might belong. Since the ranges in the map are
846  * non-overlapping, sorting by ending address has the same effect as sorting by starting address. */
847  struct RangeCompare {
848  bool operator()(const Range &a, const Range &b) const {
849  return a.last() < b.last();
850  }
851  };
852 
853  typedef std::pair<Range, Range> RangePair;
854  typedef std::pair<Range, Value> MapPair;
855  typedef std::map<Range, Value, RangeCompare> Map;
857 
858 public:
859  typedef typename Map::iterator iterator;
860  typedef typename Map::const_iterator const_iterator;
861  typedef typename Map::reverse_iterator reverse_iterator;
862  typedef typename Map::const_reverse_iterator const_reverse_iterator;
863 
864  /**************************************************************************************************************************
865  * Constructors
866  **************************************************************************************************************************/
867 public:
868 
870  RangeMap() {}
871 
873  template<class Other>
874  explicit RangeMap(const Other &other) {
875  for (typename Other::const_iterator ri=other.begin(); ri!=other.end(); ++ri) {
876  Range new_range(ri->first);
877  Value new_value(ri->second);
878  insert(new_range, new_value);
879  }
880  }
881 
882  /**************************************************************************************************************************
883  * Iterators and searching
884  **************************************************************************************************************************/
885 public:
886 
892  return ranges.begin();
893  }
895  return ranges.begin();
896  }
904  return ranges.end();
905  }
906  const_iterator end() const {
907  return ranges.end();
908  }
916  return ranges.rbegin();
917  }
919  return ranges.rbegin();
920  }
929  return ranges.rend();
930  }
932  return ranges.rend();
933  }
940  iterator find(const typename Range::Value &addr) {
941  iterator ei = lower_bound(addr);
942  if (ei==end() || Range(addr).left_of(ei->first))
943  return end();
944  return ei;
945  }
946  const_iterator find(const typename Range::Value &addr) const {
947  const_iterator ei = lower_bound(addr);
948  if (ei==end() || Range(addr).left_of(ei->first))
949  return end();
950  return ei;
951  }
958  iterator lower_bound(const typename Range::Value &addr) {
959  return ranges.lower_bound(Range(addr));
960  }
961  const_iterator lower_bound(const typename Range::Value &addr) const {
962  return ranges.lower_bound(Range(addr));
963  }
969  iterator find_prior(const typename Range::Value &addr) {
970  if (empty())
971  return end();
972  iterator lb = lower_bound(addr);
973  if (lb!=end() && lb->first.begins_before(Range(addr), false/*non-strict*/))
974  return lb;
975  if (lb==begin())
976  return end();
977  return --lb;
978  }
979  const_iterator find_prior(const typename Range::Value &addr) const {
980  if (empty())
981  return end();
982  const_iterator lb = lower_bound(addr);
983  if (lb!=end() && lb->first.begins_before(Range(addr), false/*non-strict*/))
984  return lb;
985  if (lb==begin())
986  return end();
987  return --lb;
988  }
996  iterator best_fit(const typename Range::Value &size, iterator start) {
997  iterator best = end();
998  for (iterator ri=start; ri!=end(); ++ri) {
999  if (ri->first.size()==size)
1000  return ri;
1001  if (ri->first.size()>size && (best==end() || ri->first.size()<best->first.size()))
1002  best = ri;
1003  }
1004  return best;
1005  }
1006  const_iterator best_fit(const typename Range::Value &size, const_iterator start) const {
1007  const_iterator best = end();
1008  for (const_iterator ri=start; ri!=end(); ++ri) {
1009  if (ri->first.size()==size)
1010  return ri;
1011  if (ri->first.size()>size && (best==end() || ri->first.size()<best->first.size()))
1012  best = ri;
1013  }
1014  return best;
1015  }
1022  iterator first_fit(const typename Range::Value &size, iterator start) {
1023  for (iterator ri=start; ri!=end(); ++ri) {
1024  if (ri->first.size()>=size)
1025  return ri;
1026  }
1027  return end();
1028  }
1030  for (const_iterator ri=start; ri!=end(); ++ri) {
1031  if (ri->first.size()>=size)
1032  return ri;
1033  }
1034  return end();
1035  }
1038  /**************************************************************************************************************************
1039  * Capacity
1040  **************************************************************************************************************************/
1041 public:
1042 
1044  bool empty() const {
1045  return ranges.empty();
1046  }
1047 
1050  size_t nranges() const {
1051  return ranges.size();
1052  }
1053 
1058  typename Range::Value size() const {
1059  typename Range::Value retval = 0;
1060  for (const_iterator ei=begin(); ei!=end(); ++ei)
1061  retval += ei->first.size();
1062  return retval;
1063  }
1064 
1066  typename Range::Value min() const {
1067  assert(!empty());
1068  return ranges.begin()->first.first();
1069  }
1070 
1072  typename Range::Value max() const {
1073  assert(!empty());
1074  return ranges.rbegin()->first.last();
1075  }
1076 
1078  Range minmax() const {
1079  typename Range::Value lt=min(), rt=max();
1080  return Range::inin(lt, rt);
1081  }
1082 
1083  /**************************************************************************************************************************
1084  * Low-level support functions
1085  **************************************************************************************************************************/
1086 protected:
1087 
1088  /**************************************************************************************************************************
1089  * Modifiers
1090  **************************************************************************************************************************/
1091 public:
1092 
1095  void clear(bool notify=true) {
1096  if (notify) {
1097  for (iterator ei=begin(); ei!=end(); ++ei)
1098  ei->second.removing(ei->first);
1099  }
1100  ranges.clear();
1101  }
1102 
1107  void erase(const Range &erase_range) {
1108  /* This loop figures out what needs to be removed and calls the elements' removing(), truncate(), etc. methods but does
1109  * not actually erase them from the underlying map yet. We must not erase them yet because the std::map::erase()
1110  * method doesn't return any iterator. Instead, we create a list (via an iterator range) of elements that will need to
1111  * be erased from the underlying map.
1112  *
1113  * This loop also creates a list of items that need to be inserted into the underlying map. Even though the
1114  * std::map::insert() can return an iterator, we can't call it inside the loop because then our erasure iterators will
1115  * become invalid. */
1116  if (erase_range.empty())
1117  return;
1118  Map insertions;
1119  iterator erase_begin=end();
1120  iterator ei;
1121  for (ei=lower_bound(erase_range.first()); ei!=end() && !erase_range.left_of(ei->first); ++ei) {
1122  Range found_range = ei->first;
1123  Value &v = ei->second;
1124  if (erase_range.contains(found_range)) {
1125  /* Erase entire found range. */
1126  if (erase_begin==end())
1127  erase_begin = ei;
1128  v.removing(found_range);
1129  } else if (erase_range.contained_in(found_range, true/*strict*/)) {
1130  /* Erase middle of found range. */
1131  assert(erase_begin==end());
1132  erase_begin = ei;
1133  RangePair rt = found_range.split_range_at(erase_range.last()+1);
1134  insertions[rt.second] = v.split(found_range, rt.second.first());
1135  RangePair lt = rt.first.split_range_at(erase_range.first());
1136  v.truncate(rt.first, erase_range.first());
1137  insertions[lt.first] = v;
1138  } else if (erase_range.begins_after(found_range, true/*strict*/)) {
1139  /* Erase right part of found range. */
1140  assert(erase_begin==end());
1141  erase_begin = ei;
1142  RangePair halves = found_range.split_range_at(erase_range.first());
1143  v.truncate(found_range, erase_range.first());
1144  insertions[halves.first] = v;
1145  } else if (erase_range.ends_before(found_range, true/*strict*/)) {
1146  /* Erase left part of found range. */
1147  if (erase_begin==end())
1148  erase_begin = ei;
1149  RangePair halves = found_range.split_range_at(erase_range.last()+1);
1150  insertions[halves.second] = v.split(found_range, halves.second.first());
1151  v.removing(halves.first);
1152  }
1153  }
1154 
1155  /* Inserting is easy here because we already know that no merging is necessary. */
1156  if (erase_begin!=end())
1157  ranges.erase(erase_begin, ei);
1158  ranges.insert(insertions.begin(), insertions.end());
1159 #ifdef RANGEMAP_CHECK
1160  check();
1161 #endif
1162  }
1163 
1166  template<class OtherMap>
1167  void erase_ranges(const OtherMap &other) {
1168  assert((const void*)&other!=(const void*)this);
1169  for (typename OtherMap::const_iterator ri=other.begin(); ri!=other.end(); ++ri)
1170  erase(Range::inin(ri->first.first(), ri->first.last()));
1171  }
1172 
1178  iterator insert(Range new_range, Value new_value=Value(), bool make_hole=true) {
1179  if (new_range.empty())
1180  return end();
1181 
1182  if (make_hole) {
1183  erase(new_range);
1184  } else {
1185  iterator found = lower_bound(new_range.first());
1186  if (found!=end() && new_range.overlaps(found->first))
1187  return end();
1188  }
1189 
1190  /* Attempt to merge with a left and/or right value. */
1191  iterator left = new_range.first()>Range::minimum() ? find(new_range.first()-1) : end();
1192  if (left!=end() && new_range.abuts_gt(left->first) && new_value.merge(new_range, left->first, left->second)) {
1193  new_range = left->first.join(new_range);
1194  ranges.erase(left);
1195  }
1196  iterator right = new_range.last()<new_range.maximum() ? find(new_range.last()+1) : end();
1197  if (right!=end() && new_range.abuts_lt(right->first) && new_value.merge(new_range, right->first, right->second)) {
1198  new_range = new_range.join(right->first);
1199  ranges.erase(right);
1200  }
1201 
1202  iterator retval = ranges.insert(end(), std::make_pair(new_range, new_value));
1203 #ifdef RANGEMAP_CHECK
1204  check();
1205 #endif
1206  return retval;
1207  }
1208 
1210  void insert_ranges(const RangeMap &x, bool make_hole=true) {
1211  assert(&x!=this);
1212  insert_ranges(x.begin(), x.end(), make_hole);
1213  }
1214 
1217  void insert_ranges(const_iterator start, const_iterator stop, bool make_hole=true) {
1218  for (const_iterator ri=start; ri!=stop; ri++)
1219  insert(ri->first, ri->second, make_hole);
1220  }
1221 
1222  /**************************************************************************************************************************
1223  * Predicates
1224  **************************************************************************************************************************/
1225 public:
1226 
1228  bool overlaps(const RangeMap &x) const {
1229  return first_overlap(x)!=end();
1230  }
1231 
1234  bool overlaps(const Range &r) const {
1235  if (r.empty())
1236  return false;
1237  const_iterator found = lower_bound(r.first());
1238  return found!=end() && r.overlaps(found->first);
1239  }
1240 
1243  bool distinct(const Range &r) const {
1244  return !overlaps(r);
1245  }
1246 
1249  bool distinct(const RangeMap &x) const {
1250  return first_overlap(x)==end();
1251  }
1252 
1255  bool contains(Range need) const {
1256  if (need.empty())
1257  return true;
1258  const_iterator found=find(need.first());
1259  while (1) {
1260  if (found==end())
1261  return false;
1262  if (need.begins_before(found->first))
1263  return false;
1264  assert(need.overlaps(found->first));
1265  if (need.ends_before(found->first, false/*non-strict*/))
1266  return true;
1267  need = need.split_range_at(found->first.last()+1).second;
1268  ++found;
1269  }
1270  assert(!"should not be reached");
1271  return true;
1272  }
1273 
1276  bool contains(const RangeMap &x) const {
1277  if (x.empty())
1278  return true;
1279  for (const_iterator xi=x.begin(); xi!=x.end(); ++xi) {
1280  if (!contains(xi->first))
1281  return false;
1282  }
1283  return true;
1284  }
1285 
1286  /**************************************************************************************************************************
1287  * Comparisons
1288  **************************************************************************************************************************/
1289 public:
1290 
1296  return find_overlap(begin(), end(), x);
1297  }
1299  return find_overlap(begin(), end(), x);
1300  }
1310  if (start==stop)
1311  return end();
1312 
1313  iterator ia = start;
1314  const_iterator ib = x.lower_bound(start->first.first());
1315  while (ia!=stop && ib!=x.end() && ia->first.distinct(ib->first)) {
1316  while (ia!=stop && ia->first.left_of(ib->first))
1317  ++ia;
1318  while (ib!=x.end() && ib->first.left_of(ia->first))
1319  ++ib;
1320  }
1321 
1322  return ia!=stop && ib!=x.end() && ia->first.overlaps(ib->first);
1323  }
1325  if (start==stop)
1326  return end();
1327 
1328  const_iterator ia = start;
1329  const_iterator ib = x.lower_bound(start->first.first());
1330  while (ia!=stop && ib!=x.end() && ia->first.distinct(ib->first)) {
1331  while (ia!=stop && ia->first.left_of(ib->first))
1332  ++ia;
1333  while (ib!=x.end() && ib->first.left_of(ia->first))
1334  ++ib;
1335  }
1336 
1337  return ia!=stop && ib!=x.end() && ia->first.overlaps(ib->first) ? ia : end();
1338  }
1341  /**************************************************************************************************************************
1342  * Operators
1343  **************************************************************************************************************************/
1344 public:
1345 
1347  template<class ResultMap>
1348  ResultMap invert() const {
1349  return invert_within<ResultMap>(Range::all());
1350  }
1351 
1354  template<class ResultMap>
1355  ResultMap invert_within(const Range &limits) const {
1356  ResultMap retval;
1357  if (limits.empty())
1358  return retval;
1359  typename Range::Value pending = limits.first();
1360  for (const_iterator ri=lower_bound(limits.first()); ri!=end() && !limits.left_of(ri->first); ++ri) {
1361  if (pending < ri->first.first())
1362  retval.insert(Range::inin(pending, ri->first.first()-1));
1363  pending = ri->first.last()+1;
1364  }
1365  if (pending <= limits.last())
1366  retval.insert(Range::inin(pending, limits.last()));
1367  if (!retval.empty())
1368  assert(retval.minmax().contained_in(limits, false));
1369  return retval;
1370  }
1371 
1374  RangeMap select_overlapping_ranges(const Range &selector) const {
1375  RangeMap retval;
1376  if (!selector.empty()) {
1377  for (const_iterator ri=lower_bound(selector.start()); ri!=end() && !selector.left_of(ri->first); ++ri) {
1378  if (selector.overlaps(ri->first))
1379  retval.insert(ri->first, ri->second);
1380  }
1381  }
1382  return retval;
1383  }
1384 
1385  /**************************************************************************************************************************
1386  * Debugging
1387  **************************************************************************************************************************/
1388 public:
1389 
1390  void check() const {
1391 // DQ (11/8/2011): Commented out as part of ROSE compiling this ROSE source code (EDG frontend complains about errors).
1392 #ifndef USE_ROSE
1393 #ifndef NDEBUG
1394 #define RANGEMAP_CHECK(EXPR) if (!(EXPR)) { \
1395  std::cerr <<"RangeMap::check() failed at r1=" <<r1 <<" r2=" <<r2 <<": " #EXPR "\n"; \
1396  std::cerr <<"Entire range map at point of failure:\n"; \
1397  print(std::cerr, " "); \
1398  assert(EXPR); \
1399  }
1400 
1401  for (const_iterator i1=begin(); i1!=end(); ++i1) {
1402  const Range &r1 = i1->first;
1403  const_iterator i2 = i1; ++i2;
1404  if (i2!=end()) {
1405  const Range &r2 = i2->first;
1406 
1407  RANGEMAP_CHECK(!r2.empty());
1408 
1409  RANGEMAP_CHECK(!r1.begins_with(r2));
1410  RANGEMAP_CHECK(!r2.begins_with(r1));
1411 
1412  RANGEMAP_CHECK(!r1.ends_with(r2));
1413  RANGEMAP_CHECK(!r2.ends_with(r1));
1414 
1415  RANGEMAP_CHECK(!r1.begins_after(r2, false));
1416  RANGEMAP_CHECK(!r1.begins_after(r2, true));
1417  RANGEMAP_CHECK(r2.begins_after(r1, false));
1418  RANGEMAP_CHECK(r2.begins_after(r1, true));
1419 
1420  RANGEMAP_CHECK(r1.begins_before(r2, false));
1421  RANGEMAP_CHECK(r1.begins_before(r2, true));
1422  RANGEMAP_CHECK(!r2.begins_before(r1, false));
1423  RANGEMAP_CHECK(!r2.begins_before(r1, true));
1424 
1425  RANGEMAP_CHECK(!r1.ends_after(r2, false));
1426  RANGEMAP_CHECK(!r1.ends_after(r2, true));
1427  RANGEMAP_CHECK(r2.ends_after(r1, false));
1428  RANGEMAP_CHECK(r2.ends_after(r1, true));
1429 
1430  RANGEMAP_CHECK(r1.ends_before(r2, false));
1431  RANGEMAP_CHECK(r1.ends_before(r2, true));
1432  RANGEMAP_CHECK(!r2.ends_before(r1, false));
1433  RANGEMAP_CHECK(!r2.ends_before(r1, true));
1434 
1435  RANGEMAP_CHECK(!r1.contains(r2, false));
1436  RANGEMAP_CHECK(!r1.contains(r2, true));
1437  RANGEMAP_CHECK(!r2.contains(r1, false));
1438  RANGEMAP_CHECK(!r2.contains(r1, true));
1439 
1440  RANGEMAP_CHECK(!r1.contained_in(r2, false));
1441  RANGEMAP_CHECK(!r1.contained_in(r2, true));
1442  RANGEMAP_CHECK(!r2.contained_in(r1, false));
1443  RANGEMAP_CHECK(!r2.contained_in(r1, true));
1444 
1445  RANGEMAP_CHECK(!r1.congruent(r2));
1446  RANGEMAP_CHECK(!r2.congruent(r1));
1447 
1448  RANGEMAP_CHECK(r1.left_of(r2));
1449  RANGEMAP_CHECK(!r2.left_of(r1));
1450 
1451  RANGEMAP_CHECK(!r1.right_of(r2));
1452  RANGEMAP_CHECK(r2.right_of(r1));
1453 
1454  RANGEMAP_CHECK(!r1.overlaps(r2));
1455  RANGEMAP_CHECK(!r2.overlaps(r1));
1456 
1457  RANGEMAP_CHECK(r1.distinct(r2));
1458  RANGEMAP_CHECK(r2.distinct(r1));
1459 
1460  RANGEMAP_CHECK(!r1.abuts_gt(r2)); // r1.abuts_lt(r2) is possible
1461  RANGEMAP_CHECK(!r2.abuts_lt(r1)); // r2.abuts_gt(r1) is possible
1462  }
1463  }
1464 #undef RANGEMAP_CHECK
1465 #endif
1466 #endif
1467  }
1468 
1470  void print(std::ostream &o) const {
1471  for (const_iterator ri=begin(); ri!=end(); ++ri) {
1472  std::ostringstream s;
1473  s <<ri->second;
1474  o <<(ri==begin()?"":", ") <<ri->first;
1475  if (!s.str().empty())
1476  o <<" {" <<s.str() <<"}";
1477  }
1478  }
1479 
1480  friend std::ostream& operator<<(std::ostream &o, const RangeMap &rmap) {
1481  rmap.print(o);
1482  return o;
1483  }
1484 
1485 };
1486 
1487 #endif