c++11 - A container for integer intervals, such as RangeSet, for C++ -
i trying work ranges, in ranges of numbers. mean integer intervals, in maths speak. , want store set of them. want set naturally merge (or coalesce) ranges insert.
let's go simple example, start empty set: { }
- i insert range [0,5], have { [0,5] }
- i insert range [10,15], have { [0,5], [10,15] }
- i insert range [5,7], have { [0,7], [10,15] }
- i insert range [12,17], have { [0,7], [10,17] }
- i insert range [6,13], have { [0,17] }
i found out thanks similar question exists in java as google guava library , called rangeset.
i thinking of using std::set
of std::pair
s sorted on lower bound (so first element of each pair). after each insertion have manually merge overlapping sets.
as seems common problem, implementation couldn't find due noise synonyms of "range" in c++ ? or care share own? want print final ranges bonus points generality if have other set operations.
if encode ranges sequence of endpoints , step direction, instead of start/end pairs, finding union should become easier, simple mergesort.
(0, +) (5, -) (0, +) (5, -) (10, +) (15, -) (0, +) (5, +) (5, -) (7, -) (10, +) (15, -)
look, overlapping range appears nested ranges. preserve outermost ones.
(0, +) (5, +) (5, -) (7, -) (10, +) (15, -) 1 2 2 1 1 1 <= depth (0, +) (7, -) (10, +) (15, -) (0, +) (7, -) (10, +) (12, +) (15, -) (17, -) 1 1 1 2 2 1 (0, +) (7, -) (10, +) (17, -) (0, +) (6, +) (7, -) (10, +) (13, -) (17, -) 1 2 2 2 2 1 (0, +) (17, -)
i think finding intersections becomes simple also, preserve endpoints nesting level of 2, instead of deleting them.
Comments
Post a Comment