C++ - interval tree implementation

前端 未结 5 755
情话喂你
情话喂你 2020-12-01 05:35

Does someone know any good interval tree implementation in C++?

Obviously, something template-driven, better in boost-like style.<

5条回答
  •  悲哀的现实
    2020-12-01 05:46

    I had exactly the same need. I couldn't find any suitable (simple, modern, portable) implementations, so I used a python implementation by Brent Pedersen as a guide and wrote a barebones C++ version. The IntervalTree behaves like a standard STL container, with some caveats due to its simplicity (no iterators, for instance). You use it like this ("T" is an arbitrary type):

    vector > intervals;
    // ... make intervals!
    IntervalTree tree(intervals);
    

    And you query it like this:

    vector > results;
    tree.findContained(start, stop, results);
    // results now contains Intervals which are fully contained in the query interval
    results.clear();
    tree.findOverlapping(start, stop, results);
    // results now contains Intervals which overlap the query interval
    

提交回复
热议问题