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
#![crate_type = "lib"]

pub fn index (depth: u64, offset: u64) -> u64 {
  return (offset << depth + 1) | ((1 << depth) - 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 offset_with_depth (i: u64, depth: u64) -> u64 {
  return i >> (depth + 1);
}

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

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

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

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

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

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

pub fn uncle (i: u64) -> u64 {
  return uncle_with_depth(i, depth(i));
}

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 children (i: u64) -> (u64, u64) {
  return children_with_depth(i, depth(i));
}

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 left_child (i: u64) -> u64 {
  return left_child_with_depth(i, depth(i))
}

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_child (i: u64) -> u64 {
  return right_child_with_depth(i, depth(i))
}

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 right_span (i: u64) -> u64 {
  return right_span_with_depth(i, depth(i));
}

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 left_span (i: u64) -> u64 {
  return left_span_with_depth(i, depth(i));
}

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 spans (i: u64) -> (u64, u64) {
  return spans_with_depth(i, depth(i));
}

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

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

pub fn full_roots (i: u64) -> Vec<u64> {
  let mut result = Vec::with_capacity(64);

  if i & 1 != 0 {
    return result;
  }

  let mut tmp = i >> 1;
  let mut offset = 0;
  let mut factor = 1;

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

#[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_eq!(full_roots(0), []);
  assert_eq!(full_roots(2), [0]);
  assert_eq!(full_roots(8), [3]);
  assert_eq!(full_roots(20), [7, 17]);
  assert_eq!(full_roots(18), [7, 16]);
  assert_eq!(full_roots(16), [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);
}