Struct VecHashMap

Source
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>

Source

pub fn new() -> Self

Creates a new empty VecHashMap.

§Examples
use ordered_hashmap::VecHashMap;
let ordered_map: VecHashMap<String, i32> = VecHashMap::new();
Source

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn len(&self) -> usize

Returns the number of key-value pairs in the map.

§Examples
use ordered_hashmap::VecHashMap;

let mut map = VecHashMap::new();
assert_eq!(map.len(), 0);

map.insert(1, "one");
assert_eq!(map.len(), 1);

map.insert(2, "two");
assert_eq!(map.len(), 2);

Trait Implementations§

Source§

impl<K: Clone + Eq + Hash + Clone, V: Clone> Clone for VecHashMap<K, V>

Source§

fn clone(&self) -> VecHashMap<K, V>

Returns a duplicate 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<K: Debug + Eq + Hash + Clone, V: Debug> Debug for VecHashMap<K, V>

Source§

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

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

impl<K: PartialEq + Eq + Hash + Clone, V: PartialEq> PartialEq for VecHashMap<K, V>

Source§

fn eq(&self, other: &VecHashMap<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Eq + Hash + Clone, V> StructuralPartialEq for VecHashMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for VecHashMap<K, V>

§

impl<K, V> RefUnwindSafe for VecHashMap<K, V>

§

impl<K, V> Send for VecHashMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for VecHashMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for VecHashMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for VecHashMap<K, V>
where K: UnwindSafe, V: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.