use core::borrow::Borrow;
use core::ops::{Index, IndexMut};
use super::OSBTreeMap;
use crate::Rank;
impl<K: Clone + Ord, V> OSBTreeMap<K, V> {
#[must_use]
pub fn get_by_rank(&self, rank: usize) -> Option<(&K, &V)> {
self.raw.get_by_rank(rank)
}
#[must_use]
pub fn get_by_rank_mut(&mut self, rank: usize) -> Option<(&K, &mut V)> {
self.raw.get_by_rank_mut(rank)
}
#[must_use]
pub fn rank_of<Q>(&self, key: &Q) -> Option<usize>
where
K: Borrow<Q>,
Q: ?Sized + Ord,
{
self.raw.rank_of(key)
}
}
impl<K: Clone + Ord, V> Index<Rank> for OSBTreeMap<K, V> {
type Output = V;
fn index(&self, rank: Rank) -> &Self::Output {
self.get_by_rank(rank.0).map(|(_, v)| v).expect("index out of bounds")
}
}
impl<K: Clone + Ord, V> IndexMut<Rank> for OSBTreeMap<K, V> {
fn index_mut(&mut self, rank: Rank) -> &mut Self::Output {
self.get_by_rank_mut(rank.0).map(|(_, v)| v).expect("index out of bounds")
}
}