link_cut_tree.rs 23.1 KB
Newer Older
tgerstel's avatar
tgerstel committed
1
use std::{
SirBlueRabbit's avatar
SirBlueRabbit committed
2
    cmp::Ordering,
SirBlueRabbit's avatar
SirBlueRabbit committed
3
    fmt,
tgerstel's avatar
tgerstel committed
4
5
    ops::{Index, IndexMut},
};
tgerstel's avatar
tgerstel committed
6

tgerstel's avatar
tgerstel committed
7
8
use super::collections::{Label, Pool};

tgerstel's avatar
tgerstel committed
9
10
11
12
13
14
15
16
17
18
19
20
enum Parent<V> {
    Path(Label<Node<V>>),
    BinTree(Label<Node<V>>),
    Root,
}

impl<V> Copy for Parent<V> {}

impl<V> Clone for Parent<V> {
    fn clone(&self) -> Parent<V> {
        *self
    }
tgerstel's avatar
tgerstel committed
21
22
}

SirBlueRabbit's avatar
SirBlueRabbit committed
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
impl<V> fmt::Debug for Parent<V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Parent::Path(label) => f
                .debug_struct("Parent::Path")
                .field("label", label)
                .finish(),
            Parent::BinTree(label) => f
                .debug_struct("Parent::BinTree")
                .field("label", label)
                .finish(),
            Parent::Root => f.debug_struct("Parent::Root").finish(),
        }
    }
}

impl<V> PartialEq for Parent<V> {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Parent::Path(label1) => {
                if let Parent::Path(label2) = other {
                    label1 == label2
                } else {
                    false
                }
            }
            Parent::BinTree(label1) => {
                if let Parent::BinTree(label2) = other {
                    label1 == label2
                } else {
                    false
                }
            }
            Parent::Root => matches!(other, Parent::Root),
        }
    }
}

SirBlueRabbit's avatar
SirBlueRabbit committed
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
#[derive(Copy, Clone, Debug, PartialEq)]
enum ChildType {
    Left,
    Right,
    Path,
    Root,
}

impl ChildType {
    fn mirror(&self) -> Option<Self> {
        match self {
            ChildType::Left => Some(ChildType::Right),
            ChildType::Right => Some(ChildType::Left),
            _ => None,
        }
    }
}

impl From<bool> for ChildType {
    fn from(var: bool) -> Self {
        if var {
            ChildType::Right
        } else {
            ChildType::Left
        }
    }
}

tgerstel's avatar
tgerstel committed
89
90
91
#[derive(Debug, Clone, Copy, PartialEq)]
struct Children<V>(Option<Label<Node<V>>>, Option<Label<Node<V>>>);

SirBlueRabbit's avatar
SirBlueRabbit committed
92
impl<V> Index<ChildType> for Children<V> {
tgerstel's avatar
tgerstel committed
93
94
    type Output = Option<Label<Node<V>>>;

SirBlueRabbit's avatar
SirBlueRabbit committed
95
96
97
98
99
    fn index(&self, child_type: ChildType) -> &Self::Output {
        match child_type {
            ChildType::Left => &self.0,
            ChildType::Right => &self.1,
            _ => unreachable!(),
tgerstel's avatar
tgerstel committed
100
101
102
103
        }
    }
}

SirBlueRabbit's avatar
SirBlueRabbit committed
104
105
106
107
108
109
impl<V> IndexMut<ChildType> for Children<V> {
    fn index_mut(&mut self, child_type: ChildType) -> &mut Self::Output {
        match child_type {
            ChildType::Left => &mut self.0,
            ChildType::Right => &mut self.1,
            _ => unreachable!(),
tgerstel's avatar
tgerstel committed
110
111
        }
    }
tgerstel's avatar
tgerstel committed
112
113
}

tgerstel's avatar
tgerstel committed
114
#[derive(Debug, Clone, Copy, PartialEq)]
tgerstel's avatar
tgerstel committed
115
116
117
118
pub struct Node<V> {
    value: V,
    parent: Parent<V>,
    children: Children<V>,
SirBlueRabbit's avatar
SirBlueRabbit committed
119
120
    d_flip: bool,
    d_depth: usize,
tgerstel's avatar
tgerstel committed
121
122
}

SirBlueRabbit's avatar
SirBlueRabbit committed
123
impl<V> Pool<Node<V>> {
SirBlueRabbit's avatar
SirBlueRabbit committed
124
125
    pub fn add_node(&mut self, value: V) -> Label<Node<V>> {
        self.insert(Node {
tgerstel's avatar
tgerstel committed
126
            value,
tgerstel's avatar
tgerstel committed
127
            parent: Parent::Root,
tgerstel's avatar
tgerstel committed
128
            children: Children(None, None),
SirBlueRabbit's avatar
SirBlueRabbit committed
129
130
            d_flip: false,
            d_depth: 0,
SirBlueRabbit's avatar
SirBlueRabbit committed
131
        })
tgerstel's avatar
tgerstel committed
132
133
    }

tgerstel's avatar
tgerstel committed
134
135
136
    pub fn find_root(&mut self, u: Label<Node<V>>) -> Label<Node<V>> {
        self.expose(u);
        let mut x = u;
SirBlueRabbit's avatar
SirBlueRabbit committed
137
138
        let mut flip = self[u].d_flip;
        while let Some(y) = self[x].children[ChildType::from(flip)] {
tgerstel's avatar
tgerstel committed
139
            x = y;
SirBlueRabbit's avatar
SirBlueRabbit committed
140
            flip ^= self[x].d_flip;
tgerstel's avatar
tgerstel committed
141
        }
tgerstel's avatar
tgerstel committed
142
143
        self.expose(x);
        x
tgerstel's avatar
tgerstel committed
144
145
    }

tgerstel's avatar
tgerstel committed
146
147
    pub fn cut(&mut self, c: Label<Node<V>>) {
        self.expose(c);
SirBlueRabbit's avatar
SirBlueRabbit committed
148
        let d_flip = self[c].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
149
        if let Some(p) = self[c].children[ChildType::from(d_flip)] {
tgerstel's avatar
tgerstel committed
150
            self[p].parent = Parent::Root;
SirBlueRabbit's avatar
SirBlueRabbit committed
151
            self[c].children[ChildType::from(d_flip)] = None;
tgerstel's avatar
tgerstel committed
152

SirBlueRabbit's avatar
SirBlueRabbit committed
153
154
            self[p].d_depth = self[c].d_depth - 1;
            self[c].d_depth = 0;
tgerstel's avatar
tgerstel committed
155
        }
tgerstel's avatar
tgerstel committed
156
157
    }

SirBlueRabbit's avatar
SirBlueRabbit committed
158
159
160
    pub fn link(&mut self, p: Label<Node<V>>, c: Label<Node<V>>) {
        self.expose(c);
        self.expose(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
161
        let d_flip = self[c].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
162
        self[c].children[ChildType::from(d_flip)] = Some(p);
tgerstel's avatar
tgerstel committed
163
        self[p].parent = Parent::BinTree(c);
tgerstel's avatar
tgerstel committed
164

SirBlueRabbit's avatar
SirBlueRabbit committed
165
166
        self[c].d_depth = self[p].d_depth + 1;
        self[p].d_depth = 1;
SirBlueRabbit's avatar
SirBlueRabbit committed
167
168
    }

tgerstel's avatar
tgerstel committed
169
170
    pub fn evert(&mut self, u: Label<Node<V>>) {
        self.expose(u);
SirBlueRabbit's avatar
SirBlueRabbit committed
171
        self[u].d_flip = !self[u].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
172
        self[u].d_depth = 0;
SirBlueRabbit's avatar
SirBlueRabbit committed
173
    }
tgerstel's avatar
tgerstel committed
174

SirBlueRabbit's avatar
SirBlueRabbit committed
175
176
177
    pub fn depth(&self, u: Label<Node<V>>) -> usize {
        let mut label = u;
        let mut depth: isize = 0;
SirBlueRabbit's avatar
SirBlueRabbit committed
178

SirBlueRabbit's avatar
SirBlueRabbit committed
179
        loop {
SirBlueRabbit's avatar
SirBlueRabbit committed
180
            let d_depth = self[label].d_depth as isize;
SirBlueRabbit's avatar
SirBlueRabbit committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
            if self.parent(label).is_some() {
                let d_flip = self[self.parent(label).unwrap()].d_flip;
                let child_type = self.child_type(label);

                match child_type {
                    ChildType::Left => depth -= d_depth,
                    ChildType::Right => depth += d_depth,
                    ChildType::Path => depth += d_depth,
                    ChildType::Root => unreachable!(),
                }

                if d_flip {
                    depth = -depth;
                }
                label = self.parent(label).unwrap();
            } else {
                return (depth + d_depth).abs() as usize;
            }
        }
    }

tgerstel's avatar
tgerstel committed
202
203
204
    // always expose before indexing
    pub fn index_depth(&self, u: Label<Node<V>>, i: usize) -> Label<Node<V>> {
        assert!(i <= self.depth(u));
SirBlueRabbit's avatar
SirBlueRabbit committed
205
206
207
208
209
210
211
212
213
214
215
216
217
        let mut label = u;
        let mut flip = self[u].d_flip;
        let mut depth = self[u].d_depth;
        loop {
            let side = match i.cmp(&depth) {
                Ordering::Less => flip,
                Ordering::Equal => return label,
                Ordering::Greater => !flip,
            };

            label = self[label].children[ChildType::from(side)].unwrap();
            if flip ^ side {
                depth += self[label].d_depth;
tgerstel's avatar
tgerstel committed
218
219
            } else {
                depth -= self[label].d_depth;
SirBlueRabbit's avatar
SirBlueRabbit committed
220
221
222
223
224
            }
            flip ^= self[label].d_flip;
        }
    }

tgerstel's avatar
tgerstel committed
225
226
    pub fn value_mut(&mut self, u: Label<Node<V>>) -> &mut V {
        &mut self[u].value
tgerstel's avatar
tgerstel committed
227
228
    }

tgerstel's avatar
tgerstel committed
229
230
    pub fn value(&self, u: Label<Node<V>>) -> &V {
        &self[u].value
tgerstel's avatar
tgerstel committed
231
232
    }

tgerstel's avatar
tgerstel committed
233
234
    fn expose(&mut self, u: Label<Node<V>>) {
        self.splay(u);
SirBlueRabbit's avatar
SirBlueRabbit committed
235
        if let Some(c) = self[u].children[ChildType::from(true)] {
tgerstel's avatar
tgerstel committed
236
            self[c].parent = Parent::Path(u);
SirBlueRabbit's avatar
SirBlueRabbit committed
237
            self[u].children[ChildType::from(true)] = None;
SirBlueRabbit's avatar
SirBlueRabbit committed
238
239
        }

tgerstel's avatar
tgerstel committed
240
        while let Parent::Path(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
241
            self.splay(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
242
            if let Some(x) = self[p].children[ChildType::from(true)] {
tgerstel's avatar
tgerstel committed
243
                self[x].parent = Parent::Path(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
244
            }
SirBlueRabbit's avatar
SirBlueRabbit committed
245
            self[p].children[ChildType::from(true)] = Some(u);
tgerstel's avatar
tgerstel committed
246
247
            self[u].parent = Parent::BinTree(p);
            self.rotate(u);
tgerstel's avatar
tgerstel committed
248
        }
SirBlueRabbit's avatar
SirBlueRabbit committed
249
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
250

tgerstel's avatar
tgerstel committed
251
252
253
    fn splay(&mut self, u: Label<Node<V>>) {
        self.cascade_flipped(u);
        while let Parent::BinTree(p) = self[u].parent {
tgerstel's avatar
tgerstel committed
254
            if let Parent::BinTree(_) = self[p].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
255
                if self[p].d_flip == (self.child_type(u) == self.child_type(p)) {
SirBlueRabbit's avatar
SirBlueRabbit committed
256
                    self.rotate(p);
tgerstel's avatar
tgerstel committed
257
                    self.rotate(u);
tgerstel's avatar
tgerstel committed
258
                } else {
tgerstel's avatar
tgerstel committed
259
260
                    self.rotate(u);
                    self.rotate(u);
tgerstel's avatar
tgerstel committed
261
262
                };
            } else {
tgerstel's avatar
tgerstel committed
263
                self.rotate(u);
tgerstel's avatar
tgerstel committed
264
265
                break;
            }
tgerstel's avatar
tgerstel committed
266
267
268
        }
    }

tgerstel's avatar
tgerstel committed
269
270
    fn cascade_flipped(&mut self, u: Label<Node<V>>) {
        if let Parent::BinTree(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
271
272
273
            self.cascade_flipped(p);
        }

SirBlueRabbit's avatar
SirBlueRabbit committed
274
        if self[u].d_flip {
tgerstel's avatar
tgerstel committed
275
276
            let Children(a_opt, b_opt) = self[u].children;
            self[u].children = Children(b_opt, a_opt);
SirBlueRabbit's avatar
SirBlueRabbit committed
277
            self[u].d_flip = false;
SirBlueRabbit's avatar
SirBlueRabbit committed
278
            if let Some(a) = a_opt {
SirBlueRabbit's avatar
SirBlueRabbit committed
279
                self[a].d_flip = !self[a].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
280
281
            }
            if let Some(b) = b_opt {
SirBlueRabbit's avatar
SirBlueRabbit committed
282
                self[b].d_flip = !self[b].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
283
284
285
286
            }
        }
    }

tgerstel's avatar
tgerstel committed
287
    fn rotate(&mut self, u: Label<Node<V>>) {
SirBlueRabbit's avatar
SirBlueRabbit committed
288
289
        let u_child_type = self.child_type(u);
        let x_opt = self[u].children[u_child_type.mirror().unwrap()];
tgerstel's avatar
tgerstel committed
290
        if let Parent::BinTree(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
291
292
            let u_d_depth = self[u].d_depth;
            let p_d_depth = self[p].d_depth;
tgerstel's avatar
tgerstel committed
293

SirBlueRabbit's avatar
SirBlueRabbit committed
294
295
            self[p].d_depth = u_d_depth;
            self[u].d_depth = if let Parent::BinTree(_) = self[p].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
296
                if self[p].d_flip ^ (self.child_type(u) == self.child_type(p)) {
SirBlueRabbit's avatar
SirBlueRabbit committed
297
                    u_d_depth + p_d_depth
tgerstel's avatar
tgerstel committed
298
                } else {
SirBlueRabbit's avatar
SirBlueRabbit committed
299
                    u_d_depth.abs_diff(p_d_depth)
tgerstel's avatar
tgerstel committed
300
301
                }
            } else {
SirBlueRabbit's avatar
SirBlueRabbit committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
                match self.child_type(u) {
                    ChildType::Left => {
                        if self[p].d_flip {
                            u_d_depth + p_d_depth
                        } else {
                            u_d_depth.abs_diff(p_d_depth)
                        }
                    }
                    ChildType::Right => {
                        if self[p].d_flip {
                            u_d_depth.abs_diff(p_d_depth)
                        } else {
                            u_d_depth + p_d_depth
                        }
                    }
                    _ => unreachable!(),
                }
tgerstel's avatar
tgerstel committed
319
320
321
            };

            if let Some(x) = x_opt {
SirBlueRabbit's avatar
SirBlueRabbit committed
322
323
                let x_d_depth = self[x].d_depth;
                self[x].d_depth = x_d_depth.abs_diff(u_d_depth);
tgerstel's avatar
tgerstel committed
324
325
            }

tgerstel's avatar
tgerstel committed
326
            if let Parent::BinTree(g) = self[p].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
327
328
                let p_child_type = self.child_type(p);
                self[g].children[p_child_type] = Some(u);
tgerstel's avatar
tgerstel committed
329
            }
SirBlueRabbit's avatar
SirBlueRabbit committed
330

SirBlueRabbit's avatar
SirBlueRabbit committed
331
332
            self[p].children[u_child_type] = x_opt;
            self[u].children[u_child_type.mirror().unwrap()] = Some(p);
tgerstel's avatar
tgerstel committed
333

tgerstel's avatar
tgerstel committed
334
335
            if let Some(x) = x_opt {
                self[x].parent = Parent::BinTree(p);
tgerstel's avatar
tgerstel committed
336
337
            }

tgerstel's avatar
tgerstel committed
338
339
            self[u].parent = self[p].parent;
            self[p].parent = Parent::BinTree(u);
tgerstel's avatar
tgerstel committed
340
341
342
        } else {
            unreachable!();
        };
SirBlueRabbit's avatar
SirBlueRabbit committed
343
344
    }

SirBlueRabbit's avatar
SirBlueRabbit committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
    fn child_type(&self, u: Label<Node<V>>) -> ChildType {
        match self[u].parent {
            Parent::BinTree(parent) => {
                if let Some(x) = self[parent].children[ChildType::from(false)] {
                    if u == x {
                        return ChildType::Left;
                    }
                }
                if let Some(x) = self[parent].children[ChildType::from(true)] {
                    if u == x {
                        return ChildType::Right;
                    }
                }

                unreachable!();
tgerstel's avatar
tgerstel committed
360
            }
SirBlueRabbit's avatar
SirBlueRabbit committed
361
362
            Parent::Path(_) => ChildType::Path,
            Parent::Root => ChildType::Root,
SirBlueRabbit's avatar
SirBlueRabbit committed
363
        }
SirBlueRabbit's avatar
SirBlueRabbit committed
364
365
366
367
368
369
370
    }

    fn parent(&self, u: Label<Node<V>>) -> Option<Label<Node<V>>> {
        match self[u].parent {
            Parent::BinTree(parent) => Some(parent),
            Parent::Path(parent) => Some(parent),
            Parent::Root => None,
tgerstel's avatar
tgerstel committed
371
372
373
        }
    }
}
SirBlueRabbit's avatar
SirBlueRabbit committed
374
375
376
377
378
379
380
381

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut lct = Pool::with_capacity(1);
SirBlueRabbit's avatar
SirBlueRabbit committed
382
383
384
        let v = lct.add_node(());
        lct.splay(v);
        lct.expose(v);
SirBlueRabbit's avatar
SirBlueRabbit committed
385
386
387
388
389
390
391
    }

    #[test]
    fn rotate_basic() {
        let mut lct = Pool::with_capacity(2);

        // define nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
392
393
        let o = lct.add_node("node");
        let p = lct.add_node("parent");
SirBlueRabbit's avatar
SirBlueRabbit committed
394
395

        // link nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
396
        lct[p].children[ChildType::from(false)] = Some(o);
tgerstel's avatar
tgerstel committed
397
        lct[o].parent = Parent::BinTree(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
398
399

        // rotate
SirBlueRabbit's avatar
SirBlueRabbit committed
400
        lct.rotate(o);
SirBlueRabbit's avatar
SirBlueRabbit committed
401
402

        // check children
tgerstel's avatar
tgerstel committed
403
404
        assert_eq!(lct[o].children, Children(None, Some(p)));
        assert_eq!(lct[p].children, Children(None, None));
SirBlueRabbit's avatar
SirBlueRabbit committed
405
406

        // check parents
tgerstel's avatar
tgerstel committed
407
408
        assert_eq!(lct[o].parent, Parent::Root);
        assert_eq!(lct[p].parent, Parent::BinTree(o));
SirBlueRabbit's avatar
SirBlueRabbit committed
409
410
411
412
413
414
415
    }

    #[test]
    fn rotate_attachments() {
        let mut lct = Pool::with_capacity(7);

        // define nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
416
417
418
419
420
421
422
        let o = lct.add_node("node");
        let p = lct.add_node("parent");
        let g = lct.add_node("grandparent");
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");
        let d = lct.add_node("d");
SirBlueRabbit's avatar
SirBlueRabbit committed
423
424

        // link nodes
tgerstel's avatar
tgerstel committed
425
426
427
        lct[o].children = Children(Some(a), Some(b));
        lct[p].children = Children(Some(o), Some(c));
        lct[g].children = Children(Some(p), Some(d));
tgerstel's avatar
tgerstel committed
428
429
430
431
432
433
        lct[o].parent = Parent::BinTree(p);
        lct[p].parent = Parent::BinTree(g);
        lct[a].parent = Parent::BinTree(o);
        lct[b].parent = Parent::BinTree(o);
        lct[c].parent = Parent::BinTree(p);
        lct[d].parent = Parent::BinTree(g);
SirBlueRabbit's avatar
SirBlueRabbit committed
434
435

        // rotate
SirBlueRabbit's avatar
SirBlueRabbit committed
436
437
        lct.rotate(o);
        dbg!(&lct);
SirBlueRabbit's avatar
SirBlueRabbit committed
438
439

        // check children
tgerstel's avatar
tgerstel committed
440
441
442
443
444
445
446
        assert_eq!(lct[o].children, Children(Some(a), Some(p)));
        assert_eq!(lct[p].children, Children(Some(b), Some(c)));
        assert_eq!(lct[g].children, Children(Some(o), Some(d)));
        assert_eq!(lct[a].children, Children(None, None));
        assert_eq!(lct[b].children, Children(None, None));
        assert_eq!(lct[c].children, Children(None, None));
        assert_eq!(lct[d].children, Children(None, None));
SirBlueRabbit's avatar
SirBlueRabbit committed
447
448

        // check parents
tgerstel's avatar
tgerstel committed
449
450
451
452
453
454
455
        assert_eq!(lct[o].parent, Parent::BinTree(g));
        assert_eq!(lct[p].parent, Parent::BinTree(o));
        assert_eq!(lct[g].parent, Parent::Root);
        assert_eq!(lct[a].parent, Parent::BinTree(o));
        assert_eq!(lct[b].parent, Parent::BinTree(p));
        assert_eq!(lct[c].parent, Parent::BinTree(p));
        assert_eq!(lct[d].parent, Parent::BinTree(g));
SirBlueRabbit's avatar
SirBlueRabbit committed
456
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
457
458

    #[test]
SirBlueRabbit's avatar
SirBlueRabbit committed
459
    fn rotate_flipped() {
SirBlueRabbit's avatar
SirBlueRabbit committed
460
461
462
        let mut lct = Pool::with_capacity(7);

        // define nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
463
464
465
466
467
468
469
        let o = lct.add_node("node");
        let p = lct.add_node("parent");
        let g = lct.add_node("grandparent");
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");
        let d = lct.add_node("d");
SirBlueRabbit's avatar
SirBlueRabbit committed
470
471

        // link nodes
tgerstel's avatar
tgerstel committed
472
473
474
        lct[o].children = Children(Some(a), Some(b));
        lct[p].children = Children(Some(o), Some(c));
        lct[g].children = Children(Some(p), Some(d));
tgerstel's avatar
tgerstel committed
475
476
477
478
479
480
        lct[o].parent = Parent::BinTree(p);
        lct[p].parent = Parent::BinTree(g);
        lct[a].parent = Parent::BinTree(o);
        lct[b].parent = Parent::BinTree(o);
        lct[c].parent = Parent::BinTree(p);
        lct[d].parent = Parent::BinTree(g);
SirBlueRabbit's avatar
SirBlueRabbit committed
481
482

        // reverse origin
SirBlueRabbit's avatar
SirBlueRabbit committed
483
        lct[o].d_flip = true;
SirBlueRabbit's avatar
SirBlueRabbit committed
484
485

        // rotate
SirBlueRabbit's avatar
SirBlueRabbit committed
486
        lct.cascade_flipped(o);
SirBlueRabbit's avatar
SirBlueRabbit committed
487
488
        lct.rotate(o);
        dbg!(&lct);
SirBlueRabbit's avatar
SirBlueRabbit committed
489
490

        // check children
tgerstel's avatar
tgerstel committed
491
492
493
494
495
496
497
        assert_eq!(lct[o].children, Children(Some(b), Some(p)));
        assert_eq!(lct[p].children, Children(Some(a), Some(c)));
        assert_eq!(lct[g].children, Children(Some(o), Some(d)));
        assert_eq!(lct[a].children, Children(None, None));
        assert_eq!(lct[b].children, Children(None, None));
        assert_eq!(lct[c].children, Children(None, None));
        assert_eq!(lct[d].children, Children(None, None));
SirBlueRabbit's avatar
SirBlueRabbit committed
498
499

        // check parents
tgerstel's avatar
tgerstel committed
500
501
502
503
504
505
506
        assert_eq!(lct[o].parent, Parent::BinTree(g));
        assert_eq!(lct[p].parent, Parent::BinTree(o));
        assert_eq!(lct[g].parent, Parent::Root);
        assert_eq!(lct[a].parent, Parent::BinTree(p));
        assert_eq!(lct[b].parent, Parent::BinTree(o));
        assert_eq!(lct[c].parent, Parent::BinTree(p));
        assert_eq!(lct[d].parent, Parent::BinTree(g));
SirBlueRabbit's avatar
SirBlueRabbit committed
507
508

        // check flipped
SirBlueRabbit's avatar
SirBlueRabbit committed
509
510
511
512
513
514
515
        assert!(!lct[o].d_flip);
        assert!(!lct[p].d_flip);
        assert!(!lct[g].d_flip);
        assert!(lct[a].d_flip);
        assert!(lct[b].d_flip);
        assert!(!lct[c].d_flip);
        assert!(!lct[d].d_flip);
tgerstel's avatar
tgerstel committed
516
517
518
    }

    #[test]
SirBlueRabbit's avatar
SirBlueRabbit committed
519
520
    fn expose_line() {
        let mut lct = Pool::with_capacity(5);
tgerstel's avatar
tgerstel committed
521

SirBlueRabbit's avatar
SirBlueRabbit committed
522
523
524
525
526
527
        // define nodes
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");
        let d = lct.add_node("d");
        let e = lct.add_node("e");
tgerstel's avatar
tgerstel committed
528

SirBlueRabbit's avatar
SirBlueRabbit committed
529
        // link nodes
tgerstel's avatar
tgerstel committed
530
531
        lct[b].children = Children(None, Some(c));
        lct[d].children = Children(None, Some(e));
tgerstel's avatar
tgerstel committed
532
533
534
535
        lct[c].parent = Parent::BinTree(b);
        lct[e].parent = Parent::BinTree(d);
        lct[b].parent = Parent::Path(a);
        lct[d].parent = Parent::Path(c);
SirBlueRabbit's avatar
SirBlueRabbit committed
536
537
538
539
540

        // expose
        lct.expose(d);
        dbg!(&lct);

tgerstel's avatar
tgerstel committed
541
542
543
544
545
546
        // check parents
        assert_eq!(lct[a].parent, Parent::BinTree(d));
        assert_eq!(lct[b].parent, Parent::BinTree(c));
        assert_eq!(lct[c].parent, Parent::BinTree(a));
        assert_eq!(lct[d].parent, Parent::Root);
        assert_eq!(lct[e].parent, Parent::Path(d));
tgerstel's avatar
tgerstel committed
547
548
549
    }

    #[test]
SirBlueRabbit's avatar
SirBlueRabbit committed
550
551
    fn find_root() {
        let mut lct = Pool::with_capacity(7);
tgerstel's avatar
tgerstel committed
552

SirBlueRabbit's avatar
SirBlueRabbit committed
553
554
555
556
557
558
559
560
        // define nodes
        let o = lct.add_node("node");
        let p = lct.add_node("parent");
        let g = lct.add_node("grandparent");
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");
        let d = lct.add_node("d");
tgerstel's avatar
tgerstel committed
561

SirBlueRabbit's avatar
SirBlueRabbit committed
562
        // link nodes
tgerstel's avatar
tgerstel committed
563
564
565
        lct[o].children = Children(Some(a), Some(b));
        lct[p].children = Children(Some(o), Some(c));
        lct[g].children = Children(Some(p), Some(d));
tgerstel's avatar
tgerstel committed
566
567
568
569
570
571
        lct[o].parent = Parent::BinTree(p);
        lct[p].parent = Parent::BinTree(g);
        lct[a].parent = Parent::BinTree(o);
        lct[b].parent = Parent::BinTree(o);
        lct[c].parent = Parent::BinTree(p);
        lct[d].parent = Parent::BinTree(g);
SirBlueRabbit's avatar
SirBlueRabbit committed
572
573
574

        // find root
        assert_eq!(lct.find_root(g), a);
tgerstel's avatar
tgerstel committed
575
576
577
    }

    #[test]
tgerstel's avatar
tgerstel committed
578
579
    fn link_cut() {
        let mut lct = Pool::with_capacity(2);
tgerstel's avatar
tgerstel committed
580

SirBlueRabbit's avatar
SirBlueRabbit committed
581
582
583
        // define nodes
        let a = lct.add_node("a");
        let b = lct.add_node("b");
tgerstel's avatar
tgerstel committed
584

SirBlueRabbit's avatar
SirBlueRabbit committed
585
        // link nodes
tgerstel's avatar
tgerstel committed
586
        lct.link(a, b);
SirBlueRabbit's avatar
SirBlueRabbit committed
587

tgerstel's avatar
tgerstel committed
588
589
        // cut nodes
        lct.cut(b);
tgerstel's avatar
tgerstel committed
590
591
592
    }

    #[test]
SirBlueRabbit's avatar
SirBlueRabbit committed
593
594
    fn evert_root() {
        let mut lct = Pool::with_capacity(7);
tgerstel's avatar
tgerstel committed
595

SirBlueRabbit's avatar
SirBlueRabbit committed
596
597
598
599
600
601
602
603
        // define nodes
        let o = lct.add_node("node");
        let p = lct.add_node("parent");
        let g = lct.add_node("grandparent");
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");
        let d = lct.add_node("d");
tgerstel's avatar
tgerstel committed
604

SirBlueRabbit's avatar
SirBlueRabbit committed
605
        // link nodes
tgerstel's avatar
tgerstel committed
606
607
608
        lct[o].children = Children(Some(a), Some(b));
        lct[p].children = Children(Some(o), Some(c));
        lct[g].children = Children(Some(p), Some(d));
tgerstel's avatar
tgerstel committed
609
610
611
612
613
614
        lct[o].parent = Parent::BinTree(p);
        lct[p].parent = Parent::BinTree(g);
        lct[a].parent = Parent::BinTree(o);
        lct[b].parent = Parent::BinTree(o);
        lct[c].parent = Parent::BinTree(p);
        lct[d].parent = Parent::BinTree(g);
SirBlueRabbit's avatar
SirBlueRabbit committed
615
616
617
618
619

        // evert
        lct.evert(o);
        dbg!(&lct);
        assert_eq!(lct.find_root(g), o);
tgerstel's avatar
tgerstel committed
620
    }
tgerstel's avatar
tgerstel committed
621

SirBlueRabbit's avatar
SirBlueRabbit committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
    #[test]
    fn basic_depth() {
        // generate a basic lct
        let size = 3;
        let mut lct = Pool::with_capacity(size);
        for i in 0..size {
            let _ = lct.add_node(i);
        }
        for i in 0..(size - 1) {
            let parent = Label::from(i);
            let child = Label::from(i + 1);
            lct.link(parent, child);
        }

        // check initial depth
        for i in 0..size {
            let label = Label::from(i);
            assert_eq!(lct.depth(label), i);
        }

        // evert middle node and check new depths
        lct.evert(Label::from(1));
        dbg!(&lct);
        assert_eq!(lct.depth(Label::from(0)), 1);
        assert_eq!(lct.depth(Label::from(1)), 0);
        assert_eq!(lct.depth(Label::from(2)), 1);
tgerstel's avatar
tgerstel committed
648
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
649
650

    #[test]
SirBlueRabbit's avatar
SirBlueRabbit committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
    fn rotate_depth() {
        // create lct
        let mut lct = Pool::with_capacity(2);
        let a = lct.add_node("a");
        let b = lct.add_node("b");

        // link nodes
        lct.link(a, b);

        // rotate parent
        lct.rotate(a);

        // check depth
        assert_eq!(lct.depth(a), 0);
        assert_eq!(lct.depth(b), 1);
    }

    #[test]
    fn splay_depth() {
        // create lct
        let mut lct = Pool::with_capacity(3);
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");

        // link and expose
        lct.link(a, b);
        lct.link(b, c);
        lct.splay(b);
        lct.splay(a);
        lct.splay(c);

        // check depths
        assert_eq!(lct.depth(a), 0);
        assert_eq!(lct.depth(b), 1);
        assert_eq!(lct.depth(c), 2);
    }

    #[test]
    fn expose_depth() {
        // crate lct
        let mut lct = Pool::with_capacity(3);
        let a = lct.add_node("a");
        let b = lct.add_node("b");
        let c = lct.add_node("c");

        // link and expose
        lct.link(a, b);
        lct.link(b, c);
        lct.expose(b);
        lct.expose(a);
SirBlueRabbit's avatar
SirBlueRabbit committed
702
        lct.expose(c);
SirBlueRabbit's avatar
SirBlueRabbit committed
703
704
705
706
707
708
709
710
711

        // check depths
        assert_eq!(lct.depth(a), 0);
        assert_eq!(lct.depth(b), 1);
        assert_eq!(lct.depth(c), 2);
    }

    #[test]
    fn random_evert() {
SirBlueRabbit's avatar
SirBlueRabbit committed
712
        // generate a basic lct
tgerstel's avatar
tgerstel committed
713
        let size = 3;
SirBlueRabbit's avatar
SirBlueRabbit committed
714
715
716
717
718
719
720
721
722
723
        let mut lct = Pool::with_capacity(size);
        for i in 0..size {
            let _ = lct.add_node(i);
        }
        for i in 0..(size - 1) {
            let parent = Label::from(i);
            let child = Label::from(i + 1);
            lct.link(parent, child);
        }

SirBlueRabbit's avatar
SirBlueRabbit committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
        // randomly evert
        for _ in 0..(size * size * size) {
            let root = Label::from(fastrand::usize(0..size));
            lct.evert(root);
            let label = Label::from(fastrand::usize(0..size));
            lct.expose(label);
            assert_clean(&lct);
            assert_depth(&lct, label);
        }
    }

    #[test]
    fn random_cut_link() {
        // generate a basic lct
        let size = 5;
        let mut lct = Pool::with_capacity(size);
        for i in 0..size {
            let _ = lct.add_node(i);
        }
        for i in 0..(size - 1) {
            let parent = Label::from(i);
            let child = Label::from(i + 1);
            lct.link(parent, child);
        }

        // randomly cut and link
SirBlueRabbit's avatar
SirBlueRabbit committed
750
        for _ in 0..(size * size * size) {
tgerstel's avatar
tgerstel committed
751
752
753
            let label1 = Label::from(fastrand::usize(0..size));
            let label2 = Label::from(fastrand::usize(0..size));
            if label1 != label2 {
SirBlueRabbit's avatar
SirBlueRabbit committed
754
                // restructure tree
tgerstel's avatar
tgerstel committed
755
756
757
                lct.evert(label1);
                lct.expose(label2);
                assert_depth(&lct, label2);
SirBlueRabbit's avatar
SirBlueRabbit committed
758
759
760

                // find a node to cut
                let depth = lct.depth(label2);
tgerstel's avatar
tgerstel committed
761
762
                let index = fastrand::usize(0..depth);
                let cut = lct.index_depth(label2, index + 1);
SirBlueRabbit's avatar
SirBlueRabbit committed
763

SirBlueRabbit's avatar
SirBlueRabbit committed
764
                // cut and link back together
tgerstel's avatar
tgerstel committed
765
766
767
                lct.cut(cut);
                lct.evert(label2);
                lct.link(label1, label2);
SirBlueRabbit's avatar
SirBlueRabbit committed
768
769
770
771
772
            }
            assert_clean(&lct);
        }
    }

tgerstel's avatar
tgerstel committed
773
774
775
776
777
778
779
780
    fn assert_depth<V: fmt::Debug>(lct: &Pool<Node<V>>, label: Label<Node<V>>) {
        let max_depth = lct.depth(label);
        for depth in 0..max_depth {
            let x = lct.index_depth(label, depth);
            assert_eq!(lct.depth(x), depth);
        }
    }

SirBlueRabbit's avatar
SirBlueRabbit committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    fn assert_clean<V>(lct: &Pool<Node<V>>) {
        for label in lct.labels() {
            // check parent reciprocrity
            match lct[label].parent {
                Parent::Root => (),
                Parent::Path(_) => (),
                Parent::BinTree(p) => {
                    assert!(lct[p].children.0 == Some(label) || lct[p].children.1 == Some(label))
                }
            }

            // check child reciprocrity
            if let Some(c) = lct[label].children.0 {
                assert_eq!(lct[c].parent, Parent::BinTree(label));
            }
            if let Some(c) = lct[label].children.1 {
                assert_eq!(lct[c].parent, Parent::BinTree(label));
            }
        }
    }
tgerstel's avatar
tgerstel committed
801
}