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
247
248
249
250
251
252
use std::fmt;
#[derive(Clone)]
pub struct MiniMap<K, V> {
data: Vec<(K, V)>,
}
impl<K, V> MiniMap<K, V>
where
K: Ord,
{
pub fn new() -> Self {
Self { data: vec![] }
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
match self.data.binary_search_by_key(&&key, |e| &e.0) {
Ok(idx) => Some(std::mem::replace(&mut self.data[idx], (key, value)).1),
Err(idx) => {
self.data.insert(idx, (key, value));
None
}
}
}
pub fn get(&self, key: &K) -> Option<&V> {
match self.data.binary_search_by_key(&key, |e| &e.0) {
Ok(idx) => Some(&self.data[idx].1),
Err(_) => None,
}
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
match self.data.binary_search_by_key(&key, |e| &e.0) {
Ok(idx) => Some(&mut self.data[idx].1),
Err(_) => None,
}
}
pub fn into_inner(self) -> Vec<(K, V)> {
self.data
}
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
match self.data.binary_search_by_key(&&key, |e| &e.0) {
Ok(idx) => Entry::Occupied(OccupiedEntry {
data: &mut self.data,
index: idx,
}),
Err(idx) => Entry::Vacant(VacantEntry {
data: &mut self.data,
key,
insert_position: idx,
}),
}
}
}
impl<K, V> Default for MiniMap<K, V>
where
K: Ord,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V> fmt::Debug for MiniMap<K, V>
where
K: Ord,
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map()
.entries(self.data.iter().map(|(k, v)| (k, v)))
.finish()
}
}
pub enum Entry<'a, K, V>
where
K: 'a,
V: 'a,
{
Vacant(VacantEntry<'a, K, V>),
Occupied(OccupiedEntry<'a, K, V>),
}
impl<'a, K, V> Entry<'a, K, V>
where
K: 'a,
V: 'a,
{
pub fn or_insert(self, default: V) -> &'a mut V {
self.or_insert_with(|| default)
}
pub fn or_insert_with<F>(self, default: F) -> &'a mut V
where
F: FnOnce() -> V,
{
match self {
Self::Vacant(entry) => entry.insert(default()),
Self::Occupied(entry) => entry.into_mut(),
}
}
pub fn key(&self) -> &K {
match self {
Self::Vacant(entry) => entry.key(),
Self::Occupied(entry) => entry.key(),
}
}
pub fn and_modify<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Self::Vacant(_) => self,
Self::Occupied(ref mut entry) => {
f(entry.get_mut());
self
}
}
}
pub fn or_default<F>(self) -> &'a mut V
where
V: Default,
{
self.or_insert_with(Default::default)
}
}
pub struct VacantEntry<'a, K, V>
where
K: 'a,
V: 'a,
{
data: &'a mut Vec<(K, V)>,
key: K,
insert_position: usize,
}
impl<'a, K, V> VacantEntry<'a, K, V>
where
K: 'a,
V: 'a,
{
pub fn key(&self) -> &K {
&self.key
}
pub fn into_key(self) -> K {
self.key
}
pub fn insert(self, value: V) -> &'a mut V {
self.data.insert(self.insert_position, (self.key, value));
&mut self.data[self.insert_position].1
}
}
pub struct OccupiedEntry<'a, K, V>
where
K: 'a,
V: 'a,
{
data: &'a mut Vec<(K, V)>,
index: usize,
}
impl<'a, K, V> OccupiedEntry<'a, K, V>
where
K: 'a,
V: 'a,
{
pub fn key(&self) -> &K {
&self.data[self.index].0
}
pub fn remove_entry(self) -> (K, V) {
self.data.remove(self.index)
}
pub fn get(&self) -> &V {
&self.data[self.index].1
}
pub fn get_mut(&mut self) -> &mut V {
&mut self.data[self.index].1
}
pub fn into_mut(self) -> &'a mut V {
&mut self.data[self.index].1
}
pub fn insert(&mut self, value: V) -> V {
std::mem::replace(&mut self.data[self.index].1, value)
}
pub fn remove(self) -> V {
self.remove_entry().1
}
}