|
| 1 | +use std::borrow::Borrow; |
| 2 | +use std::iter::FromIterator; |
| 3 | +use std::slice::{Iter, IterMut}; |
| 4 | +use std::vec::IntoIter; |
| 5 | + |
| 6 | +use crate::stable_hasher::{HashStable, StableHasher}; |
| 7 | + |
| 8 | +/// A map type implemented as a vector of pairs `K` (key) and `V` (value). |
| 9 | +/// It currently provides a subset of all the map operations, the rest could be added as needed. |
| 10 | +#[derive(Clone, Encodable, Decodable, Debug)] |
| 11 | +pub struct VecMap<K, V>(Vec<(K, V)>); |
| 12 | + |
| 13 | +impl<K, V> VecMap<K, V> |
| 14 | +where |
| 15 | + K: PartialEq, |
| 16 | +{ |
| 17 | + pub fn new() -> Self { |
| 18 | + VecMap(Default::default()) |
| 19 | + } |
| 20 | + |
| 21 | + /// Sets the value of the entry, and returns the entry's old value. |
| 22 | + pub fn insert(&mut self, k: K, v: V) -> Option<V> { |
| 23 | + if let Some(elem) = self.0.iter_mut().find(|(key, _)| *key == k) { |
| 24 | + Some(std::mem::replace(&mut elem.1, v)) |
| 25 | + } else { |
| 26 | + self.0.push((k, v)); |
| 27 | + None |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Gets a reference to the value in the entry. |
| 32 | + pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> |
| 33 | + where |
| 34 | + K: Borrow<Q>, |
| 35 | + Q: Eq, |
| 36 | + { |
| 37 | + self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1) |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns the value corresponding to the supplied predicate filter. |
| 41 | + /// |
| 42 | + /// The supplied predicate will be applied to each (key, value) pair and it will return a |
| 43 | + /// reference to the values where the predicate returns `true`. |
| 44 | + pub fn get_by(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> { |
| 45 | + self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1) |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns `true` if the map contains a value for the specified key. |
| 49 | + /// |
| 50 | + /// The key may be any borrowed form of the map's key type, |
| 51 | + /// [`Eq`] on the borrowed form *must* match those for |
| 52 | + /// the key type. |
| 53 | + pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool |
| 54 | + where |
| 55 | + K: Borrow<Q>, |
| 56 | + Q: Eq, |
| 57 | + { |
| 58 | + self.get(k).is_some() |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns `true` if the map contains no elements. |
| 62 | + pub fn is_empty(&self) -> bool { |
| 63 | + self.0.is_empty() |
| 64 | + } |
| 65 | + |
| 66 | + pub fn iter(&self) -> Iter<'_, (K, V)> { |
| 67 | + self.into_iter() |
| 68 | + } |
| 69 | + |
| 70 | + pub fn iter_mut(&mut self) -> IterMut<'_, (K, V)> { |
| 71 | + self.into_iter() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<K, V> Default for VecMap<K, V> { |
| 76 | + #[inline] |
| 77 | + fn default() -> Self { |
| 78 | + Self(Default::default()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<K, V> From<Vec<(K, V)>> for VecMap<K, V> { |
| 83 | + fn from(vec: Vec<(K, V)>) -> Self { |
| 84 | + Self(vec) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<K, V> Into<Vec<(K, V)>> for VecMap<K, V> { |
| 89 | + fn into(self) -> Vec<(K, V)> { |
| 90 | + self.0 |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<K, V> FromIterator<(K, V)> for VecMap<K, V> { |
| 95 | + fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self { |
| 96 | + Self(iter.into_iter().collect()) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<'a, K, V> IntoIterator for &'a VecMap<K, V> { |
| 101 | + type Item = &'a (K, V); |
| 102 | + type IntoIter = Iter<'a, (K, V)>; |
| 103 | + |
| 104 | + #[inline] |
| 105 | + fn into_iter(self) -> Self::IntoIter { |
| 106 | + self.0.iter() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V> { |
| 111 | + type Item = &'a mut (K, V); |
| 112 | + type IntoIter = IterMut<'a, (K, V)>; |
| 113 | + |
| 114 | + #[inline] |
| 115 | + fn into_iter(self) -> Self::IntoIter { |
| 116 | + self.0.iter_mut() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<K, V> IntoIterator for VecMap<K, V> { |
| 121 | + type Item = (K, V); |
| 122 | + type IntoIter = IntoIter<(K, V)>; |
| 123 | + |
| 124 | + #[inline] |
| 125 | + fn into_iter(self) -> Self::IntoIter { |
| 126 | + self.0.into_iter() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<K, V> Extend<(K, V)> for VecMap<K, V> { |
| 131 | + fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) { |
| 132 | + self.0.extend(iter); |
| 133 | + } |
| 134 | + |
| 135 | + fn extend_one(&mut self, item: (K, V)) { |
| 136 | + self.0.extend_one(item); |
| 137 | + } |
| 138 | + |
| 139 | + fn extend_reserve(&mut self, additional: usize) { |
| 140 | + self.0.extend_reserve(additional); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +impl<K, V, CTX> HashStable<CTX> for VecMap<K, V> |
| 145 | +where |
| 146 | + K: HashStable<CTX> + Eq, |
| 147 | + V: HashStable<CTX>, |
| 148 | +{ |
| 149 | + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { |
| 150 | + self.0.hash_stable(hcx, hasher) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +#[cfg(test)] |
| 155 | +mod tests; |
0 commit comments