moc/moc/
cell.rs

1use std::marker::PhantomData;
2use std::slice;
3use std::vec::IntoIter;
4
5use crate::elem::cell::Cell;
6use crate::elemset::cell::MocCells;
7use crate::idx::Idx;
8use crate::moc::{
9  CellMOCIntoIterator, CellMOCIterator, HasMaxDepth, MOCProperties, NonOverlapping, ZSorted,
10};
11use crate::qty::MocQty;
12
13/// A MOC made of (ordered and non-overlaping) cells.
14/// This is used as the result of a MOC JSON deserialization of a MOC.
15#[derive(Debug)]
16pub struct CellMOC<T: Idx, Q: MocQty<T>> {
17  depth_max: u8,
18  cells: MocCells<T, Q>,
19}
20impl<T: Idx, Q: MocQty<T>> CellMOC<T, Q> {
21  pub fn new(depth_max: u8, cells: MocCells<T, Q>) -> Self {
22    Self { depth_max, cells }
23  }
24  pub fn len(&self) -> usize {
25    self.cells.cells().len()
26  }
27  pub fn is_empty(&self) -> bool {
28    self.len() == 0
29  }
30}
31impl<T: Idx, Q: MocQty<T>> HasMaxDepth for CellMOC<T, Q> {
32  fn depth_max(&self) -> u8 {
33    self.depth_max
34  }
35}
36impl<T: Idx, Q: MocQty<T>> ZSorted for CellMOC<T, Q> {}
37impl<T: Idx, Q: MocQty<T>> NonOverlapping for CellMOC<T, Q> {}
38impl<T: Idx, Q: MocQty<T>> MOCProperties for CellMOC<T, Q> {}
39
40/// Iterator taking the ownership of the `CellMOC` it iterates over.
41pub struct CellMocIter<T: Idx, Q: MocQty<T>> {
42  depth_max: u8,
43  last: Option<Cell<T>>,
44  iter: IntoIter<Cell<T>>,
45  _qty: PhantomData<Q>,
46}
47
48impl<T: Idx, Q: MocQty<T>> HasMaxDepth for CellMocIter<T, Q> {
49  fn depth_max(&self) -> u8 {
50    self.depth_max
51  }
52}
53impl<T: Idx, Q: MocQty<T>> ZSorted for CellMocIter<T, Q> {}
54impl<T: Idx, Q: MocQty<T>> NonOverlapping for CellMocIter<T, Q> {}
55impl<T: Idx, Q: MocQty<T>> MOCProperties for CellMocIter<T, Q> {}
56impl<T: Idx, Q: MocQty<T>> Iterator for CellMocIter<T, Q> {
57  type Item = Cell<T>;
58  fn next(&mut self) -> Option<Self::Item> {
59    self.iter.next()
60  }
61}
62impl<T: Idx, Q: MocQty<T>> CellMOCIterator<T> for CellMocIter<T, Q> {
63  type Qty = Q;
64
65  fn peek_last(&self) -> Option<&Cell<T>> {
66    self.last.as_ref()
67  }
68}
69impl<T: Idx, Q: MocQty<T>> CellMOCIntoIterator<T> for CellMOC<T, Q> {
70  type Qty = Q;
71  type IntoCellMOCIter = CellMocIter<T, Self::Qty>;
72
73  fn into_cell_moc_iter(self) -> Self::IntoCellMOCIter {
74    let l = self.cells.0 .0.len();
75    let last: Option<Cell<T>> = if l > 0 {
76      Some(self.cells.0 .0[l - 1])
77    } else {
78      None
79    };
80    CellMocIter {
81      depth_max: self.depth_max,
82      last,
83      iter: self.cells.0 .0.into_vec().into_iter(),
84      _qty: PhantomData,
85    }
86  }
87}
88
89/// Iterator borrowing the `CellMOC` it iterates over.
90pub struct CellRefMocIter<'a, T: Idx, Q: MocQty<T>> {
91  depth_max: u8,
92  last: Option<Cell<T>>,
93  iter: slice::Iter<'a, Cell<T>>,
94  _qty: PhantomData<Q>,
95}
96impl<'a, T: Idx, Q: MocQty<T>> HasMaxDepth for CellRefMocIter<'a, T, Q> {
97  fn depth_max(&self) -> u8 {
98    self.depth_max
99  }
100}
101impl<'a, T: Idx, Q: MocQty<T>> ZSorted for CellRefMocIter<'a, T, Q> {}
102impl<'a, T: Idx, Q: MocQty<T>> NonOverlapping for CellRefMocIter<'a, T, Q> {}
103impl<'a, T: Idx, Q: MocQty<T>> MOCProperties for CellRefMocIter<'a, T, Q> {}
104impl<'a, T: Idx, Q: MocQty<T>> Iterator for CellRefMocIter<'a, T, Q> {
105  type Item = Cell<T>;
106  fn next(&mut self) -> Option<Self::Item> {
107    self.iter.next().cloned()
108  }
109}
110impl<'a, T: Idx, Q: MocQty<T>> CellMOCIterator<T> for CellRefMocIter<'a, T, Q> {
111  type Qty = Q;
112
113  fn peek_last(&self) -> Option<&Cell<T>> {
114    self.last.as_ref()
115  }
116}
117impl<'a, T: Idx, Q: MocQty<T>> CellMOCIntoIterator<T> for &'a CellMOC<T, Q> {
118  type Qty = Q;
119  type IntoCellMOCIter = CellRefMocIter<'a, T, Self::Qty>;
120
121  fn into_cell_moc_iter(self) -> Self::IntoCellMOCIter {
122    let l = self.cells.0 .0.len();
123    let last: Option<Cell<T>> = if l > 0 {
124      Some(self.cells.0 .0[l - 1])
125    } else {
126      None
127    };
128    CellRefMocIter {
129      depth_max: self.depth_max,
130      last,
131      iter: self.cells.0 .0.iter(),
132      _qty: PhantomData,
133    }
134  }
135}
136
137/*
138/// Iterator borrowing the `CellMOC` it iterates over.
139pub struct CellMocIterGen<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> {
140  depth_max: u8,
141  iter: I,
142  _qty: PhantomData<Q>,
143}
144impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> CellMocIterGen<T, Q, I> {
145  /// WARNING: you must be sure than the order in the input iterator is correct,
146  /// and that it does not contains duplicates, ...
147  pub fn new_unsafe(depth_max: u8, iter: I) -> Self {
148    CellMocIterGen {
149      depth_max,
150      iter,
151      _qty: PhantomData
152    }
153  }
154}
155impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> HasMaxDepth for CellMocIterGen<T, Q, I> {
156  fn depth_max(&self) -> u8 {
157    self.depth_max
158  }
159}
160impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> ZSorted for CellMocIterGen<T, Q, I> { }
161impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> NonOverlapping for CellMocIterGen<T, Q, I> { }
162impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> MOCProperties for CellMocIterGen<T, Q, I> { }
163impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> Iterator for CellMocIterGen<T, Q, I> {
164  type Item = Cell<T>;
165  fn next(&mut self) -> Option<Self::Item> {
166    self.iter.next()
167  }
168}
169impl<T: Idx, Q: MocQty<T>, I: Iterator<Item=Cell<T>>> CellMOCIterator<T> for CellMocIterGen<T, Q, I> {
170  type Qty = Q;
171
172  fn peek_last(&self) -> Option<&Cell<T>> {
173    None
174  }
175}
176*/