splay_tree 0.4.0

Splay Tree based Data Structures (map, set, heap)
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! In-place top-down splay tree implementation
use crate::iter;
use std::borrow::Borrow;
use std::cmp;
use std::cmp::Ordering;
use std::hash;
use std::mem;
use std::slice;
use std::vec::Vec;

pub type NodeIndex = u32;
const NULL_NODE: NodeIndex = u32::MAX;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Node<K, V> {
    lft: NodeIndex,
    rgt: NodeIndex,
    pub key: K,
    pub val: V,
}
impl<K, V> Node<K, V> {
    pub fn new(key: K, val: V, lft: NodeIndex, rgt: NodeIndex) -> Self {
        Node { key, val, lft, rgt }
    }
    pub fn rgt(&self) -> Option<NodeIndex> {
        if self.rgt != NULL_NODE {
            Some(self.rgt)
        } else {
            None
        }
    }
    pub fn lft(&self) -> Option<NodeIndex> {
        if self.lft != NULL_NODE {
            Some(self.lft)
        } else {
            None
        }
    }
}
impl<K, V> From<Node<K, V>> for (K, V) {
    fn from(t: Node<K, V>) -> Self {
        (t.key, t.val)
    }
}
impl<'a, K, V> From<&'a Node<K, V>> for (&'a K, &'a V) {
    fn from(t: &'a Node<K, V>) -> Self {
        (&t.key, &t.val)
    }
}
impl<'a, K, V> From<&'a mut Node<K, V>> for (&'a K, &'a mut V) {
    fn from(t: &'a mut Node<K, V>) -> Self {
        (&t.key, &mut t.val)
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Tree<K, V> {
    root: NodeIndex,
    nodes: Vec<Node<K, V>>,
}
impl<K, V> Tree<K, V>
where
    K: Ord,
{
    pub fn new() -> Self {
        Tree {
            root: 0,
            nodes: Vec::new(),
        }
    }
    pub fn contains_key<Q>(&mut self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.root().is_some_and(|root| {
            let (root, order) = self.splay(root, key);
            self.root = root;
            order == Ordering::Equal
        })
    }
    pub fn contains_key_immut<Q>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.get_immut(key).is_some()
    }
    pub fn find_lower_bound<Q>(&mut self, key: &Q) -> Option<&K>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.find_bound(|k| key.cmp(k.borrow()))
    }
    pub fn find_lower_bound_immut<Q>(&self, key: &Q) -> Option<&K>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.find_bound_immut(|k| key.cmp(k.borrow()))
    }
    pub fn find_upper_bound<Q>(&mut self, key: &Q) -> Option<&K>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.find_bound(|k| match key.cmp(k.borrow()) {
            Ordering::Equal => Ordering::Greater,
            other => other,
        })
    }
    pub fn find_upper_bound_immut<Q>(&self, key: &Q) -> Option<&K>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.find_bound_immut(|k| match key.cmp(k.borrow()) {
            Ordering::Equal => Ordering::Greater,
            other => other,
        })
    }
    pub fn get<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        if self.contains_key(key) {
            Some(&mut self.root_mut().val)
        } else {
            None
        }
    }
    pub fn get_immut<Q>(&self, key: &Q) -> Option<(&K, &V)>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        let mut index = self.root();
        while let Some(node) = index.map(|i| unsafe { self.node_ref(i) }) {
            match key.cmp(node.key.borrow()) {
                Ordering::Equal => return Some((&node.key, &node.val)),
                Ordering::Less => index = node.lft(),
                Ordering::Greater => index = node.rgt(),
            }
        }
        None
    }
    pub fn find_bound_immut<F>(&self, cmp: F) -> Option<&K>
    where
        F: Fn(&K) -> Ordering,
    {
        let mut index = self.root();
        let mut candidate = None;
        while let Some(node) = index.map(|i| unsafe { self.node_ref(i) }) {
            match cmp(&node.key) {
                Ordering::Equal => return Some(&node.key),
                Ordering::Less => {
                    candidate = Some(&node.key);
                    index = node.lft();
                }
                Ordering::Greater => index = node.rgt(),
            }
        }
        candidate
    }
    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        if let Some(root) = self.root() {
            let (root, order) = self.splay(root, &key);
            self.root = root;
            match order {
                Ordering::Equal => {
                    let old = mem::replace(&mut self.root_mut().val, value);
                    Some(old)
                }
                Ordering::Less => {
                    let lft = mem::replace(&mut self.root_mut().lft, NULL_NODE);
                    let rgt = self.root;
                    self.push_root(Node::new(key, value, lft, rgt));
                    None
                }
                Ordering::Greater => {
                    let rgt = mem::replace(&mut self.root_mut().rgt, NULL_NODE);
                    let lft = self.root;
                    self.push_root(Node::new(key, value, lft, rgt));
                    None
                }
            }
        } else {
            self.push_root(Node::new(key, value, NULL_NODE, NULL_NODE));
            None
        }
    }
    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        if self.contains_key(key) {
            Some(self.non_empty_pop_root().1)
        } else {
            None
        }
    }
    pub fn pop_last(&mut self) -> Option<(K, V)> {
        self.nodes
            .last()
            .map(|n| unsafe { &*(&n.key as *const _) })
            .map(|key| {
                self.contains_key(key);
                self.non_empty_pop_root()
            })
    }
    pub fn pop_root(&mut self) -> Option<(K, V)> {
        self.root().map(|_| self.non_empty_pop_root())
    }
    pub fn get_lftmost(&mut self) -> Option<(&K, &V)> {
        self.root().map(move |root| {
            self.root = self.splay_lftmost(root);
            self.root_ref().into()
        })
    }
    pub fn get_lftmost_immut(&self) -> Option<(&K, &V)> {
        self.root()
            .map(|i| unsafe { self.node_ref(i) })
            .map(|mut node| {
                while let Some(next) = node.lft().map(|i| unsafe { self.node_ref(i) }) {
                    node = next;
                }
                (&node.key, &node.val)
            })
    }
    pub fn take_lftmost(&mut self) -> Option<(K, V)> {
        self.root().map(|root| {
            self.root = self.splay_lftmost(root);
            self.non_empty_pop_root()
        })
    }
    pub fn get_rgtmost(&mut self) -> Option<(&K, &V)> {
        self.root().map(move |root| {
            self.root = self.splay_rgtmost(root);
            self.root_ref().into()
        })
    }
    pub fn get_rgtmost_immut(&self) -> Option<(&K, &V)> {
        self.root()
            .map(|i| unsafe { self.node_ref(i) })
            .map(|mut node| {
                while let Some(next) = node.rgt().map(|i| unsafe { self.node_ref(i) }) {
                    node = next;
                }
                (&node.key, &node.val)
            })
    }
    pub fn take_rgtmost(&mut self) -> Option<(K, V)> {
        self.root().map(|root| {
            self.root = self.splay_rgtmost(root);
            self.non_empty_pop_root()
        })
    }
    fn push_root(&mut self, node: Node<K, V>) {
        self.nodes.push(node);
        self.root = self.nodes.len() as NodeIndex - 1;
        assert!(self.root != NULL_NODE);
    }
    fn splay<Q>(&mut self, root: NodeIndex, key: &Q) -> (NodeIndex, Ordering)
    where
        K: Borrow<Q>,
        Q: ?Sized + Ord,
    {
        self.splay_by(root, |k| key.cmp(k.borrow()))
    }
    fn splay_lftmost(&mut self, root: NodeIndex) -> NodeIndex {
        self.splay_by(root, |_| Ordering::Less).0
    }
    fn splay_rgtmost(&mut self, root: NodeIndex) -> NodeIndex {
        self.splay_by(root, |_| Ordering::Greater).0
    }
    fn splay_by<F>(&mut self, mut curr_idx: NodeIndex, cmp: F) -> (NodeIndex, Ordering)
    where
        F: Fn(&K) -> Ordering,
    {
        use std::mem::replace;
        let mut lft_root_idx = NULL_NODE;
        let mut rgt_root_idx = NULL_NODE;
        let mut curr_mut = unsafe { self.aliasable_node_mut(curr_idx) };
        let mut order = cmp(curr_mut.key.borrow());
        {
            let mut lft_rgtmost_idx = &mut lft_root_idx;
            let mut rgt_lftmost_idx = &mut rgt_root_idx;
            loop {
                let mut child_idx;
                let mut child_mut;
                match order {
                    Ordering::Less if curr_mut.lft != NULL_NODE => {
                        // zig
                        child_idx = replace(&mut curr_mut.lft, NULL_NODE);
                        child_mut = unsafe { self.aliasable_node_mut(child_idx) };
                        order = cmp(child_mut.key.borrow());
                        if Ordering::Less == order && child_mut.lft != NULL_NODE {
                            // zig-zig
                            let grand_child_idx = replace(&mut child_mut.lft, NULL_NODE);
                            curr_mut.lft = replace(&mut child_mut.rgt, curr_idx);
                            curr_idx = replace(&mut child_idx, grand_child_idx);
                            curr_mut = replace(&mut child_mut, unsafe {
                                self.aliasable_node_mut(grand_child_idx)
                            });
                            order = cmp(child_mut.key.borrow());
                        }
                        *rgt_lftmost_idx = curr_idx;
                        rgt_lftmost_idx = unsafe { &mut *(&mut curr_mut.lft as *mut _) };
                    }
                    Ordering::Greater if curr_mut.rgt != NULL_NODE => {
                        // zag
                        child_idx = replace(&mut curr_mut.rgt, NULL_NODE);
                        child_mut = unsafe { self.aliasable_node_mut(child_idx) };
                        order = cmp(child_mut.key.borrow());
                        if Ordering::Greater == order && child_mut.rgt != NULL_NODE {
                            // zag-zag
                            let grand_child_idx = replace(&mut child_mut.rgt, NULL_NODE);
                            curr_mut.rgt = replace(&mut child_mut.lft, curr_idx);
                            curr_idx = replace(&mut child_idx, grand_child_idx);
                            curr_mut = replace(&mut child_mut, unsafe {
                                self.aliasable_node_mut(grand_child_idx)
                            });
                            order = cmp(child_mut.key.borrow());
                        }
                        *lft_rgtmost_idx = curr_idx;
                        lft_rgtmost_idx = unsafe { &mut *(&mut curr_mut.rgt as *mut _) };
                    }
                    _ => break,
                }
                curr_mut = child_mut;
                curr_idx = child_idx;
            }
            *lft_rgtmost_idx = replace(&mut curr_mut.lft, NULL_NODE);
            *rgt_lftmost_idx = replace(&mut curr_mut.rgt, NULL_NODE);
        }
        curr_mut.lft = lft_root_idx;
        curr_mut.rgt = rgt_root_idx;
        (curr_idx, order)
    }
    fn non_empty_pop_root(&mut self) -> (K, V) {
        let new_root = match *self.root_ref() {
            Node {
                lft: NULL_NODE,
                rgt: NULL_NODE,
                ..
            } => NULL_NODE,
            Node {
                lft,
                rgt: NULL_NODE,
                ..
            } => lft,
            Node {
                lft: NULL_NODE,
                rgt,
                ..
            } => rgt,
            Node { lft, rgt, .. } if unsafe { self.node_ref(rgt).lft } == NULL_NODE => {
                unsafe { self.node_mut(rgt).lft = lft };
                rgt
            }
            Node { lft, mut rgt, .. } => {
                let lft_rgt = mem::replace(&mut unsafe { self.node_mut(lft).rgt }, NULL_NODE);
                if lft_rgt != NULL_NODE {
                    rgt = self.splay_lftmost(rgt);
                    unsafe { self.node_mut(rgt).lft = lft_rgt };
                }
                unsafe { self.node_mut(lft).rgt = rgt };
                lft
            }
        };
        if self.len() as NodeIndex - 1 != self.root {
            let key = unsafe { &*(&self.node_ref(self.len() as NodeIndex - 1).key as *const _) };
            let _ = self.splay(new_root, key);
            let last = self.nodes.pop().unwrap();
            mem::replace(self.root_mut(), last).into()
        } else {
            self.root = new_root;
            self.nodes.pop().unwrap().into()
        }
    }
    fn find_bound<F>(&mut self, cmp: F) -> Option<&K>
    where
        F: Fn(&K) -> Ordering,
    {
        self.root().and_then(move |root| {
            let (root, order) = self.splay_by(root, cmp);
            self.root = root;
            if let Ordering::Greater = order {
                let mut root_rgt = self.root_ref().rgt;
                if root_rgt != NULL_NODE {
                    root_rgt = self.splay_lftmost(root_rgt);
                    self.root_mut().rgt = root_rgt;
                    Some(&unsafe { self.node_ref(root_rgt) }.key)
                } else {
                    None
                }
            } else {
                Some(&self.root_ref().key)
            }
        })
    }
}
impl<K, V> Tree<K, V> {
    pub fn root(&self) -> Option<NodeIndex> {
        if self.nodes.is_empty() {
            None
        } else {
            Some(self.root)
        }
    }
    pub fn root_ref(&self) -> &Node<K, V> {
        let root = self.root;
        unsafe { self.node_ref(root) }
    }
    pub fn root_mut(&mut self) -> &mut Node<K, V> {
        let root = self.root;
        unsafe { self.node_mut(root) }
    }
    pub unsafe fn node_ref(&self, i: NodeIndex) -> &Node<K, V> {
        unsafe { self.nodes.get_unchecked(i as usize) }
    }
    pub unsafe fn node_mut(&mut self, i: NodeIndex) -> &mut Node<K, V> {
        unsafe { self.nodes.get_unchecked_mut(i as usize) }
    }
    unsafe fn aliasable_node_mut<'a>(&mut self, i: NodeIndex) -> &'a mut Node<K, V> {
        unsafe { &mut *(self.node_mut(i) as *mut _) }
    }
    pub fn len(&self) -> usize {
        self.nodes.len()
    }
    #[allow(dead_code)]
    pub fn capacity(&self) -> usize {
        self.nodes.capacity()
    }
    pub fn iter(&self) -> iter::Iter<'_, K, V> {
        iter::InOrderIter::new(self.root(), &self.nodes)
    }
    pub fn iter_mut(&mut self) -> iter::IterMut<'_, K, V> {
        iter::InOrderIter::new(self.root(), &mut self.nodes)
    }
    pub fn into_iter(self) -> iter::IntoIter<K, V> {
        iter::InOrderIter::new(
            self.root(),
            iter::OwnedNodes(self.nodes.into_iter().map(Some).collect()),
        )
    }
    pub fn nodes_iter(&self) -> slice::Iter<'_, Node<K, V>> {
        self.nodes.iter()
    }
    pub fn nodes_iter_mut(&mut self) -> slice::IterMut<'_, Node<K, V>> {
        self.nodes.iter_mut()
    }
}
impl<K, V> hash::Hash for Tree<K, V>
where
    K: hash::Hash,
    V: hash::Hash,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        for e in self.iter() {
            e.hash(state);
        }
    }
}
impl<K, V> PartialEq for Tree<K, V>
where
    K: PartialEq,
    V: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a.eq(&b))
    }
}
impl<K, V> Eq for Tree<K, V>
where
    K: Eq,
    V: Eq,
{
}
impl<K, V> PartialOrd for Tree<K, V>
where
    K: PartialOrd,
    V: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        let mut i0 = self.iter();
        let mut i1 = other.iter();
        loop {
            match (i0.next(), i1.next()) {
                (None, None) => return Some(cmp::Ordering::Equal),
                (None, _) => return Some(cmp::Ordering::Less),
                (_, None) => return Some(cmp::Ordering::Greater),
                (Some(e0), Some(e1)) => match e0.partial_cmp(&e1) {
                    Some(cmp::Ordering::Equal) => {}
                    not_equal => return not_equal,
                },
            }
        }
    }
}
impl<K, V> Ord for Tree<K, V>
where
    K: Ord,
    V: Ord,
{
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        let mut i0 = self.iter();
        let mut i1 = other.iter();
        loop {
            match (i0.next(), i1.next()) {
                (None, None) => return cmp::Ordering::Equal,
                (None, _) => return cmp::Ordering::Less,
                (_, None) => return cmp::Ordering::Greater,
                (Some(e0), Some(e1)) => match e0.cmp(&e1) {
                    cmp::Ordering::Equal => {}
                    not_equal => return not_equal,
                },
            }
        }
    }
}