1 #![allow(dead_code)]
2 
3 #[derive(Clone)]
4 pub struct VecMap<K, V> {
5     vec: Vec<(K, V)>,
6 }
7 
8 impl<K: PartialEq, V> VecMap<K, V> {
9     #[inline]
with_capacity(cap: usize) -> VecMap<K, V>10     pub fn with_capacity(cap: usize) -> VecMap<K, V> {
11         VecMap {
12             vec: Vec::with_capacity(cap)
13         }
14     }
15 
16     #[inline]
insert(&mut self, key: K, value: V)17     pub fn insert(&mut self, key: K, value: V) {
18         match self.find(&key) {
19             Some(pos) => self.vec[pos] = (key, value),
20             None => self.vec.push((key, value))
21         }
22     }
23 
24     #[inline]
entry(&mut self, key: K) -> Entry<K, V>25     pub fn entry(&mut self, key: K) -> Entry<K, V> {
26         match self.find(&key) {
27             Some(pos) => Entry::Occupied(OccupiedEntry {
28                 vec: self,
29                 pos: pos,
30             }),
31             None => Entry::Vacant(VacantEntry {
32                 vec: self,
33                 key: key,
34             })
35         }
36     }
37 
38     #[inline]
get<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<&V>39     pub fn get<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<&V> {
40         self.find(key).map(move |pos| &self.vec[pos].1)
41     }
42 
43     #[inline]
get_mut<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<&mut V>44     pub fn get_mut<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<&mut V> {
45         self.find(key).map(move |pos| &mut self.vec[pos].1)
46     }
47 
48     #[inline]
contains_key<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> bool49     pub fn contains_key<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> bool {
50         self.find(key).is_some()
51     }
52 
53     #[inline]
len(&self) -> usize54     pub fn len(&self) -> usize { self.vec.len() }
55 
56     #[inline]
iter(&self) -> ::std::slice::Iter<(K, V)>57     pub fn iter(&self) -> ::std::slice::Iter<(K, V)> {
58         self.vec.iter()
59     }
60     #[inline]
remove<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<V>61     pub fn remove<K2: PartialEq<K> + ?Sized>(&mut self, key: &K2) -> Option<V> {
62         self.find(key).map(|pos| self.vec.remove(pos)).map(|(_, v)| v)
63     }
64     #[inline]
clear(&mut self)65     pub fn clear(&mut self) {
66         self.vec.clear();
67     }
68 
69     #[inline]
find<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<usize>70     fn find<K2: PartialEq<K> + ?Sized>(&self, key: &K2) -> Option<usize> {
71         self.vec.iter().position(|entry| key == &entry.0)
72     }
73 }
74 
75 pub enum Entry<'a, K: 'a, V: 'a> {
76     Vacant(VacantEntry<'a, K, V>),
77     Occupied(OccupiedEntry<'a, K, V>)
78 }
79 
80 pub struct VacantEntry<'a, K: 'a, V: 'a> {
81     vec: &'a mut VecMap<K, V>,
82     key: K,
83 }
84 
85 impl<'a, K, V> VacantEntry<'a, K, V> {
insert(self, val: V) -> &'a mut V86     pub fn insert(self, val: V) -> &'a mut V {
87         let vec = self.vec;
88         vec.vec.push((self.key, val));
89         let pos = vec.vec.len() - 1;
90         &mut vec.vec[pos].1
91     }
92 }
93 
94 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
95     vec: &'a mut VecMap<K, V>,
96     pos: usize,
97 }
98 
99 impl<'a, K, V> OccupiedEntry<'a, K, V> {
into_mut(self) -> &'a mut V100     pub fn into_mut(self) -> &'a mut V {
101         &mut self.vec.vec[self.pos].1
102     }
103 }
104