pub struct OrderedSliceMapMut<'a, K, V> { /* private fields */ }Expand description
An ordered map based on an ordered mutable slice.
This structures provides a subset of the methods offered by the standard library’s
BTreeMap.
§Examples
use static_mapset::OrderedSliceMapMut;
let mut movie_review_items = [
("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 = OrderedSliceMapMut::new(&mut movie_review_items);
// 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.
assert_eq!(movie_reviews.get("Office Space"), Some(&"Deals with real issues in the workplace."));
// iterate over everything.
for (movie, review) in &movie_reviews {
println!("{movie}: \"{review}\"");
}Implementations§
Source§impl<'a, K, V> OrderedSliceMapMut<'a, K, V>
impl<'a, K, V> OrderedSliceMapMut<'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 OrderedSliceMapMut.
Duplicate keys are removed, keeping the last occurrence.
The slice is sorted by key. Duplicate keys are removed, keeping the last occurrence.
§Complexity
Linearithmic in self.len().
§Examples
use static_mapset::OrderedSliceMapMut;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMapMut::new(&mut data);
assert_eq!(map.first_key_value(), Some((&1, &"a")));Sourcepub const fn new_unchecked(ordered_items: &'a mut [(K, V)]) -> Self
pub const fn new_unchecked(ordered_items: &'a mut [(K, V)]) -> Self
Creates an OrderedSliceMapMut from a mutable slice without sorting.
The slice must be sorted in ascending order with no duplicate keys.
§Complexity
Constant.
§Examples
use static_mapset::OrderedSliceMapMut;
let mut array = [(1, "a"), (2, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut array);
assert_eq!(map.get(&2), Some(&"b"));
// Incorrect key order or duplicates cause undefined behavior:
// let mut array = [(2, "a"), (1, "b")];
// let map = OrderedSliceMapMut::new_unchecked(&mut array);
// let mut array = [(1, "a"), (1, "b")];
// let map = OrderedSliceMapMut::new_unchecked(&mut array);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::OrderedSliceMapMut;
let mut data = [(1, "a"), (2, "b"), (3, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut data);
assert!(map.is_valid());
let mut data = [(1, "a"), (2, "b"), (2, "c")];
let map = OrderedSliceMapMut::new_unchecked(&mut data);
assert!(!map.is_valid());
let mut data = [(1.0, "a"), (2.0, "b"), (f64::NAN, "c")];
let map = OrderedSliceMapMut::new_unchecked(&mut data);
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::OrderedSliceMapMut;
let mut data = [(1, "a")];
let map = OrderedSliceMapMut::new(&mut data);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
let mut data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
let mut empty: [(i32, &str); 0] = [];
let mut map = OrderedSliceMapMut::new(&mut empty);
assert_eq!(map.first_key_value(), None);
let mut data = [(1, "b"), (2, "a")];
map = OrderedSliceMapMut::new(&mut 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::OrderedSliceMapMut;
let mut data = [(1, "a")];
let map = OrderedSliceMapMut::new(&mut data);
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
let mut data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut 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. This is potentially useful:
- for key types where non-identical keys can be considered equal;
- for getting the
&Kstored key value from a borrowed&Qlookup key; or - for getting a reference to a key with the same lifetime as the collection.
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::OrderedSliceMapMut;
#[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 mut data = [(j_a, "Paris")];
let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut data);
// f64::NAN is incomparable, so key lookups by NaN never match.
assert_eq!(map.get_key_value(&f64::NAN), None);Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Complexity
Constant.
§Examples
use static_mapset::OrderedSliceMapMut;
let mut data: &mut [(i32, &str)] = &mut [];
let map = OrderedSliceMapMut::new_unchecked(data);
assert!(map.is_empty());
let mut data = &mut [(1, "a")];
let map = OrderedSliceMapMut::new_unchecked(data);
assert!(!map.is_empty());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::OrderedSliceMapMut;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMapMut::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::OrderedSliceMapMut;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMapMut::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)>
Returns the last key-value pair in the map. The key in this pair is the maximum key in the map.
§Complexity
Constant.
§Examples
use static_mapset::OrderedSliceMapMut;
let mut data = [(1, "b"), (2, "a")];
let map = OrderedSliceMapMut::new(&mut data);
assert_eq!(map.last_key_value(), Some((&2, &"a")));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::OrderedSliceMapMut;
use std::ops::Bound;
let mut data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
use std::ops::Bound::Included;
let mut data = [
(3, "a"),
(5, "b"),
(8, "c"),
];
let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
use std::ops::Bound;
let mut data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let map = OrderedSliceMapMut::new(&mut 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 mut data = [(1.0, "a"), (2.0, "b")];
let map = OrderedSliceMapMut::new_unchecked(&mut 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::OrderedSliceMapMut;
let mut data = [(2, "b"), (1, "a")];
let map = OrderedSliceMapMut::new(&mut data);
let values: Vec<_> = map.values().cloned().collect();
assert_eq!(values, ["a", "b"]);Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&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::OrderedSliceMapMut;
let mut data = [(1, "a")];
let mut map = OrderedSliceMapMut::new(&mut data);
assert_eq!(map.get_mut(&1), Some(&mut "a"));
assert_eq!(map.get_mut(&2), None);
let mut data = [(1.0, "a"), (2.0, "b")];
let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
// 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::OrderedSliceMapMut;
let mut data = [
("a", 1),
("b", 2),
("c", 3),
];
let mut map = OrderedSliceMapMut::new(&mut data);
// 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() to find the bounds, and linear in the number of yielded elements to iterate them all.
§Examples
use static_mapset::OrderedSliceMapMut;
use std::ops::Bound;
let mut data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let mut map = OrderedSliceMapMut::new(&mut data);
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 data = [(1.0, "a"), (2.0, "b")];
let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
// 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.
If the range includes equal keys, all matching entries are yielded in key order.
§Complexity
Logarithmic in self.len().
§Examples
use static_mapset::OrderedSliceMapMut;
let mut data = [("Alice", 0), ("Bob", 0), ("Carol", 0), ("Cheryl", 0)];
let mut map: OrderedSliceMapMut<'_, &str, i32> = OrderedSliceMapMut::new(&mut data);
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::OrderedSliceMapMut;
use std::ops::Bound;
let mut data = [
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let mut map = OrderedSliceMapMut::new(&mut data);
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 data = [(1.0, "a"), (2.0, "b")];
let mut map = OrderedSliceMapMut::new_unchecked(&mut data);
// 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::OrderedSliceMapMut;
let mut data = [(1, String::from("hello")), (2, String::from("goodbye"))];
let mut a = OrderedSliceMapMut::new(&mut data);
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!")]);