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
use super::{Node,Link,Forest};
use super::{heap,Size};
use super::bfs::{BfsTree,Splitted,Split};
use super::forest::IntoIter;
use rust::*;
pub struct Tree<T> {
pub(crate) root : *mut Node<T>,
mark : heap::Phantom<T>,
}
impl<T> Tree<T> {
#[inline] pub fn new( data: T ) -> Self { Self::from( heap::make_node( data ) as *mut Link )}
#[inline] pub fn root( &self ) -> &Node<T> { unsafe { & *self.root }}
#[inline] pub fn root_mut( &mut self ) -> &mut Node<T> { unsafe { &mut *self.root }}
#[inline] fn into_data( self ) -> T {
let data = unsafe{ ptr::read( &self.root().data )};
self.clear();
data
}
#[inline] pub fn abandon( &mut self ) -> Forest<T> {
let forest = Forest::<T>::from( self.root().tail(), self.root().size );
self.root_mut().reset_child();
self.size.degree = 0;
self.size.node_cnt = 1;
forest
}
pub fn into_bfs( self ) -> BfsTree<Splitted<IntoIter<T>>> {
let size = Size{ degree: 1, node_cnt: self.root().link.size.node_cnt };
BfsTree::from( self, size )
}
#[inline] pub(crate) fn from( root: *mut Link ) -> Self { Tree{ root: root as *mut Node<T>, mark: PhantomData }}
#[inline] pub(crate) fn clear( mut self ) { self.root = null_mut(); }
}
impl<T> Split for Tree<T> {
type Item = T;
type Iter = IntoIter<T>;
fn split( mut self ) -> ( T, IntoIter<T>, u32 ) {
let node_cnt = self.root().link.size.node_cnt;
let iter = self.abandon().into_iter();
( self.into_data(), iter, node_cnt )
}
}
impl<T> IntoIterator for Tree<T> {
type Item = Tree<T>;
type IntoIter = IntoIter<T>;
#[inline] fn into_iter( self ) -> IntoIter<T> {
let mut forest = Forest::<T>::new();
forest.push_back( self );
IntoIter{ forest, marker: PhantomData }
}
}
impl<T> Borrow<Node<T>> for Tree<T> { fn borrow( &self ) -> &Node<T> { self.root() }}
impl<T> BorrowMut<Node<T>> for Tree<T> { fn borrow_mut( &mut self ) -> &mut Node<T> { self.root_mut() }}
impl<T> Deref for Tree<T> {
type Target = Node<T>;
fn deref( &self ) -> &Node<T> { unsafe { &*self.root }}
}
impl<T> DerefMut for Tree<T> {
fn deref_mut( &mut self ) -> &mut Node<T> { unsafe { &mut *self.root }}
}
impl<T:Clone> Clone for Tree<T> { fn clone( &self ) -> Self { self.root().to_owned() }}
impl<T> Drop for Tree<T> {
fn drop( &mut self ) {
if !self.root.is_null() {
while let Some(_) = self.pop_front() {}
heap::drop_node( self.root );
}
}
}
impl<T:Debug> Debug for Tree<T> { fn fmt( &self, f: &mut Formatter ) -> fmt::Result { write!( f, "{:?}", self.root() )}}
impl<T:Display> Display for Tree<T> { fn fmt( &self, f: &mut Formatter ) -> fmt::Result { write!( f, "{}", self.root() )}}
impl<T:PartialEq> PartialEq for Tree<T> {
fn eq( &self, other: &Self ) -> bool { self.root().eq( other.root() )}
fn ne( &self, other: &Self ) -> bool { self.root().ne( other.root() )}
}
impl<T:Eq> Eq for Tree<T> {}
impl<T:PartialOrd> PartialOrd for Tree<T> { #[inline] fn partial_cmp( &self, other: &Self ) -> Option<Ordering> { self.root().partial_cmp( other.root() )}}
impl<T:Ord> Ord for Tree<T> { #[inline] fn cmp( &self, other: &Self ) -> Ordering { self.root().cmp( other.root() )}}
impl<T:Hash> Hash for Tree<T> { fn hash<H:Hasher>( &self, state: &mut H ) { self.root().hash( state )}}
unsafe impl<T:Send> Send for Tree<T> {}
unsafe impl<T:Sync> Sync for Tree<T> {}