Iset
Rust crates with map and set with interval keys (ranges x..y).
Install / Use
/learn @tprodanov/IsetREADME
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.
Related Skills
node-connect
347.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
