1use smallvec_stableunion::SmallVec;
10
11#[derive(Clone, Debug)]
12pub struct PlainMap<Key: Copy, Value: Copy> {
13 pairs: SmallVec<[(Key, Value); 2]>,
14
15}
16impl<Key: PartialEq + Copy, Value: Copy> PlainMap<Key, Value> {
17 pub fn new() -> Self {
18 Self {
19 pairs: SmallVec::new(),
20
21 }
22 }
23 pub fn with_capacity(capacity: usize) -> Self {
24 Self {
25 pairs: SmallVec::with_capacity(capacity),
26 }
27 }
28
29 pub fn reserve(&mut self, additional: usize) {
30 self.pairs.reserve(additional)
31 }
32 pub fn shrink_to_fit(&mut self) {
33 self.pairs.shrink_to_fit();
34 }
35
36
37 fn find(&self, k: &Key) -> Option<usize> {
38 self.pairs.iter().position(|(x,_)| *x == *k)
39 }
40
41 pub fn insert(&mut self, k: Key, v: Value) -> Option<Value> {
42 match self.find(&k) {
43 Some(i) => {
44 Some(std::mem::replace(&mut self.pairs[i].1, v))
48 },
49 None => {
50 self.pairs.push((k,v));
51 None
52 }
53 }
54 }
55
56 pub fn get(&self, k: &Key) -> Option<&Value> {
57 match self.find(k) {
58 Some(i) => Some(&self.pairs[i].1),
59 None => None
60 }
61 }
62 pub fn get_mut(&mut self, k: &Key) -> Option<&mut Value> {
63 match self.find(k) {
64 Some(i) => Some(&mut self.pairs[i].1),
65 None => None
66 }
67 }
68 pub fn get_key_value(&self, k: &Key) -> Option<&(Key, Value)> {
69 match self.find(k) {
70 Some(i) => Some(&self.pairs[i]),
71 None => None
72 }
73 }
74 pub fn get_key_value_mut(&mut self, k: &Key) -> Option<&mut (Key, Value)> {
75 match self.find(k) {
76 Some(i) => Some(&mut self.pairs[i]),
77 None => None
78 }
79 }
80
81 pub fn contains_key(&self, k: &Key) -> bool {
82 self.find(k).is_some()
83 }
84
85 pub fn remove(&mut self, k: &Key) -> Option<Value> {
86 match self.find(k) {
87 Some(i) => Some(self.pairs.swap_remove(i).1),
88 None => None,
89 }
90 }
91 pub fn remove_entry(&mut self, k: &Key) -> Option<(Key, Value)> {
92 match self.find(k) {
93 Some(i) => Some(self.pairs.swap_remove(i)),
94 None => None,
95 }
96 }
97
98 pub fn entry(&mut self, k: Key) -> PlainMapEntry<Key, Value> {
99 PlainMapEntry {
100 parent: self,
101 k: k
102 }
103 }
104
105 pub fn capacity(&self) -> usize {
106 self.pairs.capacity()
107 }
108 pub fn len(&self) -> usize {
109 self.pairs.len()
110 }
111 pub fn is_empty(&self) -> bool {
112 self.pairs.is_empty()
113 }
114
115 pub fn clear(&mut self) {
116 self.pairs.clear();
117 }
118
119 pub fn iter(&self) -> std::slice::Iter<(Key, Value)> {
120 self.pairs.iter()
121 }
122 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (Key, Value)> {
123 self.pairs.iter_mut()
124 }
125
126 pub fn keys(&self) -> impl Iterator<Item = &Key> {
127 self.pairs.iter().map(|(k,_v)| k)
128 }
129 pub fn keys_mut(&mut self) -> impl Iterator<Item = &mut Key> {
130 self.pairs.iter_mut().map(|(k,_v)| k)
131 }
132 pub fn values(&self) -> impl Iterator<Item = &Value> {
133 self.pairs.iter().map(|(_k,v)| v)
134 }
135 pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Value> {
136 self.pairs.iter_mut().map(|(_k,v)| v)
137 }
138
139 pub fn drain(&mut self) -> smallvec_stableunion::Drain<'_, (Key, Value)> {
140 self.pairs.drain()
141 }
142}
143
144pub struct PlainMapEntry<'a, Key: Copy, Value: Copy> {
145 parent: &'a mut PlainMap<Key, Value>,
146 k: Key,
147}
148impl<'a, Key: PartialEq + Copy, Value: Copy> PlainMapEntry<'a, Key, Value> {
149 pub fn or_insert(self, default: Value) -> &'a mut Value {
150 match self.parent.find(&self.k) {
151 Some(i) => &mut self.parent.pairs[i].1,
152 None => {
153 self.parent.pairs.push((self.k, default));
154 &mut self.parent.pairs.last_mut().unwrap().1
155 }
156 }
157 }
158 pub fn or_insert_with<F: FnOnce() -> Value>(self, default: F) -> &'a mut Value {
159 match self.parent.find(&self.k) {
160 Some(i) => &mut self.parent.pairs[i].1,
161 None => {
162 self.parent.pairs.push((self.k, default()));
163 &mut self.parent.pairs.last_mut().unwrap().1
164 }
165 }
166
167 }
168 pub fn and_modify<F: FnOnce(&mut Value)>(self, f: F) -> Self {
169 match self.parent.find(&self.k) {
170 Some(i) => f(&mut self.parent.pairs[i].1),
171 None => {}
172 }
173 self
174 }
175 pub fn key(&self) -> &Key {
176 &self.k
177 }
178}
179impl<'a, Key: PartialEq + Copy, Value: Default + Copy> PlainMapEntry<'a, Key, Value> {
180 pub fn or_default(self) -> &'a mut Value {
181 self.or_insert_with(Value::default)
182 }
183}