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
use super::{Pot,Size};
use rust::*;
pub trait TreeData: Sized {}
macro_rules! primitive_impls {
($($name:ty),*) => { $(impl TreeData for $name {})* }
}
primitive_impls! {
bool, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, f32, f64
}
impl TreeData for &'static str {}
impl TreeData for String {}
pub unsafe trait TupleTree where Self: Sized {
type Data;
unsafe fn data( &self ) -> Self::Data;
fn descendants( &self, indirect_level: usize ) -> usize;
fn height( &self ) -> usize;
fn nodes( &self ) -> usize;
unsafe fn construct_node( &self, parent: usize, height: usize, offsets: &mut [usize], pot: Pot<Self::Data> );
fn construct_all_nodes( &self, parent: usize, mut pot: Pot<Self::Data> ) {
let height = self.height();
let mut offsets = Vec::with_capacity( height );
let pot_len = pot.len();
offsets.push( pot_len );
if height > 1 {
offsets.push( pot_len+1 );
for level in 2..height {
let offset = offsets[ level-1 ] + self.descendants( level-2 );
offsets.push( offset );
}
let growed = offsets[ height-1 ] + self.descendants( height-2 ) - pot_len;
pot.grow( growed );
} else {
pot.grow( 1 );
}
unsafe{ self.construct_node( parent, 0, offsets.as_mut_slice(), pot ); }
}
}
unsafe impl<T:TreeData> TupleTree for T {
type Data = T;
unsafe fn data( &self ) -> Self { ptr::read( self )}
fn descendants( &self, _indirect_level: usize ) -> usize { 0 }
fn height( &self ) -> usize { 1 }
fn nodes( &self ) -> usize { 1 }
unsafe fn construct_node( &self, parent: usize, height: usize, offsets: &mut [usize], mut pot: Pot<Self::Data> ) {
pot.gather( parent, offsets[ height ], self.data(), Size{ degree:0, node_cnt:1 });
offsets[ height ] += 1;
}
}
macro_rules! tuple_impls {
($($len:expr => ($($n:tt $name:ident)*))+) => {
$(
unsafe impl<T,$($name),*> TupleTree for (T,$($name,)*)
where T: TupleTree<Data=T> $(,$name: TupleTree<Data=T>)*
{
type Data = T;
unsafe fn data( &self ) -> T { self.0.data() }
fn descendants( &self, indirect_level: usize ) -> usize {
if indirect_level == 0 {
$len
} else {
0 $( + self.$n.descendants( indirect_level-1 ) )*
}
}
fn height( &self ) -> usize {
1 + *[ 0 $(, self.$n.height() )* ].iter().max_by( |x,y| x.cmp(y) ).unwrap()
}
fn nodes( &self ) -> usize {
[ 1 $(, self.$n.nodes() )* ].iter().sum()
}
#[allow(unused_variables)]
unsafe fn construct_node( &self, parent: usize, height: usize, offsets: &mut [usize], mut pot: Pot<Self::Data> ) {
pot.gather( parent, offsets[ height ], self.data(), Size{ degree: $len, node_cnt: self.nodes() as u32});
let pos = offsets[ height ];
offsets[ height ] += 1;
$( self.$n.construct_node( pos, height+1, offsets, pot ); )*
}
}
)+
}
}
tuple_impls! {
0 => ()
1 => (1 T1)
2 => (1 T1 2 T2)
3 => (1 T1 2 T2 3 T3)
4 => (1 T1 2 T2 3 T3 4 T4)
5 => (1 T1 2 T2 3 T3 4 T4 5 T5)
6 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
7 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
8 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
9 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
10 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
11 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
12 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
13 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
14 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
15 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
16 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16)
17 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17)
18 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18)
19 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19)
20 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20)
21 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21)
22 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22)
23 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23)
24 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24)
25 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25)
26 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26)
27 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27)
28 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28)
29 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29)
30 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29 30 T30)
31 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29 30 T30 31 T31)
}
pub struct FakeRoot<T>( pub PhantomData<T> );
pub fn fr<T>() -> FakeRoot<T> { FakeRoot( PhantomData )}
pub trait TupleForest where Self: Sized {
type Data;
fn descendants( &self, indirect_level: usize ) -> usize;
fn height( &self ) -> usize;
fn nodes( &self ) -> usize;
fn construct_all_nodes( &self, parent: usize, pot: Pot<Self::Data> );
}
macro_rules! tuple_fr_impls {
($($len:expr => ($($n:tt $name:ident)*))+) => {
$(
impl<T,$($name),*> TupleForest for (FakeRoot<T>,$($name,)*)
where T: TupleTree<Data=T> $(,$name: TupleTree<Data=T>)*
{
type Data = T;
fn descendants( &self, indirect_level: usize ) -> usize {
if indirect_level == 0 {
$len
} else {
0 $( + self.$n.descendants( indirect_level-1 ) )*
}
}
fn height( &self ) -> usize {
0 + *[ 0 $(, self.$n.height() )* ].iter().max_by( |x,y| x.cmp(y) ).unwrap()
}
fn nodes( &self ) -> usize {
[ 0 $(, self.$n.nodes() )* ].iter().sum()
}
#[allow(unused_variables)]
fn construct_all_nodes( &self, parent: usize, mut pot: Pot<Self::Data> ) {
let height = self.height()+1;
if height > 1 {
let mut offsets = Vec::with_capacity( height );
let pot_len = pot.len();
offsets.push( parent );
offsets.push( pot_len );
for level in 2..height {
let offset = offsets[ level-1 ] + self.descendants( level-2 );
offsets.push( offset );
}
let growed = offsets[ height-1 ] + self.descendants( height-2 ) - pot_len;
pot.grow( growed );
$(
unsafe{ self.$n.construct_node( offsets[0], 1, &mut offsets, pot ); }
)*
}
}
}
)+
}
}
tuple_fr_impls! {
0 => ()
1 => (1 T1)
2 => (1 T1 2 T2)
3 => (1 T1 2 T2 3 T3)
4 => (1 T1 2 T2 3 T3 4 T4)
5 => (1 T1 2 T2 3 T3 4 T4 5 T5)
6 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
7 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
8 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
9 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
10 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
11 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
12 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
13 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
14 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
15 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
16 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16)
17 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17)
18 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18)
19 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19)
20 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20)
21 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21)
22 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22)
23 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23)
24 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24)
25 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25)
26 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26)
27 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27)
28 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28)
29 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29)
30 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29 30 T30)
31 => (1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19 20 T20 21 T21 22 T22 23 T23 24 T24 25 T25 26 T26 27 T27 28 T28 29 T29 30 T30 31 T31)
}