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
//! Implements various index arithmetic functions for perfect binary trees.
#![no_std]

#[cfg(feature = "u64")]
type T = u64;

#[cfg(feature = "u128")]
type T = u128;

const BITS: T = (core::mem::size_of::<T>() * 8) as T;
const NEGATIVE_TWO: T = -2i8 as T;

/// Returns an index's family.
pub const fn expand(index: T) -> (T, T, T) {
    let left = index & NEGATIVE_TWO;
    let right = left + 1;
    let parent = left / 2;

    (left, right, parent)
}

/// Returns the index's sibling.
pub const fn sibling(index: T) -> T {
    (index & NEGATIVE_TWO) + ((index & 1) ^ 1)
}

/// Returns the first leaf of a tree rooted at `root` with a `depth`.
pub const fn first_leaf(root: T, depth: T) -> T {
    root * (1 << depth)
}

/// Returns the last leaf of a tree rooted at `root` with a `depth`.
pub const fn last_leaf(root: T, depth: T) -> T {
    let pow = 1 << depth;
    root * pow + pow - 1
}

/// Returns if `index` is in the subtree rooted at `root`.
pub const fn is_in_subtree(root: T, index: T) -> bool {
    let diff = relative_depth(root, index);
    let left_most = first_leaf(root, diff);
    let right_most = last_leaf(root, diff);

    (left_most <= index) & (index <= right_most)
}

/// Returns the depth between two general indicies.
pub const fn relative_depth(a: T, b: T) -> T {
    let a = log2(last_power_of_two(a));
    let b = log2(last_power_of_two(b));

    (b - a) as T
}

/// Returns the next power of two for `n` using bit twiddling.
///
/// Source: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
pub const fn next_power_of_two(n: T) -> T {
    let mut ret = n - 1;

    ret = ret | ret >> 1;
    ret = ret | ret >> 2;
    ret = ret | ret >> 4;
    ret = ret | ret >> 8;
    ret = ret | ret >> 16;
    ret = ret | ret >> 32;

    #[cfg(feature = "u128")]
    {
        ret = ret | ret >> 64;
    }

    ret + 1
}

/// Returns the last power of two for `n` using bit twiddling.
pub const fn last_power_of_two(n: T) -> T {
    let mut ret = n;

    ret = ret | ret >> 1;
    ret = ret | ret >> 2;
    ret = ret | ret >> 4;
    ret = ret | ret >> 8;
    ret = ret | ret >> 16;
    ret = ret | ret >> 32;

    #[cfg(feature = "u128")]
    {
        ret = ret | ret >> 64;
    }

    let (ret, overflow) = ret.overflowing_add(1);

    // This will either be the last power of two or zero if it overflowed.
    let shifted = ret.overflowing_shr(1).0;

    // Overflows only occur at `2^BITS < n < 2^BITS-1`, so the last power of
    // two would be `2^(BITS-1)`.
    let max_pow_two = 1 << (BITS - 1);

    shifted + (max_pow_two & ((overflow as T) << (BITS - 1)))
}

/// Returns the subtree root for `index` assuming the tree is `depth` deep.
pub const fn root_from_depth(index: u64, depth: u64) -> u64 {
    index / (1 << depth)
}

/// Returns the log base 2 of `n`.
pub const fn log2(n: T) -> T {
    (BITS - 1) as T - n.leading_zeros() as T
}

/// Translate the subtree index `index` rooted at `root` into a general index.
pub const fn subtree_index_to_general(root: T, index: T) -> T {
    (root * index) - (root - 1) * (index - last_power_of_two(index))
}

/// Translate the general index `index` into a subtree index rooted at `root`.
pub const fn general_index_to_subtree(root: T, index: T) -> T {
    let depth_diff = log2(last_power_of_two(index)) - log2(last_power_of_two(root));

    (1 << depth_diff) + index - ((1 << depth_diff) * root)
}

#[cfg(test)]
mod tests {
    use super::*;

    const TWO: T = 2;

    #[test]
    fn compute_depth_difference() {
        assert_eq!(relative_depth(1, 1), 0);
        assert_eq!(relative_depth(1, 2), 1);
        assert_eq!(relative_depth(1, 5), 2);
        assert_eq!(relative_depth(1, 13), 3);
        assert_eq!(relative_depth(1, 30), 4);

        assert_eq!(relative_depth(2, 30), 3);
        assert_eq!(relative_depth(7, 30), 2);
        assert_eq!(relative_depth(11, 30), 1);
        assert_eq!(relative_depth(28, 30), 0);

        assert_eq!(relative_depth(1, TWO.pow(63)), 63);
    }

    #[test]
    fn compute_root_from_depth() {
        assert_eq!(root_from_depth(2, 1), 1);
        assert_eq!(root_from_depth(3, 1), 1);

        assert_eq!(root_from_depth(16, 4), 1);
        assert_eq!(root_from_depth(16, 3), 2);
        assert_eq!(root_from_depth(16, 2), 4);
        assert_eq!(root_from_depth(16, 1), 8);
        assert_eq!(root_from_depth(16, 0), 16);

        assert_eq!(root_from_depth(31, 4), 1);
        assert_eq!(root_from_depth(31, 3), 3);
        assert_eq!(root_from_depth(31, 2), 7);
        assert_eq!(root_from_depth(31, 1), 15);
        assert_eq!(root_from_depth(31, 0), 31);

        assert_eq!(root_from_depth(22, 4), 1);
        assert_eq!(root_from_depth(22, 3), 2);
        assert_eq!(root_from_depth(22, 2), 5);
        assert_eq!(root_from_depth(22, 1), 11);
        assert_eq!(root_from_depth(22, 0), 22);

        assert_eq!(root_from_depth(25, 4), 1);
        assert_eq!(root_from_depth(25, 3), 3);
        assert_eq!(root_from_depth(25, 2), 6);
        assert_eq!(root_from_depth(25, 1), 12);
        assert_eq!(root_from_depth(25, 0), 25);
    }

    #[test]
    fn compute_expanded_tree_indexes() {
        assert_eq!(expand(2), (2, 3, 1));
        assert_eq!(expand(3), (2, 3, 1));
        assert_eq!(expand(14), (14, 15, 7));
        assert_eq!(expand(15), (14, 15, 7));
    }

    #[test]
    fn compute_sibling_index() {
        assert_eq!(sibling(2), 3);
        assert_eq!(sibling(3), 2);
        assert_eq!(sibling(6), 7);
        assert_eq!(sibling(7), 6);
        assert_eq!(sibling(100), 101);
        assert_eq!(sibling(101), 100);
    }

    #[test]
    fn next_and_last_power_of_two() {
        assert_eq!(last_power_of_two(1), 1);
        assert_eq!(last_power_of_two(2), 2);
        assert_eq!(last_power_of_two(9), 8);
        assert_eq!(last_power_of_two(1023), 512);
        assert_eq!(last_power_of_two(TWO.pow(63) + 1000), TWO.pow(63));

        #[cfg(feature = "u128")]
        assert_eq!(last_power_of_two(TWO.pow(127) + 1000), TWO.pow(127));

        assert_eq!(next_power_of_two(1), 1);
        assert_eq!(next_power_of_two(2), 2);
        assert_eq!(next_power_of_two(9), 16);
        assert_eq!(next_power_of_two(1023), 1024);
        assert_eq!(next_power_of_two(TWO.pow(62) + 1000), TWO.pow(63));

        #[cfg(feature = "u128")]
        assert_eq!(next_power_of_two(TWO.pow(126) + 1000), TWO.pow(127));
    }

    #[test]
    fn transform_subtree_index_to_general() {
        assert_eq!(subtree_index_to_general(1, 1), 1);
        assert_eq!(subtree_index_to_general(1, 2), 2);
        assert_eq!(subtree_index_to_general(1, 1000), 1000);
        assert_eq!(subtree_index_to_general(1, 99999), 99999);

        assert_eq!(subtree_index_to_general(2, 1), 2);
        assert_eq!(subtree_index_to_general(2, 2), 4);
        assert_eq!(subtree_index_to_general(2, 7), 11);
        assert_eq!(subtree_index_to_general(2, 11), 19);
        assert_eq!(subtree_index_to_general(2, 20), 36);

        assert_eq!(subtree_index_to_general(6, 1), 6);
        assert_eq!(subtree_index_to_general(6, 2), 12);
        assert_eq!(subtree_index_to_general(6, 6), 26);

        assert_eq!(subtree_index_to_general(26, 1), 26);
        assert_eq!(subtree_index_to_general(26, 2), 52);
        assert_eq!(subtree_index_to_general(26, 3), 53);

        assert_eq!(subtree_index_to_general(26, 7), 107);
        assert_eq!(subtree_index_to_general(26, 12), 212);

        assert_eq!(subtree_index_to_general(11, 17), 177);
    }

    #[test]
    fn transform_general_index_to_subtree() {
        assert_eq!(general_index_to_subtree(1, 1), 1);
        assert_eq!(general_index_to_subtree(1, 2), 2);
        assert_eq!(general_index_to_subtree(1, 1000), 1000);
        assert_eq!(general_index_to_subtree(1, 99999), 99999);

        assert_eq!(general_index_to_subtree(2, 2), 1);
        assert_eq!(general_index_to_subtree(2, 4), 2);
        assert_eq!(general_index_to_subtree(2, 11), 7);
        assert_eq!(general_index_to_subtree(2, 19), 11);
        assert_eq!(general_index_to_subtree(2, 35), 19);
        assert_eq!(general_index_to_subtree(2, 36), 20);

        assert_eq!(general_index_to_subtree(6, 6), 1);
        assert_eq!(general_index_to_subtree(6, 12), 2);
        assert_eq!(general_index_to_subtree(6, 26), 6);

        assert_eq!(general_index_to_subtree(26, 26), 1);
        assert_eq!(general_index_to_subtree(26, 52), 2);
        assert_eq!(general_index_to_subtree(26, 53), 3);
        assert_eq!(general_index_to_subtree(26, 107), 7);
        assert_eq!(general_index_to_subtree(26, 212), 12);
    }

    #[test]
    fn compute_log2() {
        assert_eq!(log2(TWO.pow(1)), 1);
        assert_eq!(log2(TWO.pow(10)), 10);
        assert_eq!(log2(TWO.pow(33)), 33);
        assert_eq!(log2(TWO.pow(45)), 45);
        assert_eq!(log2(TWO.pow(63)), 63);
    }

    #[test]
    fn compute_first_leaf() {
        assert_eq!(first_leaf(1, 1), TWO.pow(1));
        assert_eq!(first_leaf(1, 9), TWO.pow(9));
        assert_eq!(first_leaf(1, 50), TWO.pow(50));

        assert_eq!(first_leaf(2, 1), 2 * TWO.pow(1));
        assert_eq!(first_leaf(2, 4), 2 * TWO.pow(4));
        assert_eq!(first_leaf(2, 5), 2 * TWO.pow(5));

        assert_eq!(first_leaf(6, 1), 6 * TWO.pow(1));
        assert_eq!(first_leaf(6, 2), 6 * TWO.pow(2));
        assert_eq!(first_leaf(6, 11), 6 * TWO.pow(11));

        assert_eq!(first_leaf(25, 1), 25 * TWO.pow(1));
    }

    #[test]
    fn compute_last_leaf() {
        assert_eq!(last_leaf(1, 1), 3);
        assert_eq!(last_leaf(1, 9), TWO.pow(10) - 1);
        assert_eq!(last_leaf(1, 50), TWO.pow(51) - 1);

        assert_eq!(last_leaf(2, 1), 2 * TWO.pow(1) + 1);
        assert_eq!(last_leaf(2, 4), 2 * TWO.pow(4) + 15);
        assert_eq!(last_leaf(2, 5), 2 * TWO.pow(5) + 31);

        assert_eq!(last_leaf(6, 1), 6 * TWO.pow(1) + 1);
        assert_eq!(last_leaf(6, 2), 6 * TWO.pow(2) + 3);
        assert_eq!(last_leaf(6, 11), 6 * TWO.pow(11) + 2047);

        assert_eq!(last_leaf(25, 1), 25 * TWO.pow(1) + 1);
    }

    #[test]
    fn determine_if_index_is_in_subtree() {
        assert_eq!(is_in_subtree(1, 3), true);
        assert_eq!(is_in_subtree(1, 100), true);
        assert_eq!(is_in_subtree(2, 10), true);
        assert_eq!(is_in_subtree(2, 6), false);
        assert_eq!(is_in_subtree(2, 15), false);
    }
}