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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::borrow::Borrow;
use std::cell::UnsafeCell;
use std::cmp::Ordering::{Less, Equal, Greater};
use std::default::Default;
use std::iter::{FromIterator, IntoIterator};
use std::mem;
use std::ops::{Index, IndexMut};
use super::node::Node;
pub struct SplayMap<K: Ord, V> {
root: UnsafeCell<Option<Box<Node<K, V>>>>,
size: usize,
}
pub struct IntoIter<K, V> {
cur: Option<Box<Node<K, V>>>,
remaining: usize,
}
fn splay<K, V, Q: ?Sized>(key: &Q, node: &mut Box<Node<K, V>>)
where K: Ord + Borrow<Q>, Q: Ord
{
let mut newleft = None;
let mut newright = None;
{
let mut l = &mut newright;
let mut r = &mut newleft;
loop {
match key.cmp(node.key.borrow()) {
Equal => { break }
Less => {
let mut left = match node.pop_left() {
Some(left) => left, None => break
};
if key.cmp(left.key.borrow()) == Less {
mem::swap(&mut node.left, &mut left.right);
mem::swap(&mut left, node);
let none = mem::replace(&mut node.right, Some(left));
match mem::replace(&mut node.left, none) {
Some(l) => { left = l; }
None => { break }
}
}
*r = Some(mem::replace(node, left));
let tmp = r;
r = &mut tmp.as_mut().unwrap().left;
}
Greater => {
match node.pop_right() {
None => { break }
Some(mut right) => {
if key.cmp(right.key.borrow()) == Greater {
mem::swap(&mut node.right, &mut right.left);
mem::swap(&mut right, node);
let none = mem::replace(&mut node.left,
Some(right));
match mem::replace(&mut node.right, none) {
Some(r) => { right = r; }
None => { break }
}
}
*l = Some(mem::replace(node, right));
let tmp = l;
l = &mut tmp.as_mut().unwrap().right;
}
}
}
}
}
mem::swap(l, &mut node.left);
mem::swap(r, &mut node.right);
}
node.left = newright;
node.right = newleft;
}
impl<K: Ord, V> SplayMap<K, V> {
pub fn new() -> SplayMap<K, V> {
SplayMap { root: UnsafeCell::new(None), size: 0 }
}
pub fn into_iter(mut self) -> IntoIter<K, V> {
IntoIter { cur: self.root_mut().take(), remaining: self.size }
}
pub fn len(&self) -> usize { self.size }
pub fn is_empty(&self) -> bool { self.len() == 0 }
pub fn clear(&mut self) {
let iter = IntoIter {
cur: self.root_mut().take(),
remaining: self.size,
};
for _ in iter {
}
self.size = 0;
}
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Ord,
{
self.get(key).is_some()
}
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Ord,
{
unsafe {
match *self.root.get() {
Some(ref mut root) => {
splay(key, root);
if key == root.key.borrow() {
return Some(&root.value);
}
None
}
None => None,
}
}
}
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Ord,
{
match *self.root_mut() {
None => { return None; }
Some(ref mut root) => {
splay(key, root);
if key == root.key.borrow() {
return Some(&mut root.value);
}
return None;
}
}
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
match self.root_mut() {
&mut Some(ref mut root) => {
splay(&key, root);
match key.cmp(&root.key) {
Equal => {
let old = mem::replace(&mut root.value, value);
return Some(old);
}
Less => {
let left = root.pop_left();
let new = Node::new(key, value, left, None);
let prev = mem::replace(root, new);
root.right = Some(prev);
}
Greater => {
let right = root.pop_right();
let new = Node::new(key, value, None, right);
let prev = mem::replace(root, new);
root.left = Some(prev);
}
}
}
slot => {
*slot = Some(Node::new(key, value, None, None));
}
}
self.size += 1;
return None;
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Ord
{
match *self.root_mut() {
None => { return None; }
Some(ref mut root) => {
splay(key, root);
if key != root.key.borrow() { return None }
}
}
let (value, left, right) = match *self.root_mut().take().unwrap() {
Node {left, right, value, ..} => (value, left, right)
};
*self.root_mut() = match left {
None => right,
Some(mut node) => {
splay(key, &mut node);
node.right = right;
Some(node)
}
};
self.size -= 1;
return Some(value);
}
}
impl<K: Ord, V> SplayMap<K, V> {
fn root_mut(&mut self) -> &mut Option<Box<Node<K, V>>> {
unsafe { &mut *self.root.get() }
}
fn root_ref(&self) -> &Option<Box<Node<K, V>>> {
unsafe { &*self.root.get() }
}
}
impl<'a, K: Ord, V, Q: ?Sized> Index<&'a Q> for SplayMap<K, V>
where K: Borrow<Q>, Q: Ord
{
type Output = V;
fn index(&self, index: &'a Q) -> &V {
self.get(index).expect("key not present in SplayMap")
}
}
impl<'a, K: Ord, V, Q: ?Sized> IndexMut<&'a Q> for SplayMap<K, V>
where K: Borrow<Q>, Q: Ord
{
fn index_mut(&mut self, index: &'a Q) -> &mut V {
self.get_mut(index).expect("key not present in SplayMap")
}
}
impl<K: Ord, V> Default for SplayMap<K, V> {
fn default() -> SplayMap<K, V> { SplayMap::new() }
}
impl<K: Ord, V> FromIterator<(K, V)> for SplayMap<K, V> {
fn from_iter<I: IntoIterator<Item=(K, V)>>(iterator: I) -> SplayMap<K, V> {
let mut map = SplayMap::new();
map.extend(iterator);
map
}
}
impl<K: Ord, V> Extend<(K, V)> for SplayMap<K, V> {
fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, i: I) {
for (k, v) in i {
self.insert(k, v);
}
}
}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
let mut cur = match self.cur.take() {
Some(cur) => cur,
None => return None,
};
loop {
match cur.pop_left() {
Some(node) => {
let mut node = node;
cur.left = node.pop_right();
node.right = Some(cur);
cur = node;
}
None => {
self.cur = cur.pop_right();
let node = *cur;
let Node { key, value, .. } = node;
self.remaining -= 1;
return Some((key, value));
}
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<(K, V)> {
let mut cur = match self.cur.take() {
Some(cur) => cur,
None => return None,
};
loop {
match cur.pop_right() {
Some(node) => {
let mut node = node;
cur.right = node.pop_left();
node.left = Some(cur);
cur = node;
}
None => {
self.cur = cur.pop_left();
let node = *cur;
let Node { key, value, .. } = node;
self.remaining -= 1;
return Some((key, value));
}
}
}
}
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {}
impl<K: Clone + Ord, V: Clone> Clone for SplayMap<K, V> {
fn clone(&self) -> SplayMap<K, V> {
SplayMap {
root: UnsafeCell::new(self.root_ref().clone()),
size: self.size,
}
}
}
impl<K: Ord, V> Drop for SplayMap<K, V> {
fn drop(&mut self) {
self.clear();
}
}