`TreeIndex` get the nearest two keys
enhancement
Tree datastructures are great for finding entries closest to a key. I'm not familiar with the internal structure of SCC, but would it be possible to add a method for this?
Maybe something like:
```rust
/// Result of [`TreeIndex::peek_nearest`] and [`TreeIndex::peek_nearest_with`].
pub enum Nearest<T> {
/// The [`TreeIndex`] was empty.
Empty,
// TODO: Depending on the use case, this might need to be Exact(T, T, T).
// But then you would also need ExactAndSmaller and ExactAndGreater.
/// There was an exact match for the key.
Exact(T),
/// The key is inbetweeen these keys
Nearest(T, T),
/// There is only a key smaller than the key.
Smaller(T),
/// There is only a key greater than the key.
Greater(T),
}
impl<K, V> TreeIndex<K, V>
where
K: 'static + Clone + Ord,
V: 'static + Clone {
/// Returns a guarded reference to the nearest values for the specified key without acquiring locks.
///
/// The returned reference can survive as long as the associated Guard is alive.
pub fn peek_nearest<'g, Q>(&self, key: &Q, guard: &'g Guard) -> Nearest<(&'g K, &'g V)>
where
Q: Comparable<K> + ?Sized, { todo!() }
/// Peeks at the nearest key-value pairs without acquiring locks.
///
/// The `reader` is always called, even if the index is empty.
pub fn peek_nearest_with<Q, R, F: FnOnce(Nearest<(&K, &V)>) -> R>(
&self,
key: &Q,
reader: F,
) -> R
where
Q: Comparable<K> + ?Sized { todo!() }
}
```
It can also be really useful to get the first and last key of the index.
6 条评论