pub struct OrderedMap<V> { /* private fields */ }Expand description
A map from String to V that remembers the order its keys arrived in.
use structio::OrderedMap;
let mut map = OrderedMap::new();
map.insert("zeta".to_string(), 1);
map.insert("alpha".to_string(), 2);
map.insert("zeta".to_string(), 3); // replaces, keeps position
assert_eq!(map.get("zeta"), Some(&3));
assert_eq!(
map.keys().map(String::as_str).collect::<Vec<_>>(),
["zeta", "alpha"]
);See the module docs for how lookups work and what the ordering does and does not extend to.
Implementations§
Source§impl<V> OrderedMap<V>
impl<V> OrderedMap<V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
An empty map with room for capacity entries.
The table is not allocated here, however large the capacity. A table
that exists describes every entry, so one built ahead of the entries
would have to be maintained through the first eight inserts that the
whole point of LINEAR_MAX is to leave alone; the map builds it at the
entry that first needs it, sized from the entries it actually holds.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drop every entry, keeping the entries’ allocated capacity.
The table goes, since an empty map scans; a map refilled to past
LINEAR_MAX builds a new one.
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut V>
pub fn get_mut(&mut self, key: &str) -> Option<&mut V>
A mutable reference to the value key maps to.
The key itself is never handed out mutably: changing it would move the entry to a different place in the index without moving the slot that points at it.
Sourcepub fn get_key_value(&self, key: &str) -> Option<(&str, &V)>
pub fn get_key_value(&self, key: &str) -> Option<(&str, &V)>
The stored key and its value.
The key comes back as &str rather than &String, as it does from
every other accessor here; only Self::iter hands out &String,
because the writers take their entries that way.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Whether key is present.
Sourcepub fn insert(&mut self, key: String, value: V) -> Option<V>
pub fn insert(&mut self, key: String, value: V) -> Option<V>
Insert a key and value, returning the previous value if the key was already present.
A duplicate key keeps the position it already had: the map records where
a key first appeared, and writing to it again is not a reason to move
it. This is the one difference from BTreeMap::insert, which has no
position to keep.
There is no separate duplicate check to pay for. The probe that would find an existing key is the same probe that finds the bucket a new one goes in, so an insert costs one walk of the table either way.
Sourcepub fn remove(&mut self, key: &str) -> Option<V>
pub fn remove(&mut self, key: &str) -> Option<V>
Remove key, returning its value.
Entries after it shift down to close the gap, which is O(n) and is
what keeps the order intact. A swap with the last entry would be O(1)
and would scramble exactly the thing this map exists to preserve.
Sourcepub fn remove_entry(&mut self, key: &str) -> Option<(String, V)>
pub fn remove_entry(&mut self, key: &str) -> Option<(String, V)>
Remove key, returning the stored key along with its value.
Sourcepub fn sort_keys(&mut self)
pub fn sort_keys(&mut self)
Reorder the entries by key.
The escape hatch for a caller who wants a BTreeMap’s output: sort
once, deliberately, instead of paying for an ordering on every read. The
order the map was built in is gone afterwards.
Sourcepub fn get_index(&self, index: usize) -> Option<(&str, &V)>
pub fn get_index(&self, index: usize) -> Option<(&str, &V)>
The key and value at position index, in insertion order.
Sourcepub fn get_index_mut(&mut self, index: usize) -> Option<(&str, &mut V)>
pub fn get_index_mut(&mut self, index: usize) -> Option<(&str, &mut V)>
The key and mutable value at position index, in insertion order.
Sourcepub fn entry(&mut self, key: String) -> Entry<'_, V>
pub fn entry(&mut self, key: String) -> Entry<'_, V>
The entry for key, occupied or vacant.
Takes an owned key because a vacant entry will store it, and looking up
by &str first only to allocate the same string afterwards is the cost
this avoids.
Sourcepub fn iter(&self) -> Iter<'_, V> ⓘ
pub fn iter(&self) -> Iter<'_, V> ⓘ
The entries, in insertion order.
Yields (&String, &V) rather than &(String, V), which is the shape
the JSON and BEVE writers take their keyed entries in, so a map goes
straight into one.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, V> ⓘ
The entries with mutable values, in insertion order.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, V> ⓘ
The values, mutably, in insertion order.
Trait Implementations§
Source§impl<V: Clone> Clone for OrderedMap<V>
impl<V: Clone> Clone for OrderedMap<V>
Source§impl<V: Debug> Debug for OrderedMap<V>
impl<V: Debug> Debug for OrderedMap<V>
Source§impl<V> Default for OrderedMap<V>
impl<V> Default for OrderedMap<V>
impl<V: Eq> Eq for OrderedMap<V>
Sound because keys are unique: equal lengths plus a value-for-value match in one direction is a bijection, so the relation is symmetric and transitive.
Source§impl<V> Extend<(String, V)> for OrderedMap<V>
impl<V> Extend<(String, V)> for OrderedMap<V>
Source§fn extend<I: IntoIterator<Item = (String, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (String, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<V> FromIterator<(String, V)> for OrderedMap<V>
impl<V> FromIterator<(String, V)> for OrderedMap<V>
Source§impl<V> Index<&str> for OrderedMap<V>
impl<V> Index<&str> for OrderedMap<V>
Source§impl<V> IntoIterator for OrderedMap<V>
impl<V> IntoIterator for OrderedMap<V>
Source§impl<'a, V> IntoIterator for &'a OrderedMap<V>
impl<'a, V> IntoIterator for &'a OrderedMap<V>
Source§impl<'a, V> IntoIterator for &'a mut OrderedMap<V>
impl<'a, V> IntoIterator for &'a mut OrderedMap<V>
Source§impl<V: PartialEq> PartialEq for OrderedMap<V>
Equality is over the entries, not over their order: see the module
docs.
impl<V: PartialEq> PartialEq for OrderedMap<V>
Equality is over the entries, not over their order: see the module docs.
Source§impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>
OrderedMap keeps the order its members arrived in, which is what makes
it the map behind Object.
impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>
OrderedMap keeps the order its members arrived in, which is what makes
it the map behind Object.
Hand-written rather than an arm of impl_map!: that macro is generic over
the key type, and this map’s key is always a String. It reserves on the
member count, as the hashed map does, since its entries live in one vector.
Source§impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>
OrderedMap keeps the order its members arrived in, which is what makes
it the map behind Object.
impl<'de, V: Read<'de> + Default> Read<'de> for OrderedMap<V>
OrderedMap keeps the order its members arrived in, which is what makes
it the map behind Object.
Hand-written rather than an arm of impl_map!: that macro is generic over
the key type, and this map’s key is always a String.
Source§impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>where
VA: ReadAs<'de, V>,
OrderedMap’s adapter form takes one adapter rather than two, since its
key type is fixed: the object header is always the string-keyed one, and the
keys go through String’s own FromBeveKey/ToBeveKey.
impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>where
VA: ReadAs<'de, V>,
OrderedMap’s adapter form takes one adapter rather than two, since its
key type is fixed: the object header is always the string-keyed one, and the
keys go through String’s own FromBeveKey/ToBeveKey.
Source§impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>where
VA: ReadAs<'de, V>,
OrderedMap’s adapter form takes one adapter rather than two, since its
key type is fixed: OrderedMap<Millis> adapts the values and leaves the
keys to String’s own FromJsonKey/ToJsonKey.
impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>where
VA: ReadAs<'de, V>,
OrderedMap’s adapter form takes one adapter rather than two, since its
key type is fixed: OrderedMap<Millis> adapts the values and leaves the
keys to String’s own FromJsonKey/ToJsonKey.
Source§impl<V: Write> Write for OrderedMap<V>
impl<V: Write> Write for OrderedMap<V>
fn write<O: Options>(&self, w: &mut Writer<'_, O>)
Source§fn is_null(&self) -> bool
fn is_null(&self) -> bool
Options::SKIP_NULL. Read moreSource§impl<V: Write> Write for OrderedMap<V>
impl<V: Write> Write for OrderedMap<V>
Source§impl<VA, V> WriteAs<OrderedMap<V>> for OrderedMap<VA>where
VA: WriteAs<V>,
impl<VA, V> WriteAs<OrderedMap<V>> for OrderedMap<VA>where
VA: WriteAs<V>,
Source§fn is_null(value: &T) -> bool
fn is_null(value: &T) -> bool
Options::SKIP_NULL. Read more