Interleave per-dim bits into a single Z-order index, MSB first.
(coords: &[u64], bits: u32)
| 12 | |
| 13 | /// Interleave per-dim bits into a single Z-order index, MSB first. |
| 14 | pub fn encode(coords: &[u64], bits: u32) -> ArrayResult<u64> { |
| 15 | check_shape(coords.len(), bits)?; |
| 16 | let n = coords.len(); |
| 17 | let mut idx: u64 = 0; |
| 18 | for b in (0..bits).rev() { |
| 19 | for c in coords.iter().take(n) { |
| 20 | let bit = (*c >> b) & 1; |
| 21 | idx = (idx << 1) | bit; |
| 22 | } |
| 23 | } |
| 24 | Ok(idx) |
| 25 | } |
| 26 | |
| 27 | /// Inverse of [`encode`]. |
| 28 | pub fn decode(idx: u64, n: usize, bits: u32) -> ArrayResult<Vec<u64>> { |