cdshealpix 0.9.1

Rust implementation of the HEALPix tesselation.
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Contains the code for MOC compression/decompression one can made on streamed MOCs
//! (i.e. based on uniq cells iterators ordered following the natural Z-order curve order).

use super::{CheckedIterator, HasMaxDepth, HpxCell, HpxHash, MOCIterator};

///
/// # Panic
/// * this method panics if the input iterator is not ordered following the natural Z-order
///   curve ordering.
pub fn compress<H, T>(it: T) -> CompressMocIter<H, CheckedIterator<H, T>>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  let it = CheckedIterator::new(it);
  compress_unchecked(it)
}

pub fn compress_unchecked<H, T>(it: T) -> CompressMocIter<H, T>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  CompressMocIter::new_unchecked(it)
}

/// Uncompress on-the-fly, while iterating, the MOC compressed in the given slice.
/// For more information, see the `decompress` method in BMOCs.
pub fn uncompress<H: HpxHash>(compressed: &[u8]) -> UncompressMocIter<'_, H> {
  UncompressMocIter::new(compressed)
}

/// Performs an `Compression` operation on-the-fly, on the input MOC, while iterating.
pub struct CompressMocIter<H, T>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  it: T,
  curr: Option<HpxCell<H>>,
  d: u8,
  h: H,
  buff: u32, // Go down worst case: from depth 0 to 29 => 3*30*2bits + 29*1 bit => 27 Bytes
  ibit: u8,
  is_going_down: bool,
}

impl<H, T> CompressMocIter<H, T>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  ///
  /// # WARNING
  /// * The input iterator **MUST** be ordered following the natural Z-order curve order.
  ///   If it is not the case, the result will be wrong, without errors!
  /// * If you are not sure, use the `new` version.
  fn new_unchecked(mut it: T) -> CompressMocIter<H, T> {
    let depth_max = it.depth_max();
    let curr = it.next();
    CompressMocIter {
      it,
      curr,
      d: 0,
      h: H::zero(),
      buff: depth_max as u32,
      ibit: 8,
      is_going_down: true,
    }
  }

  fn push_0(&mut self) {
    self.ibit += 1;
  }
  /// n: number of 0s to be pushed
  fn push_0_x(&mut self, n: u8) {
    self.ibit += n;
  }
  fn push_1(&mut self) {
    self.buff |= 1_u32 << self.ibit;
    self.push_0();
  }
  /// Returns true if the first byte of the buffer is full
  fn is_byte_full(&self) -> bool {
    self.ibit > 7
  }
  /// Take the first byte if the buffer an replace its content with the second byte
  fn take_byte(&mut self) -> u8 {
    let r = self.buff as u8;
    self.buff >>= 8;
    self.ibit -= 8;
    r
  }

  /// Tells is there is a last byte to be taken
  fn has_last_byte(&self) -> bool {
    debug_assert!(self.ibit < 8);
    self.ibit > 0
  }

  /// Take the last byte (WARNING: to be called at the end!!)
  fn take_last_byte(&mut self) -> u8 {
    debug_assert!(self.ibit < 8);
    let r = self.buff as u8;
    self.buff >>= 8;
    self.ibit = 0;
    r
  }

  /// Returns true if the first byte of the buffer is full
  /*fn push_node_empty(&mut self) -> bool {
    self.push_node_empty_notest();
    self.is_byte_full()
  }
  fn push_node_empty_notest(&mut self) {
    self.push_1();
    self.push_0();
  }*/
  fn push_n_empty_nodes_notest(&mut self, n: H) {
    for _ in 0..n.to_u8().unwrap() {
      self.push_1();
      self.push_0();
    }
  }
  /// Returns true if the first byte of the buffer is full
  /*fn push_node_full(&mut self) -> bool {
    self.push_node_full_notest();
    self.is_byte_full()
  }*/
  fn push_node_full_notest(&mut self) {
    self.push_1();
    self.push_1();
  }

  /// Returns true if the first byte of the buffer is full
  fn push_node_partial(&mut self) -> bool {
    self.push_node_partial_notest();
    self.is_byte_full()
  }
  fn push_node_partial_notest(&mut self) {
    self.push_0();
  }
  /// Returns true if the first byte of the buffer is full
  /*fn push_leaf_empty(&mut self) -> bool {
    self.push_leaf_empty_notest();
    self.is_byte_full()
  }
  fn push_leaf_empty_notest(&mut self) {
    self.push_0();
  }*/
  fn push_n_empty_leaves_notest(&mut self, n: H) {
    self.push_0_x(n.to_u8().unwrap());
  }
  /// Returns true if the first byte of the buffer is full
  /*fn push_leaf_full(&mut self) -> bool {
    self.push_leaf_full_notest();
    self.is_byte_full()
  }*/
  fn push_leaf_full_notest(&mut self) {
    self.push_1();
  }
}

impl<H, T> HasMaxDepth for CompressMocIter<H, T>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  fn depth_max(&self) -> u8 {
    self.it.depth_max()
  }
}

impl<H, T> Iterator for CompressMocIter<H, T>
where
  H: HpxHash,
  T: MOCIterator<H>,
{
  type Item = u8;

  // WE CAN PROBABLY SIMPLIFY THIS CODE!!
  fn next(&mut self) -> Option<Self::Item> {
    let three = H::one() | (H::one() << 1);
    if self.is_byte_full() {
      return Some(self.take_byte());
    }
    match &self.curr {
      Some(hpx) => {
        let (curr_d, curr_h) = (hpx.depth, hpx.hash);
        if !self.is_going_down {
          // go up (if needed)!
          let target_h = if self.d > curr_d {
            // case previous hash deeper that current hash
            let dd = self.d - curr_d;
            curr_h << ((dd << 1) as usize)
          } else {
            // case current hash deeper (or same depth) that previous hash, need to go up?
            let dd = curr_d - self.d;
            curr_h >> ((dd << 1) as usize)
          };
          let dd = ((63 - (self.h ^ target_h).leading_zeros()) >> 1) as u8;
          let target_d = if dd > self.d { 0 } else { self.d - dd };
          // - go up to common depth (if needed)
          while self.d > target_d {
            let n = three - (self.h & three);
            debug_assert!(n <= three);
            if self.d == self.depth_max() {
              self.push_n_empty_leaves_notest(n); // Writes max 3 0s
            } else {
              self.push_n_empty_nodes_notest(n); // Writes max 6 0s
            }
            self.h >>= 2;
            self.d -= 1;
            if self.is_byte_full() {
              return Some(self.take_byte());
            }
          }
          self.h += H::one();
        }
        // - go down
        self.is_going_down = true;
        let target_d = curr_d;
        while self.d < target_d {
          let dd = target_d - self.d;
          let target_h = curr_h >> ((dd << 1) as usize);
          let n = target_h - self.h;
          self.h = target_h << 2;
          self.d += 1;
          debug_assert!(n <= three);
          self.push_n_empty_nodes_notest(n); // Writes max 6 0s
          if self.push_node_partial() {
            return Some(self.take_byte());
          }
        }
        // - same level
        debug_assert_eq!(self.d, target_d);
        let n = curr_h - self.h;
        debug_assert!(
          n <= three,
          "n: {}, curr_h: {}, self.h: {}",
          n,
          curr_h,
          self.h
        );
        if self.d == self.depth_max() {
          self.push_n_empty_leaves_notest(n);
          self.push_leaf_full_notest()
        } else {
          self.push_n_empty_nodes_notest(n);
          self.push_node_full_notest();
        }
        self.h = curr_h;
        self.curr = self.it.next();
        self.is_going_down = false;
        self.next()
      }
      None => {
        // Just need to complete
        if self.d == 0 {
          // complete depth 0 till hash 12
          let twelve = three << 2_usize;
          let eleven = twelve - H::one();
          if self.h < eleven {
            let n = eleven - self.h;
            self.h = twelve;
            if self.d == self.depth_max() {
              self.push_n_empty_leaves_notest(n); // writes max 11 0s
            } else {
              self.push_n_empty_nodes_notest(n); // writes max 22 0s
            }
            self.next()
          } else if self.has_last_byte() {
            debug_assert!(
              self.d == 0 && self.h == eleven,
              "d: {}, h: {}",
              self.d,
              self.h
            );
            Some(self.take_last_byte())
          } else {
            debug_assert!(
              self.d == 0 && self.h == eleven,
              "d: {}, h: {}",
              self.d,
              self.h
            );
            None
          }
        } else {
          // go up to depth 0
          while {
            let n = three - (self.h & three);
            if self.d == self.depth_max() {
              self.push_n_empty_leaves_notest(n); // writes max 3 0s
            } else {
              self.push_n_empty_nodes_notest(n); // writes max 6 0s
            }
            self.h >>= 2;
            self.d -= 1;
            if self.is_byte_full() {
              return Some(self.take_byte());
            }
            self.d > 0
          } {} // do-while loop
          self.next()
        }
      }
    }
  }

  /*fn size_hint(&self) -> (usize, Option<usize>) {
    let size_hint = self.it.size_hint();
    if let Some(n) = size_hint.1 {
      (0, 4 + 3 * n)
    } else {
      (0, None)
    }
  }*/
}

/// Performs an `Uncompression` operation on-the-fly, while iterating.
pub struct UncompressMocIter<'a, H: HpxHash> {
  /// Slice containing the compressed MOC
  cmoc: &'a [u8],
  depth_max: u8,
  ibyte: usize,
  ibit: u8,
  depth: u8,
  hash: H,
}

impl<'a, H: HpxHash> UncompressMocIter<'a, H> {
  ///
  /// # WARNING
  /// * The input iterator **MUST** be ordered following the natural Z-order curve order.
  ///   If it is not the case, the result will be wrong, without errors!
  /// * If you are not sure, use the `new` version.
  fn new(cmoc: &'a [u8]) -> UncompressMocIter<'a, H> {
    let depth_max = cmoc[0];
    UncompressMocIter {
      cmoc,
      depth_max,
      ibyte: 1,
      ibit: 0,
      depth: 0,
      hash: H::zero(),
    }
  }

  fn get(&mut self) -> bool {
    let r = (self.cmoc[self.ibyte] & (1_u8 << self.ibit)) != 0;
    self.ibyte += (self.ibit == 7) as usize;
    self.ibit += 1;
    self.ibit &= 7;
    r
  }

  fn get_current_cell(&self) -> HpxCell<H> {
    HpxCell {
      depth: self.depth,
      hash: self.hash,
    }
  }

  fn increment(&mut self) {
    self.go_up_if_needed();
    self.hash += H::one();
  }

  fn go_up_if_needed(&mut self) {
    let three = H::one() | (H::one() << 1);
    while self.hash & three == three && self.depth > 0 {
      self.hash >>= 2;
      self.depth -= 1;
    }
  }

  /* Both the above and this solution are equivalent.
     This one is used in NotMocIter (see op module).
     Test performances on a very large MOC?
  fn increment(&mut self) {
    self.hash += H::one();
    self.go_up_if_needed();
  }

  fn go_up_if_needed(&mut self, ) {
    let dd = self.depth.min(self.hash.trailing_zeros() as u8 / 2).min(self.depth);
    self.depth -= dd;
    self.hash >>= dd << 1;
  }*/

  fn go_down_of_1_depth(&mut self) {
    self.hash <<= 2;
    self.depth += 1;
  }
}

impl<H> HasMaxDepth for UncompressMocIter<'_, H>
where
  H: HpxHash,
{
  fn depth_max(&self) -> u8 {
    self.depth_max
  }
}

impl<H: HpxHash> Iterator for UncompressMocIter<'_, H> {
  type Item = HpxCell<H>;

  fn next(&mut self) -> Option<Self::Item> {
    let three = H::one() | (H::one() << 1);
    let twelve = three << 2_usize;
    while self.depth != 0 || self.hash != twelve {
      if self.get() {
        // bit = 1
        if self.depth == self.depth_max || self.get() {
          let res = Some(self.get_current_cell());
          self.increment();
          return res;
        } else {
          self.increment();
        }
      } else if self.depth == self.depth_max {
        // bit == 0
        self.increment();
      } else {
        // bit == 0
        debug_assert!(self.depth < self.depth_max);
        self.go_down_of_1_depth();
      }
    }
    None
  }

  fn size_hint(&self) -> (usize, Option<usize>) {
    let n_remains = 8 * (self.cmoc.len() - self.ibyte) - (self.ibit as usize);
    (0, Some(n_remains))
  }
}