mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-08 05:09:10 -07:00
36 lines
758 B
Rust
36 lines
758 B
Rust
use std::collections::BTreeMap;
|
|
|
|
use brk_vec::{IndexedVec, StoredIndex, StoredType};
|
|
|
|
#[derive(Debug)]
|
|
pub struct RangeMap<I, T>(BTreeMap<I, T>);
|
|
|
|
impl<I, T> RangeMap<I, T>
|
|
where
|
|
I: StoredIndex,
|
|
T: StoredIndex,
|
|
{
|
|
pub fn get(&self, key: I) -> Option<&T> {
|
|
self.0.range(..=key).next_back().map(|(&min, value)| {
|
|
if min > key {
|
|
unreachable!()
|
|
}
|
|
value
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<I, T> From<&IndexedVec<I, T>> for RangeMap<T, I>
|
|
where
|
|
I: StoredIndex,
|
|
T: StoredIndex + StoredType,
|
|
{
|
|
fn from(vec: &IndexedVec<I, T>) -> Self {
|
|
Self(
|
|
vec.into_iter()
|
|
.map(|(i, v)| (v.into_owned(), i))
|
|
.collect::<BTreeMap<_, _>>(),
|
|
)
|
|
}
|
|
}
|