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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! A MOC is a set of ordered, non-overlapping MOC elements, associated to a maximum depth.

use std::io::Write;
use std::ops::Range;
use std::marker::PhantomData;

use crate::deser;
use crate::idx::Idx;
use crate::qty::{MocQty, Bounded};
use crate::elem::{
  cell::Cell,
  cellcellrange::CellOrCellRange,
  range::MocRange,
};
use crate::moc::adapters::{
  RangeMOCIteratorFromCells,
  RangeMOCIteratorFromCellOrCellRanges,
  CellMOCIteratorFromRanges,
  CellOrCellRangeMOCIteratorFromCells
};
use crate::moc::range::RangeMOC;
use crate::moc::range::op::{
  check::{check, CheckedIterator},
  convert::{convert, ConvertIterator},
  degrade::{degrade, DegradeRangeIter},
  not::{not, NotRangeIter},
  and::{and, AndRangeIter},
  or::{or, OrRangeIter},
  xor::{xor, XorRangeIter},
  minus::{minus, MinusRangeIter},
};
use healpix::nested::bmoc::{BMOC, BMOCBuilderUnsafe};
use crate::deser::fits::{keywords, ranges_to_fits_ivoa};
use crate::deser::fits::error::FitsError;

pub mod range;
pub mod cell;
pub mod cellcellrange;
pub mod adapters;
pub mod builder;


/// Returns the maximum depth of an item the implementor contains.
pub trait HasMaxDepth {
  fn depth_max(&self) -> u8;
}

/// Marker trait telling that ranges lower bound or cells or indices in the implementor are sorted
/// according to the natural Z-order curve order (no hierarchical order => cells of various depth
/// are mixed).
pub trait ZSorted {}

/// Marker trait telling that something the implementor contains non-overlapping items.
pub trait NonOverlapping {}

/// Commodity trait containing all MOC properties
pub trait MOCProperties: HasMaxDepth + ZSorted + NonOverlapping { }

////////////////////////////
// Encours d'elobaration
/*pub trait MOC: MOCProperties {
  type Idx: Idx;
  type Qty: MocQty<T>;
  type RangeIt: Iterator<Item=Range<Self::Idx>>;
  /// If we need to reuse the MOC, two solutions: clone or implement MOC on &Self.
  /// Then use Cow<MOC> in operations (if needed).
  fn into_iter(self) -> RangeIt;
}
pub trait MOCIterator<T: Idx>: Iterator<Item=Range<Idx>> {
  type Qty: MocQty<T>;
}

struct SweepLine {
  left_it
  right_it
}
Iterator<Item=Some(enum(Left(Ragne), Right(Range), Common(Range)))>.. ?*/
/////////////////////////////

/// Iterator over MOC cells
pub trait CellMOCIterator<T: Idx>: Sized + MOCProperties + Iterator<Item=Cell<T>> {
  type Qty: MocQty<T>;

  /// If available, returns the upper cellthe iterator will return, without consuming
  /// the iterator.
  /// This information is available e.g. for an Iterator from a Vector, but may not be available
  /// for an Iterator coming from a stream.
  /// If available, this information can be used for fast rejection tests.
  fn peek_last(&self) -> Option<&Cell<T>>;
  
  fn cellranges(self) -> CellOrCellRangeMOCIteratorFromCells<T, Self::Qty, Self> {
    CellOrCellRangeMOCIteratorFromCells::new(self)
  }
  fn ranges(self) -> RangeMOCIteratorFromCells<T, Self::Qty, Self> {
    let last: Option<Range<T>> =  self.peek_last().map(|cell| MocRange::<T, Self::Qty>::from(cell).0);
    RangeMOCIteratorFromCells::new(self, last)
  }
  fn to_json_aladin<W: Write>(self, fold: Option<usize>, writer: W) -> std::io::Result<()> {
    deser::json::to_json_aladin(self, &fold, "", writer)
  }

}

pub trait IntoBMOC<T: Idx + Into<u64>>: CellMOCIterator<T> {
  fn into_bmoc(self) -> BMOC {
    let (_, upper_bound) = self.size_hint();
    let mut builder = BMOCBuilderUnsafe::new(self.depth_max(), upper_bound.unwrap_or(1000));
    for Cell{depth, idx} in self {
      builder.push(depth, idx.into(), true);
    }
    builder.to_bmoc()
  }
}
impl<T: Idx + Into<u64>, U: CellMOCIterator<T>> IntoBMOC<T> for U {}



/// Transform an object into an iterator over MOC cells.
pub trait CellMOCIntoIterator<T: Idx>: Sized {
  type Qty: MocQty<T>;
  type IntoCellMOCIter: CellMOCIterator<T, Qty=Self::Qty>;

  fn into_cell_moc_iter(self) -> Self::IntoCellMOCIter;
}


pub trait CellOrCellRangeMOCIterator<T: Idx>: Sized + MOCProperties + Iterator<Item=CellOrCellRange<T>> {
  type Qty: MocQty<T>;

  /// If available, returns the upper cell or cell range the iterator will return,
  /// without consuming the iterator.
  /// This information is available e.g. for an Iterator from a Vector, but may not be available
  /// for an Iterator coming from a stream.
  /// If available, this information can be used for fast rejection tests.
  fn peek_last(&self) -> Option<&CellOrCellRange<T>>;
  
  /// # WARNING
  /// - `use_offset=true` is not compatible with the current IVOA standard!
  fn to_ascii_ivoa<W: Write>(self, fold: Option<usize>, use_offset: bool, writer: W) -> std::io::Result<()> {
    deser::ascii::to_ascii_ivoa(self, &fold, use_offset, writer)
  }
  /// # WARNING
  /// - this is not compatible with the current IVOA standard!
  fn to_ascii_stream<W: Write>(self, use_offset: bool, writer: W) -> std::io::Result<()> {
    deser::ascii::to_ascii_stream(self, use_offset, writer)
  }

  fn ranges(self) -> RangeMOCIteratorFromCellOrCellRanges<T, Self::Qty, Self> {
    let last: Option<Range<T>> =  self.peek_last().map(|ccr| MocRange::<T, Self::Qty>::from(ccr).0);
    RangeMOCIteratorFromCellOrCellRanges::new(self, last)
  }
}
pub trait CellOrCellRangeMOCIntoIterator<T: Idx>: Sized {
  type Qty: MocQty<T>;
  type IntoCellOrCellRangeMOCIter: CellOrCellRangeMOCIterator<T, Qty=Self::Qty>;

  fn into_cellcellrange_moc_iter(self) -> Self::IntoCellOrCellRangeMOCIter;
}



// Convert Cell --> Ranges
// TODO: merge (Cell --> Ranges) and (CellOrCellRange --> Ranges) in a single obj using Into<Range> ?


// RangeMOCIterator<T, Q, B>: Sized + MOCProperties + Iterator<Item=B>
// with B: Borrow<MocRange<T, Q>>
// Don't do this, we will clone as iterating if necessary


pub trait RangeMOCIterator<T: Idx>: Sized + MOCProperties + Iterator<Item=Range<T>> {
  type Qty: MocQty<T>;

  /// If available, returns the last range of the Iterator (or at least a range having the last
  /// range upper bound), without consuming the iterator.
  /// This information is available e.g. for an Iterator from a Vector, but may not be available
  /// for an Iterator coming from a stream.
  /// If available, this information can be used for fast rejection tests.
  fn peek_last(&self) -> Option<&Range<T>>;

  /// For debug purpose: transform this iterator into an iterator that panics if the iterator
  /// is not made of ordered and non-overlapping ranges.
  fn into_checked(self) -> CheckedIterator<T, Self::Qty, Self> {
    check(self)
  }

  /// For debug purpose: check that this iterator is made of sorted and non-overlapping ranges.
  /// This method is to be used on `(&RangeMOC).into_range_moc_iter()`
  fn check(self) {
    for _ in self.into_checked() { }
  }

  fn to_fits_ivoa<W: Write>(
    self, 
    moc_id: Option<String>, 
    moc_type: Option<keywords::MocType>, 
    writer: W
  ) -> Result<(), FitsError> {
    ranges_to_fits_ivoa(self, moc_id, moc_type, writer) 
  }
  
  fn into_range_moc(self) -> RangeMOC<T, Self::Qty> {
    RangeMOC::new(self.depth_max(), self.collect())
  }

  fn range_sum(self) -> T {
    let mut sum = T::zero();
    for Range { start, end } in self {
      sum += end - start;
    }
    sum
  }

  fn coverage_percentage(self) -> f64 {
    let rsum = self.range_sum();
    let tot = Self::Qty::upper_bound_exclusive();
    if T::N_BITS > 52 { // 52 = n mantissa bits in a f64
      // Divide by the same power of 2, dropping the LSBs
      let shift = (T::N_BITS - 52) as u32;
      rsum.unsigned_shr(shift);
      tot.unsigned_shr(shift);
    }
    rsum.cast_to_f64() / tot.cast_to_f64()
  }

  // I have not yet found a way to ensure that the quantity is the same, with only the a different
  // type T --> U
  fn convert<U, R>(self) -> ConvertIterator<T, Self::Qty, Self, U, R>
    where
        U: Idx + From<T>,
        R: MocQty<U>,
  {
    convert(self)
  }

  fn cells(self) -> CellMOCIteratorFromRanges<T, Self::Qty, Self> {
    CellMOCIteratorFromRanges::new(self)
  }

  fn degrade(self, new_depth: u8) -> DegradeRangeIter<T, Self::Qty, Self> {
    degrade(self, new_depth)
  }

  fn not(self) -> NotRangeIter<T, Self::Qty, Self> {
    not(self)
  }

  fn and<I>(self, other: I) -> AndRangeIter<T, Self::Qty, Self, I>
    where
      I: RangeMOCIterator<T, Qty=Self::Qty>
  {
    and(self, other)
  }

  fn or<I>(self, other: I) -> OrRangeIter<T, Self::Qty, Self, I>
    where
        I: RangeMOCIterator<T, Qty=Self::Qty>
  {
    or(self, other)
  }

  fn xor<I>(self, other: I) -> XorRangeIter<T, Self::Qty, Self, I>
    where
        I: RangeMOCIterator<T, Qty=Self::Qty>
  {
    xor(self, other)
  }

  fn minus<I>(self, other: I) -> MinusRangeIter<T, Self::Qty, Self, I>
    where
        I: RangeMOCIterator<T, Qty=Self::Qty>
  {
    minus(self, other)
  }

}

pub trait RangeMOCIntoIterator<T: Idx>: Sized {
  type Qty: MocQty<T>;
  type IntoRangeMOCIter: RangeMOCIterator<T, Qty=Self::Qty>;

  fn into_range_moc_iter(self) -> Self::IntoRangeMOCIter;
}

/// Defines an empty `RangeMOCIterator<`
pub struct EmptyRangeMOCIterator<T: Idx, Q: MocQty<T>>(u8, PhantomData<T>, PhantomData<Q>);
impl<T: Idx, Q: MocQty<T>> EmptyRangeMOCIterator<T, Q> {
  pub fn new(depth: u8) -> Self {
    EmptyRangeMOCIterator(depth, PhantomData, PhantomData)
  }
}

impl<T: Idx, Q: MocQty<T>> HasMaxDepth for EmptyRangeMOCIterator<T, Q> {
  fn depth_max(&self) -> u8 {
    self.0
  }
}
impl<T: Idx, Q: MocQty<T>> ZSorted for EmptyRangeMOCIterator<T, Q> { }
impl<T: Idx, Q: MocQty<T>> NonOverlapping for EmptyRangeMOCIterator<T, Q> { }
impl<T: Idx, Q: MocQty<T>> MOCProperties for EmptyRangeMOCIterator<T, Q> { }
impl<T: Idx, Q: MocQty<T>> Iterator for EmptyRangeMOCIterator<T, Q> {
  type Item = Range<T>;
  fn next(&mut self) -> Option<Self::Item> {
    None
  }
}
impl<T: Idx, Q: MocQty<T>> RangeMOCIterator<T> for EmptyRangeMOCIterator<T, Q> {
  type Qty = Q;
  fn peek_last(&self) -> Option<&Range<T>> { None }
}


// NUniq MOC
pub struct NUniqMOC<T: Idx> {
  pub depth_max: u8,
  pub zsorted_nuniq: Vec<T>
}
impl<T: Idx> NUniqMOC<T> {
  pub fn new(depth_max: u8, zsorted_nuniq: Vec<T>) -> Self {
    Self { depth_max, zsorted_nuniq}
  }
}


#[cfg(test)]
mod tests {

  use std::ops::Range;
  use std::cmp::Ordering;

  use crate::qty::Hpx;
  use crate::elem::{
    cell::Cell,
    cellrange::CellRange,
    cellcellrange::CellOrCellRange
  };
  use crate::elemset::range::{
    MocRanges,
    hpx::HpxUniq2DepthIdxIter
  };
  use crate::moc::{
    RangeMOCIterator, RangeMOCIntoIterator,
    CellMOCIterator, CellOrCellRangeMOCIterator,
    range::RangeMOC
  };

  #[test]
  fn test_range2cells_1() {
    let ranges = MocRanges::<u64, Hpx<u64>>::new_unchecked(vec![0..5]);
    let rm = RangeMOC::new(29, ranges);
    let rit = (&rm).into_range_moc_iter();
    let v1: Vec<Cell<u64>> = rit.cells().collect();
    assert_eq!(v1, vec![
      Cell::new(28, 0),
      Cell::new(29, 4)
    ]);

    let v2: Vec<(i8, u64)> = HpxUniq2DepthIdxIter::new(rm.into_moc_ranges()).collect();
    assert_eq!(v2, vec![
      (28, 0),
      (29, 4)
    ]);
  }

  #[test]
  fn test_range2cells_2() {
    let ranges: Vec<Range<u64>> = vec![2..8];
    let ranges = MocRanges::<u64, Hpx<u64>>::new_unchecked(ranges);
    let rm = RangeMOC::new(29, ranges);
    let rit = (&rm).into_range_moc_iter();
    let v1: Vec<Cell<u64>> = rit.cells().collect();
    assert_eq!(v1, vec![
      Cell::new(29, 2),
      Cell::new(29, 3),
      Cell::new(28, 1)
    ]);

    let v2: Vec<(i8, u64)> = HpxUniq2DepthIdxIter::new(rm.into_moc_ranges()).collect();
    assert_eq!(v2, vec![
      (28, 1),
      (29, 2),
      (29, 3)
    ]);
  }

  #[test]
  fn test_range2cells_3() {
    let ranges = MocRanges::<u64, Hpx<u64>>::new_unchecked(vec![
      0..5,
      6..59,
      78..6953,
      12458..55587,
      55787..65587
    ]);
    let rm = RangeMOC::new(29, ranges);
    let rit = (&rm).into_range_moc_iter();
    let mut v1: Vec<Cell<u64>> = rit.cells().collect();
    // println!("{:?}", v1);
    v1.sort_by(
      |a, b| match a.depth.cmp(&b.depth) {
        Ordering::Less => Ordering::Less,
        Ordering::Greater => Ordering::Greater,
        Ordering::Equal => a.idx.cmp(&b.idx)
      });

    let v2: Vec<(i8, u64)> = HpxUniq2DepthIdxIter::new(rm.into_moc_ranges()).collect();
    // println!("{:?}", v2);
    assert_eq!(v1.len(), v2.len());
    for (Cell{depth, idx}, (depth2, idx2)) in v1.into_iter().zip(v2.into_iter())  {
      assert_eq!(depth, depth2 as u8);
      assert_eq!(idx, idx2);
    }
  }

  #[test]
  fn test_range2cellrange() {
    let ranges: Vec<Range<u64>> = vec![2..8];
    let ranges = MocRanges::<u64, Hpx<u64>>::new_unchecked(ranges);
    let rm = RangeMOC::new(29, ranges);
    let res: Vec<CellOrCellRange<u64>> = (&rm).into_range_moc_iter().cells().cellranges().collect();
    assert_eq!(res, vec![
      CellOrCellRange::CellRange(CellRange::new(29, 2, 4)),
      CellOrCellRange::Cell(Cell::new(28, 1)),
    ]);

  }

  #[test]
  fn test_to_ascii() {
    let ranges = MocRanges::<u64, Hpx<u64>>::new_unchecked(vec![
      0..5,
      6..59,
      78..6953,
      12458..55587,
      55787..65587
    ]);
    let rm = RangeMOC::new(29, ranges);
    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_ivoa(Some(80), false, &mut sink)
      .unwrap();
    // println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_ivoa(Some(80), true, &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_ivoa(None, false, &mut sink)
      .unwrap();
    println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_ivoa(None, true, &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_stream(false, &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .cellranges()
      .to_ascii_stream(true, &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));
  }

  #[test]
  fn test_to_json() {
    let ranges =MocRanges::<u64, Hpx<u64>>::new_unchecked(vec![
      0..5,
      6..59,
      78..6953,
      12458..55587,
      55787..65587
    ]);
    let rm = RangeMOC::new(29, ranges);
    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .to_json_aladin(Some(40), &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));

    let mut sink = Vec::new();
    (&rm).into_range_moc_iter()
      .cells()
      .to_json_aladin(None, &mut sink)
      .unwrap();
    //  println!("{}\n", &String::from_utf8_lossy(&sink));
  }

}