ivanpinezhaninov / intervaltree Goto Github PK
View Code? Open in Web Editor NEWA red-black self-balancing interval tree
License: MIT License
A red-black self-balancing interval tree
License: MIT License
// Create an interval tree
// Intervals::IntervalTree<int, int> intervalTree; // Work fine
Intervals::IntervalTree<int, const int> intervalTree; /* Doesn't work
error: use of deleted function ‘Intervals::Interval<int, const int>& Intervals::Interval<int, const int>::operator=(Intervals::Interval<int, const int>&&)’
*__position = std::forward<_Arg>(__arg);
error: use of deleted function ‘Intervals::Interval<int, const int>& Intervals::Interval<int, const int>::operator=(Intervals::Interval<int, const int>&&)’
*--__result = std::move(*--__last);
*/
// Insert intervals to the tree
intervalTree.insert({ 20, 30, 1 });
intervalTree.insert({ 40, 60, 2 });
intervalTree.insert({ 70, 90, 3 });
intervalTree.insert({ 60, 70, 4 });
intervalTree.insert({ 40, 90, 5 });
intervalTree.insert({ 80, 90, 6 });
// Find intervals
auto overlappingIntervals = intervalTree.findOverlappingIntervals({ 50, 80 });
auto innerIntervals = intervalTree.findInnerIntervals({ 50, 80 });
auto outerIntervals = intervalTree.findOuterIntervals({ 50, 80 });
auto intervalsContainPoint = intervalTree.findIntervalsContainPoint(50);
// Print all intervals
std::cout << "All intervals:" << std::endl;
for (auto interval : intervalTree.intervals()) {
std::cout << interval << std::endl;
}
std::cout << std::endl;
// Print overlapping intervals
std::cout << "Overlapping intervals:" << std::endl;
for (auto interval : overlappingIntervals) {
std::cout << interval << std::endl;
}
std::cout << std::endl;
// Print inner intervals
std::cout << "Inner intervals:" << std::endl;
for (auto interval : innerIntervals) {
std::cout << interval << std::endl;
}
std::cout << std::endl;
// Print outer intervals
std::cout << "Outer intervals:" << std::endl;
for (auto interval : outerIntervals) {
std::cout << interval << std::endl;
}
std::cout << std::endl;
// Print intervals contain the point
std::cout << "Intervals contain the point:" << std::endl;
for (auto interval : intervalsContainPoint) {
std::cout << interval << std::endl;
}
I wonder why is the less-than operator implemented such as to ignore the value, while the value is used in the equality operator? Strictly speaking types that implement the concept of equality need not necessarily implement less-than and vice-versa. It seems that the goal was to implement both operator via less-than over the fields.
The reason I'm asking this is practical: When adding Interval
objects to sets or maps the behavior is different to adding them to an IntervalTree
.
IntervalTree/include/intervaltree.hpp
Lines 108 to 119 in 1e4541b
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.