Trait sorted-collections::sortedmap::SortedMapExt
[-]
[+]
[src]
pub trait SortedMapExt<K, V> where K: Clone + Ord {
type RangeRemove;
fn first(&self) -> Option<&K>;
fn first_remove(&mut self) -> Option<(K, V)>;
fn last(&self) -> Option<&K>;
fn last_remove(&mut self) -> Option<(K, V)>;
fn ceiling(&self, key: &K) -> Option<&K>;
fn ceiling_remove(&mut self, key: &K) -> Option<(K, V)>;
fn floor(&self, key: &K) -> Option<&K>;
fn floor_remove(&mut self, key: &K) -> Option<(K, V)>;
fn higher(&self, key: &K) -> Option<&K>;
fn higher_remove(&mut self, key: &K) -> Option<(K, V)>;
fn lower(&self, key: &K) -> Option<&K>;
fn lower_remove(&mut self, key: &K) -> Option<(K, V)>;
fn range_remove(&mut self, from_key: Bound<&K>, to_key: Bound<&K>) -> RangeRemove;
}
An extension trait for a Map
whose keys have a defined total ordering.
This trait defines convenience methods which take advantage of the map's ordering.
Associated Types
type RangeRemove
A by-value iterator yielding key-value pairs whose keys fall within a given range and which have just been removed from this map.
Required Methods
fn first(&self) -> Option<&K>
Returns an immutable reference to the first (least) key currently in this map.
Returns None
if this map is empty.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.first().unwrap(), &1u32); }
fn first_remove(&mut self) -> Option<(K, V)>
Removes and returns the first (least) key currently in this map and its associated value.
Returns None
if this map is empty.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.first_remove().unwrap(), (1u32, 1u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(2u32, 2u32), (3, 3), (4, 4), (5, 5)]); }
fn last(&self) -> Option<&K>
Returns an immutable reference to the last (greatest) key currently in this map.
Returns None
if this map is empty.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.last().unwrap(), &5u32); }
fn last_remove(&mut self) -> Option<(K, V)>
Removes and returns the last (greatest) key currently in this map and its associated value.
Returns None
if this map is empty.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.last_remove().unwrap(), (5u32, 5u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4)]); }
fn ceiling(&self, key: &K) -> Option<&K>
Returns an immutable reference to the least key in this map greater than or equal to key
.
Returns None
if there is no such key.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.ceiling(&3).unwrap(), &3u32); }
fn ceiling_remove(&mut self, key: &K) -> Option<(K, V)>
Removes and returns the least key in this map greater than or equal to key
and its
associated value.
Returns None
if there is no such element.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.ceiling_remove(&3).unwrap(), (3u32, 3u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (2, 2), (4, 4), (5, 5)]); }
fn floor(&self, key: &K) -> Option<&K>
Returns an immutable reference to the greatest key in this map less than or equal to key
.
Returns None
if there is no such key.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.floor(&3).unwrap(), &3u32); }
fn floor_remove(&mut self, key: &K) -> Option<(K, V)>
Removes and returns the greatest key in this map less than or equal to key
and its
associated value.
Returns None
if there is no such element.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.floor_remove(&3).unwrap(), (3u32, 3u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (2, 2), (4, 4), (5, 5)]); }
fn higher(&self, key: &K) -> Option<&K>
Returns an immutable reference to the least key in this map strictly greater than key
.
Returns None
if there is no such key.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.higher(&3).unwrap(), &4u32); }
fn higher_remove(&mut self, key: &K) -> Option<(K, V)>
Removes and returns the least key in this map strictly greater than key
and its
associated value.
Returns None
if there is no such element.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.higher_remove(&3).unwrap(), (4u32, 4u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (2, 2), (3, 3), (5, 5)]); }
fn lower(&self, key: &K) -> Option<&K>
Returns an immutable reference to the greatest key in this map strictly less than key
.
Returns None
if there is no such key.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.lower(&3).unwrap(), &2u32); }
fn lower_remove(&mut self, key: &K) -> Option<(K, V)>
Removes and returns the greatest key in this map strictly less than key
and its
associated value.
Returns None
if there is no such element.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.lower_remove(&3).unwrap(), (2u32, 2u32)); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (3, 3), (4, 4), (5, 5)]); }
fn range_remove(&mut self, from_key: Bound<&K>, to_key: Bound<&K>) -> RangeRemove
Removes the key-value pairs of this map whose keys lie in the range starting at from_key
and ending at to_key
, and returns a double-ended by-value iterator over the removed pairs.
If from_key
is Unbounded
, then it will be treated as "negative infinity", and
if to_key
is Unbounded
, then it will be treated as "positive infinity". Thus,
range_remove(Unbounded, Unbounded)
will clear the map and return a by-value
iterator over all of the pairs which were in it.
Examples
extern crate "sorted-collections" as sorted_collections; use std::collections::Bound::{Excluded, Included}; use std::collections::BTreeMap; use sorted_collections::SortedMapExt; fn main() { let mut map: BTreeMap<u32, u32> = vec![(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5)].into_iter().collect(); assert_eq!(map.range_remove(Included(&2), Excluded(&4)).collect::<Vec<(u32, u32)>>(), vec![(2u32, 2u32), (3, 3)]); assert_eq!(map.into_iter().collect::<Vec<(u32, u32)>>(), vec![(1u32, 1u32), (4, 4), (5, 5)]); }
Implementors
impl<'a, K, V> SortedMapExt<K, V> for BTreeMap<K, V> where K: Clone + Ord