All Projects → 5cript → interval-tree

5cript / interval-tree

Licence: CC0-1.0 license
A C++ header only interval tree implementation.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to interval-tree

rust-lapper
Rust implementation of a fast, easy, interval tree library nim-lapper
Stars: ✭ 39 (+2.63%)
Mutual labels:  interval-tree, interval
animate
👾 Create and manage animation functions with AnimationFrame API.
Stars: ✭ 11 (-71.05%)
Mutual labels:  interval
RangeTree
A generic interval tree implementation in C#
Stars: ✭ 144 (+278.95%)
Mutual labels:  interval-tree
algorithm-structure
2021年最新总结 500个常用数据结构,算法,算法导论,面试常用,大厂高级工程师整理总结
Stars: ✭ 590 (+1452.63%)
Mutual labels:  red-black-tree
Libft
42 library of basic C functions - queues, lists, memory operations and more 😄
Stars: ✭ 21 (-44.74%)
Mutual labels:  red-black-tree
BokkyPooBahsRedBlackTreeLibrary
BokkyPooBah's Red-Black Binary Search Tree Library
Stars: ✭ 117 (+207.89%)
Mutual labels:  red-black-tree
Fastrange
A fast alternative to the modulo reduction
Stars: ✭ 230 (+505.26%)
Mutual labels:  interval
task-bundle
Scheduling of tasks for symfony made simple
Stars: ✭ 33 (-13.16%)
Mutual labels:  interval
Intervals.jl
Non-iterable ranges
Stars: ✭ 29 (-23.68%)
Mutual labels:  intervals
RedBlackTree-An-Intuitive-Approach
Demystifying Red Black Trees
Stars: ✭ 26 (-31.58%)
Mutual labels:  red-black-tree
bbst-showdown
Fast AVL Trees & WAVL Trees in Java
Stars: ✭ 24 (-36.84%)
Mutual labels:  red-black-tree
Algorithms
Java implementation for Introduction to Algorithms book.
Stars: ✭ 58 (+52.63%)
Mutual labels:  red-black-tree
executor
Gnome Shell Extension - Execute multiple shell commands periodically with separate intervals and display the output in gnome top bar.
Stars: ✭ 45 (+18.42%)
Mutual labels:  interval
DSA
Data Structures and Algorithms
Stars: ✭ 13 (-65.79%)
Mutual labels:  interval-tree
cstl
STL style library with red-black tree implementation in C
Stars: ✭ 34 (-10.53%)
Mutual labels:  red-black-tree
lua-algorithms
Lua algorithms library that covers commonly used data structures and algorithms
Stars: ✭ 57 (+50%)
Mutual labels:  red-black-tree
Gods
GoDS (Go Data Structures). Containers (Sets, Lists, Stacks, Maps, Trees), Sets (HashSet, TreeSet, LinkedHashSet), Lists (ArrayList, SinglyLinkedList, DoublyLinkedList), Stacks (LinkedListStack, ArrayStack), Maps (HashMap, TreeMap, HashBidiMap, TreeBidiMap, LinkedHashMap), Trees (RedBlackTree, AVLTree, BTree, BinaryHeap), Comparators, Iterators, …
Stars: ✭ 10,883 (+28539.47%)
Mutual labels:  red-black-tree
ptScheduler
Pretty tiny Scheduler or ptScheduler is an Arduino library for writing non-blocking periodic tasks easily.
Stars: ✭ 14 (-63.16%)
Mutual labels:  intervals
numeric
numeric facilities for C++ 14; dual numbers, dual quaternions, constrained numbers, intervals
Stars: ✭ 21 (-44.74%)
Mutual labels:  interval
Lock-free-Red-black-tree
Implementation of lock-free red-black tree using CAS
Stars: ✭ 21 (-44.74%)
Mutual labels:  red-black-tree

interval-tree

A C++ header only interval tree implementation, which takes a red black tree as its base to inhibit degeneration to linked lists.

How an interval tree looks like:

ExampleTree

Example

// #include <interval-tree/draw.hpp> // to draw tree. this is not header only anymore.
#include <interval-tree/interval_tree.hpp>

int main()
{
  using namespace lib_interval_tree;

  // interval_tree <interval <int>>;
  interval_tree_t <int> tree;

  tree.insert(make_safe_interval<int>(21, 16)); // make_safe_interval swaps low and high if not in right order.
  tree.insert({8, 9});
  tree.insert({25, 30});
  tree.insert({5, 8});
  tree.insert({15, 23});
  tree.insert({17, 19});
  tree.insert({26, 26});
  tree.insert({0, 3});
  tree.insert({6, 10});
  tree.insert({19, 20});

  tree.deoverlap();
  
  for (auto const& i : tree)
  {
    std::cout << "[" << i.low() << ", " << i.high() << "]\n";
  }
}

Compile & Run Testing

Having googletest (find here on github) installed / built is a requirement to run the tests. Create a build folder, navigate there, run cmake and build the tree-tests target. You might have to adapt the linker line for gtest, if you built it yourself and didn't install it into your system. If you want to generate the pretty drawings, install cairo, pull the submodule and pass INT_TREE_DRAW_EXAMPLES=on to the cmake command line to generate a drawings/make_drawings executeable.

Free Functions

interval<NumericT, Kind> make_safe_interval(NumericT border1, NumericT border2)

Creates an interval where the borders are sorted so the lower border is the first one.

Members of IntervalTree

iterator insert(interval_type const& ival)

Adds an interval into the tree.

Parameters

  • ival An interval

Returns: An iterator to the inserted element.


iterator insert_overlap(interval_type const& ival)

Inserts an interval into the tree if no other interval overlaps it. Otherwise merge the interval with the one being overlapped.

Parameters

  • ival An interval
  • exclusive Exclude borders from overlap check. Defaults to false.
  • mergeSetOverlapping If the result of interval::join is a collection of intervals, shall each be inserted with more overlap searches? Defaults to false

Returns: An iterator to the inserted element.


iterator erase(iterator iter)

Removes the interval given by iterator from the tree. (does not invalidate iterators).

Parameters

  • iter A valid non-end iterator

Returns: An iterator to the next element.


size_type size() const

Returns the amount of nodes in the tree.

Returns: The amount of tree nodes.


(const)iterator find(interval_type const& ival)

Finds the first interval in the interval tree that has an exact match. WARNING: There is no special handling for floats.

Parameters

  • ival The interval to find.

Returns: An iterator to the found element, or std::end(tree).


(const)iterator find(interval_type const& ival, CompareFunctionT const& compare)

Finds the first interval in the interval tree that has the following statement evaluate to true: compare(interval_in_tree, ival); Allows for propper float comparisons.

Parameters

  • ival The interval to find.
  • compare The compare function to compare intervals with. Function is called like so: compare(interval_in_tree, ival).

Returns: An iterator to the found element, or std::end(tree).


(const)iterator find_all(interval_type const& ival, OnFindFunctionT const& on_find)

Find all intervals in the tree matching ival.

Parameters

  • ival The interval to find.
  • on_find A function of type bool(iterator) that is called when an interval was found. Return true to continue, false to preemptively abort search.

Example

tree.insert({3, 7});
tree.insert({3, 7});
tree.insert({8, 9});
tree.find_all({3, 7}, [](auto iter) /* iter will be const_iterator if tree is const */ {
  // will find all intervals that are exactly {3,7} here.
  return true; // continue
});

Returns: An iterator to the found element, or std::end(tree).


(const)iterator find_all(interval_type const& ival, OnFindFunctionT const& on_find, CompareFunctionT const& compare)

Find all intervals in the tree that the compare function returns true for.

Parameters

  • ival The interval to find.
  • compare The compare function to compare intervals with. Function is called like so: compare(interval_in_tree, ival).
  • on_find A function of type bool(iterator) that is called when an interval was found. Return true to continue, false to preemptively abort search.

Returns: An iterator to the found element, or std::end(tree).


(const)iterator find_next_in_subtree(iterator from, interval_type const& ival)

Finds the next exact match EXCLUDING from in the subtree originating from "from". You cannot find all matches this way, use find_all for that.

Parameters

  • from The iterator to start from. (including this iterator!)
  • ival The interval to find.

Returns: An iterator to the found element, or std::end(tree).


(const)iterator find_next_in_subtree(iterator from, interval_type const& ival, CompareFunctionT const& compare)

Finds the next exact match EXCLUDING from in the subtree originating from "from". You cannot find all matches this way, use find_all for that.

Parameters

  • from The iterator to start from (including this iterator!)
  • ival The interval to find.
  • compare The compare function to compare intervals with. Function is called like so: compare(interval_in_tree, ival).

Returns: An iterator to the found element, or std::end(tree).


(const)iterator overlap_find(interval_type const& ival, bool exclusive)

Finds the first interval in the interval tree that overlaps the given interval.

Parameters

  • ival The interval to find an overlap for.
  • exclusive Exclude borders from overlap check. Defaults to false.

Returns: An iterator to the found element, or std::end(tree).


(const)iterator overlap_find_all(interval_type const& ival, OnFindFunctionT const& on_find, bool exclusive)

Finds the first interval in the interval tree that overlaps the given interval.

Parameters

  • ival The interval to find an overlap for.
  • on_find A function of type bool(iterator) that is called when an interval was found. Return true to continue, false to preemptively abort search.
  • exclusive Exclude borders from overlap check. Defaults to false.

Example

tree.insert({0, 5});
tree.insert({5, 10});
tree.insert({10, 15});
tree.overlap_find_all({5, 5}, [](auto iter) /* iter will be const_iterator if tree is const */ {
  // called with {0, 5} and {5, 10} in unspecified order.
  return true; // continue
});

Returns: An iterator to the found element, or std::end(tree).


(const)iterator overlap_find_next_in_subtree(interval_type const& ival, bool exclusive)

Finds the next interval in the subtree originating in ival that overlaps the given interval. You cannot find all matches this way, use overlap_find_all for that.

Parameters

  • ival The interval to find an overlap for.
  • exclusive Exclude borders from overlap check. Defaults to false.

Returns: An iterator to the found element, or std::end(tree).


interval_tree& deoverlap()

Merges all overlapping intervals within the tree. After calling deoverlap, the tree will only contain disjoint intervals.

Returns: *this

After deoverlap

AfterDeoverlap

interval_tree deoverlap_copy()

Same as deoverlap, but not inplace


interval_tree punch(interval_type const& ival)

Removes all intervals from ival and produces a tree that contains the remaining intervals. The tree must be deoverlapped, or the result is undefined. ival is expected to encompass the entire interval range.

Returns: A new interval_tree containing the gaps.

After punching (with [0, 50])

AfterPunch


interval_tree punch()

Same as punch(interval_type const& ival), but with ival = [lowest_lower_bound, highest_upper_bound], resulting in only the gaps between existing intervals.


bool empty() const noexcept

Returns whether or not the tree is empty.

Returns: Is this tree empty?


iterator begin()

Returns the iterator of the interval with the lowest lower_bound.

Returns: begin iterator.


iterator end()

Returns a past the end iterator.

Returns: past the end iterator.


iterator cbegin()

Returns the const_iterator of the interval with the lowest lower_bound.

Returns: begin iterator.


iterator cend()

Returns a past the end const_iterator.

Returns: past the end const_iterator.


Members of Interval

You can implement your own interval if you provide all the same functions.

using value_type

The underlying interval numerical type

using interval_kind

The interval kind. You dont need to provides this typedef in your interval class.

friend bool operator==(interval const& lhs, interval const& other)

Comparison operator.

friend bool operator!=(interval const& lhs, interval const& other)

Comparison operator.

value_type low() const

Lower bound.

value_type high() const

Upper bound.

bool overlaps(value_type l, value_type h) const

Overlap these bounds with this interval (closed)?

bool overlaps_exclusive(value_type l, value_type h) const

Overlap these bounds with this interval excluding borders?

bool overlaps(interval const& other) const

Like overlaps with lower and upper bound.

bool overlaps_exclusive(interval const& other) const

Like overlaps with lower and upper bound.

bool within(value_type value) const

Is the value within the interval (closed)?

bool within(interval const& other) const

Is the interval within the interval?

value_type operator-(interval const& other) const

Calculates the distance between the two intervals. Overlapping intervals have 0 distance.

value_type size() const

Returns high - low.

interval join(interval const& other) const

Joins 2 intervals and whatever is inbetween.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].