1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::borrow::Borrow;

use crate::vec::{Entry, OccupiedEntry, VacantEntry};

/// A trait extension that allows vectors to be treated as associative arrays.
pub trait AssocExt<K, V> {
    /// Get a key's entry for in-place manipulation.
    ///
    /// ```rust
    /// use assoc::AssocExt;
    ///
    /// let mut count = Vec::new();
    /// for x in vec!["a", "b", "c", "b"] {
    ///     *count.entry(x).or_insert(0) += 1;
    /// }
    /// assert_eq!(count.get(&"b"), Some(&2));
    /// ```
    fn entry(&mut self, key: K) -> Entry<K, V>;

    /// Get a reference to the value associated with a key.
    ///
    /// ```rust
    /// use assoc::AssocExt;
    ///
    /// let map = vec![("a", 1), ("b", 2)];
    /// assert_eq!(map.get(&"a"), Some(&1));
    /// ```
    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;

    /// Get a mutable reference to the value associated with a key.
    ///
    /// ```rust
    /// use assoc::AssocExt;
    ///
    /// let mut map = vec![("a", 1), ("b", 2)];
    /// *map.get_mut(&"a").unwrap() += 1;
    /// assert_eq!(map.get(&"a"), Some(&2));
    /// ```
    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;

    /// Insert a key-value pair into the associative array.
    /// If the map previously had the key, then the old value is returned. Otherwise, `None` is
    /// returned.
    ///
    /// ```rust
    /// use assoc::AssocExt;
    ///
    /// let mut map = vec![("b", 3)];
    /// assert_eq!(AssocExt::insert(&mut map, "a", 1), None);
    /// assert_eq!(AssocExt::insert(&mut map, "a", 2), Some(1));
    /// ```
    fn insert(&mut self, key: K, value: V) -> Option<V>;

    /// Remove a key from the map, returning the value if it was previously in the map.
    ///
    /// ```rust
    /// use assoc::AssocExt;
    ///
    /// let mut map = vec![("a", 1)];
    /// assert_eq!(AssocExt::remove(&mut map, "a"), Some(1));
    /// assert_eq!(AssocExt::remove(&mut map, "a"), None);
    /// ```
    fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;
}

impl<K, V> AssocExt<K, V> for Vec<(K, V)>
where
    K: PartialEq,
{
    fn entry(&mut self, key: K) -> Entry<K, V> {
        let found = self.iter_mut().enumerate().find(|(_, (k, _))| k == &key);
        match found {
            None => Entry::Vacant(VacantEntry::new(self, key)),
            Some((index, _)) => Entry::Occupied(OccupiedEntry::new(self, key, index)),
        }
    }

    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        self.iter().find(|(k, _)| k.borrow() == key).map(|(_, v)| v)
    }

    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        self.iter_mut()
            .find(|(k, _)| k.borrow() == key)
            .map(|(_, v)| v)
    }

    fn insert(&mut self, key: K, value: V) -> Option<V> {
        match self.entry(key) {
            Entry::Occupied(mut entry) => Some(entry.insert(value)),
            Entry::Vacant(entry) => {
                entry.insert(value);
                None
            }
        }
    }

    fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        let found = self
            .iter_mut()
            .enumerate()
            .find(|(_, (k, _))| k.borrow() == key);
        match found {
            None => None,
            Some((index, _)) => {
                let (_, v) = self.swap_remove(index);
                Some(v)
            }
        }
    }
}

/// This has the same API as [`AssocExt`] but with the additional constraint `K: Eq`.
///
/// ```compile_fail
/// use assoc::AssocStrictExt;
///
/// let map = vec![(1.0, 1), (2.0, 2)];
/// map.entry(1.0);
/// ```
pub trait AssocStrictExt<K, V> {
    /// Get a key's entry for in-place manipulation.
    ///
    /// ```rust
    /// use assoc::AssocStrictExt;
    ///
    /// let mut count = Vec::new();
    /// for x in vec!["a", "b", "c", "b"] {
    ///     *count.entry(x).or_insert(0) += 1;
    /// }
    /// assert_eq!(count.get(&"b"), Some(&2));
    /// ```
    fn entry(&mut self, key: K) -> Entry<K, V>;

    /// Get a reference to the value associated with a key.
    ///
    /// ```rust
    /// use assoc::AssocStrictExt;
    ///
    /// let map = vec![("a", 1), ("b", 2)];
    /// assert_eq!(map.get(&"a"), Some(&1));
    /// ```
    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;

    /// Get a mutable reference to the value associated with a key.
    ///
    /// ```rust
    /// use assoc::AssocStrictExt;
    ///
    /// let mut map = vec![("a", 1), ("b", 2)];
    /// *map.get_mut(&"a").unwrap() += 1;
    /// assert_eq!(map.get(&"a"), Some(&2));
    /// ```
    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;

    /// Insert a key-value pair into the associative array.
    /// If the map previously had the key, then the old value is returned. Otherwise, `None` is
    /// returned.
    ///
    /// ```rust
    /// use assoc::AssocStrictExt;
    ///
    /// let mut map = vec![("b", 3)];
    /// assert_eq!(AssocStrictExt::insert(&mut map, "a", 1), None);
    /// assert_eq!(AssocStrictExt::insert(&mut map, "a", 2), Some(1));
    /// ```
    fn insert(&mut self, key: K, value: V) -> Option<V>;

    /// Remove a key from the map, returning the value if it was previously in the map.
    ///
    /// ```rust
    /// use assoc::AssocStrictExt;
    ///
    /// let mut map = vec![("a", 1)];
    /// assert_eq!(AssocStrictExt::remove(&mut map, "a"), Some(1));
    /// assert_eq!(AssocStrictExt::remove(&mut map, "a"), None);
    /// ```
    fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized;
}

impl<K, V> AssocStrictExt<K, V> for Vec<(K, V)>
where
    K: Eq,
{
    fn entry(&mut self, key: K) -> Entry<K, V> {
        AssocExt::entry(self, key)
    }

    fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        AssocExt::get(self, key)
    }

    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        AssocExt::get_mut(self, key)
    }

    fn insert(&mut self, key: K, value: V) -> Option<V> {
        AssocExt::insert(self, key, value)
    }

    fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: PartialEq + ?Sized,
    {
        AssocExt::remove(self, key)
    }
}