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::HashMap
, though it
requires Ord
instead of Hash
.
Implementations
sourceimpl<K, V, S> LiteMap<K, V, S> where
S: Store<K, V>,
impl<K, V, S> LiteMap<K, V, S> where
S: Store<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.
sourcepub unsafe fn from_tuple_vec_unchecked(values: S) -> Self
pub unsafe fn from_tuple_vec_unchecked(values: S) -> Self
sourcepub fn into_tuple_vec(self) -> S
pub fn into_tuple_vec(self) -> S
Convert a LiteMap
into a Vec<(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.
sourceimpl<K, V, S> LiteMap<K, V, S> where
K: Ord,
S: Store<K, V>,
impl<K, V, S> LiteMap<K, V, S> where
K: Ord,
S: Store<K, V>,
sourcepub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Ord,
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Ord,
Get the value associated with key
, if it exists.
use litemap::LiteMap;
let mut map = LiteMap::new();
map.insert(1, "one");
map.insert(2, "two");
assert_eq!(map.get(&1), Some(&"one"));
assert_eq!(map.get(&3), None);
sourcepub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Ord,
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Ord,
Returns whether key
is contained in this map
use litemap::LiteMap;
let mut map = LiteMap::new();
map.insert(1, "one");
map.insert(2, "two");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&3), false);
sourcepub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Ord,
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Ord,
Get the value associated with key
, if it exists, as a mutable reference.
use litemap::LiteMap;
let mut map = LiteMap::new();
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 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::new();
assert!(map.try_append(1, "uno").is_none());
assert!(map.try_append(3, "tres").is_none());
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::new();
assert!(map.try_append(1, "uno").is_none());
assert!(map.try_append(3, "tres").is_none());
assert_eq!(map.last(), Some((&3, &"tres")));
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();
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();
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();
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 remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Ord,
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Ord,
Remove the value at key
, returning it if it exists.
use litemap::LiteMap;
let mut map = LiteMap::new();
map.insert(1, "one");
map.insert(2, "two");
assert_eq!(map.remove(&1), Some("one"));
assert_eq!(map.get(&1), None);
sourceimpl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S> where
K: Ord,
S: StoreIterable<'a, K, V> + StoreFromIterator<K, V>,
impl<'a, K: 'a, V: 'a, S> LiteMap<K, V, S> where
K: Ord,
S: StoreIterable<'a, K, V> + StoreFromIterator<K, V>,
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();
map1.insert(1, "one");
map1.insert(2, "two");
let mut map2 = LiteMap::new();
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());
sourceimpl<K, V, S> LiteMap<K, V, S> where
K: Ord,
S: Store<K, V>,
impl<K, V, S> LiteMap<K, V, S> where
K: Ord,
S: Store<K, V>,
sourcepub fn find_index<Q: ?Sized>(&self, key: &Q) -> Result<usize, usize> where
K: Borrow<Q>,
Q: Ord,
pub fn find_index<Q: ?Sized>(&self, key: &Q) -> Result<usize, usize> where
K: Borrow<Q>,
Q: Ord,
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.
sourceimpl<'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 Iterator<Item = (&'a K, &'a V)> + DoubleEndedIterator
pub fn iter(
&'a self
) -> impl Iterator<Item = (&'a K, &'a V)> + DoubleEndedIterator
Produce an ordered iterator over key-value pairs
sourcepub fn iter_keys(&'a self) -> impl Iterator<Item = &'a K> + DoubleEndedIterator
pub fn iter_keys(&'a self) -> impl Iterator<Item = &'a K> + DoubleEndedIterator
Produce an ordered iterator over keys
sourcepub fn iter_values(
&'a self
) -> impl Iterator<Item = &'a V> + DoubleEndedIterator
pub fn iter_values(
&'a self
) -> impl Iterator<Item = &'a V> + DoubleEndedIterator
Produce an iterator over values, ordered by their keys
sourceimpl<K, V, S> LiteMap<K, V, S> where
S: Store<K, V>,
impl<K, V, S> LiteMap<K, V, S> where
S: Store<K, V>,
sourcepub fn retain<F>(&mut self, predicate: F) where
F: FnMut(&K, &V) -> bool,
pub fn retain<F>(&mut self, predicate: F) where
F: FnMut(&K, &V) -> bool,
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();
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);
Trait Implementations
sourceimpl<'de, K, V, R> Deserialize<'de> for LiteMap<K, V, R> where
K: Ord + Deserialize<'de>,
V: Deserialize<'de>,
R: Store<K, V>,
impl<'de, K, V, R> Deserialize<'de> for LiteMap<K, V, R> where
K: Ord + Deserialize<'de>,
V: Deserialize<'de>,
R: Store<K, V>,
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<K: Ord, V: Ord, S: Ord> Ord for LiteMap<K, V, S>
impl<K: Ord, V: Ord, S: Ord> Ord for LiteMap<K, V, S>
sourceimpl<K: PartialEq, V: PartialEq, S: PartialEq> PartialEq<LiteMap<K, V, S>> for LiteMap<K, V, S>
impl<K: PartialEq, V: PartialEq, S: PartialEq> PartialEq<LiteMap<K, V, S>> for LiteMap<K, V, S>
sourceimpl<K: PartialOrd, V: PartialOrd, S: PartialOrd> PartialOrd<LiteMap<K, V, S>> for LiteMap<K, V, S>
impl<K: PartialOrd, V: PartialOrd, S: PartialOrd> PartialOrd<LiteMap<K, V, S>> for LiteMap<K, V, S>
sourcefn partial_cmp(&self, other: &LiteMap<K, V, S>) -> Option<Ordering>
fn partial_cmp(&self, other: &LiteMap<K, V, S>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<K, V, R> Serialize for LiteMap<K, V, R> where
K: Serialize,
V: Serialize,
R: Store<K, V> + Serialize,
impl<K, V, R> Serialize for LiteMap<K, V, R> where
K: Serialize,
V: Serialize,
R: Store<K, V> + Serialize,
sourceimpl<'a, K, V, S> Yokeable<'a> for LiteMap<K, V, S> where
K: 'static,
V: 'static,
S: 'static,
impl<'a, K, V, S> Yokeable<'a> for LiteMap<K, V, S> where
K: 'static,
V: 'static,
S: 'static,
type Output = LiteMap<K, V, S>
type Output = LiteMap<K, V, S>
This type MUST be Self
with the 'static
replaced with 'a
, i.e. Self<'a>
sourcefn transform(&self) -> &Self::Output
fn transform(&self) -> &Self::Output
This method must cast self
between &'a Self<'static>
and &'a Self<'a>
. Read more
sourcefn transform_owned(self) -> Self::Output
fn transform_owned(self) -> Self::Output
This method must cast self
between Self<'static>
and Self<'a>
. Read more
sourceunsafe fn make(this: Self::Output) -> Self
unsafe fn make(this: Self::Output) -> Self
This method can be used to cast away Self<'a>
’s lifetime. Read more
sourcefn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
This method must cast self
between &'a mut Self<'static>
and &'a mut Self<'a>
,
and pass it to f
. Read more
impl<K: Eq, V: Eq, S: Eq> Eq for LiteMap<K, V, S>
impl<'a, K, V, S> IsCovariant<'a> for LiteMap<K, V, S> where
K: 'static,
V: 'static,
S: 'static,
impl<K, V, S> StructuralEq for LiteMap<K, V, S>
impl<K, V, S> StructuralPartialEq for LiteMap<K, V, S>
Auto Trait Implementations
impl<K, V, S> RefUnwindSafe for LiteMap<K, V, S> where
K: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V, S> Send for LiteMap<K, V, S> where
K: Send,
S: Send,
V: Send,
impl<K, V, S> Sync for LiteMap<K, V, S> where
K: Sync,
S: Sync,
V: Sync,
impl<K, V, S> Unpin for LiteMap<K, V, S> where
K: Unpin,
S: Unpin,
V: Unpin,
impl<K, V, S> UnwindSafe for LiteMap<K, V, S> where
K: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more