Crates.io | unbounded-interval-tree |
lib.rs | unbounded-interval-tree |
version | 1.1.2 |
source | src |
created_at | 2019-11-08 17:50:30.712009 |
updated_at | 2022-10-22 00:28:29.266255 |
description | An interval tree working with inclusive/exclusive bounds, as well as unbounded intervals. Provides helpers to fetch overlapping intervals, and difference of intervals. |
homepage | |
repository | https://github.com/jonathanGB/unbounded-interval-tree |
max_upload_size | |
id | 179439 |
size | 66,995 |
A Rust implementation of an interval tree, based on the one described by Cormen et al., (2009), Introduction to Algorithms (3rd ed., Section 14.3: Interval trees, pp. 348–354). An interval tree is useful to query efficiently a database of intervals. This implementation is generic in that it works with intervals of values implementing Ord+Clone
traits. The bounds can be inclusive, exclusive, or unbounded. Here are some examples of valid intervals:
I would suggest to look at the examples part of the documentation (as they are tested by the Rust ecosystem), but here's a current example.
use unbounded_interval_tree::IntervalTree;
use std::ops::Bound::{Included, Excluded, Unbounded};
// Default interval tree.
let mut tree = IntervalTree::default();
// Ranges are defined as a 2-ple of Bounds.
let interval1 = (Included(5), Excluded(9));
let interval2 = (Unbounded, Included(-2));
let interval3 = (Included(30), Included(30));
// Add intervals to the tree.
tree.insert(interval1);
tree.insert(interval2);
tree.insert(interval3);
// Iterate through the intervals inorder.
for (start, end) in tree.iter() {
println!("Start: {:?}\tEnd: {:?}", start, end);
}
// Get overlapping intervals.
let overlaps = tree.get_interval_overlaps(&(0..30));
// Get the difference between the database
// of intervals and the query interval.
let diff = tree.get_interval_difference(&(0..=30));
What's next...
delete
branch).
remove_random_leaf
method to the API. Removing leaves is significantly simpler with this data structure, hence I started by tackling this problem.