libtree 0.1.0

a crate for general or game tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
use crate::{Cursor, CursorMut, UnsafeCursor};
use std::collections::LinkedList;
use std::convert::Into;
use std::marker::PhantomData;
use std::ptr::NonNull;

/// Represent a potential pointer to another Node
pub type Link<T> = Option<NonNull<Node<T>>>;

/// Represents a pointer to child node. Because childs pointers are stored in a [Vec], the null
/// case is handled by an empty Vec.
pub type ChildLink<T> = NonNull<Node<T>>;

/// Struture to represent a node in a tree
pub(crate) struct Node<T> {
    pub father: Link<T>,
    pub childs: Vec<ChildLink<T>>,
    pub elem: T,
}

/// The main structure in this tree crate
///
/// ## Data structure
/// A [Tree] object stores two raw pointers toward \[Node\](private struct) in the tree. The first one is the root,
/// and this the field that "owns" the data in the tree. What it means is that when Tree is
/// dropped, it is through the root.
/// The second field is another raw pointer, the 'current' pointer that allows to explore and
/// manipulate the tree. Every operation on the tree will always manipulate the 'current' pointer,
/// therefore we will describe the operation in terms of operation on 'current'.
///
/// If not written, every methods will panic if called on an empty tree. The main reason is that
/// most of them become ambigous if 'root' is None, therefore, except for very specific case, code
/// will panic.
/// Other possible panics are referenced in the documentation.
///
/// ## References
/// In order to have a concurrent exploration of the tree, this tree crate implements a special
/// type of cursor (as in [here](https://rust-unofficial.github.io/too-many-lists/fifth.html)).
/// This is due to the fact that in order to move around the tree, you need to change the 'current' pointer of the tree and therefore
/// invalidating every normal references to the tree. Check [Cursor], [CursorMut] and [UnsafeCursor]
/// for more detail.
pub struct Tree<T> {
    root: Link<T>,
    current: Link<T>,
    _boo: PhantomData<T>,
}

impl<T> Tree<T> {
    /// Creates a [Tree] from el. root and current will be pointing to the node holding el.
    pub fn from_element(el: T) -> Self {
        let node = unsafe {
            NonNull::new_unchecked(Box::into_raw(Box::new(Node {
                elem: el,
                childs: Vec::new(),
                father: None,
            })))
        };

        Tree {
            root: Some(node),
            current: Some(node),
            _boo: PhantomData,
        }
    }

    /// Return true if the tree is empty, i.e. if 'root' = None.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(10);
    /// tree.push_iter(vec![1, 2, 3]);
    /// // empty the tree if called at root, check documentation for into_vec
    /// tree.into_vec();
    /// assert!(tree.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.root.is_none()
    }

    /// Push el to 'current'.child as a new node in the tree.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(1);
    /// tree.push(2);
    /// tree.navigate_to(0);
    /// assert_eq!(tree.peek(), &2);
    /// ```
    pub fn push(&mut self, el: T) {
        if self.is_empty() {
            panic!("Tried to push an element to an empty tree")
        }
        unsafe {
            let current_node = &mut *(self.current.unwrap().as_ptr());
            current_node
                .childs
                .push(NonNull::new_unchecked(Box::into_raw(Box::new(Node {
                    elem: el,
                    childs: Vec::new(),
                    father: self.current,
                }))))
        }
    }

    /// Convenient method to push the elements of an iterator into the tree.
    /// It's litteraly : for el in iter.into_iter() { tree.push(el) }
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// tree.navigate_to(2);
    /// assert_eq!(tree.peek(), &3);
    /// ```
    pub fn push_iter<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>,
    {
        for el in iter.into_iter() {
            self.push(el);
        }
    }

    /// Insert el into 'current'.childs at index.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 3]);
    /// tree.insert(1, 2);
    /// tree.navigate_to(1);
    /// assert_eq!(tree.peek(), &2);
    /// ```
    pub fn insert(&mut self, index: usize, el: T) {
        if self.is_empty() {
            panic!("Tried to insert an element to an empty tree");
        }
        unsafe {
            let current_node = &mut *(self.current.unwrap().as_ptr());
            current_node.childs.insert(
                index,
                NonNull::new_unchecked(Box::into_raw(Box::new(Node {
                    elem: el,
                    childs: Vec::new(),
                    father: self.current,
                }))),
            );
        }
    }

    /// Set current to 'current'.childs\[index\], therefore navigating current to it's idx childs.
    ///
    /// # Panic
    ///
    /// This method will panic if index > tree.childs_len()
    pub fn navigate_to(&mut self, index: usize) {
        if self.is_empty() {
            panic!("Tried to move to with an empty tree");
        }

        let current_node = unsafe { &*(self.current.unwrap().as_ptr()) };
        if index >= current_node.childs.len() {
            panic!(
                "Tried to move to children {} of current node, but current node has only {} childs",
                index,
                current_node.childs.len()
            );
        }
        self.current = Some(current_node.childs[index]);
    }

    /// Set current to 'current'.father, therefore naviguating current to it's father
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(10);
    /// tree.push(-10);
    /// tree.navigate_to(0);
    /// tree.ascend();
    /// assert_eq!(tree.peek(), &10);
    /// ```
    ///
    /// # Panics
    ///
    /// This method will panic if 'current' has no father, ie 'current'.father == None (woof woof
    /// python).
    pub fn ascend(&mut self) {
        if self.is_empty() {
            panic!("Tried to move up with an empty tree");
        }

        let current_node = unsafe { &(*self.current.unwrap().as_ptr()) };
        if current_node.father.is_none() {
            panic!("Tried to move up but current has no father");
        }
        self.current = current_node.father;
    }

    /// Return true if current has a father. Note that it will return false and not panic is tree
    /// is empty.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(10);
    /// assert_eq!(tree.has_father(), false);
    /// tree.push(10);
    /// tree.navigate_to(0);
    /// assert_eq!(tree.has_father(), true);
    /// ```
    pub fn has_father(&self) -> bool {
        if self.is_empty() {
            return false;
        }
        unsafe { (*self.current.unwrap().as_ptr()).father.is_some() }
    }

    /// Set 'current' to 'tree.root', therefore navigating the tree back to root.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(1);
    /// tree.push(2);
    /// tree.navigate_to(0);
    /// tree.push(3);
    /// tree.navigate_to(0);
    /// assert_eq!(tree.peek(), &3);
    /// tree.go_to_root();
    /// assert_eq!(tree.peek(), &1);
    /// ```
    pub fn go_to_root(&mut self) {
        if self.is_empty() {
            panic!("Tried to move to root on an empty tree");
        }
        self.current = self.root;
    }

    /// Peek at 'current', returning a reference to the element stored in 'current'
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let tree = Tree::from_element(32);
    /// assert_eq!(tree.peek(), &32);
    /// ```
    pub fn peek(&self) -> &T {
        if self.is_empty() {
            panic!("Tried to peek on an empty tree");
        }
        unsafe { &(*self.current.unwrap().as_ptr()).elem }
    }

    /// Same as [Tree::peek], but returns a mutable reference instead
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(3);
    /// assert_eq!(tree.peek_mut(), &mut 3);
    /// ```
    pub fn peek_mut(&mut self) -> &mut T {
        if self.is_empty() {
            panic!("Tried to peek mut on an empty tree");
        }
        unsafe { &mut (*self.current.unwrap().as_ptr()).elem }
    }

    /// Peek on 'current'.childs\[index\]
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// assert_eq!(tree.peek_child(2), &3);
    /// ```
    ///
    /// # Panics
    /// This method will panic if index >= tree.childs_len()
    pub fn peek_child(&self, index: usize) -> &T {
        if self.is_empty() {
            panic!("Tried to call peek_child on an empty tree");
        }

        if index >= self.childs_len() {
            panic!(
                "Tried to call peek_child on child {} but current has only {} childs",
                index,
                self.childs_len()
            );
        }

        unsafe { &(*(*self.current.unwrap().as_ptr()).childs[index].as_ptr()).elem }
    }

    /// Same as [Tree::peek_child] but returns a mutable reference.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// assert_eq!(tree.peek_child_mut(2), &mut 3);
    /// ```
    ///
    /// # Panics
    /// This method will panic if index >= tree.childs_len()
    pub fn peek_child_mut(&mut self, index: usize) -> &mut T {
        if self.is_empty() {
            panic!("Tried to call peek_child_mut on an empty tree");
        }

        if index >= self.childs_len() {
            panic!(
                "Tried to call peek_child_mut on child {} but current has only {} childs",
                index,
                self.childs_len()
            );
        }

        unsafe { &mut (*(*self.current.unwrap().as_ptr()).childs[index].as_ptr()).elem }
    }
    /// Returns 'current'.childs.len
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(10);
    /// tree.push_iter(vec![1, 2, 3]);
    /// assert_eq!(tree.childs_len(), 3);
    /// ```
    pub fn childs_len(&self) -> usize {
        if self.is_empty() {
            panic!("Tried to call childs_len on an empty tree");
        }
        unsafe { (*self.current.unwrap().as_ptr()).childs.len() }
    }

    /// Return an iterator over the elements of current
    pub fn iter_childs(&self) -> ChildIterator<'_, T> {
        if self.is_empty() {
            panic!("Tried to call iter_childs on an empty tree");
        }
        ChildIterator {
            current: self.current.unwrap(),
            i: 0,
            len: self.childs_len(),
            _boo: PhantomData,
        }
    }

    /// Return a mutuable iterator over the elements of current
    pub fn iter_childs_mut(&self) -> ChildIteratorMut<'_, T> {
        if self.is_empty() {
            panic!("Tried to call iter_childs on an empty tree");
        }
        ChildIteratorMut {
            current: self.current.unwrap(),
            i: 0,
            len: self.childs_len(),
            _boo: PhantomData,
        }
    }

    /// Insert the other tree into 'current'.childs at index
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree1 = Tree::from_element(0);
    /// tree1.push_iter(vec![1, 2, 3]);
    /// let mut tree2 = Tree::from_element(4);
    /// tree2.push_iter(vec![5, 6]);
    /// tree1.join(tree2, 1);
    /// tree1.navigate_to(1);
    /// assert_eq!(tree1.iter_childs().collect::<Vec<&i32>>(), vec![&5, &6]);
    /// ```
    ///
    /// # Panics
    /// This method panic if either of the trees are empty
    pub fn join(&mut self, mut other: Tree<T>, index: usize) {
        if self.is_empty() || other.root.is_none() {
            panic!("Tried to call join on an empty tree");
        }

        let other_root = other.root.unwrap();
        // Very important, otherwise, when other get dropped, it will dropped it's old data in
        // current tree
        other.root = None;
        unsafe {
            (*other_root.as_ptr()).father = self.current;
            (*self.current.unwrap().as_ptr())
                .childs
                .insert(index, other_root);
        }
    }

    /// Remove from 'current' the subtree rooted in 'current'.childs\[index\] and return it as a new
    /// tree. This method also serves a remove method. It can also be used to dropped the subtree
    /// above the node you want to split at. It really does a lot of things...
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2]);
    /// tree.navigate_to(1);
    /// tree.push_iter(vec![3, 4]);
    /// tree.ascend();
    /// let split_tree = tree.split(1);
    /// assert_eq!(split_tree.peek(), &2);
    /// assert_eq!(
    ///     split_tree.iter_childs().collect::<Vec<&i32>>(),
    ///     vec![&3, &4]
    /// );
    /// assert_eq!(split_tree.has_father(), false);
    /// ```
    ///
    /// # Panics
    /// This method will panic if index >= tree.childs_len()
    pub fn split(&mut self, index: usize) -> Tree<T> {
        if self.is_empty() {
            panic!("Tried to call split on an empty tree");
        }

        let current = self.current.unwrap();
        unsafe {
            if index >= self.childs_len() {
                panic!(
                    "Tried to call split with index {} but current has only {} childs",
                    index,
                    self.childs_len()
                );
            }

            let split_node = (*current.as_ptr()).childs.remove(index);
            (*split_node.as_ptr()).father = None;
            Tree {
                root: Some(split_node),
                current: Some(split_node),
                _boo: PhantomData,
            }
        }
    }

    /// Return a [Cursor] pointing at 'current'
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let tree = Tree::from_element(5);
    /// let cursor = tree.cursor();
    /// assert_eq!(cursor.peek(), &5);
    /// ```
    pub fn cursor(&self) -> Cursor<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor on an empty tree");
        }

        Cursor {
            current: self.current.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Return a [CursorMut] pointing at 'current'
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(5);
    /// let mut cursor = tree.cursor_mut();
    /// assert_eq!(cursor.peek_mut(), &mut 5);
    /// ```
    pub fn cursor_mut(&mut self) -> CursorMut<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor on an empty tree");
        }

        CursorMut {
            current: self.current.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Return an [UnsafeCursor] pointing at 'current'
    ///
    /// # Safety
    /// Creating an UnsafeCursor is not unsafe in itself, but its utilisation can lead to unsafe
    /// behaviour. Please read carefully [UnsafeCursor] documentation.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let tree = Tree::from_element(5);
    /// let mut cursor = tree.unsafe_cursor();
    /// unsafe {assert_eq!(cursor.peek_mut(), &mut 5)};
    /// ```
    pub fn unsafe_cursor(&self) -> UnsafeCursor<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor on an empty tree");
        }

        UnsafeCursor {
            current: self.current.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Return a [Cursor] pointing at 'root'
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(3);
    /// tree.push(10);
    /// tree.navigate_to(0);
    /// let cursor = tree.cursor_root();
    /// assert_eq!(cursor.peek(), &3);
    /// ```
    pub fn cursor_root(&self) -> Cursor<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor_root on an empty tree");
        }

        Cursor {
            current: self.root.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Return a [CursorMut] pointing at 'root'
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(3);
    /// tree.push(10);
    /// tree.navigate_to(0);
    /// let mut cursor = tree.cursor_root_mut();
    /// assert_eq!(cursor.peek(), &3);
    /// ```
    pub fn cursor_root_mut(&mut self) -> CursorMut<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor_root on an empty tree");
        }

        CursorMut {
            current: self.root.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Return an [UnsafeCursor] pointing at 'root'
    ///
    /// # Safety
    /// Creating an UnsafeCursor is not unsafe in itself, but its utilisation can lead to unsafe
    /// behaviour. Please read carefully [UnsafeCursor] documentation.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(5);
    /// tree.push(10);
    /// tree.navigate_to(0);
    /// let mut cursor = tree.unsafe_cursor_root();
    /// unsafe {assert_eq!(cursor.peek_mut(), &mut 5)};
    /// ```
    pub fn unsafe_cursor_root(&self) -> UnsafeCursor<'_, T> {
        if self.is_empty() {
            panic!("Tried to call cursor on an empty tree");
        }

        UnsafeCursor {
            current: self.root.unwrap(),
            _boo: PhantomData,
        }
    }

    /// Collect the subtree in a depth-first order rooted at 'current' into a vec, and ascend 'current'.
    /// If 'current' is at 'root', and so it cannot ascend, the tree becomes an empty tree (and so
    /// most method will therefore fail).
    /// This method is the main way to collect back elements stored in the tree.
    ///
    /// This behaviour is different from `Into<Vec>` implemented, where the whole tree is turned into
    /// a Vec, and not just a subtree.
    ///
    /// # Examples
    /// ```
    /// // Example on a subtree
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// tree.navigate_to(0);
    /// tree.push_iter(vec![9, 10]);
    /// tree.navigate_to(0);
    /// tree.push(15);
    /// tree.go_to_root();
    /// tree.navigate_to(0);
    /// assert_eq!(tree.into_vec(), vec![1, 9, 15, 10]);
    /// // 'current' has ascend and is now at 'root'
    /// assert_eq!(tree.peek(), &0);
    /// ```
    /// ```
    /// // Example at 'root'
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element([0, 0]);
    /// tree.push_iter([[1, 2], [3, 4]]);
    /// assert_eq!(tree.into_vec(), vec![[0, 0], [1, 2], [3, 4]]);
    /// // Calling tree.peek() will now panic because tree has become empty
    /// ```
    pub fn into_vec(&mut self) -> Vec<T> {
        if self.is_empty() {
            panic!("Tried to call into_vec on an empty tree");
        }

        if !self.has_father() {
            // we are at root
            let mut container = Vec::new();
            _into_vec_rec(self.root.unwrap(), &mut container);
            // Clean pointer to avoid so that the tree drop won't cause double free
            self.root = None;
            self.current = None;
            container
        } else {
            // we are not a root, so we ascend and we split the branch that is to be turned into a
            // vec
            let mut container = Vec::new();
            let old_current = self.current.unwrap();
            self.ascend();
            unsafe {
                for (idx, child) in (*self.current.unwrap().as_ptr()).childs.iter().enumerate() {
                    if *child == old_current {
                        let mut old_tree = self.split(idx);
                        _into_vec_rec(old_current, &mut container);
                        // Clean pointer to avoid so that the tree drop won't cause double free
                        old_tree.root = None;
                        old_tree.current = None;
                        break;
                    }
                }
            }
            container
        }
    }

    /// Iterate over references of element stored in the subtree rooted at 'current' in a
    /// depth-first way. This is done
    /// by creating a Vec and pushing every references into this Vec and then returning an iterator
    /// over this Vec. As it may not be very memory efficient, you might check [Tree::lazyiter].
    /// Also note that this method will not panic if called on an empty tree.
    ///
    /// Because the behaviour of iter is not very explicit, [Tree] does not implement the Iterator
    /// trait.
    ///
    /// If you need to iter over the whole tree but without navigating 'current', you can use a
    /// [Cursor] and send him to root and then call [Cursor::iter].
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// tree.navigate_to(1);
    /// tree.push(4);
    /// tree.ascend();
    /// assert_eq!(tree.iter().collect::<Vec<&i32>>(), vec![&0, &1, &2, &4, &3]);
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        if self.is_empty() {
            return Vec::new().into_iter();
        }
        let mut container = Vec::new();
        _iter_rec(self.current.unwrap(), &mut container);
        container.into_iter()
    }

    /// Iterate over mutable references of element stored in the subtree rooted at 'current' in a
    /// depth-first way. This is done
    /// by creating a Vec and pushing every references into this Vec and then returning an iterator
    /// over this Vec. As it may not be very memory efficient, you might check [Tree::lazyiter].
    /// Also note that this method will not panic if called on an empty tree.
    ///
    /// If you need to iter over the whole tree but without navigating 'current', you can use a
    /// [CursorMut] and send him to root and then call [CursorMut::iter].
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// tree.navigate_to(1);
    /// tree.push(4);
    /// tree.ascend();
    /// assert_eq!(tree.iter_mut().collect::<Vec<&mut i32>>(), vec![&mut 0, &mut 1, &mut 2, &mut 4, &mut 3]);
    /// ```
    pub fn iter_mut(&self) -> impl Iterator<Item = &mut T> {
        if self.is_empty() {
            return Vec::new().into_iter();
        }
        let mut container = Vec::new();
        _iter_rec_mut(self.current.unwrap(), &mut container);
        container.into_iter()
    }

    /// Iterate over the subtree rooted at 'current' in a lazy depth-first way, returning
    /// references to the elements stored in the subtree. Although it is lazy iteration, meaning it is
    /// less stressfull for memory, it is slower than [Tree::iter], because the cursor that is used
    /// to move around the tree has to keep tracks of which branches it has already explored.
    ///
    /// # Examples
    /// ```
    /// # use libtree::Tree;
    /// let mut tree = Tree::from_element(0);
    /// tree.push_iter(vec![1, 2, 3]);
    /// tree.navigate_to(1);
    /// tree.push_iter(vec![9, 8]);
    /// tree.ascend();
    /// tree.navigate_to(0);
    /// tree.push_iter(vec![9, 10]);
    /// tree.navigate_to(0);
    /// tree.push(15);
    /// tree.go_to_root();
    /// assert_eq!(
    ///     tree.lazyiter().collect::<Vec<&i32>>(),
    ///     vec![&0, &1, &9, &15, &10, &2, &9, &8, &3]
    /// );
    /// tree.navigate_to(1);
    /// assert_eq!(tree.lazyiter().collect::<Vec<&i32>>(), vec![&2, &9, &8]);
    /// ```
    pub fn lazyiter(&self) -> LazyTreeIterator<'_, T> {
        if self.is_empty() {
            panic!("Tried to call lazyiter on an empty tree");
        }
        let mut idx_vec = LinkedList::new();
        idx_vec.push_back(0);
        LazyTreeIterator {
            cursor: self.cursor(),
            idx_list: idx_vec,
            _boo: PhantomData,
        }
    }

    pub fn lazyiter_mut(&mut self) -> LazyTreeIteratorMut<'_, T> {
        if self.is_empty() {
            panic!("Tried to call lazyiter_mut on an empty tree");
        }

        let mut idx_vec = LinkedList::new();
        idx_vec.push_back(0);
        LazyTreeIteratorMut {
            cursor: self.unsafe_cursor(),
            idx_list: idx_vec,
            _boo: PhantomData,
        }
    }
}

pub struct ChildIterator<'a, T> {
    pub(crate) current: ChildLink<T>,
    pub(crate) i: usize,
    pub(crate) _boo: PhantomData<&'a T>,
    pub(crate) len: usize,
}

impl<'a, T> Iterator for ChildIterator<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        if self.i < self.len {
            let item = unsafe { &(*(*self.current.as_ptr()).childs[self.i].as_ptr()).elem };
            self.i += 1;
            Some(item)
        } else {
            None
        }
    }
}

pub struct ChildIteratorMut<'a, T> {
    pub(crate) current: ChildLink<T>,
    pub(crate) i: usize,
    pub(crate) _boo: PhantomData<&'a T>,
    pub(crate) len: usize,
}

impl<'a, T> Iterator for ChildIteratorMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if self.i < self.len {
            let item = unsafe { &mut (*(*self.current.as_ptr()).childs[self.i].as_ptr()).elem };
            self.i += 1;
            Some(item)
        } else {
            None
        }
    }
}

pub struct LazyTreeIterator<'a, T> {
    pub(crate) cursor: Cursor<'a, T>,
    pub(crate) idx_list: LinkedList<usize>,
    pub(crate) _boo: PhantomData<&'a T>,
}

impl<'a, T> Iterator for LazyTreeIterator<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx_list.is_empty() {
            return None;
        }

        let res;
        if self.cursor.childs_len() == 0 {
            res = Some(self.cursor.peek());
            self.cursor.ascend();
            self.idx_list.pop_back();
        } else if *self.idx_list.back().unwrap() < self.cursor.childs_len() {
            if *self.idx_list.back().unwrap() == 0 {
                res = Some(self.cursor.peek());
                self.cursor.navigate_to(0);
                *self.idx_list.back_mut().unwrap() += 1;
                self.idx_list.push_back(0);
            } else {
                self.cursor.navigate_to(*self.idx_list.back().unwrap());
                *self.idx_list.back_mut().unwrap() += 1;
                self.idx_list.push_back(0);
                res = self.next();
            }
        } else {
            self.idx_list.pop_back();
            if self.cursor.has_father() {
                self.cursor.ascend();
            }
            res = self.next();
        }

        res
    }
}

pub struct LazyTreeIteratorMut<'a, T> {
    pub(crate) cursor: UnsafeCursor<'a, T>,
    pub(crate) idx_list: LinkedList<usize>,
    pub(crate) _boo: PhantomData<&'a T>,
}

impl<'a, T> Iterator for LazyTreeIteratorMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx_list.is_empty() {
            return None;
        }

        let res;
        if self.cursor.childs_len() == 0 {
            unsafe {
                res = Some(self.cursor.peek_mut());
            }
            self.cursor.ascend();
            self.idx_list.pop_back();
        } else if *self.idx_list.back().unwrap() < self.cursor.childs_len() {
            if *self.idx_list.back().unwrap() == 0 {
                unsafe {
                    res = Some(self.cursor.peek_mut());
                }
                self.cursor.navigate_to(0);
                *self.idx_list.back_mut().unwrap() += 1;
                self.idx_list.push_back(0);
            } else {
                self.cursor.navigate_to(*self.idx_list.back().unwrap());
                *self.idx_list.back_mut().unwrap() += 1;
                self.idx_list.push_back(0);
                res = self.next();
            }
        } else {
            self.idx_list.pop_back();
            if self.cursor.has_father() {
                self.cursor.ascend();
            }
            res = self.next();
        }

        res
    }
}
/// Recursive function to gather reference of the subtree into container.
pub fn _iter_rec<T>(link: ChildLink<T>, container: &mut Vec<&T>) {
    unsafe {
        container.push(&(*link.as_ptr()).elem);
        for child in (*link.as_ptr()).childs.iter() {
            _iter_rec(*child, container);
        }
    }
}

/// Recursive function to gather mutable reference of the subtree into container.
pub fn _iter_rec_mut<T>(link: ChildLink<T>, container: &mut Vec<&mut T>) {
    unsafe {
        container.push(&mut (*link.as_ptr()).elem);
        for child in (*link.as_ptr()).childs.iter() {
            _iter_rec_mut(*child, container);
        }
    }
}

/// Reursive function to turn a subtree into a vec.
fn _into_vec_rec<T>(link_node: ChildLink<T>, container: &mut Vec<T>) {
    unsafe {
        let boxed_node = Box::from_raw(link_node.as_ptr());
        let (el, childs) = (boxed_node.elem, boxed_node.childs);
        container.push(el);

        for child in childs {
            _into_vec_rec(child, container);
        }
    }
}

/// Recursive function to clone the tree under cursor.
fn _clone_rec<T>(
    cursor: &mut Cursor<'_, T>,
    new_tree: &mut Tree<T>,
    tree: &Tree<T>,
) -> Option<NonNull<Node<T>>>
where
    T: Clone,
{
    let mut res = None;
    if cursor.current == tree.current.unwrap() {
        res = new_tree.current;
    }

    if cursor.childs_len() > 0 {
        for i in 0..cursor.childs_len() {
            new_tree.push(cursor.peek_child(i).clone());
            new_tree.navigate_to(i);
            cursor.navigate_to(i);
            let ret = _clone_rec(cursor, new_tree, tree);
            if ret.is_some() {
                res = ret;
            }
            new_tree.ascend();
            cursor.ascend();
        }
    }

    res
}

impl<T> Default for Tree<T> {
    fn default() -> Self {
        Tree {
            current: None,
            root: None,
            _boo: PhantomData,
        }
    }
}

impl<T> Clone for Tree<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        if self.is_empty() {
            panic!("Tried to call clone on an empty tree");
        }

        let mut cursor = self.cursor_root();
        let mut new_tree = Self::from_element(cursor.peek().clone());
        let new_current = _clone_rec(&mut cursor, &mut new_tree, self);
        new_tree.current = new_current;
        new_tree
    }
}

impl<T> Into<Vec<T>> for Tree<T> {
    fn into(mut self) -> Vec<T> {
        self.go_to_root();
        self.into_vec()
    }
}

impl<T> Drop for Tree<T> {
    fn drop(&mut self) {
        if self.root.is_some() {
            self.go_to_root();

            for _ in 0..self.childs_len() {
                self.split(0);
            }
            unsafe {
                let _ = Box::from_raw(self.current.unwrap().as_ptr());
            }
        }
    }
}

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

    #[test]
    fn peek() {
        let mut tree = Tree::from_element(4);
        assert_eq!(tree.peek(), &4);
        assert_eq!(tree.peek_mut(), &mut 4);
    }

    #[test]
    fn move_to_no_panic() {
        let mut tree = Tree::from_element(3);
        tree.push(5);
        tree.push(-1);
        tree.navigate_to(1);
        assert_eq!(tree.peek(), &-1);
    }

    #[test]
    #[should_panic(
        expected = "Tried to move to children 1 of current node, but current node has only 1 childs"
    )]
    fn move_to_panic() {
        let mut tree = Tree::from_element(3);
        tree.push(3);
        tree.navigate_to(1);
    }

    #[test]
    fn move_up() {
        let mut tree = Tree::from_element(20);
        tree.push(1);
        tree.navigate_to(0);
        tree.ascend();
        assert_eq!(tree.peek(), &20);
    }

    #[test]
    fn move_to_root() {
        let mut tree = Tree::from_element(10);
        tree.push(1);
        tree.navigate_to(0);
        tree.push(15);
        tree.navigate_to(0);
        tree.go_to_root();
        assert_eq!(tree.peek(), &10);
    }

    #[test]
    fn push() {
        let mut tree = Tree::from_element(10);
        tree.push(2);
        tree.push(3);
        tree.push(4);
        let vec: Vec<&i32> = tree.iter_childs().collect();
        assert_eq!(vec, vec![&2, &3, &4]);
    }

    #[test]
    fn insert() {
        let mut tree = Tree::from_element(10);
        tree.push(1);
        tree.push(3);
        tree.insert(1, 2);
        assert_eq!(tree.iter_childs().collect::<Vec<&i32>>(), vec![&1, &2, &3]);
    }

    #[test]
    fn iter_mutchilds() {
        let mut tree = Tree::from_element(10);
        tree.push_iter(vec![1, 2, 3]);
        assert_eq!(
            tree.iter_childs_mut().collect::<Vec<&mut i32>>(),
            vec![&mut 1, &mut 2, &mut 3]
        );
    }

    #[test]
    fn join() {
        let mut tree1 = Tree::from_element(0);
        let mut tree2 = Tree::from_element(1);
        tree2.push_iter(vec![2, 3]);
        tree1.join(tree2, 0);
        tree1.navigate_to(0);
        assert_eq!(tree1.peek(), &1);
        assert_eq!(tree1.iter_childs().collect::<Vec<&i32>>(), vec![&2, &3]);
    }

    #[test]
    fn split() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2]);
        tree.navigate_to(1);
        tree.push_iter(vec![3, 4]);
        tree.ascend();
        let split_tree = tree.split(1);
        assert!(!split_tree.has_father());
        assert_eq!(split_tree.peek(), &2);
        assert_eq!(
            split_tree.iter_childs().collect::<Vec<&i32>>(),
            vec![&3, &4]
        );

        std::mem::drop(tree);
        assert_eq!(split_tree.peek(), &2);
    }

    #[test]
    #[should_panic(expected = "Tried to call split with index 3 but current has only 0 childs")]
    fn split_panic() {
        let mut tree = Tree::from_element(0);
        tree.split(3);
    }

    #[test]
    #[should_panic(
        expected = "Tried to move to children 1 of current node, but current node has only 1 childs"
    )]
    fn split_no_dangling_pointer() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2]);
        let _ = tree.split(1);
        tree.navigate_to(1);
    }

    #[test]
    fn clone() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2, 3]);
        tree.navigate_to(0);
        tree.push_iter(vec![4, 5]);
        tree.ascend();
        tree.navigate_to(1);
        tree.push_iter(vec![6, 7, 8]);
        tree.ascend();
        tree.navigate_to(2);
        tree.push(9);
        tree.navigate_to(0);
        tree.push_iter(vec![1, 2, 3]);

        let mut clone = tree.clone();
        std::mem::drop(tree);

        assert_eq!(clone.peek(), &9);
        assert_eq!(clone.iter_childs().collect::<Vec<&i32>>(), vec![&1, &2, &3]);
        clone.ascend();
        clone.ascend();
        clone.navigate_to(1);
        assert_eq!(clone.iter_childs().collect::<Vec<&i32>>(), vec![&6, &7, &8]);
        clone.go_to_root();
        assert_eq!(clone.peek(), &0);
        assert_eq!(clone.iter_childs().collect::<Vec<&i32>>(), vec![&1, &2, &3]);
    }

    #[test]
    fn memory_leak() {
        let mut tree = Tree::from_element(vec![1, 2, 3]);
        tree.push(vec![4, 5, 6]);
    }

    #[test]
    fn into_vec() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2, 3]);
        tree.navigate_to(1);
        tree.push_iter(vec![9, 8]);
        tree.ascend();
        tree.navigate_to(0);
        tree.push_iter(vec![9, 10]);
        tree.navigate_to(0);
        tree.push(15);
        tree.go_to_root();
        tree.navigate_to(0);
        let vec = tree.into_vec();
        assert_eq!(vec, vec![1, 9, 15, 10]);
        assert_eq!(tree.peek(), &0);
        tree.navigate_to(0);
        assert_eq!(tree.peek(), &2);
        tree.navigate_to(1);
        assert_eq!(tree.peek(), &8)
    }

    #[test]
    #[should_panic(expected = "Tried to peek on an empty tree")]
    fn into_vec_root() {
        let mut tree = Tree::from_element([0, 0]);
        tree.push_iter([[1, 2], [3, 4]]);
        assert_eq!(tree.into_vec(), vec![[0, 0], [1, 2], [3, 4]]);
        tree.peek();
    }

    #[test]
    fn lazyiter() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2, 3]);
        tree.navigate_to(1);
        tree.push_iter(vec![9, 8]);
        tree.ascend();
        tree.navigate_to(0);
        tree.push_iter(vec![9, 10]);
        tree.navigate_to(0);
        tree.push(15);
        tree.go_to_root();
        assert_eq!(
            tree.lazyiter().collect::<Vec<&i32>>(),
            vec![&0, &1, &9, &15, &10, &2, &9, &8, &3]
        );
        tree.navigate_to(1);
        assert_eq!(tree.lazyiter().collect::<Vec<&i32>>(), vec![&2, &9, &8]);
    }

    #[test]
    fn lazyiter_mut() {
        let mut tree = Tree::from_element(0);
        tree.push_iter(vec![1, 2, 3]);
        tree.navigate_to(1);
        tree.push_iter(vec![9, 8]);
        tree.ascend();
        tree.navigate_to(0);
        tree.push_iter(vec![9, 10]);
        tree.navigate_to(0);
        tree.push(15);
        tree.go_to_root();
        assert_eq!(
            tree.lazyiter_mut().collect::<Vec<&mut i32>>(),
            vec![&mut 0, &mut 1, &mut 9, &mut 15, &mut 10, &mut 2, &mut 9, &mut 8, &mut 3]
        );

        for el in tree.lazyiter_mut() {
            *el += 1
        }
        assert_eq!(
            tree.lazyiter().collect::<Vec<&i32>>(),
            vec![&1, &2, &10, &16, &11, &3, &10, &9, &4]
        );

        tree.navigate_to(1);
        for el in tree.lazyiter_mut() {
            *el += 10;
        }
        tree.go_to_root();

        assert_eq!(tree.into_vec(), [1, 2, 10, 16, 11, 13, 20, 19, 4]);
    }
}