link_cut_tree.rs 21 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

tgerstel's avatar
tgerstel committed
175
    /*     pub fn first_ancestor(&mut self, u: Label<Node<V>>, v: Label<Node<V>>) -> Label<Node<V>> {
tgerstel's avatar
tgerstel committed
176
        self.expose(u);
tgerstel's avatar
tgerstel committed
177
178
        self.expose(v);

tgerstel's avatar
tgerstel committed
179
180
181
        let mut x = u;
        while let Parent::BinTree(y) = self[x].parent {
            x = y;
tgerstel's avatar
tgerstel committed
182
        }
tgerstel's avatar
tgerstel committed
183

tgerstel's avatar
tgerstel committed
184
185
        match self[x].parent {
            Parent::BinTree(_) => unreachable!(),
tgerstel's avatar
tgerstel committed
186
            Parent::Path(y) => y,
tgerstel's avatar
tgerstel committed
187
188
            Parent::Root => v,
        }
tgerstel's avatar
tgerstel committed
189
    } */
tgerstel's avatar
tgerstel committed
190

SirBlueRabbit's avatar
SirBlueRabbit committed
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
    pub fn depth(&self, u: Label<Node<V>>) -> usize {
        let mut label = u;
        let mut depth: isize = 0;
        loop {
            if self.parent(label).is_some() {
                let d_depth = self[label].d_depth as isize;
                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 {
                let d_depth = self[label].d_depth as isize;
                return (depth + d_depth).abs() as usize;
            }
        }
    }

tgerstel's avatar
tgerstel committed
218
219
220
    // 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
221
222
223
224
225
226
227
228
229
230
231
232
233
        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
234
235
            } else {
                depth -= self[label].d_depth;
SirBlueRabbit's avatar
SirBlueRabbit committed
236
237
238
239
240
            }
            flip ^= self[label].d_flip;
        }
    }

tgerstel's avatar
tgerstel committed
241
242
    pub fn value_mut(&mut self, u: Label<Node<V>>) -> &mut V {
        &mut self[u].value
tgerstel's avatar
tgerstel committed
243
244
    }

tgerstel's avatar
tgerstel committed
245
246
    pub fn value(&self, u: Label<Node<V>>) -> &V {
        &self[u].value
tgerstel's avatar
tgerstel committed
247
248
    }

tgerstel's avatar
tgerstel committed
249
250
    fn expose(&mut self, u: Label<Node<V>>) {
        self.splay(u);
SirBlueRabbit's avatar
SirBlueRabbit committed
251
        if let Some(c) = self[u].children[ChildType::from(true)] {
tgerstel's avatar
tgerstel committed
252
            self[c].parent = Parent::Path(u);
SirBlueRabbit's avatar
SirBlueRabbit committed
253
            self[u].children[ChildType::from(true)] = None;
SirBlueRabbit's avatar
SirBlueRabbit committed
254
255
        }

tgerstel's avatar
tgerstel committed
256
        while let Parent::Path(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
257
            self.splay(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
258
            if let Some(x) = self[p].children[ChildType::from(true)] {
tgerstel's avatar
tgerstel committed
259
                self[x].parent = Parent::Path(p);
SirBlueRabbit's avatar
SirBlueRabbit committed
260
            }
SirBlueRabbit's avatar
SirBlueRabbit committed
261
            self[p].children[ChildType::from(true)] = Some(u);
tgerstel's avatar
tgerstel committed
262
263
            self[u].parent = Parent::BinTree(p);
            self.rotate(u);
tgerstel's avatar
tgerstel committed
264
        }
SirBlueRabbit's avatar
SirBlueRabbit committed
265
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
266

tgerstel's avatar
tgerstel committed
267
268
269
    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
270
            if let Parent::BinTree(_) = self[p].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
271
                if self[p].d_flip == (self.child_type(u) == self.child_type(p)) {
SirBlueRabbit's avatar
SirBlueRabbit committed
272
                    self.rotate(p);
tgerstel's avatar
tgerstel committed
273
                    self.rotate(u);
tgerstel's avatar
tgerstel committed
274
                } else {
tgerstel's avatar
tgerstel committed
275
276
                    self.rotate(u);
                    self.rotate(u);
tgerstel's avatar
tgerstel committed
277
278
                };
            } else {
tgerstel's avatar
tgerstel committed
279
                self.rotate(u);
tgerstel's avatar
tgerstel committed
280
281
                break;
            }
tgerstel's avatar
tgerstel committed
282
283
284
        }
    }

tgerstel's avatar
tgerstel committed
285
286
    fn cascade_flipped(&mut self, u: Label<Node<V>>) {
        if let Parent::BinTree(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
287
288
289
            self.cascade_flipped(p);
        }

SirBlueRabbit's avatar
SirBlueRabbit committed
290
        if self[u].d_flip {
tgerstel's avatar
tgerstel committed
291
292
            let Children(a_opt, b_opt) = self[u].children;
            self[u].children = Children(b_opt, a_opt);
SirBlueRabbit's avatar
SirBlueRabbit committed
293
            self[u].d_flip = false;
SirBlueRabbit's avatar
SirBlueRabbit committed
294
            if let Some(a) = a_opt {
SirBlueRabbit's avatar
SirBlueRabbit committed
295
                self[a].d_flip = !self[a].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
296
297
            }
            if let Some(b) = b_opt {
SirBlueRabbit's avatar
SirBlueRabbit committed
298
                self[b].d_flip = !self[b].d_flip;
SirBlueRabbit's avatar
SirBlueRabbit committed
299
300
301
302
            }
        }
    }

tgerstel's avatar
tgerstel committed
303
    fn rotate(&mut self, u: Label<Node<V>>) {
SirBlueRabbit's avatar
SirBlueRabbit committed
304
305
        let u_child_type = self.child_type(u);
        let x_opt = self[u].children[u_child_type.mirror().unwrap()];
tgerstel's avatar
tgerstel committed
306
        if let Parent::BinTree(p) = self[u].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
307
308
            let u_d_depth = self[u].d_depth;
            let p_d_depth = self[p].d_depth;
tgerstel's avatar
tgerstel committed
309

SirBlueRabbit's avatar
SirBlueRabbit committed
310
311
            self[p].d_depth = u_d_depth;
            self[u].d_depth = if let Parent::BinTree(_) = self[p].parent {
SirBlueRabbit's avatar
SirBlueRabbit committed
312
                if self[p].d_flip ^ (self.child_type(u) == self.child_type(p)) {
SirBlueRabbit's avatar
SirBlueRabbit committed
313
                    u_d_depth + p_d_depth
tgerstel's avatar
tgerstel committed
314
                } else {
SirBlueRabbit's avatar
SirBlueRabbit committed
315
                    u_d_depth.abs_diff(p_d_depth)
tgerstel's avatar
tgerstel committed
316
317
                }
            } else {
SirBlueRabbit's avatar
SirBlueRabbit committed
318
                u_d_depth.abs_diff(p_d_depth)
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
401
        lct.rotate(o);
        dbg!(&lct);
SirBlueRabbit's avatar
SirBlueRabbit committed
402
403

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

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

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

        // define nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
417
418
419
420
421
422
423
        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
424
425

        // link nodes
tgerstel's avatar
tgerstel committed
426
427
428
        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
429
430
431
432
433
434
        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
435
436

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

        // check children
tgerstel's avatar
tgerstel committed
441
442
443
444
445
446
447
        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
448
449

        // check parents
tgerstel's avatar
tgerstel committed
450
451
452
453
454
455
456
        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
457
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
458
459

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

        // define nodes
SirBlueRabbit's avatar
SirBlueRabbit committed
464
465
466
467
468
469
470
        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
471
472

        // link nodes
tgerstel's avatar
tgerstel committed
473
474
475
        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
476
477
478
479
480
481
        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
482
483

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

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

        // check children
tgerstel's avatar
tgerstel committed
492
493
494
495
496
497
498
        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
499
500

        // check parents
tgerstel's avatar
tgerstel committed
501
502
503
504
505
506
507
        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
508
509

        // check flipped
SirBlueRabbit's avatar
SirBlueRabbit committed
510
511
512
513
514
515
516
        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
517
518
519
    }

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

SirBlueRabbit's avatar
SirBlueRabbit committed
523
524
525
526
527
528
        // 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
529

SirBlueRabbit's avatar
SirBlueRabbit committed
530
        // link nodes
tgerstel's avatar
tgerstel committed
531
532
        lct[b].children = Children(None, Some(c));
        lct[d].children = Children(None, Some(e));
tgerstel's avatar
tgerstel committed
533
534
535
536
        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
537
538
539
540
541

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

tgerstel's avatar
tgerstel committed
542
543
544
545
546
547
        // 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
548
549
550
    }

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

SirBlueRabbit's avatar
SirBlueRabbit committed
554
555
556
557
558
559
560
561
        // 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
562

SirBlueRabbit's avatar
SirBlueRabbit committed
563
        // link nodes
tgerstel's avatar
tgerstel committed
564
565
566
        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
567
568
569
570
571
572
        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
573
574
575

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

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

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

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

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

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

SirBlueRabbit's avatar
SirBlueRabbit committed
597
598
599
600
601
602
603
604
        // 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
605

SirBlueRabbit's avatar
SirBlueRabbit committed
606
        // link nodes
tgerstel's avatar
tgerstel committed
607
608
609
        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
610
611
612
613
614
615
        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
616
617
618
619
620

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

SirBlueRabbit's avatar
SirBlueRabbit committed
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
648
    #[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
649
    }
SirBlueRabbit's avatar
SirBlueRabbit committed
650
651
652
653

    #[test]
    fn random_scan() {
        // generate a basic lct
tgerstel's avatar
tgerstel committed
654
        let size = 3;
SirBlueRabbit's avatar
SirBlueRabbit committed
655
656
657
658
659
660
661
662
663
664
665
666
        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 mutate lct and check integrity
        for _ in 0..(size * size * size) {
tgerstel's avatar
tgerstel committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
            let label1 = Label::from(fastrand::usize(0..size));
            let label2 = Label::from(fastrand::usize(0..size));
            if label1 != label2 {
                dbg!(&lct);
                lct.evert(label1);
                dbg!(&lct);
                lct.expose(label2);
                dbg!(&lct);
                dbg!(label1);
                dbg!(label2);
                let depth = lct.depth(label2);
                assert_depth(&lct, label2);
                let index = fastrand::usize(0..depth);
                let cut = lct.index_depth(label2, index + 1);
                lct.cut(cut);
                lct.evert(label2);
                lct.link(label1, label2);
SirBlueRabbit's avatar
SirBlueRabbit committed
684
685
686
687
688
            }
            assert_clean(&lct);
        }
    }

tgerstel's avatar
tgerstel committed
689
690
691
692
693
694
695
696
    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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    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
717
}