pub struct OrderedArrayMap<K, V, const S: usize> { /* private fields */ }Expand description
An ordered map based on an ordered array.
This structures provides a subset of the methods offered by the standard library’s
BTreeMap.
§Examples
use static_mapset::OrderedArrayMap;
let movie_reviews = OrderedArrayMap::new([
("Office Space", "Deals with real issues in the workplace."),
("Pulp Fiction", "Masterpiece."),
("The Godfather", "Very enjoyable."),
("The Blues Brothers", "Eye lyked it a lot.")
]);
// 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<K, V, const S: usize> OrderedArrayMap<K, V, S>
impl<K, V, const S: usize> OrderedArrayMap<K, V, S>
Sourcepub fn new(items: [(K, V); S]) -> Selfwhere
K: Ord,
pub fn new(items: [(K, V); S]) -> Selfwhere
K: Ord,
Sorts the array and creates an OrderedArrayMap.
Duplicate keys are removed, keeping the last occurrence.
§Complexity
Linearithmic in self.len().
§Examples
use static_mapset::OrderedArrayMap;
let map = OrderedArrayMap::new([(2, "b"), (1, "a"), (2, "c")]);
assert_eq!(map.first_key_value(), Some((&1, &"a")));
assert_eq!(map.get(&2), Some(&"c"));Sourcepub const fn new_unchecked(ordered_items: [(K, V); S]) -> Self
pub const fn new_unchecked(ordered_items: [(K, V); S]) -> Self
Creates an OrderedArrayMap from an owned array without sorting.
The array must be sorted in ascending order with no duplicate keys.
§Complexity
Constant.
§Examples
use static_mapset::OrderedArrayMap;
static MAP: OrderedArrayMap<i32, &str, 2> = OrderedArrayMap::new_unchecked([(1, "a"), (2, "b")]);
assert_eq!(MAP.get(&2), Some(&"b"));
// Incorrect key order or duplicates cause undefined behavior:
// let map = OrderedArrayMap::new_unchecked([(2, "a"), (1, "b")]);
// let map = OrderedArrayMap::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::OrderedArrayMap;
let map = OrderedArrayMap::new_unchecked([(1, "a"), (2, "b"), (3, "b")]);
assert!(map.is_valid());
let map = OrderedArrayMap::new_unchecked([(1, "a"), (2, "b"), (2, "c")]);
assert!(!map.is_valid());
let map = OrderedArrayMap::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::OrderedArrayMap;
let map = OrderedArrayMap::new([(1, "a")]);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
let map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// 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::OrderedArrayMap;
let map: OrderedArrayMap<i32, &str, 0> = OrderedArrayMap::new([]);
assert_eq!(map.first_key_value(), None);
let map = OrderedArrayMap::new([(1, "b"), (2, "a")]);
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::OrderedArrayMap;
let map = OrderedArrayMap::new([(1, "a")]);
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
let map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// 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::OrderedArrayMap;
#[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 map = OrderedArrayMap::new([(j_a, "Paris")]);
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 map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// 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::OrderedArrayMap;
let map = OrderedArrayMap::new([(2, "b"), (1, "a")]);
let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((*first_key, *first_value), (1, "a"));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().
§Examples
use static_mapset::OrderedArrayMap;
use std::ops::Bound;
let map = OrderedArrayMap::from([
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
]);
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 map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// 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::OrderedArrayMap;
use std::ops::Bound::Included;
let map = OrderedArrayMap::new([
(3, "a"),
(5, "b"),
(8, "c")
]);
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::OrderedArrayMap;
use std::ops::Bound;
let map = OrderedArrayMap::from([
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
]);
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 map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// 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::OrderedArrayMap;
let map = OrderedArrayMap::new([(2, "b"), (1, "a")]);
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values, ["a", "b"]);Sourcepub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>
pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<&'a mut V>
Returns a mutable 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. If equal keys are present, the first matching entry in key order is returned.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedArrayMap;
let mut map = OrderedArrayMap::new([(1, "a")]);
assert_eq!(map.get_mut(&1), Some(&mut "a"));
assert_eq!(map.get_mut(&2), None);
let mut map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// f64::NAN is incomparable, so key lookups by NaN never match.
assert_eq!(map.get_mut(&f64::NAN), None);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
Gets a mutable iterator over the entries of the map, sorted by key.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedArrayMap;
let mut map = OrderedArrayMap::from([
("a", 1),
("b", 2),
("c", 3),
]);
// add 10 to the value if the key isn't "a"
for (key, value) in map.iter_mut() {
if key != &"a" {
*value += 10;
}
}
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values, [1, 12, 13]);Sourcepub fn lower_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
pub fn lower_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
Returns a CursorMut 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.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedArrayMap;
use std::ops::Bound;
let mut map = OrderedArrayMap::from([
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
]);
let mut cursor = map.lower_bound_mut(Bound::Included(&2));
assert_eq!(cursor.peek_prev(), Some((&1, &mut "a")));
assert_eq!(cursor.peek_next(), Some((&2, &mut "b")));
let mut cursor = map.lower_bound_mut(Bound::Excluded(&2));
assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
let mut cursor = map.lower_bound_mut(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), None);
assert_eq!(cursor.peek_next(), Some((&1, &mut "a")));
let mut map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let mut cursor = map.lower_bound_mut(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some((&2.0, &mut "b")));Sourcepub fn range_mut<Q, R>(&mut self, range: R) -> RangeMut<'_, K, V>
pub fn range_mut<Q, R>(&mut self, range: R) -> RangeMut<'_, K, V>
Constructs a mutable 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::OrderedArrayMap;
let mut map = OrderedArrayMap::new([
("Alice", 0),
("Bob", 0),
("Carol", 0),
("Cheryl", 0)
]);
for (_, balance) in map.range_mut("B".."Cheryl") {
*balance += 100;
}
for (name, balance) in &map {
println!("{name} => {balance}");
}Sourcepub fn upper_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
pub fn upper_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V>
Returns a CursorMut 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.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedArrayMap;
use std::ops::Bound;
let mut map = OrderedArrayMap::from([
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
]);
let mut cursor = map.upper_bound_mut(Bound::Included(&3));
assert_eq!(cursor.peek_prev(), Some((&3, &mut "c")));
assert_eq!(cursor.peek_next(), Some((&4, &mut "d")));
let mut cursor = map.upper_bound_mut(Bound::Excluded(&3));
assert_eq!(cursor.peek_prev(), Some((&2, &mut "b")));
assert_eq!(cursor.peek_next(), Some((&3, &mut "c")));
let mut cursor = map.upper_bound_mut(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), Some((&4, &mut "d")));
assert_eq!(cursor.peek_next(), None);
let mut map = OrderedArrayMap::new_unchecked([(1.0, "a"), (2.0, "b")]);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let mut cursor = map.upper_bound_mut(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some((&2.0, &mut "b")));Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
Gets a mutable iterator over the values of the map, in order by key.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedArrayMap;
let mut a = OrderedArrayMap::new([
(1, String::from("hello")),
(2, String::from("goodbye"))
]);
for value in a.values_mut() {
value.push_str("!");
}
let values: Vec<String> = a.values().cloned().collect();
assert_eq!(values, [String::from("hello!"),
String::from("goodbye!")]);Sourcepub fn into_keys(self) -> IntoKeys<K, V, S> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V, S> ⓘ
Creates a consuming iterator visiting all the keys, in sorted order.
The map cannot be used after calling this.
The iterator element type is K.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedArrayMap;
let a = OrderedArrayMap::new([(2, "b"), (1, "a")]);
let keys: Vec<i32> = a.into_keys().collect();
assert_eq!(keys, [1, 2]);Sourcepub fn into_values(self) -> IntoValues<K, V, S> ⓘ
pub fn into_values(self) -> IntoValues<K, V, S> ⓘ
Creates a consuming iterator visiting all the values, in order by key.
The map cannot be used after calling this.
The iterator element type is V.
§Complexity
Linear in self.len() to iterate all elements.
§Examples
use static_mapset::OrderedArrayMap;
let a = OrderedArrayMap::new([(1, "hello"), (2, "goodbye")]);
let values: Vec<&str> = a.into_values().collect();
assert_eq!(values, ["hello", "goodbye"]);