pub struct VecHashMap<K: Eq + Hash + Clone, V> {
pub values: Vec<(K, V)>,
pub map: HashMap<K, usize>,
}
Expand description
A custom VecHashMap
struct that maintains the order of keys.
It wraps a Vec
to store keys and a HashMap
to store key-value pairs.
Fields§
§values: Vec<(K, V)>
§map: HashMap<K, usize>
Implementations§
Source§impl<K: Eq + Hash + Clone, V> VecHashMap<K, V>
impl<K: Eq + Hash + Clone, V> VecHashMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty VecHashMap
.
§Examples
use ordered_hashmap::VecHashMap;
let ordered_map: VecHashMap<String, i32> = VecHashMap::new();
Sourcepub fn contains_key(&self, k: &K) -> bool
pub fn contains_key(&self, k: &K) -> bool
Returns true
if the map contains a value for the specified key.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
assert!(ordered_map.contains_key(&"key1".to_string()));
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Inserts a key-value pair into the map. If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old value is returned.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 99);
assert_eq!(ordered_map.get(&"key1".to_string()), Some(&99));
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
assert_eq!(ordered_map.remove(&"key1".to_string()), Some(42));
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Gets the value of the specified key.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
assert_eq!(ordered_map.get(&"key1".to_string()), Some(&42));
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Gets a mutable reference to the value of the specified key.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
*ordered_map.get_mut(&"key1".to_string()).unwrap() += 1;
assert_eq!(ordered_map.get(&"key1".to_string()), Some(&43));
Sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
Returns an iterator over the values in the ordered hash map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
let values: Vec<_> = ordered_map.values().collect();
assert_eq!(values, vec![&42, &24]);
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
Returns a mutable iterator over the values in the ordered hash map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
ordered_map.values_mut().for_each(|value| *value += 1);
assert_eq!(ordered_map.get(&"key1".to_string()), Some(&43));
assert_eq!(ordered_map.get(&"key2".to_string()), Some(&25));
Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over the keys in the ordered hash map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
let keys: Vec<_> = ordered_map.keys().collect();
assert_eq!(keys, vec![&"key1".to_string(), &"key2".to_string()]);
Sourcepub fn iter(&self) -> impl Iterator<Item = &(K, V)>
pub fn iter(&self) -> impl Iterator<Item = &(K, V)>
Returns an iterator over the key-value pairs in the ordered hash map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
let pairs: Vec<_> = ordered_map.iter().collect();
assert_eq!(pairs, vec![&("key1".to_string(), 42), &("key2".to_string(), 24)]);
Sourcepub fn into_iter(self) -> IntoIter<(K, V)>
pub fn into_iter(self) -> IntoIter<(K, V)>
Returns an into iterator of the key-value pairs in the map, in the order corresponding to their keys.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
let pairs: Vec<_> = ordered_map.iter().collect();
assert_eq!(pairs, vec![&("key1".to_string(), 42), &("key2".to_string(), 24)]);
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (K, V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (K, V)>
Returns a mutable iterator over the key-value pairs in the ordered hash map.
§Examples
use ordered_hashmap::VecHashMap;
let mut ordered_map = VecHashMap::new();
ordered_map.insert("key1".to_string(), 42);
ordered_map.insert("key2".to_string(), 24);
ordered_map.iter_mut().for_each(|(key, value)| *value += 1);
assert_eq!(ordered_map.get(&"key1".to_string()), Some(&43));
assert_eq!(ordered_map.get(&"key2".to_string()), Some(&25));
Sourcepub fn into_values(self) -> VecHashMapIntoValuesIter<K, V> ⓘ
pub fn into_values(self) -> VecHashMapIntoValuesIter<K, V> ⓘ
Returns a vector of the values in the map, in the order corresponding to their keys.
§Examples
use ordered_hashmap::VecHashMap;
let mut map = VecHashMap::new();
map.insert(1, "one");
map.insert(2, "two");
map.insert(3, "three");
let values: Vec<_> = map.into_values().collect();
assert_eq!(values, vec!["one", "two", "three"]);
Sourcepub fn extend(&mut self, slice: VecHashMap<K, V>)
pub fn extend(&mut self, slice: VecHashMap<K, V>)
Adds all key-value pairs from another VecHashMap
to this one, without replacing any existing pairs.
§Examples
use ordered_hashmap::VecHashMap;
let mut map1 = VecHashMap::new();
map1.insert(1, "one");
let mut map2 = VecHashMap::new();
map2.insert(2, "two");
map1.extend(map2);
assert_eq!(map1.get(&1), Some(&"one"));
assert_eq!(map1.get(&2), Some(&"two"));
Trait Implementations§
Source§impl<K: Clone + Eq + Hash + Clone, V: Clone> Clone for VecHashMap<K, V>
impl<K: Clone + Eq + Hash + Clone, V: Clone> Clone for VecHashMap<K, V>
Source§fn clone(&self) -> VecHashMap<K, V>
fn clone(&self) -> VecHashMap<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more