Skip to content

Commit

Permalink
data: /intervals/ for structures for intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
rpitasky committed Nov 18, 2024
1 parent 33613e8 commit 2c4d1cc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
25 changes: 25 additions & 0 deletions ti-basic-optimizer/src/data/intervals/interval_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! # Interval Tree
//! Interval Tree implementation
//!
//! This just does a linear scan for stabbing queries. This is a great target for optimizing the compiler.
use std::ops::Range;

#[derive(Debug)]
pub struct IntervalTree<T: Ord + Clone> {
data: Vec<Range<T>>,
}

impl<T: Ord + Clone> IntervalTree<T> {
pub fn new(mut ranges: Vec<Range<T>>) -> Self {
Self { data: ranges }
}

pub fn stab(&self, point: T) -> Vec<Range<T>> {
self.data
.iter()
.filter(|&range| range.contains(&point))
.cloned()
.collect()
}
}
5 changes: 5 additions & 0 deletions ti-basic-optimizer/src/data/intervals/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod interval_tree;
pub use interval_tree::IntervalTree;

pub mod partition_map;
pub use partition_map::PartitionMap;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! Map from non-overlapping half-open `[begin, end)` subsets of a range to arbitrary
//! data. The subsets must cover the entire set.
/// Map from non-overlapping half-open `[begin, end)` subsets of a range to arbitrary
/// data. The subsets must cover the entire set.
#[derive(Debug)]
pub struct PartitionMap<K: Ord + Sized, V: Sized> {
pub breaks: Vec<K>,
pub values: Vec<V>,
Expand Down
2 changes: 1 addition & 1 deletion ti-basic-optimizer/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod graphviz;

pub mod graph_like;

pub mod partition_map;
pub mod intervals;

0 comments on commit 2c4d1cc

Please sign in to comment.