pub struct LiteMap<K, V, 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::HashMap, though it requires Ord instead of Hash.

Implementations

Construct a new LiteMap

Construct a new LiteMap with a given capacity

The number of elements in the LiteMap

Whether the LiteMap is empty

Remove all elements from the LiteMap

Reserve capacity for additional more elements to be inserted into the LiteMap to avoid frequent reallocations.

See Vec::reserve() for more information.

Convert a Vec<(K, V)> into a LiteMap.

Safety

The vec must be sorted and have no duplicate keys.

Convert a LiteMap into a Vec<(K, V)>.

Get the key-value pair residing at a particular index

In most cases, prefer LiteMap::get() over this method.

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);

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);

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"));

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")));

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")));

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);

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);

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);

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);

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());

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.

Produce an ordered iterator over key-value pairs

Produce an ordered iterator over keys

Produce an iterator over values, ordered by their keys

Produce an ordered mutable iterator over key-value pairs

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more

This method must cast self between Self<'static> and Self<'a>. Read more

This method can be used to cast away Self<'a>’s lifetime. Read more

This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.