Skip to content

Commit d5062de

Browse files
authored
inline all methods (#39)
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent dc2038f commit d5062de

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/iterator.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl Iterator {
2626
/// use flat_tree::Iterator;
2727
/// assert_eq!(Iterator::new(0).take(3).collect::<Vec<usize>>(), [2, 4, 6]);
2828
/// ```
29+
#[inline]
2930
pub fn new(index: usize) -> Self {
3031
let mut instance = Self {
3132
index: 0,
@@ -59,6 +60,7 @@ impl Iterator {
5960
/// iter.seek(2);
6061
/// assert_eq!(iter.next(), Some(4));
6162
/// ```
63+
#[inline]
6264
pub fn seek(&mut self, index: usize) {
6365
self.index = index;
6466
if is_odd(self.index) {
@@ -105,6 +107,7 @@ impl Iterator {
105107
/// assert_eq!(iter.prev(), 2);
106108
/// assert_eq!(iter.prev(), 0);
107109
/// ```
110+
#[inline]
108111
pub fn prev(&mut self) -> usize {
109112
if self.offset == 0 {
110113
return self.index;
@@ -122,6 +125,7 @@ impl Iterator {
122125
/// assert_eq!(flat_tree::Iterator::new(1).sibling(), 5);
123126
/// assert_eq!(flat_tree::Iterator::new(4).sibling(), 6);
124127
/// ```
128+
#[inline]
125129
pub fn sibling(&mut self) -> usize {
126130
if self.is_left() {
127131
self.next().unwrap() // this is always safe
@@ -138,6 +142,7 @@ impl Iterator {
138142
/// assert_eq!(flat_tree::Iterator::new(1).parent(), 3);
139143
/// assert_eq!(flat_tree::Iterator::new(4).parent(), 5);
140144
/// ```
145+
#[inline]
141146
pub fn parent(&mut self) -> usize {
142147
if is_odd(self.offset) {
143148
self.index -= self.factor / 2;
@@ -160,6 +165,7 @@ impl Iterator {
160165
/// assert_eq!(flat_tree::Iterator::new(23).left_span(), 16);
161166
/// assert_eq!(flat_tree::Iterator::new(27).left_span(), 24);
162167
/// ```
168+
#[inline]
163169
pub fn left_span(&mut self) -> usize {
164170
self.index = self.index + 1 - self.factor / 2;
165171
self.offset = self.index / 2;
@@ -177,6 +183,7 @@ impl Iterator {
177183
/// assert_eq!(flat_tree::Iterator::new(23).right_span(), 30);
178184
/// assert_eq!(flat_tree::Iterator::new(27).right_span(), 30);
179185
/// ```
186+
#[inline]
180187
pub fn right_span(&mut self) -> usize {
181188
self.index = self.index + self.factor / 2 - 1;
182189
self.offset = self.index / 2;
@@ -192,6 +199,7 @@ impl Iterator {
192199
/// assert_eq!(flat_tree::Iterator::new(3).left_child(), 1);
193200
/// assert_eq!(flat_tree::Iterator::new(7).left_child(), 3);
194201
/// ```
202+
#[inline]
195203
pub fn left_child(&mut self) -> usize {
196204
if self.factor == 2 {
197205
return self.index;
@@ -210,6 +218,7 @@ impl Iterator {
210218
/// assert_eq!(flat_tree::Iterator::new(3).right_child(), 5);
211219
/// assert_eq!(flat_tree::Iterator::new(7).right_child(), 11);
212220
/// ```
221+
#[inline]
213222
pub fn right_child(&mut self) -> usize {
214223
if self.factor == 2 {
215224
return self.index;
@@ -224,6 +233,7 @@ impl Iterator {
224233
impl iter::Iterator for Iterator {
225234
type Item = usize;
226235

236+
#[inline]
227237
fn next(&mut self) -> Option<Self::Item> {
228238
self.offset += 1;
229239
self.index += self.factor;
@@ -232,11 +242,13 @@ impl iter::Iterator for Iterator {
232242
}
233243

234244
impl Default for Iterator {
245+
#[inline]
235246
fn default() -> Self {
236247
Self::new(0)
237248
}
238249
}
239250

251+
#[inline]
240252
fn two_pow(n: usize) -> usize {
241253
if n < 31 {
242254
1 << n

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub use iterator::Iterator;
2020
/// assert_eq!(flat_tree::index(3, 0), 7);
2121
/// assert_eq!(flat_tree::index(3, 1), 23);
2222
/// ```
23+
#[inline]
2324
pub fn index(depth: usize, offset: usize) -> usize {
2425
(offset << (depth + 1)) | ((1 << depth) - 1)
2526
}
@@ -34,6 +35,7 @@ pub fn index(depth: usize, offset: usize) -> usize {
3435
/// assert_eq!(flat_tree::depth(3), 2);
3536
/// assert_eq!(flat_tree::depth(4), 0);
3637
/// ```
38+
#[inline]
3739
pub fn depth(i: usize) -> usize {
3840
let mut depth = 0;
3941
let mut i = i;
@@ -54,6 +56,7 @@ pub fn depth(i: usize) -> usize {
5456
/// assert_eq!(flat_tree::offset_with_depth(3, 2), 0);
5557
/// assert_eq!(flat_tree::offset_with_depth(4, 0), 2);
5658
/// ```
59+
#[inline]
5760
pub fn offset_with_depth(i: usize, depth: usize) -> usize {
5861
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
5962
if is_even(i) {
@@ -73,6 +76,7 @@ pub fn offset_with_depth(i: usize, depth: usize) -> usize {
7376
/// assert_eq!(flat_tree::offset(3), 0);
7477
/// assert_eq!(flat_tree::offset(4), 2);
7578
/// ```
79+
#[inline]
7680
pub fn offset(i: usize) -> usize {
7781
offset_with_depth(i, depth(i))
7882
}
@@ -87,6 +91,7 @@ pub fn offset(i: usize) -> usize {
8791
/// assert_eq!(flat_tree::parent_with_depth(3, 2), 7);
8892
/// assert_eq!(flat_tree::parent_with_depth(4, 0), 5);
8993
/// ```
94+
#[inline]
9095
pub fn parent_with_depth(i: usize, depth: usize) -> usize {
9196
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
9297
index(depth + 1, offset_with_depth(i, depth) >> 1)
@@ -104,6 +109,7 @@ pub fn parent_with_depth(i: usize, depth: usize) -> usize {
104109
/// assert_eq!(flat_tree::parent(2), 1);
105110
/// assert_eq!(flat_tree::parent(1), 3);
106111
/// ```
112+
#[inline]
107113
pub fn parent(i: usize) -> usize {
108114
parent_with_depth(i, depth(i))
109115
}
@@ -118,6 +124,7 @@ pub fn parent(i: usize) -> usize {
118124
/// assert_eq!(flat_tree::sibling_with_depth(3, 2), 11);
119125
/// assert_eq!(flat_tree::sibling_with_depth(4, 0), 6);
120126
/// ```
127+
#[inline]
121128
pub fn sibling_with_depth(i: usize, depth: usize) -> usize {
122129
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
123130
index(depth, offset(i) ^ 1)
@@ -132,6 +139,7 @@ pub fn sibling_with_depth(i: usize, depth: usize) -> usize {
132139
/// assert_eq!(flat_tree::sibling(1), 5);
133140
/// assert_eq!(flat_tree::sibling(5), 1);
134141
/// ```
142+
#[inline]
135143
pub fn sibling(i: usize) -> usize {
136144
sibling_with_depth(i, depth(i))
137145
}
@@ -145,6 +153,7 @@ pub fn sibling(i: usize) -> usize {
145153
/// assert_eq!(flat_tree::uncle_with_depth(2, 0), 5);
146154
/// assert_eq!(flat_tree::uncle_with_depth(5, 1), 11);
147155
/// ```
156+
#[inline]
148157
pub fn uncle_with_depth(i: usize, depth: usize) -> usize {
149158
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
150159
sibling_with_depth(parent_with_depth(i, depth), depth + 1)
@@ -159,6 +168,7 @@ pub fn uncle_with_depth(i: usize, depth: usize) -> usize {
159168
/// assert_eq!(flat_tree::uncle(1), 11);
160169
/// assert_eq!(flat_tree::uncle(5), 11);
161170
/// ```
171+
#[inline]
162172
pub fn uncle(i: usize) -> usize {
163173
uncle_with_depth(i, depth(i))
164174
}
@@ -172,6 +182,7 @@ pub fn uncle(i: usize) -> usize {
172182
/// assert_eq!(flat_tree::children_with_depth(3, 2), Some((1, 5)));
173183
/// assert_eq!(flat_tree::children_with_depth(9, 1), Some((8, 10)));
174184
/// ```
185+
#[inline]
175186
pub fn children_with_depth(i: usize, depth: usize) -> Option<(usize, usize)> {
176187
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
177188
if is_even(i) {
@@ -193,6 +204,7 @@ pub fn children_with_depth(i: usize, depth: usize) -> Option<(usize, usize)> {
193204
/// assert_eq!(flat_tree::children(3), Some((1, 5)));
194205
/// assert_eq!(flat_tree::children(9), Some((8, 10)));
195206
/// ```
207+
#[inline]
196208
pub fn children(i: usize) -> Option<(usize, usize)> {
197209
children_with_depth(i, depth(i))
198210
}
@@ -206,6 +218,7 @@ pub fn children(i: usize) -> Option<(usize, usize)> {
206218
/// assert_eq!(flat_tree::left_child_with_depth(3, 2), Some(1));
207219
/// ```
208220
// TODO: handle errors
221+
#[inline]
209222
pub fn left_child_with_depth(i: usize, depth: usize) -> Option<usize> {
210223
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
211224
if is_even(i) {
@@ -225,6 +238,7 @@ pub fn left_child_with_depth(i: usize, depth: usize) -> Option<usize> {
225238
/// assert_eq!(flat_tree::left_child(1), Some(0));
226239
/// assert_eq!(flat_tree::left_child(3), Some(1));
227240
/// ```
241+
#[inline]
228242
pub fn left_child(i: usize) -> Option<usize> {
229243
left_child_with_depth(i, depth(i))
230244
}
@@ -237,6 +251,7 @@ pub fn left_child(i: usize) -> Option<usize> {
237251
/// assert_eq!(flat_tree::right_child_with_depth(1, 1), Some(2));
238252
/// assert_eq!(flat_tree::right_child_with_depth(3, 2), Some(5));
239253
/// ```
254+
#[inline]
240255
pub fn right_child_with_depth(i: usize, depth: usize) -> Option<usize> {
241256
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
242257
if is_even(i) {
@@ -257,6 +272,7 @@ pub fn right_child_with_depth(i: usize, depth: usize) -> Option<usize> {
257272
/// assert_eq!(flat_tree::right_child(3), Some(5));
258273
/// ```
259274
// TODO: handle errors
275+
#[inline]
260276
pub fn right_child(i: usize) -> Option<usize> {
261277
right_child_with_depth(i, depth(i))
262278
}
@@ -271,6 +287,7 @@ pub fn right_child(i: usize) -> Option<usize> {
271287
/// assert_eq!(flat_tree::right_span_with_depth(23, 3), 30);
272288
/// assert_eq!(flat_tree::right_span_with_depth(27, 2), 30);
273289
/// ```
290+
#[inline]
274291
pub fn right_span_with_depth(i: usize, depth: usize) -> usize {
275292
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
276293
if depth == 0 {
@@ -290,6 +307,7 @@ pub fn right_span_with_depth(i: usize, depth: usize) -> usize {
290307
/// assert_eq!(flat_tree::right_span(23), 30);
291308
/// assert_eq!(flat_tree::right_span(27), 30);
292309
/// ```
310+
#[inline]
293311
pub fn right_span(i: usize) -> usize {
294312
right_span_with_depth(i, depth(i))
295313
}
@@ -304,6 +322,7 @@ pub fn right_span(i: usize) -> usize {
304322
/// assert_eq!(flat_tree::left_span_with_depth(23, 3), 16);
305323
/// assert_eq!(flat_tree::left_span_with_depth(27, 2), 24);
306324
/// ```
325+
#[inline]
307326
pub fn left_span_with_depth(i: usize, depth: usize) -> usize {
308327
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
309328
if depth == 0 {
@@ -323,6 +342,7 @@ pub fn left_span_with_depth(i: usize, depth: usize) -> usize {
323342
/// assert_eq!(flat_tree::left_span(23), 16);
324343
/// assert_eq!(flat_tree::left_span(27), 24);
325344
/// ```
345+
#[inline]
326346
pub fn left_span(i: usize) -> usize {
327347
left_span_with_depth(i, depth(i))
328348
}
@@ -338,6 +358,7 @@ pub fn left_span(i: usize) -> usize {
338358
/// assert_eq!(flat_tree::spans_with_depth(23, 3), (16, 30));
339359
/// assert_eq!(flat_tree::spans_with_depth(27, 2), (24, 30));
340360
/// ```
361+
#[inline]
341362
pub fn spans_with_depth(i: usize, depth: usize) -> (usize, usize) {
342363
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
343364
(
@@ -356,6 +377,7 @@ pub fn spans_with_depth(i: usize, depth: usize) -> (usize, usize) {
356377
/// assert_eq!(flat_tree::spans(23), (16, 30));
357378
/// assert_eq!(flat_tree::spans(27), (24, 30));
358379
/// ```
380+
#[inline]
359381
pub fn spans(i: usize) -> (usize, usize) {
360382
spans_with_depth(i, depth(i))
361383
}
@@ -371,6 +393,7 @@ pub fn spans(i: usize) -> (usize, usize) {
371393
/// assert_eq!(flat_tree::count_with_depth(23, 3), 15);
372394
/// assert_eq!(flat_tree::count_with_depth(27, 2), 7);
373395
/// ```
396+
#[inline]
374397
pub fn count_with_depth(i: usize, depth: usize) -> usize {
375398
debug_assert_eq!(depth, self::depth(i), "Invalid depth.");
376399
(2 << depth) - 1
@@ -387,6 +410,7 @@ pub fn count_with_depth(i: usize, depth: usize) -> usize {
387410
/// assert_eq!(flat_tree::count(23), 15);
388411
/// assert_eq!(flat_tree::count(27), 7);
389412
/// ```
413+
#[inline]
390414
pub fn count(i: usize) -> usize {
391415
count_with_depth(i, depth(i))
392416
}
@@ -426,6 +450,7 @@ pub fn count(i: usize) -> usize {
426450
/// full_roots(16, &mut nodes);
427451
/// assert_eq!(nodes, [7]);
428452
/// ```
453+
#[inline]
429454
pub fn full_roots(i: usize, nodes: &mut Vec<usize>) {
430455
assert!(
431456
is_even(i),

0 commit comments

Comments
 (0)