-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.rs
239 lines (213 loc) · 5.4 KB
/
collections.rs
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
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::{FromIterator, FusedIterator};
use std::mem;
use std::option;
use std::vec;
/// Container holding 0/1/n items, avoiding allocation in the 0/1 cases
#[derive(Debug)]
pub enum ZeroOneMany<T> {
Zero,
One(T),
Many(Vec<T>),
}
impl<T> IntoIterator for ZeroOneMany<T> {
type Item = T;
type IntoIter = ZeroOneManyIter<T>;
fn into_iter(self) -> Self::IntoIter {
match self {
ZeroOneMany::Zero => ZeroOneManyIter::Option(None.into_iter()),
ZeroOneMany::One(x) => ZeroOneManyIter::Option(Some(x).into_iter()),
ZeroOneMany::Many(v) => ZeroOneManyIter::Vec(v.into_iter()),
}
}
}
pub enum ZeroOneManyIter<T> {
Option(option::IntoIter<T>),
Vec(vec::IntoIter<T>),
}
impl<T> Iterator for ZeroOneManyIter<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
match self {
ZeroOneManyIter::Option(o) => o.next(),
ZeroOneManyIter::Vec(v) => v.next(),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
ZeroOneManyIter::Option(o) => o.size_hint(),
ZeroOneManyIter::Vec(v) => v.size_hint(),
}
}
}
impl<T> DoubleEndedIterator for ZeroOneManyIter<T> {
fn next_back(&mut self) -> Option<T> {
match self {
ZeroOneManyIter::Option(o) => o.next_back(),
ZeroOneManyIter::Vec(v) => v.next_back(),
}
}
}
impl<T> ExactSizeIterator for ZeroOneManyIter<T> {}
impl<T> FusedIterator for ZeroOneManyIter<T> {}
/// The fundamental sum type.
#[derive(Debug)]
pub enum Either<A, B> {
A(A),
B(B),
}
/// A HashMap holding a reference to its parent, allowing efficient scope-based lookup.
#[derive(Debug)]
pub struct StackedMap<'a, K, V>
where
K: Eq + Hash,
{
parent: Option<&'a Self>,
map: HashMap<K, V>,
}
impl<'a, K, V> Default for StackedMap<'a, K, V>
where
K: Eq + Hash,
{
fn default() -> Self {
Self {
parent: None,
map: Default::default(),
}
}
}
#[allow(dead_code)]
impl<'a, K, V> StackedMap<'a, K, V>
where
K: Eq + Hash,
{
pub fn child(&self) -> StackedMap<'_, K, V> {
StackedMap {
parent: Some(self),
map: Default::default(),
}
}
pub fn get_all<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.get_self(k)
.or_else(|| self.parent.and_then(|p| p.get_all(k)))
}
pub fn get_self<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.get(k)
}
pub fn insert_self(&mut self, k: K, v: V) -> Option<V> {
self.map.insert(k, v)
}
pub fn remove_self<Q>(&mut self, k: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.remove(k)
}
pub fn clear_self(&mut self) {
self.map.clear();
}
}
impl<'a, K, V> FromIterator<(K, V)> for StackedMap<'a, K, V>
where
K: Eq + Hash,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
Self {
parent: None,
map: HashMap::from_iter(iter),
}
}
}
/// A HashMap that avoids allocation for empty and singleton maps.
#[derive(Debug, Clone)]
pub enum SmallMap<K, V>
where
K: Eq + Hash,
{
Zero,
One(K, V),
Many(HashMap<K, V>),
}
impl<K, V> Default for SmallMap<K, V>
where
K: Eq + Hash,
{
fn default() -> Self {
Self::Zero
}
}
#[allow(dead_code)]
impl<K, V> SmallMap<K, V>
where
K: Eq + Hash,
{
pub fn get<Q>(&self, q: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self {
Self::Zero => None,
Self::One(k, v) if k.borrow() == q => Some(v),
Self::One(_, _) => None,
Self::Many(map) => map.get(q),
}
}
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
match self {
zero @ Self::Zero => {
*zero = Self::One(k, v);
None
}
Self::One(old_k, old_v) if old_k == &k => {
let old_v = mem::replace(old_v, v);
Some(old_v)
}
one @ Self::One(_, _) => {
let (old_k, old_v) = match mem::take(one) {
Self::One(old_k, old_v) => (old_k, old_v),
_ => unreachable!(),
};
let mut map = HashMap::with_capacity(2);
map.insert(old_k, old_v);
map.insert(k, v);
*one = Self::Many(map);
None
}
Self::Many(map) => map.insert(k, v),
}
}
pub fn remove<Q>(&mut self, q: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self {
Self::Zero => None,
Self::One(k, _) if (&*k).borrow() == q => match mem::replace(self, Self::Zero) {
Self::One(_, v) => Some(v),
_ => unreachable!(),
},
Self::One(_, _) => None,
Self::Many(map) => map.remove(q),
}
}
pub fn clear(&mut self) {
match self {
Self::Zero => {}
one @ Self::One(_, _) => *one = Self::Zero,
Self::Many(map) => map.clear(),
}
}
}