Skip to content

Commit fa7df19

Browse files
authored
usize (#11)
1 parent 6090cf9 commit fa7df19

File tree

2 files changed

+45
-91
lines changed

2 files changed

+45
-91
lines changed

README.md

+6-61
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,20 @@
33
[![crates.io version][1]][2] [![build status][3]][4]
44
[![downloads][5]][6] [![docs.rs docs][7]][8]
55

6-
> Rust version of [flat-tree](https://github.com/mafintosh/flat-tree). A series of functions to map a binary tree to a list.
6+
Rust version of [mafintosh/flat-tree](https://github.com/mafintosh/flat-tree).
7+
Map a binary tree to a list.
78

8-
## Usage
9+
- [Documentation][8]
10+
- [Crates.io][2]
911

10-
``` rs
12+
## Usage
13+
```rust
1114
extern crate flat_tree;
1215

1316
let parent = flat_tree::parent(0);
1417
println!("parent of 0 is {}", parent);
1518
```
1619

17-
## API
18-
19-
#### `index(depth: u64, offset: u64) -> u64`
20-
21-
Returns the flat-tree of the tree node at the specified depth and offset
22-
23-
#### `depth(node: u64) -> u64`
24-
25-
Returns the depth of a node
26-
27-
#### `offset(node: u64) -> u64`
28-
29-
Returns the offset of a node
30-
31-
#### `parent(node: u64) -> u64`
32-
33-
Returns the parent of a node
34-
35-
#### `sibling(node: u64) -> u64`
36-
37-
Returns the sibling of a node
38-
39-
#### `uncle(node: u64) -> u64`
40-
41-
Returns the parent's sibling of a node
42-
43-
#### `children(node: u64) -> (u64, u64)`
44-
45-
Returns both children of a node.
46-
47-
#### `left_child(node: u64) -> u64`
48-
49-
Returns only the left child of a node
50-
51-
#### `right_child(node: u64) -> u64`
52-
53-
Returns only the right child of a node
54-
55-
#### `right_span(node: u64) -> u64`
56-
57-
Returns the right most node in the tree that i spans
58-
59-
#### `left_span(node: u64) -> u64`
60-
61-
Returns the left most node in the tree that i spans
62-
63-
#### `spans(node: u64) -> (u64, u64)`
64-
65-
Returns the left and right most nodes in the tree that i spans
66-
67-
#### `count(node: u64) -> u64`
68-
69-
Returns how many nodes are in the tree that the node spans
70-
71-
#### `full_roots(node: u64) -> Vec<u64>`
72-
73-
Returns all the previous fully rooted trees before the node
74-
7520
## License
7621
[MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE)
7722

src/lib.rs

+39-30
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/// assert_eq!(flat_tree::index(0, 1), 2);
2525
/// assert_eq!(flat_tree::index(0, 2), 4);
2626
/// ```
27-
pub fn index(depth: u64, offset: u64) -> u64 {
27+
pub fn index(depth: usize, offset: usize) -> usize {
2828
(offset << depth + 1) | ((1 << depth) - 1)
2929
}
3030

@@ -38,7 +38,7 @@ pub fn index(depth: u64, offset: u64) -> u64 {
3838
/// assert_eq!(flat_tree::depth(3), 2);
3939
/// assert_eq!(flat_tree::depth(4), 0);
4040
/// ```
41-
pub fn depth(i: u64) -> u64 {
41+
pub fn depth(i: usize) -> usize {
4242
let mut depth = 0;
4343
let mut i = i;
4444
while is_odd(i) {
@@ -49,7 +49,7 @@ pub fn depth(i: u64) -> u64 {
4949
}
5050

5151
/// 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 {
5353
if is_even(i) {
5454
i / 2
5555
} else {
@@ -67,12 +67,12 @@ pub fn offset_with_depth(i: u64, depth: u64) -> u64 {
6767
/// assert_eq!(flat_tree::offset(3), 0);
6868
/// assert_eq!(flat_tree::offset(4), 2);
6969
/// ```
70-
pub fn offset(i: u64) -> u64 {
70+
pub fn offset(i: usize) -> usize {
7171
offset_with_depth(i, depth(i))
7272
}
7373

7474
/// 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 {
7676
index(depth + 1, offset_with_depth(i, depth) >> 1)
7777
}
7878

@@ -88,12 +88,12 @@ pub fn parent_with_depth(i: u64, depth: u64) -> u64 {
8888
/// assert_eq!(flat_tree::parent(2), 1);
8989
/// assert_eq!(flat_tree::parent(1), 3);
9090
/// ```
91-
pub fn parent(i: u64) -> u64 {
91+
pub fn parent(i: usize) -> usize {
9292
parent_with_depth(i, depth(i))
9393
}
9494

9595
/// 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 {
9797
index(depth, offset(i) ^ 1)
9898
}
9999

@@ -106,29 +106,32 @@ pub fn sibling_with_depth(i: u64, depth: u64) -> u64 {
106106
/// assert_eq!(flat_tree::sibling(1), 5);
107107
/// assert_eq!(flat_tree::sibling(5), 1);
108108
/// ```
109-
pub fn sibling(i: u64) -> u64 {
109+
pub fn sibling(i: usize) -> usize {
110110
sibling_with_depth(i, depth(i))
111111
}
112112

113113
/// 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 {
115115
sibling_with_depth(parent_with_depth(i, depth), depth + 1)
116116
}
117117

118118
/// Returns the parent's sibling, of a node.
119-
pub fn uncle(i: u64) -> u64 {
119+
pub fn uncle(i: usize) -> usize {
120120
uncle_with_depth(i, depth(i))
121121
}
122122

123123
/// 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)> {
125125
if is_even(i) {
126126
None
127127
} else if depth == 0 {
128128
Some((i, i))
129129
} else {
130130
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+
))
132135
}
133136
}
134137

@@ -141,19 +144,22 @@ pub fn children_with_depth(i: u64, depth: u64) -> Option<(u64, u64)> {
141144
/// assert_eq!(flat_tree::children(3), Some((1, 5)));
142145
/// assert_eq!(flat_tree::children(9), Some((8, 10)));
143146
/// ```
144-
pub fn children(i: u64) -> Option<(u64, u64)> {
147+
pub fn children(i: usize) -> Option<(usize, usize)> {
145148
children_with_depth(i, depth(i))
146149
}
147150

148151
/// Returns only the left child of a node, with a depth
149152
// 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> {
151154
if is_even(i) {
152155
None
153156
} else if depth == 0 {
154157
Some(i)
155158
} 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+
))
157163
}
158164
}
159165

@@ -165,18 +171,21 @@ pub fn left_child_with_depth(i: u64, depth: u64) -> Option<u64> {
165171
/// assert_eq!(flat_tree::left_child(1), Some(0));
166172
/// assert_eq!(flat_tree::left_child(3), Some(1));
167173
/// ```
168-
pub fn left_child(i: u64) -> Option<u64> {
174+
pub fn left_child(i: usize) -> Option<usize> {
169175
left_child_with_depth(i, depth(i))
170176
}
171177

172178
/// 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> {
174180
if is_even(i) {
175181
None
176182
} else if depth == 0 {
177183
Some(i)
178184
} 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+
))
180189
}
181190
}
182191

@@ -189,12 +198,12 @@ pub fn right_child_with_depth(i: u64, depth: u64) -> Option<u64> {
189198
/// assert_eq!(flat_tree::right_child(3), Some(5));
190199
/// ```
191200
// TODO: handle errors
192-
pub fn right_child(i: u64) -> Option<u64> {
201+
pub fn right_child(i: usize) -> Option<usize> {
193202
right_child_with_depth(i, depth(i))
194203
}
195204

196205
/// 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 {
198207
if depth == 0 {
199208
i
200209
} else {
@@ -212,12 +221,12 @@ pub fn right_span_with_depth(i: u64, depth: u64) -> u64 {
212221
/// assert_eq!(flat_tree::right_span(23), 30);
213222
/// assert_eq!(flat_tree::right_span(27), 30);
214223
/// ```
215-
pub fn right_span(i: u64) -> u64 {
224+
pub fn right_span(i: usize) -> usize {
216225
right_span_with_depth(i, depth(i))
217226
}
218227

219228
/// 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 {
221230
if depth == 0 {
222231
i
223232
} else {
@@ -235,13 +244,13 @@ pub fn left_span_with_depth(i: u64, depth: u64) -> u64 {
235244
/// assert_eq!(flat_tree::left_span(23), 16);
236245
/// assert_eq!(flat_tree::left_span(27), 24);
237246
/// ```
238-
pub fn left_span(i: u64) -> u64 {
247+
pub fn left_span(i: usize) -> usize {
239248
left_span_with_depth(i, depth(i))
240249
}
241250

242251
/// Returns the left and right most nodes in the tree that the node spans, with
243252
/// 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) {
245254
(
246255
left_span_with_depth(i, depth),
247256
right_span_with_depth(i, depth),
@@ -258,12 +267,12 @@ pub fn spans_with_depth(i: u64, depth: u64) -> (u64, u64) {
258267
/// assert_eq!(flat_tree::spans(23), (16, 30));
259268
/// assert_eq!(flat_tree::spans(27), (24, 30));
260269
/// ```
261-
pub fn spans(i: u64) -> (u64, u64) {
270+
pub fn spans(i: usize) -> (usize, usize) {
262271
spans_with_depth(i, depth(i))
263272
}
264273

265274
/// 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 {
267276
(2 << depth) - 1
268277
}
269278

@@ -278,7 +287,7 @@ pub fn count_with_depth(_: u64, depth: u64) -> u64 {
278287
/// assert_eq!(flat_tree::count(23), 15);
279288
/// assert_eq!(flat_tree::count(27), 7);
280289
/// ```
281-
pub fn count(i: u64) -> u64 {
290+
pub fn count(i: usize) -> usize {
282291
count_with_depth(i, depth(i))
283292
}
284293

@@ -293,7 +302,7 @@ pub fn count(i: u64) -> u64 {
293302
/// assert_eq!(flat_tree::full_roots(18), [7, 16]);
294303
/// assert_eq!(flat_tree::full_roots(16), [7]);
295304
/// ```
296-
pub fn full_roots(i: u64) -> Vec<u64> {
305+
pub fn full_roots(i: usize) -> Vec<usize> {
297306
let mut result = Vec::with_capacity(64);
298307

299308
if is_odd(i) {
@@ -321,7 +330,7 @@ pub fn full_roots(i: u64) -> Vec<u64> {
321330
}
322331

323332
#[inline(always)]
324-
fn is_even(num: u64) -> bool {
333+
fn is_even(num: usize) -> bool {
325334
(num & 1) == 0
326335
}
327336
#[test]
@@ -333,7 +342,7 @@ fn test_is_even() {
333342
}
334343

335344
#[inline(always)]
336-
fn is_odd(num: u64) -> bool {
345+
fn is_odd(num: usize) -> bool {
337346
(num & 1) != 0
338347
}
339348
#[test]

0 commit comments

Comments
 (0)