9 releases

0.2.2 Apr 5, 2023
0.2.1 Apr 5, 2023
0.1.1 Jan 28, 2022
0.0.5 Jun 18, 2021
0.0.3 Mar 20, 2020

#137 in Data structures

Download history 2731/week @ 2023-12-14 1820/week @ 2023-12-21 1109/week @ 2023-12-28 2065/week @ 2024-01-04 2828/week @ 2024-01-11 2487/week @ 2024-01-18 2005/week @ 2024-01-25 3381/week @ 2024-02-01 2408/week @ 2024-02-08 3391/week @ 2024-02-15 3344/week @ 2024-02-22 2669/week @ 2024-02-29 3403/week @ 2024-03-07 4085/week @ 2024-03-14 2498/week @ 2024-03-21 2417/week @ 2024-03-28

12,753 downloads per month
Used in 24 crates (5 directly)

MIT license

135KB
2.5K SLoC

This crates implements map and set with interval keys (ranges x..y). IntervalMap is implemented using red-black binary tree, where each node contains information about the smallest start and largest end in its subtree. The tree takes O(N) space and allows insertion, removal and search in O(log N). IntervalMap allows to search for all entries overlapping a query (interval or a point, output would be sorted by keys) in O(log N + K) where K is the size of the output.

IntervalSet is a newtype over IntervalMap with empty values.

Usage

The following code constructs a small interval map and search for intervals/values overlapping various queries.

use iset::interval_map;

let mut map = interval_map!{ 20..30 => 'a', 15..25 => 'b', 10..20 => 'c' };
assert_eq!(map.insert(10..20, 'd'), Some('c'));
assert_eq!(map.insert(5..15, 'e'), None);

// Iterator over all pairs (range, value). Output is sorted.
let a: Vec<_> = map.iter(..).collect();
assert_eq!(a, &[(5..15, &'e'), (10..20, &'d'), (15..25, &'b'), (20..30, &'a')]);

// Iterate over intervals that overlap query (..20 here). Output is sorted.
let b: Vec<_> = map.intervals(..20).collect();
assert_eq!(b, &[5..15, 10..20, 15..25]);

assert_eq!(map[15..25], 'b');
// Replace 15..25 => 'b' into 'z'.
*map.get_mut(15..25).unwrap() = 'z';

// Iterate over values that overlap query (20.. here). Output is sorted by intervals.
let c: Vec<_> = map.values(20..).collect();
assert_eq!(c, &[&'z', &'a']);

// Remove 10..20 => 'd'.
assert_eq!(map.remove(10..20), Some('d'));

println!("{:?}", map);
// {5..15 => 'e', 15..25 => 'z', 20..30 => 'a'}

You can find more detailed usage here.

Changelog

You can find changelog here.

Issues

Please submit issues here or send them to timofey.prodanov[at]gmail.com.

Dependencies

~180KB