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
use super::{Node,Size,NullIndex,TREE,FOREST,NULL,tree_null,forest_null};
use indexed::Pool;
use rust::*;
pub struct Pot<T> {
pub(crate) nodes: NonNull<Pool<Node<T>>>,
}
impl<T> Copy for Pot<T> {}
impl<T> Clone for Pot<T> { fn clone( &self ) -> Self { Pot{ nodes: self.nodes }}}
impl<T> Deref for Pot<T> {
type Target = Pool<Node<T>>;
fn deref( &self ) -> &Self::Target { unsafe{ self.nodes.as_ref() }}
}
impl<T> DerefMut for Pot<T> {
fn deref_mut( &mut self ) -> &mut Self::Target { unsafe{ self.nodes.as_mut() }}
}
impl<T> Pot<T> {
#[inline] pub(crate) fn new_tree() -> Self {
let mut pool = Pool::new_unmanaged();
pool.push( tree_null() );
Pot{ nodes: unsafe{ NonNull::new_unchecked( Box::into_raw( pool ))}}
}
#[inline] pub(crate) fn new_forest() -> Self {
let mut pool = Pool::new_unmanaged();
pool.push( forest_null() );
Pot{ nodes: unsafe{ NonNull::new_unchecked( Box::into_raw( pool ))}}
}
#[inline] pub(crate) fn grow( &mut self, node_cnt: usize ) {
let len = self.len();
let cap = self.capacity();
if len + node_cnt > cap {
self.reserve( len + node_cnt - cap );
}
unsafe{ self.set_len( len + node_cnt ); }
}
#[inline] pub(crate) fn gather( &mut self, parent: usize, child: usize, data: T, size: Size ) {
let mut node = Node {
next : child as u32,
child : u32::null(),
prev : child as u32,
parent : parent as u32,
size ,
adjoined : size.degree,
index : child as u32,
data ,
};
if !parent.is_null() {
if !self[ parent ].child.is_null() {
node.set_prev( self.tail( parent ));
node.set_next( self.head( parent ));
self.adopt( parent, child, child );
}
self[ parent ].set_child( child );
}
if self.len() <= child {
self.grow( 1 );
}
unsafe{ self.write( child, node )}
}
#[inline] pub(crate) fn nth_child( &self, mut index: usize, mut nth: usize ) -> Option<usize> {
if nth < self.degree( index ) {
let mut adjoined = self.adjoined( index );
index = self.head( index );
if nth < adjoined {
return Some( index + nth );
} else {
index = self.next( index + adjoined-1 );
nth -= 1;
loop {
if self.is_forest( index ) {
adjoined = self.degree( index );
if nth < adjoined {
return Some( index + nth );
} else {
nth -= adjoined;
index = self.next( index );
}
} else {
if nth == 0 {
return Some( index );
} else {
nth -= 1;
index = self.next( index );
}
}
}
}
} else {
None
}
}
#[allow( dead_code )]
#[inline] pub(crate) fn prev( &self, index: usize ) -> usize { self[ index ].prev as usize }
#[inline] pub(crate) fn next( &self, index: usize ) -> usize { self[ index ].next as usize }
#[allow( dead_code )]
#[inline]
pub(crate) fn prev_sib( &self, index: usize ) -> usize {
let parent = self.parent( index );
if parent.is_null() || !self.is_forest( parent ) {
self.next( index )
} else {
if index == self.head( parent ) {
self.prev( parent )
} else {
self.prev( index )
}
}
}
#[allow( dead_code )]
#[inline]
pub(crate) fn next_sib( &self, index: usize ) -> usize {
let parent = self.parent( index );
if parent.is_null() || !self.is_forest( parent ) {
self.next( index )
} else {
if index == self.tail( parent ) {
self.next( parent )
} else {
self.next( index )
}
}
}
#[inline] pub(crate) fn is_forest_pot( &self ) -> bool { self.is_forest( NULL )}
#[inline] pub(crate) fn set_tree_pot( &mut self ) { self[ NULL ].adjoined = TREE; }
#[inline] pub(crate) fn set_forest_pot( &mut self ) { self[ NULL ].adjoined = FOREST; }
#[inline] pub(crate) fn adjoined( &self, index: usize )-> usize { self[ index ].adjoined as usize }
#[inline] pub(crate) fn degree( &self, index: usize ) -> usize { self[ index ].size.degree as usize }
#[inline] pub(crate) fn node_cnt( &self, index: usize ) -> usize { self[ index ].size.node_cnt as usize }
#[inline] pub(crate) fn is_leaf( &self, index: usize ) -> bool { self[ index ].child.is_null() }
#[inline] pub(crate) fn is_forest( &self, index: usize ) -> bool { self[ index ].is_forest() }
#[inline] pub(crate) fn parent( &self, index: usize ) -> usize { self[ index ].parent as usize }
#[inline] pub(crate) fn reset_sib( &mut self, index: usize ) { self[ index ].set_prev( index ); self[ index ].set_next( index ); }
#[inline] pub(crate) fn reset_parent( &mut self, index: usize ) { self[ index ].set_parent( usize::null() ); }
#[inline] pub(crate) fn tail( &self, index: usize ) -> usize { self[ index ].child() }
#[inline] pub(crate) fn head( &self, index: usize ) -> usize {
if self.tail( index ).is_null() {
usize::null()
} else {
self[ self.tail( index ) ].next()
}
}
#[inline] pub(crate) unsafe fn new_tail( &self, index: usize ) -> usize {
let tail = self.tail( index );
self[ tail ].prev as usize
}
#[inline] pub(crate) fn adopt( &mut self, parent: usize, begin: usize, end: usize ) {
let parent_head = self.head( parent );
self[ parent_head ].set_prev( begin );
let parent_tail = self.tail( parent );
self[ parent_tail ].set_next( end );
}
#[inline] pub(crate) unsafe fn drop( this: Self ) { let _ = Box::from_raw( this.nodes.as_ptr() ); }
}
impl<T:Debug> Debug for Pot<T> {
fn fmt( &self, f: &mut Formatter ) -> fmt::Result {
let n = if self.is_forest_pot() { 2 } else { 1 };
for node in self.iter().skip( n ) {
writeln!( f, "[{}] _{} <{} {}> ({},{}-{}) ^{} {:?}",
node.index,
node.child,
node.prev, node.next,
node.size.node_cnt, node.size.degree, node.adjoined,
node.parent,
node.data
)?;
}
write!( f, "" )
}
}