1 #ifndef ROSE_RANGEMAP_H
2 #define ROSE_RANGEMAP_H
14 #ifndef __STDC_FORMAT_MACROS
15 #define __STDC_FORMAT_MACROS
49 typedef std::pair<Range, Range>
Pair;
81 explicit Range(
const Other &other)
180 return std::make_pair(left, right);
210 std::vector<Range> retval;
212 retval.push_back(*
this);
539 template<
class Other>
545 assert(!my_range.empty());
552 assert(new_end>my_range.first() && new_end<=my_range.last());
570 assert(!my_range.empty() && !other_range.empty());
579 assert(my_range.contains(
Range(new_end)));
583 void print(std::ostream &o)
const {}
596 template<
class R,
class T>
621 assert(!my_range.empty());
626 assert(new_end>my_range.first() && new_end<=my_range.last());
631 assert(!my_range.empty() && !other_range.empty());
632 return get()==other_value.
get();
637 assert(my_range.contains(
Range(new_end)));
663 template<
class R,
class T>
694 assert(!my_range.
empty());
699 assert(new_end>my_range.
first() && new_end<=my_range.
last());
704 assert(!my_range.
empty() && !other_range.
empty());
705 return get()==other_value.
get();
718 virtual void print(std::ostream &o)
const {
837 template<
class R,
class T=RangeMapVo
id<R> >
849 return a.last() < b.last();
855 typedef std::map<Range, Value, RangeCompare>
Map;
873 template<
class 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);
942 if (ei==
end() ||
Range(addr).left_of(ei->first))
948 if (ei==
end() ||
Range(addr).left_of(ei->first))
973 if (lb!=
end() && lb->first.begins_before(
Range(addr),
false))
983 if (lb!=
end() && lb->first.begins_before(
Range(addr),
false))
999 if (ri->first.size()==
size)
1001 if (ri->first.size()>size && (best==
end() || ri->first.size()<best->first.size()))
1009 if (ri->first.size()==
size)
1011 if (ri->first.size()>size && (best==
end() || ri->first.size()<best->first.size()))
1024 if (ri->first.size()>=
size)
1031 if (ri->first.size()>=
size)
1061 retval += ei->first.size();
1068 return ranges.begin()->first.first();
1074 return ranges.rbegin()->first.last();
1098 ei->second.removing(ei->first);
1116 if (erase_range.
empty())
1123 Value &v = ei->second;
1124 if (erase_range.
contains(found_range)) {
1126 if (erase_begin==
end())
1129 }
else if (erase_range.
contained_in(found_range,
true)) {
1131 assert(erase_begin==
end());
1134 insertions[rt.second] = v.
split(found_range, rt.second.
first());
1137 insertions[lt.first] = v;
1138 }
else if (erase_range.
begins_after(found_range,
true)) {
1140 assert(erase_begin==
end());
1144 insertions[halves.first] = v;
1145 }
else if (erase_range.
ends_before(found_range,
true)) {
1147 if (erase_begin==
end())
1150 insertions[halves.second] = v.
split(found_range, halves.second.
first());
1156 if (erase_begin!=
end())
1157 ranges.erase(erase_begin, ei);
1158 ranges.insert(insertions.begin(), insertions.end());
1159 #ifdef RANGEMAP_CHECK
1166 template<
class OtherMap>
1168 assert((
const void*)&other!=(
const void*)
this);
1169 for (
typename OtherMap::const_iterator ri=other.begin(); ri!=other.end(); ++ri)
1179 if (new_range.
empty())
1186 if (found!=
end() && new_range.
overlaps(found->first))
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);
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);
1203 #ifdef RANGEMAP_CHECK
1219 insert(ri->first, ri->second, make_hole);
1264 assert(need.
overlaps(found->first));
1270 assert(!
"should not be reached");
1315 while (ia!=stop && ib!=x.
end() && ia->first.distinct(ib->first)) {
1316 while (ia!=stop && ia->first.left_of(ib->first))
1318 while (ib!=x.
end() && ib->first.left_of(ia->first))
1322 return ia!=stop && ib!=x.
end() && ia->first.overlaps(ib->first);
1330 while (ia!=stop && ib!=x.
end() && ia->first.distinct(ib->first)) {
1331 while (ia!=stop && ia->first.left_of(ib->first))
1333 while (ib!=x.
end() && ib->first.left_of(ia->first))
1337 return ia!=stop && ib!=x.
end() && ia->first.overlaps(ib->first) ? ia :
end();
1347 template<
class ResultMap>
1349 return invert_within<ResultMap>(
Range::all());
1354 template<
class ResultMap>
1361 if (pending < ri->first.first())
1362 retval.insert(
Range::inin(pending, ri->first.first()-1));
1363 pending = ri->first.last()+1;
1365 if (pending <= limits.
last())
1367 if (!retval.empty())
1368 assert(retval.minmax().contained_in(limits,
false));
1376 if (!selector.
empty()) {
1379 retval.
insert(ri->first, ri->second);
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, " "); \
1464 #undef RANGEMAP_CHECK
1472 std::ostringstream s;
1474 o <<(ri==
begin()?
"":
", ") <<ri->first;
1475 if (!s.str().empty())
1476 o <<
" {" <<s.str() <<
"}";