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
use super::{Node,Link,Tree};
use rust::*;
pub struct Subnode<'a, T:'a>{
node : &'a mut Node<T>,
prev : *mut Link,
parent : *mut Link,
}
impl<'a, T:'a> Subnode<'a,T> {
#[inline] pub fn insert_before( &mut self, mut sib: Tree<T> ) {
unsafe {
sib.root_mut().set_sib( self.node.plink() );
(*self.prev).set_sib( sib.root_mut().plink() );
}
sib.clear();
}
#[inline] pub fn insert_after( &mut self, mut sib: Tree<T> ) {
unsafe {
(*sib.root_mut()).set_sib( self.node.next );
self.node.set_sib( sib.root_mut().plink() );
if (*self.parent).tail() == self.node.plink() {
(*self.parent).set_child( sib.root_mut().plink() );
}
}
sib.clear();
}
#[inline] pub fn depart( self ) -> Tree<T> {
unsafe {
if (*self.parent).tail() == self.node.plink() {
(*self.parent).set_child( if self.node.has_no_sib() { null_mut() } else { self.prev });
}
(*self.prev).set_sib( self.node.next );
self.node.reset_sib();
Tree::from( self.node.plink() )
}
}
}
impl<'a, T:'a> Deref for Subnode<'a,T> {
type Target = Node<T>;
fn deref( &self ) -> &Node<T> { self.node }
}
impl<'a, T:'a> DerefMut for Subnode<'a,T> { fn deref_mut( &mut self ) -> &mut Node<T> { self.node }}
pub struct OntoIter<'a, T:'a>{
pub(crate) next : *mut Link,
pub(crate) curr : *mut Link,
pub(crate) prev : *mut Link,
pub(crate) child : *mut Link,
pub(crate) parent : *mut Link,
pub(crate) mark : PhantomData<&'a mut Node<T>>,
}
impl<'a, T:'a> Iterator for OntoIter<'a,T> {
type Item = Subnode<'a,T>;
#[inline] fn next( &mut self ) -> Option<Subnode<'a,T>> {
if !self.child.is_null() {
if !self.curr.is_null() {
if self.curr == self.child || self.curr == self.next {
return None;
}
unsafe {
if (*self.prev).next != self.next {
self.prev = self.curr;
}
}
}
self.curr = self.next;
if !self.next.is_null() {
let curr = self.next;
unsafe {
self.next = (*curr).next;
return Some( Subnode{ node: &mut *( curr as *mut Node<T> ), prev: self.prev, parent: self.parent });
}
}
}
None
}
}