Struct kempt::map::Map

source ·
pub struct Map<Key, Value>
where Key: Sort<Key>,
{ /* private fields */ }
Expand description

An ordered Key/Value map.

This type is similar to BTreeMap, but utilizes a simpler storage model. Additionally, it provides a more thorough interface and has a merge_with() function.

This type is designed for collections with a limited number of keys. In general, this collection excels when there are fewer entries, while HashMap or BTreeMap will be better choices with larger numbers of entries. Additionally, HashMap will perform better if comparing the keys is expensive.

Implementations§

source§

impl<Key, Value> Map<Key, Value>
where Key: Sort<Key>,

source

pub const fn new() -> Self

Returns an empty map.

source

pub fn with_capacity(capacity: usize) -> Self

Returns a map with enough memory allocated to store capacity elements without reallocation.

source

pub fn capacity(&self) -> usize

Returns the current capacity this map can hold before it must reallocate.

source

pub fn insert(&mut self, key: Key, value: Value) -> Option<Field<Key, Value>>

Inserts key and value. If an entry already existed for key, the value being overwritten is returned.

source

pub fn insert_with( &mut self, key: Key, value: impl FnOnce() -> Value ) -> Option<Key>

Inserts an entry with key only if the map does not already contain that key.

If an existing key is found, Some(key) is returned. If an existing key isn’t found, value() will be called, a new entry will be inserted, and None will be returned.

This is similar to using Map::entry, except this function does not require that Key implement ToOwned.

source

pub fn contains<SearchFor>(&self, key: &SearchFor) -> bool
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Returns true if this object contains key.

source

pub fn get<SearchFor>(&self, key: &SearchFor) -> Option<&Value>
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Returns the value associated with key, if found.

source

pub fn get_mut<SearchFor>(&mut self, key: &SearchFor) -> Option<&mut Value>
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Returns a mutable value associated with key, if found.

source

pub fn get_field<SearchFor>( &self, key: &SearchFor ) -> Option<&Field<Key, Value>>
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Returns the field associated with key, if found.

source

pub fn get_field_mut<SearchFor>( &mut self, key: &SearchFor ) -> Option<&mut Field<Key, Value>>
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Returns the a mutable reference to the field associated with key, if found.

source

pub fn field(&self, index: usize) -> Option<&Field<Key, Value>>

Returns the Field at the specified index, or None if the index is outside of the bounds of this collection.

source

pub fn field_mut(&mut self, index: usize) -> Option<&mut Field<Key, Value>>

Returns a mutable reference to the Field at the specified index, or None if the index is outside of the bounds of this collection.

source

pub fn remove<SearchFor>( &mut self, key: &SearchFor ) -> Option<Field<Key, Value>>
where Key: Sort<SearchFor>, SearchFor: ?Sized,

Removes the value associated with key, if found.

source

pub fn remove_by_index(&mut self, index: usize) -> Field<Key, Value>

Removes the field at index.

§Panics

This function will panic if index is outside of the bounds of this collection.

source

pub fn len(&self) -> usize

Returns the number of fields in this object.

source

pub fn is_empty(&self) -> bool

Returns true if this object has no fields.

source

pub fn entry<'key, SearchFor>( &mut self, key: impl Into<SearchKey<'key, Key, SearchFor>> ) -> Entry<'_, 'key, Key, Value, SearchFor>
where Key: Sort<SearchFor> + Borrow<SearchFor>, SearchFor: ToOwned<Owned = Key> + ?Sized + 'key,

Returns an Entry for the associated key.

source

pub fn iter(&self) -> Iter<'_, Key, Value>

Returns an iterator over the fields in this object.

source

pub fn iter_mut(&mut self) -> IterMut<'_, Key, Value>

Returns an iterator over the fields in this object, with mutable access.

source

pub fn keys(&self) -> Keys<'_, Key, Value>

Returns an iterator over the keys in this object.

source

pub fn values(&self) -> Values<'_, Key, Value>

Returns an iterator over the values in this object.

source

pub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value>

Returns an iterator over the fields in this object, with mutable access.

source

pub fn into_values(self) -> IntoValues<Key, Value>

Returns an iterator returning all of the values contained in this object.

source

pub fn merged_with( self, other: &Self, filter: impl FnMut(&Key, &Value) -> Option<Value>, merge: impl FnMut(&Key, &mut Value, &Value) ) -> Self
where Key: Clone, Value: Clone,

Merges the fields from self and other into a new object, returning the updated object.

  • If a field is contained in other but not contained in self, filter() is called. If filter() returns a value, the returned value is inserted into the new object using the original key.
  • If a field is contained in both other and self, merge() is called with mutable access to a clone of the value from self and a reference to the value from other. The merge() function is responsible for updating the value if needed to complete the merge. The merged value is inserted into the returned object.
  • If a field is contained in self but not in other, it is always cloned.
use kempt::Map;

let a: Map<&'static str, usize> = [("a", 1), ("b", 2)].into_iter().collect();
let b: Map<&'static str, usize> = [("a", 1), ("c", 3)].into_iter().collect();
let merged = a.merged_with(&b, |_key, b| Some(*b), |_key, a, b| *a += *b);

assert_eq!(merged.get(&"a"), Some(&2));
assert_eq!(merged.get(&"b"), Some(&2));
assert_eq!(merged.get(&"c"), Some(&3));
source

pub fn merge_with( &mut self, other: &Self, filter: impl FnMut(&Key, &Value) -> Option<Value>, merge: impl FnMut(&Key, &mut Value, &Value) )
where Key: Clone,

Merges the fields from other into self.

  • If a field is contained in other but not contained in self, filter() is called. If filter() returns a value, the returned value is inserted into self using the original key.
  • If a field is contained in both other and self, merge() is called with mutable access to the value from self and a reference to the value from other. The merge() function is responsible for updating the value if needed to complete the merge.
  • If a field is contained in self but not in other, it is ignored.
use kempt::Map;

let mut a: Map<&'static str, usize> = [("a", 1), ("b", 2)].into_iter().collect();
let b: Map<&'static str, usize> = [("a", 1), ("c", 3)].into_iter().collect();
a.merge_with(&b, |_key, b| Some(*b), |_key, a, b| *a += *b);
assert_eq!(a.get(&"a"), Some(&2));
assert_eq!(a.get(&"b"), Some(&2));
assert_eq!(a.get(&"c"), Some(&3));
source

pub fn drain(&mut self) -> Drain<'_, Key, Value>

Returns an iterator that returns all of the elements in this collection. After the iterator is dropped, this object will be empty.

source

pub fn clear(&mut self)

Clears the contents of this collection.

This does not return any allocated memory to the OS.

source

pub fn shrink_to_fit(&mut self)

Resizes this collection to fit its contents exactly.

This function will reallocate its internal storage to fit the contents of this collection’s current size. If the allocation is already the correct size, this is a no-op.

source

pub fn shrink_to(&mut self, min_capacity: usize)

Resizes this collection to be able to hold min_capacity.

This function will reallocate its internal storage to fit the contents of this collection’s current size. If the allocation is already the correct size, this is a no-op.

If the length of this collection is larger than min_capacity, this function will behave identically to shrink_to_fit().

source

pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Key, Value>

Returns an iterator that yields Unioned entries.

The iterator will return a single result for each unique Key contained in either self or other. If both collections contain a key, the iterator will contain Unioned::Both for that key.

This iterator is guaranteed to return results in the sort order of the Key type.

source

pub fn intersection<'a>( &'a self, other: &'a Self ) -> Intersection<'a, Key, Value>

Returns an iterator that yields entries that appear in both self and other.

The iterator will return a result for each Key contained in both self and other. If a particular key is only found in one collection, it will not be included.

This iterator is guaranteed to return results in the sort order of the Key type.

source

pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Key, Value>

Returns an iterator that yields entries that appear in self, but not in other.

The iterator will return a result for each Key contained in self but not contained in other. If a Key is only in other or is in both collections, it will not be returned.

This iterator is guaranteed to return results in the sort order of the Key type.

Trait Implementations§

source§

impl<Key, Value: Clone> Clone for Map<Key, Value>
where Key: Sort<Key> + Clone,

source§

fn clone(&self) -> Map<Key, Value>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Key, Value> Debug for Map<Key, Value>
where Key: Debug + Sort<Key>, Value: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Key, Value> Default for Map<Key, Value>
where Key: Sort<Key>,

source§

fn default() -> Self

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

impl<Key, Value> FromIterator<(Key, Value)> for Map<Key, Value>
where Key: Sort<Key>,

source§

fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<Key, Value: Hash> Hash for Map<Key, Value>
where Key: Sort<Key> + Hash,

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<'a, SearchFor, Key, V> Index<&'a SearchFor> for Map<Key, V>
where Key: Sort<Key> + Sort<SearchFor>, SearchFor: ?Sized,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: &'a SearchFor) -> &Self::Output

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

impl<'a, SearchFor, Key, V> IndexMut<&'a SearchFor> for Map<Key, V>
where Key: Sort<Key> + Sort<SearchFor>, SearchFor: ?Sized,

source§

fn index_mut(&mut self, index: &'a SearchFor) -> &mut Self::Output

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

impl<'a, Key, Value> IntoIterator for &'a Map<Key, Value>
where Key: Sort<Key>,

§

type IntoIter = Iter<'a, Key, Value>

Which kind of iterator are we turning this into?
§

type Item = &'a Field<Key, Value>

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, Key, Value> IntoIterator for &'a mut Map<Key, Value>
where Key: Sort<Key>,

§

type IntoIter = IterMut<'a, Key, Value>

Which kind of iterator are we turning this into?
§

type Item = (&'a Key, &'a mut Value)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<Key, Value> IntoIterator for Map<Key, Value>
where Key: Sort<Key>,

§

type IntoIter = IntoIter<Key, Value>

Which kind of iterator are we turning this into?
§

type Item = Field<Key, Value>

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<Key, Value: Ord> Ord for Map<Key, Value>
where Key: Sort<Key> + Ord,

source§

fn cmp(&self, other: &Map<Key, Value>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<Key, Value: PartialEq> PartialEq for Map<Key, Value>
where Key: Sort<Key> + PartialEq,

source§

fn eq(&self, other: &Map<Key, Value>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Key, Value: PartialOrd> PartialOrd for Map<Key, Value>
where Key: Sort<Key> + PartialOrd,

source§

fn partial_cmp(&self, other: &Map<Key, Value>) -> Option<Ordering>

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

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 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl<Key, Value: Eq> Eq for Map<Key, Value>
where Key: Sort<Key> + Eq,

source§

impl<Key, Value> StructuralPartialEq for Map<Key, Value>
where Key: Sort<Key>,

Auto Trait Implementations§

§

impl<Key, Value> Freeze for Map<Key, Value>

§

impl<Key, Value> RefUnwindSafe for Map<Key, Value>
where Key: RefUnwindSafe, Value: RefUnwindSafe,

§

impl<Key, Value> Send for Map<Key, Value>
where Key: Send, Value: Send,

§

impl<Key, Value> Sync for Map<Key, Value>
where Key: Sync, Value: Sync,

§

impl<Key, Value> Unpin for Map<Key, Value>
where Key: Unpin, Value: Unpin,

§

impl<Key, Value> UnwindSafe for Map<Key, Value>
where Key: UnwindSafe, Value: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<Key, SearchFor> Sort<SearchFor> for Key
where Key: Ord + PartialOrd<SearchFor>,

source§

fn compare(&self, b: &SearchFor) -> Ordering

Compare self and other, returning the comparison result. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.