Git Product home page Git Product logo

ivanpinezhaninov / intervaltree Goto Github PK

View Code? Open in Web Editor NEW
28.0 28.0 5.0 88 KB

A red-black self-balancing interval tree

License: MIT License

CMake 3.71% C++ 96.29%
balanced-tree balanced-trees cplusplus cplusplus-11 cpp cpp11 header-only interval interval-tree intervals intervaltree red-black-tree red-black-trees redblacktree redblacktrees segment segment-tree segments segmenttree self-balancing-trees

intervaltree's People

Contributors

ivanpinezhaninov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

intervaltree's Issues

Doesn't work with const ValueType

    // 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;
    }

Inconsistent interval operators

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.

bool operator==(const Interval &other) const
{
return !(low < other.low || other.low < low
|| high < other.high || other.high < high
|| value < other.value || other.value < value);
}
bool operator<(const Interval &other) const
{
return (low < other.low || high < other.high);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.