24
24
/// assert_eq!(flat_tree::index(0, 1), 2);
25
25
/// assert_eq!(flat_tree::index(0, 2), 4);
26
26
/// ```
27
- pub fn index ( depth : u64 , offset : u64 ) -> u64 {
27
+ pub fn index ( depth : usize , offset : usize ) -> usize {
28
28
( offset << depth + 1 ) | ( ( 1 << depth) - 1 )
29
29
}
30
30
@@ -38,7 +38,7 @@ pub fn index(depth: u64, offset: u64) -> u64 {
38
38
/// assert_eq!(flat_tree::depth(3), 2);
39
39
/// assert_eq!(flat_tree::depth(4), 0);
40
40
/// ```
41
- pub fn depth ( i : u64 ) -> u64 {
41
+ pub fn depth ( i : usize ) -> usize {
42
42
let mut depth = 0 ;
43
43
let mut i = i;
44
44
while is_odd ( i) {
@@ -49,7 +49,7 @@ pub fn depth(i: u64) -> u64 {
49
49
}
50
50
51
51
/// Returns the offset of a node with a depth.
52
- pub fn offset_with_depth ( i : u64 , depth : u64 ) -> u64 {
52
+ pub fn offset_with_depth ( i : usize , depth : usize ) -> usize {
53
53
if is_even ( i) {
54
54
i / 2
55
55
} else {
@@ -67,12 +67,12 @@ pub fn offset_with_depth(i: u64, depth: u64) -> u64 {
67
67
/// assert_eq!(flat_tree::offset(3), 0);
68
68
/// assert_eq!(flat_tree::offset(4), 2);
69
69
/// ```
70
- pub fn offset ( i : u64 ) -> u64 {
70
+ pub fn offset ( i : usize ) -> usize {
71
71
offset_with_depth ( i, depth ( i) )
72
72
}
73
73
74
74
/// Returns the parent of a node with a depth.
75
- pub fn parent_with_depth ( i : u64 , depth : u64 ) -> u64 {
75
+ pub fn parent_with_depth ( i : usize , depth : usize ) -> usize {
76
76
index ( depth + 1 , offset_with_depth ( i, depth) >> 1 )
77
77
}
78
78
@@ -88,12 +88,12 @@ pub fn parent_with_depth(i: u64, depth: u64) -> u64 {
88
88
/// assert_eq!(flat_tree::parent(2), 1);
89
89
/// assert_eq!(flat_tree::parent(1), 3);
90
90
/// ```
91
- pub fn parent ( i : u64 ) -> u64 {
91
+ pub fn parent ( i : usize ) -> usize {
92
92
parent_with_depth ( i, depth ( i) )
93
93
}
94
94
95
95
/// Returns the sibling of a node with a depth.
96
- pub fn sibling_with_depth ( i : u64 , depth : u64 ) -> u64 {
96
+ pub fn sibling_with_depth ( i : usize , depth : usize ) -> usize {
97
97
index ( depth, offset ( i) ^ 1 )
98
98
}
99
99
@@ -106,29 +106,32 @@ pub fn sibling_with_depth(i: u64, depth: u64) -> u64 {
106
106
/// assert_eq!(flat_tree::sibling(1), 5);
107
107
/// assert_eq!(flat_tree::sibling(5), 1);
108
108
/// ```
109
- pub fn sibling ( i : u64 ) -> u64 {
109
+ pub fn sibling ( i : usize ) -> usize {
110
110
sibling_with_depth ( i, depth ( i) )
111
111
}
112
112
113
113
/// Returns the parent's sibling, of a node, with a depth.
114
- pub fn uncle_with_depth ( i : u64 , depth : u64 ) -> u64 {
114
+ pub fn uncle_with_depth ( i : usize , depth : usize ) -> usize {
115
115
sibling_with_depth ( parent_with_depth ( i, depth) , depth + 1 )
116
116
}
117
117
118
118
/// Returns the parent's sibling, of a node.
119
- pub fn uncle ( i : u64 ) -> u64 {
119
+ pub fn uncle ( i : usize ) -> usize {
120
120
uncle_with_depth ( i, depth ( i) )
121
121
}
122
122
123
123
/// Returns both children of a node, with a depth.
124
- pub fn children_with_depth ( i : u64 , depth : u64 ) -> Option < ( u64 , u64 ) > {
124
+ pub fn children_with_depth ( i : usize , depth : usize ) -> Option < ( usize , usize ) > {
125
125
if is_even ( i) {
126
126
None
127
127
} else if depth == 0 {
128
128
Some ( ( i, i) )
129
129
} else {
130
130
let offset = offset_with_depth ( i, depth) * 2 ;
131
- Some ( ( index ( depth - 1 , offset) , index ( depth - 1 , offset + 1 ) ) )
131
+ Some ( (
132
+ index ( depth - 1 , offset) ,
133
+ index ( depth - 1 , offset + 1 ) ,
134
+ ) )
132
135
}
133
136
}
134
137
@@ -141,19 +144,22 @@ pub fn children_with_depth(i: u64, depth: u64) -> Option<(u64, u64)> {
141
144
/// assert_eq!(flat_tree::children(3), Some((1, 5)));
142
145
/// assert_eq!(flat_tree::children(9), Some((8, 10)));
143
146
/// ```
144
- pub fn children ( i : u64 ) -> Option < ( u64 , u64 ) > {
147
+ pub fn children ( i : usize ) -> Option < ( usize , usize ) > {
145
148
children_with_depth ( i, depth ( i) )
146
149
}
147
150
148
151
/// Returns only the left child of a node, with a depth
149
152
// TODO: handle errors
150
- pub fn left_child_with_depth ( i : u64 , depth : u64 ) -> Option < u64 > {
153
+ pub fn left_child_with_depth ( i : usize , depth : usize ) -> Option < usize > {
151
154
if is_even ( i) {
152
155
None
153
156
} else if depth == 0 {
154
157
Some ( i)
155
158
} else {
156
- Some ( index ( depth - 1 , offset_with_depth ( i, depth) << 1 ) )
159
+ Some ( index (
160
+ depth - 1 ,
161
+ offset_with_depth ( i, depth) << 1 ,
162
+ ) )
157
163
}
158
164
}
159
165
@@ -165,18 +171,21 @@ pub fn left_child_with_depth(i: u64, depth: u64) -> Option<u64> {
165
171
/// assert_eq!(flat_tree::left_child(1), Some(0));
166
172
/// assert_eq!(flat_tree::left_child(3), Some(1));
167
173
/// ```
168
- pub fn left_child ( i : u64 ) -> Option < u64 > {
174
+ pub fn left_child ( i : usize ) -> Option < usize > {
169
175
left_child_with_depth ( i, depth ( i) )
170
176
}
171
177
172
178
/// Returns only the left child of a node, with a depth.
173
- pub fn right_child_with_depth ( i : u64 , depth : u64 ) -> Option < u64 > {
179
+ pub fn right_child_with_depth ( i : usize , depth : usize ) -> Option < usize > {
174
180
if is_even ( i) {
175
181
None
176
182
} else if depth == 0 {
177
183
Some ( i)
178
184
} else {
179
- Some ( index ( depth - 1 , ( offset_with_depth ( i, depth) << 1 ) + 1 ) )
185
+ Some ( index (
186
+ depth - 1 ,
187
+ ( offset_with_depth ( i, depth) << 1 ) + 1 ,
188
+ ) )
180
189
}
181
190
}
182
191
@@ -189,12 +198,12 @@ pub fn right_child_with_depth(i: u64, depth: u64) -> Option<u64> {
189
198
/// assert_eq!(flat_tree::right_child(3), Some(5));
190
199
/// ```
191
200
// TODO: handle errors
192
- pub fn right_child ( i : u64 ) -> Option < u64 > {
201
+ pub fn right_child ( i : usize ) -> Option < usize > {
193
202
right_child_with_depth ( i, depth ( i) )
194
203
}
195
204
196
205
/// Returns the right most node in the tree that the node spans, with a depth.
197
- pub fn right_span_with_depth ( i : u64 , depth : u64 ) -> u64 {
206
+ pub fn right_span_with_depth ( i : usize , depth : usize ) -> usize {
198
207
if depth == 0 {
199
208
i
200
209
} else {
@@ -212,12 +221,12 @@ pub fn right_span_with_depth(i: u64, depth: u64) -> u64 {
212
221
/// assert_eq!(flat_tree::right_span(23), 30);
213
222
/// assert_eq!(flat_tree::right_span(27), 30);
214
223
/// ```
215
- pub fn right_span ( i : u64 ) -> u64 {
224
+ pub fn right_span ( i : usize ) -> usize {
216
225
right_span_with_depth ( i, depth ( i) )
217
226
}
218
227
219
228
/// Returns the left most node in the tree that the node spans, with a depth.
220
- pub fn left_span_with_depth ( i : u64 , depth : u64 ) -> u64 {
229
+ pub fn left_span_with_depth ( i : usize , depth : usize ) -> usize {
221
230
if depth == 0 {
222
231
i
223
232
} else {
@@ -235,13 +244,13 @@ pub fn left_span_with_depth(i: u64, depth: u64) -> u64 {
235
244
/// assert_eq!(flat_tree::left_span(23), 16);
236
245
/// assert_eq!(flat_tree::left_span(27), 24);
237
246
/// ```
238
- pub fn left_span ( i : u64 ) -> u64 {
247
+ pub fn left_span ( i : usize ) -> usize {
239
248
left_span_with_depth ( i, depth ( i) )
240
249
}
241
250
242
251
/// Returns the left and right most nodes in the tree that the node spans, with
243
252
/// a depth.
244
- pub fn spans_with_depth ( i : u64 , depth : u64 ) -> ( u64 , u64 ) {
253
+ pub fn spans_with_depth ( i : usize , depth : usize ) -> ( usize , usize ) {
245
254
(
246
255
left_span_with_depth ( i, depth) ,
247
256
right_span_with_depth ( i, depth) ,
@@ -258,12 +267,12 @@ pub fn spans_with_depth(i: u64, depth: u64) -> (u64, u64) {
258
267
/// assert_eq!(flat_tree::spans(23), (16, 30));
259
268
/// assert_eq!(flat_tree::spans(27), (24, 30));
260
269
/// ```
261
- pub fn spans ( i : u64 ) -> ( u64 , u64 ) {
270
+ pub fn spans ( i : usize ) -> ( usize , usize ) {
262
271
spans_with_depth ( i, depth ( i) )
263
272
}
264
273
265
274
/// Returns how many nodes are in the tree that the node spans, with a depth.
266
- pub fn count_with_depth ( _: u64 , depth : u64 ) -> u64 {
275
+ pub fn count_with_depth ( _: usize , depth : usize ) -> usize {
267
276
( 2 << depth) - 1
268
277
}
269
278
@@ -278,7 +287,7 @@ pub fn count_with_depth(_: u64, depth: u64) -> u64 {
278
287
/// assert_eq!(flat_tree::count(23), 15);
279
288
/// assert_eq!(flat_tree::count(27), 7);
280
289
/// ```
281
- pub fn count ( i : u64 ) -> u64 {
290
+ pub fn count ( i : usize ) -> usize {
282
291
count_with_depth ( i, depth ( i) )
283
292
}
284
293
@@ -293,7 +302,7 @@ pub fn count(i: u64) -> u64 {
293
302
/// assert_eq!(flat_tree::full_roots(18), [7, 16]);
294
303
/// assert_eq!(flat_tree::full_roots(16), [7]);
295
304
/// ```
296
- pub fn full_roots ( i : u64 ) -> Vec < u64 > {
305
+ pub fn full_roots ( i : usize ) -> Vec < usize > {
297
306
let mut result = Vec :: with_capacity ( 64 ) ;
298
307
299
308
if is_odd ( i) {
@@ -321,7 +330,7 @@ pub fn full_roots(i: u64) -> Vec<u64> {
321
330
}
322
331
323
332
#[ inline( always) ]
324
- fn is_even ( num : u64 ) -> bool {
333
+ fn is_even ( num : usize ) -> bool {
325
334
( num & 1 ) == 0
326
335
}
327
336
#[ test]
@@ -333,7 +342,7 @@ fn test_is_even() {
333
342
}
334
343
335
344
#[ inline( always) ]
336
- fn is_odd ( num : u64 ) -> bool {
345
+ fn is_odd ( num : usize ) -> bool {
337
346
( num & 1 ) != 0
338
347
}
339
348
#[ test]
0 commit comments