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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
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
648
649
650
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//!

//! Generic, general purpose tree data structures.

//!

pub use self::error::TreeError;
use slab;
use std::ops::{Index, IndexMut};
use crate::tree::traversal::Traversable;
use crate::types::{Children, DefaultIndexType, IndexType, NodeIndex, Tgf, Values, ValueType};

#[cfg(test)]
mod tests;

pub mod error;

/// This trait defines the fundamental operations of a generic forest.

pub trait GenericForest<V, Ix = DefaultIndexType>: Traversable<V, Ix>
    where
        V: ValueType,
        Ix: IndexType,
{
    /// Inserts `value` into the forest as a new root node and return the assigned `NodeIndex`.

    fn insert(&mut self, value: V) -> NodeIndex<Ix>;

    /// Inserts `value` into the forest as a new node. which will be the last child of the

    /// node indexed by `parent`. If the operation has been completed successfully, the

    /// index of the new child is returned. Otherwise, in particular if `parent` is not a

    /// valid node index, an error is returned.

    fn insert_child(
        &mut self,
        parent: NodeIndex<Ix>,
        value: V,
    ) -> Result<NodeIndex<Ix>, TreeError<Ix>>;

    /// Inserts `value` into the forest as a new node. which will be a child of the

    /// node indexed by `parent` at the position specified by `pos`. If `pos` is greater than or

    /// equal to the number of children of `parent`, the new child will be the new last child.

    /// If the operation has been completed successfully, the index of the new child is returned.

    /// Otherwise, if `parent` is not a valid node index, an error is returned.

    fn insert_child_at(
        &mut self,
        parent: NodeIndex<Ix>,
        pos: usize,
        value: V,
    ) -> Result<NodeIndex<Ix>, TreeError<Ix>>;

    /// Removes the tree node indexed by `node`, returning its content in case of a valid index.

    /// If the removed node has children, they will become children of the parent of the removed node,

    /// replacing the removed node. If the removed node has no parent, its children will become roots.

    fn remove(&mut self, node: NodeIndex<Ix>) -> Result<V, TreeError<Ix>>;

    /// Removes the tree node indexed by `node` and its subtree, returning the contents of the

    /// removed nodes in case of a valid index. The returned values will be collected in pre-order.

    fn remove_subtree(&mut self, node: NodeIndex<Ix>)
                      -> Result<Vec<(NodeIndex, V)>, TreeError<Ix>>;

    /// Adds the root node `child` as the new last child of the node indexed by `parent`. If the operation has

    /// been completed successfully, `Ok(())` is returned. If the forest has not been changed, an error

    /// is returned. This will be the case if:

    ///

    /// - the node indexed by `child` is not a tree root, i.e. has a parent.

    /// - the node indexed by `parent` is a node in the tree rooted in `child`.

    /// - either `parent` or `child` is not a valid node index.

    fn set_as_child(
        &mut self,
        parent: NodeIndex<Ix>,
        child: NodeIndex<Ix>,
    ) -> Result<(), TreeError<Ix>>;

    /// Adds the root node `child` as a child of the node indexed by `parent` at the position specified

    /// by `pos`. If `pos` is greater than or equal to the number of children of `parent`, the new

    /// child will be the new last child. If the operation has been completed successfully, `Ok(())`

    /// is returned. If the forest has not been changed, an error is returned. This will be the case if:

    ///

    /// - the node indexed by `child` is not a tree root, i.e. has a parent.

    /// - the node indexed by `parent` is a node in the tree rooted in `child`.

    /// - either `parent` or `child` is not a valid node index.

    fn set_as_child_at(
        &mut self,
        parent: NodeIndex<Ix>,
        child: NodeIndex<Ix>,
        pos: usize,
    ) -> Result<(), TreeError<Ix>>;

    /// Removes the node indexed by `node` as a child of its parent, thus making it a new root

    /// node of the forest. If the operation has been completed successfully, `Ok(())` is returned.

    /// If `node` is not a valid not index, an error is returned.

    fn remove_as_child(&mut self, node: NodeIndex<Ix>) -> Result<(), TreeError<Ix>>;
}

#[derive(Debug, Clone)]
struct Node<V>
    where
        V: ValueType,
{
    value: V,
    child_count: usize,
    context: NodeContext,
}

impl<V> Node<V>
    where
        V: ValueType,
{
    fn new(value: V) -> Self {
        Node {
            value,
            child_count: 0,
            context: NodeContext::new(),
        }
    }
}

#[derive(Copy, Clone, Debug)]
struct NodeContext {
    parent: Option<NodeIndex>,
    first_child: Option<NodeIndex>,
    last_child: Option<NodeIndex>,
    prev_sibling: Option<NodeIndex>,
    next_sibling: Option<NodeIndex>,
}

impl NodeContext {
    fn new() -> Self {
        NodeContext {
            parent: None,
            first_child: None,
            last_child: None,
            prev_sibling: None,
            next_sibling: None,
        }
    }
}

struct ChildIterator<'slf, V>
    where
        V: ValueType,
{
    forest: &'slf Forest<V>,
    current: Option<NodeIndex>,
}

impl<'slf, V> ChildIterator<'slf, V>
    where
        V: 'slf + ValueType,
{
    fn new(forest: &'slf Forest<V>, node: NodeIndex) -> Self {
        ChildIterator {
            forest,
            current: forest
                .arena
                .get(node.index())
                .and_then(|n| n.context.first_child),
        }
    }
}

impl<'slf, V> Iterator for ChildIterator<'slf, V>
    where
        V: 'slf + ValueType,
{
    type Item = NodeIndex;
    fn next(&mut self) -> Option<NodeIndex> {
        let ret = self.current;
        match ret {
            Some(n) => {
                self.current = self.forest.arena[n.index()].context.next_sibling;
                ret
            }
            None => None,
        }
    }
}

/// `Forest<V>` is a general purpose data structure for holding a forest of trees. Its tree nodes

/// are held in a [memory arena][1] and are addressed through their associated `NodeIndex`.

///

/// `Forest` is parameterized over:

/// - Associated values of type `V`, where `V` must implement the trait [`ValueType`][2]

///

/// The tree nodes of the forest are organized in a first child/next-sibling representation,

/// augmented by last child/previous sibling links. In other words, the children of tree node are

/// maintained in doubly linked list, where the head and tail of the list are the first and last

/// child values of the children's parent node.

///

/// Therefore, no allocations and re-allocations are necessary when adding children to nodes.

/// Re-allocation will only take place if the underlying memory arena has reached its capacity.

///

/// However, care must be taken when accessing a child node by its position, that is, by using

/// the method [`child(node, pos)`][3] of the [`Traversal`][4] trait, because to access a child by

/// its position in the list, the list has to be iterated from the beginning. Using access by

/// position is therefore not very efficient. This caveat also applies to the generic traversal

/// iterators provided by the [`traversal`][5] module, which are build on access by position.

///

/// In order to iterate over the children of node, the trait [`Children`][6] is available.

///

/// To illustrate the above explations see the following example:

///

/// ```

/// use outils::prelude::*;

/// use outils::tree::traversal::GeneralDfsValues;

/// let mut forest = Forest::new(10);

///

/// // Create a root with 9 child nodes.

/// let root = forest.insert(9);

/// for i in (0..9) {

///     forest.insert_child(root, i);

/// }

///

/// // Inefficient iteration:

/// for pos in (0..9) {

///     let index = forest.child(root, pos).expect("Should not fail here!"); // Inefficient!

///     let value = forest.value(index).expect("Should not fail here!");

///     assert_eq!(*value, pos);

/// }

///

/// // Also inefficient is using the provided, generic traversals:

/// let seq: Vec<&usize> = GeneralDfsValues::new(&forest, root).collect(); // Will use child()!

/// assert_eq!(seq, vec![&9, &0, &1, &2, &3, &4, &5, &6, &7, &8]);

///

/// // Efficient iteration:

/// let mut pos = 0;

/// for child in forest.children(root) {

///     let value = forest.value(child).expect("Should not fail here!");

///     assert_eq!(*value, pos);

///     pos += 1;

/// }

/// ```

///

/// [1]: https://en.wikipedia.org/wiki/Region-based_memory_management

/// [2]: ../../types/trait.ValueType.html

/// [3]: ../traversal/trait.Traversable.html#tymethod.child

/// [4]: ../traversal/trait.Traversable.html

/// [5]: ../traversal/index.html

/// [6]: ../../types/trait.Children.html

#[derive(Clone, Debug)]
pub struct Forest<V>
    where
        V: ValueType,
{
    arena: slab::Slab<Node<V>>,
    roots: Vec<NodeIndex>,
}

impl<V> Forest<V>
    where
        V: ValueType,
{
    /// Construct a new empty `Forest` with an initial capacity of `size`.

    pub fn new(size: usize) -> Self {
        Forest {
            arena: slab::Slab::with_capacity(size),
            roots: Vec::new(),
        }
    }

    /// Returns the index of the first child node of the tree node indexed by `node`. If the node

    /// has no children or `node` is not a valid index, `None` is returned.

    ///

    /// ```

    /// use outils::prelude::*;

    ///

    /// let mut tree = Forest::new(10);

    /// let root = tree.insert(1);

    /// let first_child = tree.insert_child(root, 2).expect("Should not fail here");

    /// let second_child = tree.insert_child(root, 3).expect("Should not fail here");

    ///

    /// assert_eq!(tree.first_child(root), Some(first_child));

    /// ```

    pub fn first_child(&self, parent: NodeIndex) -> Option<NodeIndex> {
        self.arena
            .get(parent.index())
            .and_then(|p| p.context.first_child)
    }

    /// Returns the index of the last child node of the tree node indexed by `node`. If the node

    /// has no children or `node` is not a valid index, `None` is returned.

    ///

    /// ```

    /// use outils::prelude::*;

    ///

    /// let mut tree = Forest::new(10);

    /// let root = tree.insert(1);

    /// let first_child = tree.insert_child(root, 2).expect("Should not fail here");

    /// let second_child = tree.insert_child(root, 3).expect("Should not fail here");

    ///

    /// assert_eq!(tree.last_child(root), Some(second_child));

    /// ```

    pub fn last_child(&self, parent: NodeIndex) -> Option<NodeIndex> {
        self.arena
            .get(parent.index())
            .and_then(|p| p.context.last_child)
    }

    /// Returns the index of the previous sibling node of the tree node indexed by `node`. If the

    /// node is the first child of its parent or `node` is not a valid index, `None` is returned.

    ///

    /// ```

    /// use outils::prelude::*;

    ///

    /// let mut tree = Forest::new(10);

    /// let root = tree.insert(1);

    /// let first_child = tree.insert_child(root, 2).expect("Should not fail here");

    /// let second_child = tree.insert_child(root, 3).expect("Should not fail here");

    ///

    /// assert_eq!(tree.prev_sibling(second_child), Some(first_child));

    /// ```

    pub fn prev_sibling(&self, node: NodeIndex) -> Option<NodeIndex> {
        self.arena
            .get(node.index())
            .and_then(|n| n.context.prev_sibling)
    }

    /// Returns the index of the next sibling node of the tree node indexed by `node`. If the

    /// node is the last child of its parent or `node` is not a valid index, `None` is returned.

    ///

    /// ```

    /// use outils::prelude::*;

    ///

    /// let mut tree = Forest::new(10);

    /// let root = tree.insert(1);

    /// let first_child = tree.insert_child(root, 2).expect("Should not fail here");

    /// let second_child = tree.insert_child(root, 3).expect("Should not fail here");

    ///

    /// assert_eq!(tree.next_sibling(first_child), Some(second_child));

    /// ```

    pub fn next_sibling(&self, node: NodeIndex) -> Option<NodeIndex> {
        self.arena
            .get(node.index())
            .and_then(|n| n.context.next_sibling)
    }

    /// Returns the list of root node indices of this forest. The values are not returned in any

    /// particular order.

    ///

    /// ```

    /// use outils::prelude::*;

    ///

    /// let mut tree = Forest::new(10);

    /// let first_root = tree.insert(1);

    /// let second_root = tree.insert(2);

    ///

    /// let roots = tree.roots();

    /// assert!(roots.contains(&first_root));

    /// assert!(roots.contains(&second_root));

    /// ```

    pub fn roots(&self) -> &Vec<NodeIndex> {
        &self.roots
    }
}

impl<'slf, V> Children<'slf> for Forest<V>
    where
        V: 'slf + ValueType,
{
    fn children(&'slf self, node: NodeIndex) -> Box<dyn Iterator<Item=NodeIndex> + 'slf> {
        Box::new(ChildIterator::new(self, node))
    }
}

impl<V> Index<NodeIndex> for Forest<V>
    where
        V: ValueType,
{
    type Output = V;
    fn index(&self, index: NodeIndex) -> &V {
        &self.arena[index.index()].value
    }
}

impl<V> IndexMut<NodeIndex> for Forest<V>
    where
        V: ValueType,
{
    fn index_mut(&mut self, index: NodeIndex) -> &mut V {
        &mut self.arena[index.index()].value
    }
}

impl<V> Traversable<V> for Forest<V>
    where
        V: ValueType,
{
    /// Returns the index of the root node of the tree containing the tree node indexed by `node`.

    fn root(&self, node: NodeIndex) -> Option<NodeIndex> {
        if let Some(_n) = self.arena.get(node.index()) {
            let mut child = node;
            while let Some(parent) = self.arena[child.index()].context.parent {
                child = parent;
            }
            return Some(child);
        }
        None
    }

    /// Immutably access the value stored in the tree node indexed by `node`.

    fn value(&self, node: NodeIndex) -> Option<&V> {
        self.arena.get(node.index()).map(|n| &n.value)
    }

    /// Mutably access the value stored in the tree node indexed by `node`.

    fn value_mut(&mut self, node: NodeIndex) -> Option<&mut V> {
        self.arena.get_mut(node.index()).map(|n| &mut n.value)
    }

    /// Returns the index of parent node tree node indexed by `node`.

    fn parent(&self, node: NodeIndex) -> Option<NodeIndex> {
        self.arena.get(node.index()).and_then(|n| n.context.parent)
    }

    /// Returns the index of the child node at position `pos` of  the tree node indexed by `node`.

    fn child(&self, node: NodeIndex, pos: usize) -> Option<NodeIndex> {
        ChildIterator::new(self, node).nth(pos)
    }

    /// Returns the number of child nodes of the tree node indexed by `node`.

    fn child_count(&self, node: NodeIndex) -> usize {
        self.arena.get(node.index()).map_or(0, |n| n.child_count)
    }

    /// Returns the total number of tree nodes of the tree `self`.

    fn node_count(&self) -> usize {
        self.arena.len()
    }
}

impl<'slf, V> Values<'slf, V> for Forest<V>
    where
        V: 'slf + ValueType,
{
    /// Returns a boxed iterator over the stored values and their corresponding

    /// tree node indices held by `self`.

    fn values(&'slf self) -> Box<dyn Iterator<Item=(NodeIndex, &'slf V)> + 'slf> {
        Box::new(self.arena.iter().map(|(i, v)| (NodeIndex(i), &v.value)))
    }
}

impl<V> GenericForest<V> for Forest<V>
    where
        V: ValueType,
{
    /// Inserts `value` into the forest as a new root node and return the assigned `NodeIndex`.

    fn insert(&mut self, value: V) -> NodeIndex {
        let node = NodeIndex(self.arena.insert(Node::new(value)));
        self.roots.push(node);
        node
    }

    /// Inserts `value` into the forest as a new node. which will be the last child of the

    /// node indexed by `parent`. If the operation has been completed successfully, the

    /// index of the new child is returned. Otherwise, in particular if `parent` is not a

    /// valid node index, an error is returned.

    fn insert_child(&mut self, parent: NodeIndex, value: V) -> Result<NodeIndex, TreeError> {
        if !self.arena.contains(parent.index()) {
            return Err(TreeError::invalid_node_index("insert_child()", parent));
        }
        let child = NodeIndex(self.arena.insert(Node::new(value)));

        match self.arena[parent.index()].context.last_child {
            Some(last) => {
                self.arena[last.index()].context.next_sibling = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
                self.arena[child.index()].context.prev_sibling = Some(last);
            }
            None => {
                self.arena[parent.index()].context.first_child = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
        }
        self.arena[child.index()].context.parent = Some(parent);
        self.arena[parent.index()].child_count += 1;
        Ok(child)
    }

    /// Inserts `value` into the forest as a new node. which will be a child of the

    /// node indexed by `parent` at the position specified by `pos`. If `pos` is greater than or

    /// equal to the number of children of `parent`, the new child will be the new last child.

    /// If the operation has been completed successfully, the index of the new child is returned.

    /// Otherwise, if `parent` is not a valid node index, an error is returned.

    fn insert_child_at(
        &mut self,
        parent: NodeIndex,
        pos: usize,
        value: V,
    ) -> Result<NodeIndex, TreeError> {
        if !self.arena.contains(parent.index()) {
            return Err(TreeError::invalid_node_index("insert_child_at()", parent));
        }
        let child = NodeIndex(self.arena.insert(Node::new(value)));

        let mut prev = None;
        let mut next = self.arena[parent.index()].context.first_child;
        let mut i = 0;

        while i < pos {
            match next {
                Some(n) => {
                    prev = next;
                    next = self.arena[n.index()].context.next_sibling;
                    i += 1;
                }
                None => {
                    break;
                }
            }
        }
        match (prev, next) {
            (None, Some(n)) => {
                self.arena[n.index()].context.prev_sibling = Some(child);
                self.arena[parent.index()].context.first_child = Some(child);
            }
            (Some(p), Some(n)) => {
                self.arena[n.index()].context.prev_sibling = Some(child);
                self.arena[p.index()].context.next_sibling = Some(child);
            }
            (Some(p), None) => {
                self.arena[p.index()].context.next_sibling = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
            (None, None) => {
                self.arena[parent.index()].context.first_child = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
        }
        self.arena[child.index()].context.parent = Some(parent);
        self.arena[child.index()].context.prev_sibling = prev;
        self.arena[child.index()].context.next_sibling = next;
        self.arena[parent.index()].child_count += 1;
        Ok(child)
    }

    /// Removes the tree node indexed by `node`, returning its content in case of a valid index.

    /// If the removed node has children, they will become children of the parent of the removed node,

    /// replacing the removed node. If the removed node has no parent, its children will become roots.

    fn remove(&mut self, node: NodeIndex) -> Result<V, TreeError> {
        if !self.arena.contains(node.index()) {
            return Err(TreeError::invalid_node_index("remove()", node));
        }

        let context = self.arena[node.index()].context;
        match context.parent {
            Some(parent) => {
                match (
                    context.first_child,
                    context.last_child,
                    context.prev_sibling,
                    context.next_sibling,
                ) {
                    (Some(first), Some(last), Some(prev), Some(next)) => {
                        self.arena[prev.index()].context.next_sibling = Some(first);
                        self.arena[next.index()].context.prev_sibling = Some(last);
                        self.arena[first.index()].context.prev_sibling = Some(prev);
                        self.arena[last.index()].context.next_sibling = Some(next);
                    }
                    (Some(first), Some(last), Some(prev), None) => {
                        self.arena[parent.index()].context.last_child = Some(last);
                        self.arena[first.index()].context.prev_sibling = Some(prev);
                        self.arena[prev.index()].context.next_sibling = Some(first);
                    }
                    (Some(first), Some(last), None, Some(next)) => {
                        self.arena[parent.index()].context.first_child = Some(first);
                        self.arena[last.index()].context.next_sibling = Some(next);
                        self.arena[next.index()].context.prev_sibling = Some(last);
                    }
                    (Some(first), Some(last), None, None) => {
                        self.arena[parent.index()].context.first_child = Some(first);
                        self.arena[parent.index()].context.last_child = Some(last);
                    }
                    (None, None, Some(prev), Some(next)) => {
                        self.arena[prev.index()].context.next_sibling = Some(next);
                        self.arena[next.index()].context.prev_sibling = Some(prev);
                    }

                    (None, None, Some(prev), None) => {
                        self.arena[parent.index()].context.last_child = Some(prev);
                        self.arena[prev.index()].context.next_sibling = None;
                    }

                    (None, None, None, Some(next)) => {
                        self.arena[parent.index()].context.first_child = Some(next);
                        self.arena[next.index()].context.prev_sibling = None;
                    }

                    (None, None, None, None) => {
                        self.arena[parent.index()].context.first_child = None;
                        self.arena[parent.index()].context.last_child = None;
                    }
                    _ => panic!("remove(): first and last child indices are incorrect"),
                }
                let mut child = self.arena[node.index()].context.first_child;
                while let Some(c) = child {
                    child = self.arena[c.index()].context.next_sibling;
                    self.arena[c.index()].context.parent = Some(parent);
                }
                self.arena[parent.index()].child_count -= 1;
                self.arena[parent.index()].child_count += self.arena[node.index()].child_count;
            }
            None => {
                self.roots.retain(|&r| r != node);
                let mut child = self.arena[node.index()].context.first_child;
                while let Some(c) = child {
                    child = self.arena[c.index()].context.next_sibling;
                    self.arena[c.index()].context.parent = None;
                    self.arena[c.index()].context.prev_sibling = None;
                    self.arena[c.index()].context.next_sibling = None;
                    self.roots.push(c);
                }
            }
        }
        Ok(self.arena.remove(node.index()).value)
    }

    /// Removes the tree node indexed by `node` and its subtree, returning the contents of the

    /// removed nodes in case of a valid index. The returned values will be collected in pre-order.

    fn remove_subtree(&mut self, node: NodeIndex) -> Result<Vec<(NodeIndex, V)>, TreeError> {
        if self.remove_as_child(node).is_err() {
            return Err(TreeError::invalid_node_index("remove_subtree()", node));
        }

        let mut stack = Vec::with_capacity(self.arena[node.index()].child_count + 1);
        let mut ret = Vec::with_capacity(stack.capacity());
        stack.push(node);
        while let Some(parent) = stack.pop() {
            let mut child = self.arena[parent.index()].context.last_child;
            while let Some(c) = child {
                stack.push(c);
                child = self.arena[c.index()].context.prev_sibling;
            }
            ret.push((parent, self.arena.remove(parent.index()).value));
        }
        self.roots.retain(|&r| r != node);
        Ok(ret)
    }

    /// Adds the root node `child` as the new last child of the node indexed by `parent`. If the operation has

    /// been completed successfully, `Ok(())` is returned. If the forest has not been changed, an error

    /// is returned. This will be the case if:

    ///

    /// - the node indexed by `child` is not a tree root, i.e. has a parent.

    /// - the node indexed by `parent` is a node in the tree rooted in `child`.

    /// - either `parent` or `child` is not a valid node index.

    fn set_as_child(&mut self, parent: NodeIndex, child: NodeIndex) -> Result<(), TreeError> {
        if !self.arena.contains(parent.index()) {
            return Err(TreeError::invalid_node_index("set_as_child()", parent));
        }
        if !self.arena.contains(child.index()) {
            return Err(TreeError::invalid_node_index("set_as_child()", child));
        }
        if self.arena[child.index()].context.parent.is_some() {
            return Err(TreeError::expected_root_node("set_as_child()", child));
        }
        if self.root(parent) == Some(child) {
            return Err(TreeError::expected_non_ancestor_node(
                "set_as_child()",
                parent,
                child,
            ));
        }

        match self.arena[parent.index()].context.last_child {
            Some(last) => {
                self.arena[last.index()].context.next_sibling = Some(child);
                self.arena[child.index()].context.prev_sibling = Some(last);
                self.arena[parent.index()].context.last_child = Some(child);
            }
            None => {
                self.arena[parent.index()].context.first_child = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
        }
        self.arena[child.index()].context.parent = Some(parent);
        self.arena[parent.index()].child_count += 1;
        self.roots.retain(|&r| r != child);
        Ok(())
    }

    /// Adds the root node `child` as a child of the node indexed by `parent` at the position specified

    /// by `pos`. If `pos` is greater than or equal to the number of children of `parent`, the new

    /// child will be the new last child. If the operation has been completed successfully, `Ok(())`

    /// is returned. If the forest has not been changed, an error is returned. This will be the case if:

    ///

    /// - the node indexed by `child` is not a tree root, i.e. has a parent.

    /// - the node indexed by `parent` is a node in the tree rooted in `child`.

    /// - either `parent` or `child` is not a valid node index.

    fn set_as_child_at(
        &mut self,
        parent: NodeIndex,
        child: NodeIndex,
        pos: usize,
    ) -> Result<(), TreeError> {
        if !self.arena.contains(parent.index()) {
            return Err(TreeError::invalid_node_index("set_as_child_at()", parent));
        }
        if !self.arena.contains(child.index()) {
            return Err(TreeError::invalid_node_index("set_as_child_at()", child));
        }
        if self.arena[child.index()].context.parent.is_some() {
            return Err(TreeError::expected_root_node("set_as_child_at()", child));
        }
        if self.root(parent) == Some(child) {
            return Err(TreeError::expected_non_ancestor_node(
                "set_as_child_at()",
                parent,
                child,
            ));
        }

        let mut prev = None;
        let mut next = self.arena[parent.index()].context.first_child;
        let mut i = 0;

        while i < pos {
            match next {
                Some(n) => {
                    prev = next;
                    next = self.arena[n.index()].context.next_sibling;
                    i += 1;
                }
                None => {
                    break;
                }
            }
        }
        match (prev, next) {
            (None, Some(n)) => {
                self.arena[n.index()].context.prev_sibling = Some(child);
                self.arena[parent.index()].context.first_child = Some(child);
            }
            (Some(p), Some(n)) => {
                self.arena[n.index()].context.prev_sibling = Some(child);
                self.arena[p.index()].context.next_sibling = Some(child);
            }
            (Some(p), None) => {
                self.arena[p.index()].context.next_sibling = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
            (None, None) => {
                self.arena[parent.index()].context.first_child = Some(child);
                self.arena[parent.index()].context.last_child = Some(child);
            }
        }
        self.arena[child.index()].context.parent = Some(parent);
        self.arena[child.index()].context.prev_sibling = prev;
        self.arena[child.index()].context.next_sibling = next;
        self.arena[parent.index()].child_count += 1;
        self.roots.retain(|&r| r != child);
        Ok(())
    }

    /// Removes the node indexed by `node` as a child of its parent, thus making it a new root

    /// node of the forest. If the operation has been completed successfully, `Ok(())` is returned.

    /// If `node` is not a valid not index, an error is returned.

    fn remove_as_child(&mut self, node: NodeIndex) -> Result<(), TreeError> {
        if !self.arena.contains(node.index()) {
            return Err(TreeError::invalid_node_index("remove_as_child()", node));
        }
        let context = self.arena[node.index()].context;

        if let Some(parent) = context.parent {
            match (context.prev_sibling, context.next_sibling) {
                (Some(prev), Some(next)) => {
                    self.arena[prev.index()].context.next_sibling = Some(next);
                    self.arena[next.index()].context.prev_sibling = Some(prev);
                }
                (Some(prev), None) => {
                    self.arena[prev.index()].context.next_sibling = None;
                    self.arena[parent.index()].context.last_child = Some(prev);
                }
                (None, Some(next)) => {
                    self.arena[next.index()].context.prev_sibling = None;
                    self.arena[parent.index()].context.first_child = Some(next);
                }
                (None, None) => {
                    self.arena[parent.index()].context.first_child = None;
                    self.arena[parent.index()].context.last_child = None;
                }
            }
            self.arena[parent.index()].child_count -= 1;
            self.roots.push(node);
        }
        self.arena[node.index()].context = NodeContext {
            parent: None,
            first_child: context.first_child,
            last_child: context.last_child,
            prev_sibling: None,
            next_sibling: None,
        };
        Ok(())
    }
}

impl<V> Tgf for Forest<V>
    where
        V: ValueType,
{
    fn to_tgf(&self) -> String {
        let mut nodes = String::from("");
        let mut edges = String::from("");
        let iter = self.arena.iter();
        for (index, node) in iter {
            nodes.push_str(format!("{}", index).as_str());
            nodes.push_str(" [K = ");
            nodes.push_str(format!("{}", index).as_str());
            nodes.push_str(", V = ");
            nodes.push_str(format!("{:?}", node.value).as_str());
            nodes.push_str("]\n");

            for child in self.children(NodeIndex(index)).enumerate() {
                edges.push_str(format!("{}", index).as_str());
                edges.push_str(" ");
                edges.push_str(format!("{}", child.1.index()).as_str());
                edges.push_str(" ");
                edges.push_str(format!("{}", child.0).as_str());
                edges.push_str("\n");
            }
        }
        nodes.push_str("#\n");
        nodes.push_str(edges.as_str());
        nodes
    }
}