moc/moc/range/op/
convert.rs

1use std::{marker::PhantomData, ops::Range};
2
3use crate::{
4  idx::Idx,
5  moc::{HasMaxDepth, MOCProperties, NonOverlapping, RangeMOCIterator, ZSorted},
6  qty::MocQty,
7};
8
9/// Decorator that converts from one datatype to another datatype.
10/*pub fn convert_to_u64<T, Q, I, R>(it: I) -> ConvertIterator<T, Q, I, u64, R>
11  where
12    T: Idx,
13    Q: MocQty<T>,
14    I: RangeMOCIterator<T, Qty=Q>,
15    R: MocQty<u64>,
16{
17  ConvertIterator::<T, Q, I, u64, R>::new(it)
18}*/
19
20/// Decorator that converts from one datatype to another datatype.
21pub fn convert<T, Q, I, U, R>(it: I) -> ConvertIterator<T, Q, I, U, R>
22where
23  T: Idx,
24  Q: MocQty<T>,
25  I: RangeMOCIterator<T, Qty = Q>,
26  U: Idx + From<T>,
27  R: MocQty<U>,
28{
29  ConvertIterator::new(it)
30}
31
32/// Decorator that converts from one datatype to another datatype.
33pub struct ConvertIterator<T, Q, I, U, R>
34where
35  T: Idx,
36  Q: MocQty<T>,
37  I: RangeMOCIterator<T, Qty = Q>,
38  U: Idx + From<T>,
39  R: MocQty<U>,
40{
41  last: Option<Range<U>>,
42  it: I,
43  _t_type: PhantomData<T>,
44  _q_type: PhantomData<Q>,
45  _r_type: PhantomData<R>,
46}
47
48impl<T, Q, I, U, R> ConvertIterator<T, Q, I, U, R>
49where
50  T: Idx,
51  Q: MocQty<T>,
52  I: RangeMOCIterator<T, Qty = Q>,
53  U: Idx + From<T>,
54  R: MocQty<U>,
55{
56  pub fn new(it: I) -> ConvertIterator<T, Q, I, U, R> {
57    let last = it.peek_last().map(|r| r.start.convert()..r.end.convert());
58    ConvertIterator {
59      last,
60      it,
61      _t_type: PhantomData,
62      _q_type: PhantomData,
63      _r_type: PhantomData,
64    }
65  }
66}
67
68impl<T, Q, I, U, R> HasMaxDepth for ConvertIterator<T, Q, I, U, R>
69where
70  T: Idx,
71  Q: MocQty<T>,
72  I: RangeMOCIterator<T, Qty = Q>,
73  U: Idx + From<T>,
74  R: MocQty<U>,
75{
76  fn depth_max(&self) -> u8 {
77    self.it.depth_max().min(R::MAX_DEPTH)
78  }
79}
80impl<T, Q, I, U, R> ZSorted for ConvertIterator<T, Q, I, U, R>
81where
82  T: Idx,
83  Q: MocQty<T>,
84  I: RangeMOCIterator<T, Qty = Q>,
85  U: Idx + From<T>,
86  R: MocQty<U>,
87{
88}
89impl<T, Q, I, U, R> NonOverlapping for ConvertIterator<T, Q, I, U, R>
90where
91  T: Idx,
92  Q: MocQty<T>,
93  I: RangeMOCIterator<T, Qty = Q>,
94  U: Idx + From<T>,
95  R: MocQty<U>,
96{
97}
98impl<T, Q, I, U, R> MOCProperties for ConvertIterator<T, Q, I, U, R>
99where
100  T: Idx,
101  Q: MocQty<T>,
102  I: RangeMOCIterator<T, Qty = Q>,
103  U: Idx + From<T>,
104  R: MocQty<U>,
105{
106}
107impl<T, Q, I, U, R> Iterator for ConvertIterator<T, Q, I, U, R>
108where
109  T: Idx,
110  Q: MocQty<T>,
111  I: RangeMOCIterator<T, Qty = Q>,
112  U: Idx + From<T>,
113  R: MocQty<U>,
114{
115  type Item = Range<U>;
116
117  fn next(&mut self) -> Option<Self::Item> {
118    self.it.next().map(|r| r.start.convert()..r.end.convert())
119  }
120
121  fn size_hint(&self) -> (usize, Option<usize>) {
122    self.it.size_hint()
123  }
124}
125
126impl<T, Q, I, U, R> RangeMOCIterator<U> for ConvertIterator<T, Q, I, U, R>
127where
128  T: Idx,
129  Q: MocQty<T>,
130  I: RangeMOCIterator<T, Qty = Q>,
131  U: Idx + From<T>,
132  R: MocQty<U>,
133{
134  type Qty = R;
135
136  fn peek_last(&self) -> Option<&Range<U>> {
137    self.last.as_ref()
138  }
139}
140
141/// Decorator that converts from u64 to another datatype.
142/// WARNING: to be called only on MOC having a depth smaller than Q::MAX_DEPTH,
143/// **YOU** have to degrade first!!
144pub fn convert_from_u64<Q1, T, Q, I>(it: I) -> ConvertFromU64Iterator<Q1, T, Q, I>
145where
146  Q1: MocQty<u64>,
147  T: Idx,
148  Q: MocQty<T>,
149  I: RangeMOCIterator<u64, Qty = Q1>,
150{
151  ConvertFromU64Iterator::new(it)
152}
153
154/// Decorator that converts from one datatype to another datatype.
155pub struct ConvertFromU64Iterator<Q1, T, Q, I>
156where
157  Q1: MocQty<u64>,
158  T: Idx,
159  Q: MocQty<T>,
160  I: RangeMOCIterator<u64, Qty = Q1>,
161{
162  last: Option<Range<T>>,
163  it: I,
164  _t_type: PhantomData<T>,
165  _q_type: PhantomData<Q>,
166  _q1_type: PhantomData<Q1>,
167}
168
169impl<Q1, T, Q, I> ConvertFromU64Iterator<Q1, T, Q, I>
170where
171  Q1: MocQty<u64>,
172  T: Idx,
173  Q: MocQty<T>,
174  I: RangeMOCIterator<u64, Qty = Q1>,
175{
176  pub fn new(it: I) -> ConvertFromU64Iterator<Q1, T, Q, I> {
177    let last = it
178      .peek_last()
179      .map(|r| T::from_u64_idx(r.start)..T::from_u64_idx(r.end));
180    ConvertFromU64Iterator {
181      last,
182      it,
183      _t_type: PhantomData,
184      _q_type: PhantomData,
185      _q1_type: PhantomData,
186    }
187  }
188}
189
190impl<Q1, T, Q, I> HasMaxDepth for ConvertFromU64Iterator<Q1, T, Q, I>
191where
192  Q1: MocQty<u64>,
193  T: Idx,
194  Q: MocQty<T>,
195  I: RangeMOCIterator<u64, Qty = Q1>,
196{
197  fn depth_max(&self) -> u8 {
198    self.it.depth_max().min(Q::MAX_DEPTH)
199  }
200}
201impl<Q1, T, Q, I> ZSorted for ConvertFromU64Iterator<Q1, T, Q, I>
202where
203  Q1: MocQty<u64>,
204  T: Idx,
205  Q: MocQty<T>,
206  I: RangeMOCIterator<u64, Qty = Q1>,
207{
208}
209impl<Q1, T, Q, I> NonOverlapping for ConvertFromU64Iterator<Q1, T, Q, I>
210where
211  Q1: MocQty<u64>,
212  T: Idx,
213  Q: MocQty<T>,
214  I: RangeMOCIterator<u64, Qty = Q1>,
215{
216}
217impl<Q1, T, Q, I> MOCProperties for ConvertFromU64Iterator<Q1, T, Q, I>
218where
219  Q1: MocQty<u64>,
220  T: Idx,
221  Q: MocQty<T>,
222  I: RangeMOCIterator<u64, Qty = Q1>,
223{
224}
225impl<Q1, T, Q, I> Iterator for ConvertFromU64Iterator<Q1, T, Q, I>
226where
227  Q1: MocQty<u64>,
228  T: Idx,
229  Q: MocQty<T>,
230  I: RangeMOCIterator<u64, Qty = Q1>,
231{
232  type Item = Range<T>;
233
234  fn next(&mut self) -> Option<Self::Item> {
235    self
236      .it
237      .next()
238      .map(|r| T::from_u64_idx(r.start)..T::from_u64_idx(r.end))
239  }
240
241  fn size_hint(&self) -> (usize, Option<usize>) {
242    self.it.size_hint()
243  }
244}
245
246impl<Q1, T, Q, I> RangeMOCIterator<T> for ConvertFromU64Iterator<Q1, T, Q, I>
247where
248  Q1: MocQty<u64>,
249  T: Idx,
250  Q: MocQty<T>,
251  I: RangeMOCIterator<u64, Qty = Q1>,
252{
253  type Qty = Q;
254
255  fn peek_last(&self) -> Option<&Range<T>> {
256    self.last.as_ref()
257  }
258}
259
260/// Decorator that converts from u64 to another datatype.
261pub fn convert_to_u64<T, Q, I, R>(it: I) -> ConvertToU64Iterator<T, Q, I, R>
262where
263  T: Idx,
264  Q: MocQty<T>,
265  I: RangeMOCIterator<T, Qty = Q>,
266  R: MocQty<u64>,
267{
268  ConvertToU64Iterator::new(it)
269}
270
271/// Decorator that converts from one datatype to another datatype.
272pub struct ConvertToU64Iterator<T, Q, I, R>
273where
274  T: Idx,
275  Q: MocQty<T>,
276  I: RangeMOCIterator<T, Qty = Q>,
277  R: MocQty<u64>,
278{
279  last: Option<Range<u64>>,
280  it: I,
281  _t_type: PhantomData<T>,
282  _q_type: PhantomData<Q>,
283  _r_type: PhantomData<R>,
284}
285
286impl<T, Q, I, R> ConvertToU64Iterator<T, Q, I, R>
287where
288  T: Idx,
289  Q: MocQty<T>,
290  I: RangeMOCIterator<T, Qty = Q>,
291  R: MocQty<u64>,
292{
293  pub fn new(it: I) -> ConvertToU64Iterator<T, Q, I, R> {
294    let last = it
295      .peek_last()
296      .map(|r| r.start.to_u64_idx()..r.end.to_u64_idx());
297    ConvertToU64Iterator {
298      last,
299      it,
300      _t_type: PhantomData,
301      _q_type: PhantomData,
302      _r_type: PhantomData,
303    }
304  }
305}
306
307impl<T, Q, I, R> HasMaxDepth for ConvertToU64Iterator<T, Q, I, R>
308where
309  T: Idx,
310  Q: MocQty<T>,
311  I: RangeMOCIterator<T, Qty = Q>,
312  R: MocQty<u64>,
313{
314  fn depth_max(&self) -> u8 {
315    self.it.depth_max()
316  }
317}
318impl<T, Q, I, R> ZSorted for ConvertToU64Iterator<T, Q, I, R>
319where
320  T: Idx,
321  Q: MocQty<T>,
322  I: RangeMOCIterator<T, Qty = Q>,
323  R: MocQty<u64>,
324{
325}
326impl<T, Q, I, R> NonOverlapping for ConvertToU64Iterator<T, Q, I, R>
327where
328  T: Idx,
329  Q: MocQty<T>,
330  I: RangeMOCIterator<T, Qty = Q>,
331  R: MocQty<u64>,
332{
333}
334impl<T, Q, I, R> MOCProperties for ConvertToU64Iterator<T, Q, I, R>
335where
336  T: Idx,
337  Q: MocQty<T>,
338  I: RangeMOCIterator<T, Qty = Q>,
339  R: MocQty<u64>,
340{
341}
342impl<T, Q, I, R> Iterator for ConvertToU64Iterator<T, Q, I, R>
343where
344  T: Idx,
345  Q: MocQty<T>,
346  I: RangeMOCIterator<T, Qty = Q>,
347  R: MocQty<u64>,
348{
349  type Item = Range<u64>;
350
351  fn next(&mut self) -> Option<Self::Item> {
352    self
353      .it
354      .next()
355      .map(|r| r.start.to_u64_idx()..r.end.to_u64_idx())
356  }
357
358  fn size_hint(&self) -> (usize, Option<usize>) {
359    self.it.size_hint()
360  }
361}
362
363impl<T, Q, I, R> RangeMOCIterator<u64> for ConvertToU64Iterator<T, Q, I, R>
364where
365  T: Idx,
366  Q: MocQty<T>,
367  I: RangeMOCIterator<T, Qty = Q>,
368  R: MocQty<u64>,
369{
370  type Qty = R;
371
372  fn peek_last(&self) -> Option<&Range<u64>> {
373    self.last.as_ref()
374  }
375}