pub struct OrderedSliceMap<'a, K, V> { /* private fields */ }Expand description
An ordered map based on an ordered slice.
This structures provides a subset of the methods offered by the standard library’s
BTreeMap.
§Examples
use static_mapset::OrderedSliceMap;
let movie_reviews_data = [
("Office Space", "Deals with real issues in the workplace."),
("Pulp Fiction", "Masterpiece."),
("The Godfather", "Very enjoyable."),
("The Blues Brothers", "Eye lyked it a lot.")
];
let movie_reviews = OrderedSliceMap::new_unchecked(&movie_reviews_data);
// check for a specific one.
if !movie_reviews.contains_key("Les Misérables") {
println!("We've got {} reviews, but Les Misérables ain't one.",
movie_reviews.len());
}
// look up the values associated with some keys.
let to_find = ["Up!", "Office Space"];
for movie in &to_find {
match movie_reviews.get(movie) {
Some(review) => println!("{movie}: {review}"),
None => println!("{movie} is unreviewed.")
}
}
// Look up the value for a key (will panic if the key is not found).
println!("Movie review: {}", movie_reviews[&"Office Space"]);
// iterate over everything.
for (movie, review) in &movie_reviews {
println!("{movie}: \"{review}\"");
}Implementations§
Source§impl<'a, K, V> OrderedSliceMap<'a, K, V>
impl<'a, K, V> OrderedSliceMap<'a, K, V>
Sourcepub fn new(items: &'a mut [(K, V)]) -> Selfwhere
K: Ord,
pub fn new(items: &'a mut [(K, V)]) -> Selfwhere
K: Ord,
Sorts the slice and creates an OrderedSliceMap.
Duplicate keys are removed, keeping the last occurrence.
§Complexity
Linearithmic in self.len().
§Examples
use static_mapset::OrderedSliceMap;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMap::new(&mut data);
assert_eq!(map.first_key_value(), Some((&1, &"a")));Sourcepub const fn new_unchecked(ordered_items: &'a [(K, V)]) -> Self
pub const fn new_unchecked(ordered_items: &'a [(K, V)]) -> Self
Creates an OrderedSliceMap from an immutable slice without sorting.
The slice must be sorted in ascending order with no duplicate keys.
§Complexity
Constant.
§Examples
use static_mapset::OrderedSliceMap;
static MAP: OrderedSliceMap<i32, &str> = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b")]);
assert_eq!(MAP.get(&2), Some(&"b"));
// Incorrect key order or duplicates cause undefined behavior:
// let map = OrderedSliceMap::new_unchecked(&[(2, "a"), (1, "b")]);
// let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (1, "b")]);Sourcepub fn is_valid(&self) -> boolwhere
K: PartialOrd,
pub fn is_valid(&self) -> boolwhere
K: PartialOrd,
Returns true if the map is ordered correctly.
§Complexity
Linear in self.len().
§Examples
use static_mapset::OrderedSliceMap;
let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b"), (3, "b")]);
assert!(map.is_valid());
let map = OrderedSliceMap::new_unchecked(&[(1, "a"), (2, "b"), (2, "c")]);
assert!(!map.is_valid());
let map = OrderedSliceMap::new_unchecked(&[(1.0, "a"), (2.0, "b"), (f64::NAN, "c")]);
assert!(!map.is_valid());Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the map’s key type, but the ordering
on the borrowed form must match the ordering on the key type.
If equal keys are present, this returns true if any matching key exists.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedSliceMap;
let data = [(1, "a")];
let map = OrderedSliceMap::new_unchecked(&data);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
let data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMap::new_unchecked(&data);
// f64::NAN is incomparable, so key lookups by NaN never match.
assert_eq!(map.contains_key(&f64::NAN), false);Sourcepub fn first_key_value(&self) -> Option<(&K, &V)>
pub fn first_key_value(&self) -> Option<(&K, &V)>
Returns the first key-value pair in the map. The key in this pair is the minimum key in the map.
§Complexity
Constant.
§Examples
use static_mapset::OrderedSliceMap;
let empty: &[(i32, &str)] = &[];
let map = OrderedSliceMap::new_unchecked(empty);
assert_eq!(map.first_key_value(), None);
let data = [(1, "b"), (2, "a")];
let map = OrderedSliceMap::new_unchecked(&data);
assert_eq!(map.first_key_value(), Some((&1, &"b")));Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedSliceMap;
let data = [(1, "a")];
let map = OrderedSliceMap::new_unchecked(&data);
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
let data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMap::new_unchecked(&data);
// f64::NAN is incomparable, so key lookups by NaN never match.
assert_eq!(map.get(&f64::NAN), None);Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type. If equal keys are present, the first matching entry in key order is returned.
§Complexity
Logarithmic in self.len().
§Examples
use std::cmp::Ordering;
use static_mapset::OrderedSliceMap;
#[derive(Clone, Copy, Debug)]
struct S {
id: u32,
name: &'static str, // ignored by equality and ordering operations
}
impl PartialEq for S {
fn eq(&self, other: &S) -> bool {
self.id == other.id
}
}
impl Eq for S {}
impl PartialOrd for S {
fn partial_cmp(&self, other: &S) -> Option<Ordering> {
self.id.partial_cmp(&other.id)
}
}
impl Ord for S {
fn cmp(&self, other: &S) -> Ordering {
self.id.cmp(&other.id)
}
}
let j_a = S { id: 1, name: "Jessica" };
let j_b = S { id: 1, name: "Jess" };
let p = S { id: 2, name: "Paul" };
assert_eq!(j_a, j_b);
let data = [(j_a, "Paris")];
let map = OrderedSliceMap::new_unchecked(&data);
assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
assert_eq!(map.get_key_value(&p), None);
let data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMap::new_unchecked(&data);
// f64::NAN is incomparable, so key lookups by NaN never match.
assert_eq!(map.get_key_value(&f64::NAN), None);Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Gets an iterator over the entries of the map, sorted by key.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedSliceMap;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMap::new(&mut data);
let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((*first_key, *first_value), (1, "a"));Sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
Gets an iterator over the keys of the map, in sorted order.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedSliceMap;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMap::new(&mut data);
let keys: Vec<_> = map.keys().cloned().collect();
assert_eq!(keys, [1, 2]);Sourcepub fn last_key_value(&self) -> Option<(&K, &V)>
pub fn last_key_value(&self) -> Option<(&K, &V)>
Sourcepub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
pub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
Returns a Cursor pointing at the gap before the smallest key
greater than the given bound.
Passing Bound::Included(x) will return a cursor pointing to the
gap before the smallest key greater than or equal to x.
Passing Bound::Excluded(x) will return a cursor pointing to the
gap before the smallest key greater than x.
Passing Bound::Unbounded will return a cursor pointing to the
gap before the smallest key in the map.
With equal keys, Included points before the first equal key and Excluded points after the last equal key.
§Complexity
Logarithmic in self.len() to find the bounds, and linear in the number of yielded elements to iterate them all.
§Examples
use static_mapset::OrderedSliceMap;
use std::ops::Bound;
let data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let map = OrderedSliceMap::new_unchecked(&data);
let cursor = map.lower_bound(Bound::Included(&2));
assert_eq!(cursor.peek_prev(), Some((&1, &"a")));
assert_eq!(cursor.peek_next(), Some((&2, &"b")));
let cursor = map.lower_bound(Bound::Excluded(&2));
assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
assert_eq!(cursor.peek_next(), Some((&3, &"c")));
let cursor = map.lower_bound(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), None);
assert_eq!(cursor.peek_next(), Some((&1, &"a")));
let data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMap::new_unchecked(&data);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let cursor = map.lower_bound(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some((&2.0, &"b")));Sourcepub fn range<Q, R>(&self, range: R) -> Range<'_, K, V>
pub fn range<Q, R>(&self, range: R) -> Range<'_, K, V>
Constructs a double-ended iterator over a sub-range of elements in the map.
The simplest way is to use the range syntax min..max, thus range(min..max) will
yield elements from min (inclusive) to max (exclusive).
The range may also be entered as (Bound<T>, Bound<T>), so for example
range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive
range from 4 to 10.
§Panics
Panics if range start > end.
Panics if range start == end and both bounds are Excluded.
Panics if start (or end() is incomparable and end (or start) is bounded.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedSliceMap;
use std::ops::Bound::Included;
let data = [
(3, "a"),
(5, "b"),
(8, "c"),
];
let map = OrderedSliceMap::new_unchecked(&data);
for (&key, &value) in map.range((Included(&4), Included(&8))) {
println!("{key}: {value}");
}
assert_eq!(Some((&5, &"b")), map.range(4..).next());Sourcepub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
pub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V>
Returns a Cursor pointing at the gap after the greatest key
smaller than the given bound.
Passing Bound::Included(x) will return a cursor pointing to the
gap after the greatest key smaller than or equal to x.
Passing Bound::Excluded(x) will return a cursor pointing to the
gap after the greatest key smaller than x.
Passing Bound::Unbounded will return a cursor pointing to the
gap after the greatest key in the map.
With equal keys, Excluded points before the first equal key and Included points after the last equal key.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedSliceMap;
use std::ops::Bound;
let data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let map = OrderedSliceMap::new_unchecked(&data);
let cursor = map.upper_bound(Bound::Included(&3));
assert_eq!(cursor.peek_prev(), Some((&3, &"c")));
assert_eq!(cursor.peek_next(), Some((&4, &"d")));
let cursor = map.upper_bound(Bound::Excluded(&3));
assert_eq!(cursor.peek_prev(), Some((&2, &"b")));
assert_eq!(cursor.peek_next(), Some((&3, &"c")));
let cursor = map.upper_bound(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), Some((&4, &"d")));
assert_eq!(cursor.peek_next(), None);
let data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMap::new_unchecked(&data);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let cursor = map.upper_bound(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some((&2.0, &"b")));Sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
Gets an iterator over the values of the map, in order by key.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedSliceMap;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMap::new(&mut data);
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values, ["a", "b"]);