C++ - interval tree implementation

2019-01-08 18:20发布

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

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

And another question - if somebody tested, does a basic std::vector-based interval tree implementation with sorting can beat the generic interval tree (with O(lg) operations) in practice?

5条回答
疯言疯语
2楼-- · 2019-01-08 18:44

if you don't mind translating a c# implementation to c++, goto http://code.google.com/p/intervaltree/ .based on an avl self balancing tree.

查看更多
何必那么认真
3楼-- · 2019-01-08 18:50

I uploaded simple implementation of Interval Tree to github: https://github.com/coolsoftware/ITree

Look class itree in itree.h.

查看更多
做自己的国王
4楼-- · 2019-01-08 19:01

Boost-like ? Boost ICL!

The Boost Interval Container Library

查看更多
在下西门庆
5楼-- · 2019-01-08 19:06

There appears to be one in the NCBI C++ Toolkit.

Jury's still out on whether it's "good," though (and even whether it's template-driven; I'm still somewhat new to C++, so I'm not entirely sure that it is, but I suspect as much).

查看更多
何必那么认真
6楼-- · 2019-01-08 19:08

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<Interval<T> > intervals;
// ... make intervals!
IntervalTree<T> tree(intervals);

And you query it like this:

vector<Interval<T> > 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
查看更多
登录 后发表回答