#interval-tree #avl-tree #interval #tree #range #query #stabbing

interavl

An optimised interval tree for efficient interval stabbing

2 unstable releases

0.2.0 Aug 17, 2024
0.1.0 Jul 28, 2024

#695 in Data structures

Download history 6/week @ 2024-08-21 14/week @ 2024-09-11 11/week @ 2024-09-18 6/week @ 2024-09-25 1/week @ 2024-10-02 1/week @ 2024-10-09 1/week @ 2024-10-23 15/week @ 2024-10-30 23/week @ 2024-11-06 44/week @ 2024-11-13 35/week @ 2024-11-20 28/week @ 2024-11-27 35/week @ 2024-12-04

146 downloads per month

Apache-2.0

73KB
1.5K SLoC

crates.io docs.rs

Interavl

This crate implements an interval tree backed by an augmented, balanced binary search tree (an AVL tree - interavl - get it?!)

The IntervalTree maps half-open intervals to opaque values, and provides efficient querying of all stored (interval, value) tuples that match a variety of temporal relations described by Allen's interval algebra.

Use It

use interavl::IntervalTree;

// Initialise an empty tree.
let mut t = IntervalTree::default();

// Insert interval & value tuples into the tree.
//
// Intervals can be composed of any type that implements "Ord" such
// as timestamps, strings, etc.
t.insert(24..80, "Bob");
t.insert(10..100, "Doris");
t.insert(40..55, "Frank");
t.insert(100..400, "Nigel");

// Find which intervals overlap with a query interval:
for (interval, name) in t.iter_overlaps(&(42..50)) {
	println!("{name} overlaps (matching interval is {interval:?})");
}

Performance

Lookup operations against an IntervalTree are fast, executing against millions / billions of keys per second:

Tree Size Build Tree Lookup Value Stabbing Query
100 tuples 3µs 7ns 59ns
1,000 tuples 81µs 8ns 101ns
10,000 tuples 1ms 9ns 172ns

Interval stabbing and membership queries are particularly fast due to the use of subtree pruning to reduce the search space.[^filter-perf]

  • Build Tree: insert the N keys listed into an empty tree (inclusive of random value generation)
  • Lookup Value: find an interval in the tree and return the value for it
  • Stabbing Query: yield all intervals that overlap with a given query interval

The above measurements capture the single-threaded performance of operations against a tree containing varying numbers of keys on a M1 MacBook Pro.

The benchmarks to generate these numbers are included in this repo (run cargo bench).

[^filter-perf]: Interval stabbing filters ~53 billion intervals per second.

No runtime deps