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>,
impl<Key, Value> Map<Key, Value>where
Key: Sort<Key>,
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Returns a map with enough memory allocated to store capacity
elements
without reallocation.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the current capacity this map can hold before it must reallocate.
sourcepub fn insert(&mut self, key: Key, value: Value) -> Option<Field<Key, Value>>
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.
sourcepub fn insert_with(
&mut self,
key: Key,
value: impl FnOnce() -> Value
) -> Option<Key>
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
.
sourcepub fn contains<SearchFor>(&self, key: &SearchFor) -> bool
pub fn contains<SearchFor>(&self, key: &SearchFor) -> bool
Returns true if this object contains key
.
sourcepub fn get<SearchFor>(&self, key: &SearchFor) -> Option<&Value>
pub fn get<SearchFor>(&self, key: &SearchFor) -> Option<&Value>
Returns the value associated with key
, if found.
sourcepub fn get_mut<SearchFor>(&mut self, key: &SearchFor) -> Option<&mut Value>
pub fn get_mut<SearchFor>(&mut self, key: &SearchFor) -> Option<&mut Value>
Returns a mutable value associated with key
, if found.
sourcepub fn get_field<SearchFor>(
&self,
key: &SearchFor
) -> Option<&Field<Key, Value>>
pub fn get_field<SearchFor>( &self, key: &SearchFor ) -> Option<&Field<Key, Value>>
Returns the field associated with key
, if found.
sourcepub fn get_field_mut<SearchFor>(
&mut self,
key: &SearchFor
) -> Option<&mut Field<Key, Value>>
pub fn get_field_mut<SearchFor>( &mut self, key: &SearchFor ) -> Option<&mut Field<Key, Value>>
Returns the a mutable reference to the field associated with key
, if
found.
sourcepub fn field(&self, index: usize) -> Option<&Field<Key, Value>>
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.
sourcepub fn field_mut(&mut self, index: usize) -> Option<&mut Field<Key, Value>>
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.
sourcepub fn remove<SearchFor>(
&mut self,
key: &SearchFor
) -> Option<Field<Key, Value>>
pub fn remove<SearchFor>( &mut self, key: &SearchFor ) -> Option<Field<Key, Value>>
Removes the value associated with key
, if found.
sourcepub fn remove_by_index(&mut self, index: usize) -> Field<Key, Value>
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.
sourcepub fn entry<'key, SearchFor>(
&mut self,
key: impl Into<SearchKey<'key, Key, SearchFor>>
) -> Entry<'_, 'key, Key, Value, SearchFor>
pub fn entry<'key, SearchFor>( &mut self, key: impl Into<SearchKey<'key, Key, SearchFor>> ) -> Entry<'_, 'key, Key, Value, SearchFor>
Returns an Entry
for the associated key.
sourcepub fn iter(&self) -> Iter<'_, Key, Value> ⓘ
pub fn iter(&self) -> Iter<'_, Key, Value> ⓘ
Returns an iterator over the fields in this object.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> ⓘ
Returns an iterator over the fields in this object, with mutable access.
sourcepub fn values(&self) -> Values<'_, Key, Value> ⓘ
pub fn values(&self) -> Values<'_, Key, Value> ⓘ
Returns an iterator over the values in this object.
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value> ⓘ
Returns an iterator over the fields in this object, with mutable access.
sourcepub fn into_values(self) -> IntoValues<Key, Value> ⓘ
pub fn into_values(self) -> IntoValues<Key, Value> ⓘ
Returns an iterator returning all of the values contained in this object.
sourcepub fn merged_with(
self,
other: &Self,
filter: impl FnMut(&Key, &Value) -> Option<Value>,
merge: impl FnMut(&Key, &mut Value, &Value)
) -> Self
pub fn merged_with( self, other: &Self, filter: impl FnMut(&Key, &Value) -> Option<Value>, merge: impl FnMut(&Key, &mut Value, &Value) ) -> Self
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 inself
,filter()
is called. Iffilter()
returns a value, the returned value is inserted into the new object using the original key. - If a field is contained in both
other
andself
,merge()
is called with mutable access to a clone of the value fromself
and a reference to the value fromother
. Themerge()
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 inother
, 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));
sourcepub fn merge_with(
&mut self,
other: &Self,
filter: impl FnMut(&Key, &Value) -> Option<Value>,
merge: impl FnMut(&Key, &mut Value, &Value)
)where
Key: Clone,
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 inself
,filter()
is called. Iffilter()
returns a value, the returned value is inserted intoself
using the original key. - If a field is contained in both
other
andself
,merge()
is called with mutable access to the value fromself
and a reference to the value fromother
. Themerge()
function is responsible for updating the value if needed to complete the merge. - If a field is contained in
self
but not inother
, 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));
sourcepub fn drain(&mut self) -> Drain<'_, Key, Value> ⓘ
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.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the contents of this collection.
This does not return any allocated memory to the OS.
sourcepub fn shrink_to_fit(&mut self)
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.
sourcepub fn shrink_to(&mut self, min_capacity: usize)
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()
.
sourcepub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, Key, Value> ⓘ
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.
sourcepub fn intersection<'a>(
&'a self,
other: &'a Self
) -> Intersection<'a, Key, Value> ⓘ
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.
sourcepub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, Key, Value> ⓘ
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> FromIterator<(Key, Value)> for Map<Key, Value>where
Key: Sort<Key>,
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
fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self
source§impl<'a, SearchFor, Key, V> Index<&'a SearchFor> for Map<Key, V>
impl<'a, SearchFor, Key, V> Index<&'a SearchFor> for Map<Key, V>
source§impl<'a, SearchFor, Key, V> IndexMut<&'a SearchFor> for Map<Key, V>
impl<'a, SearchFor, Key, V> IndexMut<&'a SearchFor> for Map<Key, V>
source§impl<'a, Key, Value> IntoIterator for &'a Map<Key, Value>where
Key: Sort<Key>,
impl<'a, Key, Value> IntoIterator for &'a Map<Key, Value>where
Key: Sort<Key>,
source§impl<'a, Key, Value> IntoIterator for &'a mut Map<Key, Value>where
Key: Sort<Key>,
impl<'a, Key, Value> IntoIterator for &'a mut Map<Key, Value>where
Key: Sort<Key>,
source§impl<Key, Value> IntoIterator for Map<Key, Value>where
Key: Sort<Key>,
impl<Key, Value> IntoIterator for Map<Key, Value>where
Key: Sort<Key>,
source§impl<Key, Value: Ord> Ord for Map<Key, Value>
impl<Key, Value: Ord> Ord for Map<Key, Value>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<Key, Value: PartialEq> PartialEq for Map<Key, Value>
impl<Key, Value: PartialEq> PartialEq for Map<Key, Value>
source§impl<Key, Value: PartialOrd> PartialOrd for Map<Key, Value>where
Key: Sort<Key> + PartialOrd,
impl<Key, Value: PartialOrd> PartialOrd for Map<Key, Value>where
Key: Sort<Key> + PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more