pub struct LiteMap<K: ?Sized, V: ?Sized, S = Vec<(K, V)>> { /* private fields */ }
Expand description
A simple “flat” map based on a sorted vector
See the module level documentation for why one should use this.
The API is roughly similar to that of std::collections::BTreeMap
.
Implementations§
Source§impl<K, V, S> LiteMap<K, V, S>
impl<K, V, S> LiteMap<K, V, S>
Sourcepub const fn from_sorted_store_unchecked(values: S) -> Self
pub const fn from_sorted_store_unchecked(values: S) -> Self
Construct a new LiteMap
using the given values
The store must be sorted and have no duplicate keys.
Source§impl<K, V> LiteMap<K, V, Vec<(K, V)>>
impl<K, V> LiteMap<K, V, Vec<(K, V)>>
Sourcepub fn into_tuple_vec(self) -> Vec<(K, V)>
pub fn into_tuple_vec(self) -> Vec<(K, V)>
Convert a LiteMap
into a sorted Vec<(K, V)>
.
Source§impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>where
S: Store<K, V>,
impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>where
S: Store<K, V>,
Sourcepub fn get_indexed(&self, index: usize) -> Option<(&K, &V)>
pub fn get_indexed(&self, index: usize) -> Option<(&K, &V)>
Get the key-value pair residing at a particular index
In most cases, prefer LiteMap::get()
over this method.
Sourcepub fn first(&self) -> Option<(&K, &V)>
pub fn first(&self) -> Option<(&K, &V)>
Get the lowest-rank key/value pair from the LiteMap
, if it exists.
§Examples
use litemap::LiteMap;
let mut map =
LiteMap::<i32, &str, Vec<_>>::from_iter([(1, "uno"), (3, "tres")]);
assert_eq!(map.first(), Some((&1, &"uno")));
Sourcepub fn last(&self) -> Option<(&K, &V)>
pub fn last(&self) -> Option<(&K, &V)>
Get the highest-rank key/value pair from the LiteMap
, if it exists.
§Examples
use litemap::LiteMap;
let mut map =
LiteMap::<i32, &str, Vec<_>>::from_iter([(1, "uno"), (3, "tres")]);
assert_eq!(map.last(), Some((&3, &"tres")));
Sourcepub fn to_boxed_keys_values<KB: ?Sized, VB: ?Sized, SB>(
&self,
) -> LiteMap<Box<KB>, Box<VB>, SB>
pub fn to_boxed_keys_values<KB: ?Sized, VB: ?Sized, SB>( &self, ) -> LiteMap<Box<KB>, Box<VB>, SB>
Returns a new LiteMap
with owned keys and values.
The trait bounds allow transforming most slice and string types.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<&str, &str> = LiteMap::new_vec();
map.insert("one", "uno");
map.insert("two", "dos");
let boxed_map: LiteMap<Box<str>, Box<str>> = map.to_boxed_keys_values();
assert_eq!(boxed_map.get("one"), Some(&Box::from("uno")));
Sourcepub fn to_boxed_keys<KB: ?Sized, SB>(&self) -> LiteMap<Box<KB>, V, SB>
pub fn to_boxed_keys<KB: ?Sized, SB>(&self) -> LiteMap<Box<KB>, V, SB>
Returns a new LiteMap
with owned keys and cloned values.
The trait bounds allow transforming most slice and string types.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<&str, usize> = LiteMap::new_vec();
map.insert("one", 11);
map.insert("two", 22);
let boxed_map: LiteMap<Box<str>, usize> = map.to_boxed_keys();
assert_eq!(boxed_map.get("one"), Some(&11));
Sourcepub fn to_boxed_values<VB: ?Sized, SB>(&self) -> LiteMap<K, Box<VB>, SB>
pub fn to_boxed_values<VB: ?Sized, SB>(&self) -> LiteMap<K, Box<VB>, SB>
Returns a new LiteMap
with cloned keys and owned values.
The trait bounds allow transforming most slice and string types.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<usize, &str> = LiteMap::new_vec();
map.insert(11, "uno");
map.insert(22, "dos");
let boxed_map: LiteMap<usize, Box<str>> = map.to_boxed_values();
assert_eq!(boxed_map.get(&11), Some(&Box::from("uno")));
Source§impl<K, V: ?Sized, S> LiteMap<K, V, S>
impl<K, V: ?Sized, S> LiteMap<K, V, S>
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Get the value associated with key
, if it exists.
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
assert_eq!(map.get(&1), Some(&"one"));
assert_eq!(map.get(&3), None);
Sourcepub fn get_by(&self, predicate: impl FnMut(&K) -> Ordering) -> Option<&V>
pub fn get_by(&self, predicate: impl FnMut(&K) -> Ordering) -> Option<&V>
Binary search the map with predicate
to find a key, returning the value.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns whether key
is contained in this map
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
assert!(map.contains_key(&1));
assert!(!map.contains_key(&3));
Sourcepub fn find_index<Q>(&self, key: &Q) -> Result<usize, usize>
pub fn find_index<Q>(&self, key: &Q) -> Result<usize, usize>
Obtain the index for a given key, or if the key is not found, the index at which it would be inserted.
(The return value works equivalently to slice::binary_search_by()
)
The indices returned can be used with Self::get_indexed()
. Prefer using
Self::get()
directly where possible.
Source§impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>where
S: StoreSlice<K, V>,
impl<K: ?Sized, V: ?Sized, S> LiteMap<K, V, S>where
S: StoreSlice<K, V>,
Sourcepub fn get_indexed_range(
&self,
range: Range<usize>,
) -> Option<LiteMap<K, V, &S::Slice>>
pub fn get_indexed_range( &self, range: Range<usize>, ) -> Option<LiteMap<K, V, &S::Slice>>
Creates a new LiteMap
from a range of the current LiteMap
.
§Examples
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
map.insert(3, "three");
let mut sub_map = map.get_indexed_range(1..3).expect("valid range");
assert_eq!(sub_map.get(&1), None);
assert_eq!(sub_map.get(&2), Some(&"two"));
assert_eq!(sub_map.get(&3), Some(&"three"));
Sourcepub fn as_sliced(&self) -> LiteMap<K, V, &S::Slice>
pub fn as_sliced(&self) -> LiteMap<K, V, &S::Slice>
Borrows this LiteMap
as one of its slice type.
This can be useful in situations where you need a LiteMap
by value but do not want
to clone the owned version.
§Examples
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
let borrowed_map = map.as_sliced();
assert_eq!(borrowed_map.get(&1), Some(&"one"));
assert_eq!(borrowed_map.get(&2), Some(&"two"));
Source§impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: Store<K, V>,
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: Store<K, V>,
Sourcepub fn to_borrowed_keys_values<KB: ?Sized, VB: ?Sized, SB>(
&'a self,
) -> LiteMap<&'a KB, &'a VB, SB>
pub fn to_borrowed_keys_values<KB: ?Sized, VB: ?Sized, SB>( &'a self, ) -> LiteMap<&'a KB, &'a VB, SB>
Returns a new LiteMap
with keys and values borrowed from this one.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<Box<usize>, String> = LiteMap::new_vec();
map.insert(Box::new(1), "one".to_string());
map.insert(Box::new(2), "two".to_string());
let borrowed_map: LiteMap<&usize, &str> = map.to_borrowed_keys_values();
assert_eq!(borrowed_map.get(&1), Some(&"one"));
Sourcepub fn to_borrowed_keys<KB: ?Sized, SB>(&'a self) -> LiteMap<&'a KB, V, SB>
pub fn to_borrowed_keys<KB: ?Sized, SB>(&'a self) -> LiteMap<&'a KB, V, SB>
Returns a new LiteMap
with keys borrowed from this one and cloned values.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<Box<usize>, String> = LiteMap::new_vec();
map.insert(Box::new(1), "one".to_string());
map.insert(Box::new(2), "two".to_string());
let borrowed_map: LiteMap<&usize, String> = map.to_borrowed_keys();
assert_eq!(borrowed_map.get(&1), Some(&"one".to_string()));
Sourcepub fn to_borrowed_values<VB: ?Sized, SB>(&'a self) -> LiteMap<K, &'a VB, SB>
pub fn to_borrowed_values<VB: ?Sized, SB>(&'a self) -> LiteMap<K, &'a VB, SB>
Returns a new LiteMap
with values borrowed from this one and cloned keys.
§Examples
use litemap::LiteMap;
let mut map: LiteMap<Box<usize>, String> = LiteMap::new_vec();
map.insert(Box::new(1), "one".to_string());
map.insert(Box::new(2), "two".to_string());
let borrowed_map: LiteMap<Box<usize>, &str> = map.to_borrowed_values();
assert_eq!(borrowed_map.get(&1), Some(&"one"));
Source§impl<K, V, S> LiteMap<K, V, S>where
S: StoreMut<K, V>,
impl<K, V, S> LiteMap<K, V, S>where
S: StoreMut<K, V>,
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a new LiteMap
with a given capacity
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve capacity for additional
more elements to be inserted into
the LiteMap
to avoid frequent reallocations.
See Vec::reserve()
for more information.
Source§impl<K, V, S> LiteMap<K, V, S>
impl<K, V, S> LiteMap<K, V, S>
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Get the value associated with key
, if it exists, as a mutable reference.
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
if let Some(mut v) = map.get_mut(&1) {
*v = "uno";
}
assert_eq!(map.get(&1), Some(&"uno"));
Sourcepub fn try_append(&mut self, key: K, value: V) -> Option<(K, V)>
pub fn try_append(&mut self, key: K, value: V) -> Option<(K, V)>
Appends value
with key
to the end of the underlying vector, returning
key
and value
if it failed. Useful for extending with an existing
sorted list.
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
assert!(map.try_append(1, "uno").is_none());
assert!(map.try_append(3, "tres").is_none());
assert!(
matches!(map.try_append(3, "tres-updated"), Some((3, "tres-updated"))),
"append duplicate of last key",
);
assert!(
matches!(map.try_append(2, "dos"), Some((2, "dos"))),
"append out of order"
);
assert_eq!(map.get(&1), Some(&"uno"));
// contains the original value for the key: 3
assert_eq!(map.get(&3), Some(&"tres"));
// not appended since it wasn't in order
assert_eq!(map.get(&2), None);
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Insert value
with key
, returning the existing value if it exists.
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
assert_eq!(map.get(&1), Some(&"one"));
assert_eq!(map.get(&3), None);
Sourcepub fn try_insert(&mut self, key: K, value: V) -> Option<(K, V)>
pub fn try_insert(&mut self, key: K, value: V) -> Option<(K, V)>
Attempts to insert a unique entry into the map.
If key
is not already in the map, inserts it with the corresponding value
and returns None
.
If key
is already in the map, no change is made to the map, and the key and value
are returned back to the caller.
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(3, "three");
// 2 is not yet in the map...
assert_eq!(map.try_insert(2, "two"), None);
assert_eq!(map.len(), 3);
// ...but now it is.
assert_eq!(map.try_insert(2, "TWO"), Some((2, "TWO")));
assert_eq!(map.len(), 3);
Sourcepub fn try_get_or_insert<E>(
&mut self,
key: K,
value: impl FnOnce(&K) -> Result<V, E>,
) -> Result<(usize, &V), E>
pub fn try_get_or_insert<E>( &mut self, key: K, value: impl FnOnce(&K) -> Result<V, E>, ) -> Result<(usize, &V), E>
Attempts to insert a unique entry into the map.
If key
is not already in the map, invokes the closure to compute value
, inserts
the pair into the map, and returns a reference to the value. The closure is passed
a reference to the key
argument.
If key
is already in the map, a reference to the existing value is returned.
Additionally, the index of the value in the map is returned. If it is not desirable
to hold on to the mutable reference’s lifetime, the index can be used to access the
element via LiteMap::get_indexed()
.
The closure returns a Result
to allow for a fallible insertion function. If the
creation of value
is infallible, you can use core::convert::Infallible
.
use litemap::LiteMap;
/// Helper function to unwrap an `Infallible` result from the insertion function
fn unwrap_infallible<T>(result: Result<T, core::convert::Infallible>) -> T {
result.unwrap_or_else(|never| match never {})
}
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(3, "three");
// 2 is not yet in the map...
let result1 = unwrap_infallible(
map.try_get_or_insert(2, |_| Ok("two"))
);
assert_eq!(result1.1, &"two");
assert_eq!(map.len(), 3);
// ...but now it is.
let result1 = unwrap_infallible(
map.try_get_or_insert(2, |_| Ok("TWO"))
);
assert_eq!(result1.1, &"two");
assert_eq!(map.len(), 3);
Source§impl<K, V, S> LiteMap<K, V, S>
impl<K, V, S> LiteMap<K, V, S>
Sourcepub fn extend_from_litemap(&mut self, other: Self) -> Option<Self>
pub fn extend_from_litemap(&mut self, other: Self) -> Option<Self>
Insert all elements from other
into this LiteMap
.
If other
contains keys that already exist in self
, the values in other
replace the
corresponding ones in self
, and the rejected items from self
are returned as a new
LiteMap
. Otherwise, None
is returned.
The implementation of this function is optimized if self
and other
have no overlap.
§Examples
use litemap::LiteMap;
let mut map1 = LiteMap::new_vec();
map1.insert(1, "one");
map1.insert(2, "two");
let mut map2 = LiteMap::new_vec();
map2.insert(2, "TWO");
map2.insert(4, "FOUR");
let leftovers = map1.extend_from_litemap(map2);
assert_eq!(map1.len(), 3);
assert_eq!(map1.get(&1), Some("one").as_ref());
assert_eq!(map1.get(&2), Some("TWO").as_ref());
assert_eq!(map1.get(&4), Some("FOUR").as_ref());
let map3 = leftovers.expect("Duplicate keys");
assert_eq!(map3.len(), 1);
assert_eq!(map3.get(&2), Some("two").as_ref());
Source§impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: StoreIterable<'a, K, V>,
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: StoreIterable<'a, K, V>,
Sourcepub fn iter(&'a self) -> impl DoubleEndedIterator<Item = (&'a K, &'a V)>
pub fn iter(&'a self) -> impl DoubleEndedIterator<Item = (&'a K, &'a V)>
Produce an ordered iterator over key-value pairs
Sourcepub fn iter_keys(&'a self) -> impl DoubleEndedIterator<Item = &'a K>
pub fn iter_keys(&'a self) -> impl DoubleEndedIterator<Item = &'a K>
Produce an ordered iterator over keys
Sourcepub fn iter_values(&'a self) -> impl DoubleEndedIterator<Item = &'a V>
pub fn iter_values(&'a self) -> impl DoubleEndedIterator<Item = &'a V>
Produce an iterator over values, ordered by their keys
Source§impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: StoreIterableMut<'a, K, V>,
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S>where
S: StoreIterableMut<'a, K, V>,
Sourcepub fn iter_mut(
&'a mut self,
) -> impl DoubleEndedIterator<Item = (&'a K, &'a mut V)>
pub fn iter_mut( &'a mut self, ) -> impl DoubleEndedIterator<Item = (&'a K, &'a mut V)>
Produce an ordered mutable iterator over key-value pairs
Source§impl<K, V, S> LiteMap<K, V, S>where
S: StoreMut<K, V>,
impl<K, V, S> LiteMap<K, V, S>where
S: StoreMut<K, V>,
Sourcepub fn retain<F>(&mut self, predicate: F)
pub fn retain<F>(&mut self, predicate: F)
Retains only the elements specified by the predicate.
In other words, remove all elements such that f((&k, &v))
returns false
.
§Example
use litemap::LiteMap;
let mut map = LiteMap::new_vec();
map.insert(1, "one");
map.insert(2, "two");
map.insert(3, "three");
// Retain elements with odd keys
map.retain(|k, _| k % 2 == 1);
assert_eq!(map.get(&1), Some(&"one"));
assert_eq!(map.get(&2), None);
Source§impl<'a, K, V> LiteMap<K, V, &'a [(K, V)]>
impl<'a, K, V> LiteMap<K, V, &'a [(K, V)]>
Sourcepub const fn const_len(&self) -> usize
pub const fn const_len(&self) -> usize
Const version of LiteMap::len()
for a slice store.
Note: This function will no longer be needed if const trait behavior is stabilized.
§Examples
use litemap::LiteMap;
static map: LiteMap<&str, usize, &[(&str, usize)]> =
LiteMap::from_sorted_store_unchecked(&[("a", 11), ("b", 22)]);
static len: usize = map.const_len();
assert_eq!(len, 2);
Sourcepub const fn const_is_empty(&self) -> bool
pub const fn const_is_empty(&self) -> bool
Const version of LiteMap::is_empty()
for a slice store.
Note: This function will no longer be needed if const trait behavior is stabilized.
§Examples
use litemap::LiteMap;
static map: LiteMap<&str, usize, &[(&str, usize)]> =
LiteMap::from_sorted_store_unchecked(&[]);
static is_empty: bool = map.const_is_empty();
assert!(is_empty);
Sourcepub const fn const_get_indexed_or_panic(&self, index: usize) -> &'a (K, V)
pub const fn const_get_indexed_or_panic(&self, index: usize) -> &'a (K, V)
Const version of LiteMap::get_indexed()
for a slice store.
Note: This function will no longer be needed if const trait behavior is stabilized.
§Panics
Panics if the index is out of bounds.
§Examples
use litemap::LiteMap;
static map: LiteMap<&str, usize, &[(&str, usize)]> =
LiteMap::from_sorted_store_unchecked(&[("a", 11), ("b", 22)]);
static t: &(&str, usize) = map.const_get_indexed_or_panic(0);
assert_eq!(t.0, "a");
assert_eq!(t.1, 11);
Source§impl<'a, V> LiteMap<&'a str, V, &'a [(&'a str, V)]>
impl<'a, V> LiteMap<&'a str, V, &'a [(&'a str, V)]>
Sourcepub const fn const_get_with_index(&self, key: &str) -> Option<(usize, &'a V)>
pub const fn const_get_with_index(&self, key: &str) -> Option<(usize, &'a V)>
Const function to get the value associated with a &str
key, if it exists.
Also returns the index of the value.
Note: This function will no longer be needed if const trait behavior is stabilized.
§Examples
use litemap::LiteMap;
static map: LiteMap<&str, usize, &[(&str, usize)]> =
LiteMap::from_sorted_store_unchecked(&[
("abc", 11),
("bcd", 22),
("cde", 33),
("def", 44),
("efg", 55),
]);
static d: Option<(usize, &usize)> = map.const_get_with_index("def");
assert_eq!(d, Some((3, &44)));
static n: Option<(usize, &usize)> = map.const_get_with_index("dng");
assert_eq!(n, None);
Source§impl<'a, V> LiteMap<&'a [u8], V, &'a [(&'a [u8], V)]>
impl<'a, V> LiteMap<&'a [u8], V, &'a [(&'a [u8], V)]>
Sourcepub const fn const_get_with_index(&self, key: &[u8]) -> Option<(usize, &'a V)>
pub const fn const_get_with_index(&self, key: &[u8]) -> Option<(usize, &'a V)>
Const function to get the value associated with a &[u8]
key, if it exists.
Also returns the index of the value.
Note: This function will no longer be needed if const trait behavior is stabilized.
§Examples
use litemap::LiteMap;
static map: LiteMap<&[u8], usize, &[(&[u8], usize)]> =
LiteMap::from_sorted_store_unchecked(&[
(b"abc", 11),
(b"bcd", 22),
(b"cde", 33),
(b"def", 44),
(b"efg", 55),
]);
static d: Option<(usize, &usize)> = map.const_get_with_index(b"def");
assert_eq!(d, Some((3, &44)));
static n: Option<(usize, &usize)> = map.const_get_with_index(b"dng");
assert_eq!(n, None);