2022-02-21 06:14:13 +00:00
|
|
|
#[cfg(feature = "map_first_last")]
|
|
|
|
pub trait BTreeMapExt<K, V> {
|
|
|
|
fn first_key(&self) -> Option<&K>;
|
|
|
|
fn last_key(&self) -> Option<&K>;
|
|
|
|
fn pop_first_value(&mut self) -> Option<V>;
|
|
|
|
fn pop_last_value(&mut self) -> Option<V>;
|
|
|
|
}
|
2022-02-21 05:14:48 +00:00
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
#[cfg(feature = "map_first_last")]
|
|
|
|
impl<K: Ord + Clone, V> BTreeMapExt<K, V> for std::collections::BTreeMap<K, V> {
|
|
|
|
fn first_key(&self) -> Option<&K> {
|
|
|
|
self.keys().next()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn last_key(&self) -> Option<&K> {
|
|
|
|
self.keys().next_back()
|
|
|
|
}
|
|
|
|
fn pop_first_value(&mut self) -> Option<V> {
|
|
|
|
self.first_entry().map(|x| x.remove())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_last_value(&mut self) -> Option<V> {
|
|
|
|
self.last_entry().map(|x| x.remove())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "map_first_last"))]
|
2022-02-21 05:14:48 +00:00
|
|
|
pub trait BTreeMapExt<K, V> {
|
2022-02-21 06:14:13 +00:00
|
|
|
fn first_key(&self) -> Option<&K>;
|
|
|
|
fn last_key(&self) -> Option<&K>;
|
|
|
|
fn pop_first_key(&mut self) -> Option<K>;
|
|
|
|
fn pop_last_key(&mut self) -> Option<K>;
|
|
|
|
fn pop_first_value(&mut self) -> Option<V>;
|
|
|
|
fn pop_last_value(&mut self) -> Option<V>;
|
2022-02-21 05:14:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
#[cfg(not(feature = "map_first_last"))]
|
|
|
|
impl<K: Ord + Clone, V> BTreeMapExt<K, V> for std::collections::BTreeMap<K, V> {
|
|
|
|
fn first_key(&self) -> Option<&K> {
|
2022-02-21 05:14:48 +00:00
|
|
|
self.keys().next()
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn last_key(&self) -> Option<&K> {
|
2022-02-21 05:14:48 +00:00
|
|
|
self.keys().next_back()
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn pop_first_key(&mut self) -> Option<K> {
|
|
|
|
let key = self.first_key()?;
|
2022-02-21 05:14:48 +00:00
|
|
|
let key = key.clone(); // ownership hack
|
|
|
|
Some(self.remove_entry(&key).unwrap().0)
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn pop_last_key(&mut self) -> Option<K> {
|
|
|
|
let key = self.last_key()?;
|
2022-02-21 05:14:48 +00:00
|
|
|
let key = key.clone(); // ownership hack
|
|
|
|
Some(self.remove_entry(&key).unwrap().0)
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn pop_first_value(&mut self) -> Option<V> {
|
|
|
|
let key = self.first_key()?;
|
2022-02-21 05:14:48 +00:00
|
|
|
let key = key.clone(); // ownership hack
|
|
|
|
Some(self.remove_entry(&key).unwrap().1)
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn pop_last_value(&mut self) -> Option<V> {
|
|
|
|
let key = self.last_key()?;
|
2022-02-21 05:14:48 +00:00
|
|
|
let key = key.clone(); // ownership hack
|
|
|
|
Some(self.remove_entry(&key).unwrap().1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
#[cfg(feature = "map_first_last")]
|
|
|
|
pub trait BTreeSetExt<K> {}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "map_first_last"))]
|
2022-02-21 05:14:48 +00:00
|
|
|
pub trait BTreeSetExt<K> {
|
2022-02-21 06:14:13 +00:00
|
|
|
fn last(&self) -> Option<&K>;
|
|
|
|
fn pop_last(&mut self) -> Option<K>;
|
2022-02-21 05:14:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
#[cfg(not(feature = "map_first_last"))]
|
|
|
|
impl<K: Ord + Clone> BTreeSetExt<K> for std::collections::BTreeSet<K> {
|
|
|
|
fn last(&self) -> Option<&K> {
|
2022-02-21 05:14:48 +00:00
|
|
|
self.iter().next_back()
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:14:13 +00:00
|
|
|
fn pop_last(&mut self) -> Option<K> {
|
2022-02-23 20:23:09 +00:00
|
|
|
#[allow(unstable_name_collisions)]
|
2022-02-21 06:14:13 +00:00
|
|
|
let key = self.last()?;
|
2022-02-21 05:14:48 +00:00
|
|
|
let key = key.clone(); // ownership hack
|
|
|
|
self.take(&key)
|
|
|
|
}
|
|
|
|
}
|