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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#![crate_type = "lib"]

#[derive(Debug)]
pub enum IndexError {
    NonDepth0BlockError,
}

pub fn full_roots(i: u64) -> Result<Vec<u64>, IndexError> {
    if i & 1 != 0 {
        return Err(IndexError::NonDepth0BlockError);
    }

    let mut result = Vec::with_capacity(64);
    let mut tmp = i >> 1;
    let mut offset = 0;
    let mut factor = 1;

    loop {
        if tmp == 0 {
            return Ok(result);
        }

        while factor * 2 <= tmp {
            factor *= 2;
        }
        result.push(offset + factor - 1);
        offset += 2 * factor;
        tmp -= factor;
        factor = 1;
    }
}

pub fn depth(i: u64) -> u64 {
    let mut depth = 0;
    let mut i = i;
    while i & 1 != 0 {
        i >>= 1;
        depth += 1;
    }
    return depth;
}

pub fn sibling(i: u64) -> u64 {
    return sibling_with_depth(i, depth(i));
}

pub fn parent(i: u64) -> u64 {
    return parent_with_depth(i, depth(i));
}

pub fn left_child(i: u64) -> u64 {
    return left_child_with_depth(i, depth(i));
}

pub fn right_child(i: u64) -> u64 {
    return right_child_with_depth(i, depth(i));
}

pub fn children(i: u64) -> (u64, u64) {
    return children_with_depth(i, depth(i));
}

pub fn left_span(i: u64) -> u64 {
    return left_span_with_depth(i, depth(i));
}

pub fn right_span(i: u64) -> u64 {
    return right_span_with_depth(i, depth(i));
}

pub fn count(i: u64) -> u64 {
    return count_with_depth(i, depth(i));
}

pub fn spans(i: u64) -> (u64, u64) {
    return spans_with_depth(i, depth(i));
}

pub fn index(depth: u64, offset: u64) -> u64 {
    return (offset << depth + 1) | ((1 << depth) - 1);
}

pub fn offset(i: u64) -> u64 {
    return offset_with_depth(i, depth(i));
}

pub fn offset_with_depth(i: u64, depth: u64) -> u64 {
    return i >> (depth + 1);
}

pub fn parent_with_depth(i: u64, depth: u64) -> u64 {
    return index(depth + 1, offset_with_depth(i, depth) >> 1);
}

pub fn sibling_with_depth(i: u64, depth: u64) -> u64 {
    return index(depth, offset(i) ^ 1);
}

pub fn children_with_depth(i: u64, depth: u64) -> (u64, u64) {
    if depth == 0 {
        return (i, i);
    }

    let off = offset_with_depth(i, depth) >> 1;
    return (index(depth - 1, off), index(depth - 1, off + 1));
}

pub fn left_child_with_depth(i: u64, depth: u64) -> u64 {
    if depth == 0 {
        return i;
    }
    return index(depth - 1, offset_with_depth(i, depth) << 1);
}

pub fn right_child_with_depth(i: u64, depth: u64) -> u64 {
    if depth == 0 {
        return i;
    }
    return index(depth - 1, (offset_with_depth(i, depth) << 1) + 1);
}


pub fn right_span_with_depth(i: u64, depth: u64) -> u64 {
    if depth == 0 {
        return i;
    }
    return (offset_with_depth(i, depth) + 1) * (2 << depth) - 2;
}

pub fn left_span_with_depth(i: u64, depth: u64) -> u64 {
    if depth == 0 {
        return i;
    }
    return offset_with_depth(i, depth) * (2 << depth);
}

pub fn spans_with_depth(i: u64, depth: u64) -> (u64, u64) {
    return (left_span_with_depth(i, depth), right_span_with_depth(i, depth));
}

pub fn count_with_depth(_: u64, depth: u64) -> u64 {
    return (2 << depth) - 1;
}

fn two_pow(n: u64) -> u64 {
    if n < 31 {
        return 1 << n;
    } else {
        return (1 << 30) * (1 << (n - 30));
    }
}

pub struct Iterator {
    index: u64,
    offset: u64,
    factor: u64,
}

impl Iterator {
    pub fn new(i: u64) -> Iterator {
        let mut iter = Iterator {
            index: 0,
            offset: 0,
            factor: 0,
        };
        iter.seek(i);
        return iter;
    }

    pub fn seek(&mut self, i: u64) {
        self.index = i;
        if self.index & 1 != 0 {
            self.offset = offset(i);
            self.factor = two_pow(depth(i) + 1);
        } else {
            self.offset = i / 2;
            self.factor = 2;
        }
    }

    pub fn is_left(&self) -> bool {
        return (self.offset & 1) != 0;
    }

    pub fn is_right(&self) -> bool {
        return !self.is_left();
    }

    pub fn prev(&mut self) -> u64 {
        if self.offset != 0 {
            return self.index;
        }
        self.offset -= 1;
        self.index -= self.factor;
        return self.index;
    }

    pub fn next(&mut self) -> u64 {
        self.offset += 1;
        self.index += self.factor;
        return self.index;
    }

    pub fn sibling(&mut self) -> u64 {
        if self.is_left() {
            return self.next();
        } else {
            return self.prev();
        }
    }

    pub fn parent(&mut self) -> u64 {
        if self.offset & 1 != 0 {
            self.index -= self.factor / 2;
            self.offset = (self.offset - 1) / 2;
        } else {
            self.index += self.factor / 2;
            self.offset /= 2;
        }
        self.factor *= 2;
        return self.index;
    }

    pub fn left_span(&mut self) -> u64 {
        self.index = self.index - self.factor / 2 + 1;
        self.offset = self.offset / 2;
        self.factor = 2;
        return self.index;
    }

    pub fn right_span(&mut self) -> u64 {
        self.index = self.index + self.factor / 2 + 1;
        self.offset = self.offset / 2;
        self.factor = 2;
        return self.index;
    }

    pub fn left_child(&mut self) -> u64 {
        if self.factor == 2 {
            return self.index;
        }

        self.factor /= 2;
        self.index -= self.factor / 2;
        self.offset *= 2;
        return self.index;
    }

    pub fn right_child(&mut self) -> u64 {
        if self.factor == 2 {
            return self.index;
        }

        self.factor /= 2;
        self.index += self.factor / 2;
        self.offset *= 2;
        return self.index;
    }
}


#[test]
fn test_base_blocks() {
    assert_eq!(index(0, 0), 0);
    assert_eq!(index(0, 1), 2);
    assert_eq!(index(0, 2), 4);
}

#[test]
fn test_parents() {
    assert_eq!(index(1, 0), 1);
    assert_eq!(index(1, 1), 5);
    assert_eq!(index(2, 0), 3);

    assert_eq!(parent(0), 1);
    assert_eq!(parent(2), 1);
    assert_eq!(parent(1), 3);
}

#[test]
fn test_children() {
    assert_eq!(children(0), (0, 0));
    assert_eq!(children(1), (0, 2));
    assert_eq!(children(3), (1, 5));
}

#[test]
fn test_left_child() {
    assert_eq!(left_child(0), 0);
    assert_eq!(left_child(1), 0);
    assert_eq!(left_child(3), 1);
}

#[test]
fn test_right_child() {
    assert_eq!(right_child(0), 0);
    assert_eq!(right_child(1), 2);
    assert_eq!(right_child(3), 5);
}

#[test]
fn test_siblings() {
    assert_eq!(sibling(0), 2);
    assert_eq!(sibling(2), 0);
    assert_eq!(sibling(1), 5);
    assert_eq!(sibling(5), 1);
}

#[test]
fn test_full_roots() {
    assert!(full_roots(0).is_ok(),
            "should only look up roots for depth(0) blocks");
    assert_eq!(full_roots(2).unwrap(), [0]);
    assert_eq!(full_roots(8).unwrap(), [3]);
    assert_eq!(full_roots(20).unwrap(), [7, 17]);
    assert_eq!(full_roots(18).unwrap(), [7, 16]);
    assert_eq!(full_roots(16).unwrap(), [7]);
}

#[test]
fn test_depths() {
    assert_eq!(depth(0), 0);
    assert_eq!(depth(1), 1);
    assert_eq!(depth(2), 0);
    assert_eq!(depth(3), 2);
    assert_eq!(depth(4), 0);
}

#[test]
fn test_offsets() {
    assert_eq!(offset(0), 0);
    assert_eq!(offset(1), 0);
    assert_eq!(offset(2), 1);
    assert_eq!(offset(3), 0);
    assert_eq!(offset(4), 2);
}

#[test]
fn test_spans() {
    assert_eq!(spans(0), (0, 0));
    assert_eq!(spans(1), (0, 2));
    assert_eq!(spans(3), (0, 6));
    assert_eq!(spans(23), (16, 30));
    assert_eq!(spans(27), (24, 30));
}

#[test]
fn test_left_span() {
    assert_eq!(left_span(0), 0);
    assert_eq!(left_span(1), 0);
    assert_eq!(left_span(3), 0);
    assert_eq!(left_span(23), 16);
    assert_eq!(left_span(27), 24);
}

#[test]
fn test_right_span() {
    assert_eq!(right_span(0), 0);
    assert_eq!(right_span(1), 2);
    assert_eq!(right_span(3), 6);
    assert_eq!(right_span(23), 30);
    assert_eq!(right_span(27), 30);
}

#[test]
fn test_count() {
    assert_eq!(count(0), 1);
    assert_eq!(count(1), 3);
    assert_eq!(count(3), 7);
    assert_eq!(count(5), 3);
    assert_eq!(count(23), 15);
    assert_eq!(count(27), 7);
}

#[test]
fn test_parent_gt_int32() {
    assert_eq!(parent(10000000000), 10000000001);
}

#[test]
fn test_child_to_parent_to_child() {
    let mut child = 0;
    for _ in 0..50 {
        child = parent(child)
    }
    assert_eq!(child, 1125899906842623);
    for _ in 0..50 {
        child = left_child(child)
    }
    assert_eq!(child, 0);
}

#[test]
fn test_iterator() {
    let mut iter = Iterator::new(0);

    assert_eq!(iter.index, 0);
    assert_eq!(iter.parent(), 1);
    assert_eq!(iter.parent(), 3);
    assert_eq!(iter.parent(), 7);
    assert_eq!(iter.right_child(), 11);
    assert_eq!(iter.left_child(), 9);
    assert_eq!(iter.next(), 13);
    assert_eq!(iter.left_span(), 12);
}

#[test]
fn test_iterator_non_leaf_start() {
    let mut iter = Iterator::new(1);

    assert_eq!(iter.index, 1);
    assert_eq!(iter.parent(), 3);
    assert_eq!(iter.parent(), 7);
    assert_eq!(iter.right_child(), 11);
    assert_eq!(iter.left_child(), 9);
    assert_eq!(iter.next(), 13);
    assert_eq!(iter.left_span(), 12);
}