mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-11 06:39:08 -07:00
computer: convert stores to vecs part 1
This commit is contained in:
35
crates/brk_computer/src/stateful/range_map.rs
Normal file
35
crates/brk_computer/src/stateful/range_map.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
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<_, _>>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user