1use crate::iterators::bit_distributor::{BitDistributor, BitDistributorOutputType};
10use crate::num::arithmetic::traits::{DivMod, DivisibleBy};
11use crate::num::basic::unsigneds::PrimitiveUnsigned;
12use crate::num::conversion::traits::{ExactFrom, WrappingFrom};
13use core::cmp::Ordering::*;
14use core::marker::PhantomData;
15
16#[doc(hidden)]
17#[derive(Clone, Debug)]
18pub struct SameWidthIteratorToBitChunks<
19 I: Iterator<Item = T>,
20 T: PrimitiveUnsigned,
21 U: PrimitiveUnsigned,
22> {
23 xs: I,
24 width: u64,
25 phantom: PhantomData<*const U>,
26}
27
28impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>
29 SameWidthIteratorToBitChunks<I, T, U>
30{
31 fn next_with_wrapping<F: Fn(T) -> U>(&mut self, wrap: F) -> Option<Option<U>> {
32 self.xs.next().map(|x| {
33 if x.significant_bits() <= self.width {
34 Some(wrap(x))
35 } else {
36 None
37 }
38 })
39 }
40}
41
42const fn same_width_iterator_to_bit_chunks<
43 I: Iterator<Item = T>,
44 T: PrimitiveUnsigned,
45 U: PrimitiveUnsigned,
46>(
47 xs: I,
48 width: u64,
49) -> SameWidthIteratorToBitChunks<I, T, U> {
50 SameWidthIteratorToBitChunks {
51 xs,
52 width,
53 phantom: PhantomData,
54 }
55}
56
57#[doc(hidden)]
58#[derive(Clone, Debug)]
59pub struct EvenFractionIteratorToBitChunks<
60 I: Iterator<Item = T>,
61 T: PrimitiveUnsigned,
62 U: PrimitiveUnsigned,
63> {
64 xs: I,
65 x: T,
66 multiple: u64,
67 x_width: u64,
68 y_width: u64,
69 counter: u64,
70 phantom: PhantomData<*const U>,
71}
72
73impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>
74 EvenFractionIteratorToBitChunks<I, T, U>
75{
76 fn next_with_wrapping<F: Fn(T) -> U>(&mut self, wrap: F) -> Option<Option<U>> {
77 if self.counter == 0 {
78 let x = self.xs.next()?;
79 if x.significant_bits() > self.x_width {
80 return Some(None);
81 }
82 self.x = x;
83 self.counter = self.multiple;
84 } else {
85 self.x >>= self.y_width;
86 }
87 let y = wrap(self.x.mod_power_of_2(self.y_width));
88 self.counter -= 1;
89 Some(Some(y))
90 }
91}
92
93const fn even_fraction_iterator_to_bit_chunks<
94 I: Iterator<Item = T>,
95 T: PrimitiveUnsigned,
96 U: PrimitiveUnsigned,
97>(
98 xs: I,
99 multiple: u64,
100 out_chunk_size: u64,
101) -> EvenFractionIteratorToBitChunks<I, T, U> {
102 EvenFractionIteratorToBitChunks {
103 xs,
104 x: T::ZERO,
105 multiple,
106 x_width: multiple * out_chunk_size,
107 y_width: out_chunk_size,
108 counter: 0,
109 phantom: PhantomData,
110 }
111}
112
113#[doc(hidden)]
114#[derive(Clone, Debug)]
115pub struct EvenMultipleIteratorToBitChunks<
116 I: Iterator<Item = T>,
117 T: PrimitiveUnsigned,
118 U: PrimitiveUnsigned,
119> {
120 xs: I,
121 x_width: u64,
122 y_width: u64,
123 done: bool,
124 phantom: PhantomData<*const U>,
125}
126
127impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>
128 EvenMultipleIteratorToBitChunks<I, T, U>
129{
130 fn next_with_wrapping<F: Fn(T) -> U>(&mut self, wrap: F) -> Option<Option<U>> {
131 if self.done {
132 return None;
133 }
134 let mut y = U::ZERO;
135 let mut shift = 0;
136 while shift < self.y_width {
137 if let Some(x) = self.xs.next() {
138 if x.significant_bits() > self.x_width {
139 return Some(None);
140 }
141 y |= wrap(x) << shift;
142 shift += self.x_width;
143 } else {
144 self.done = true;
145 break;
146 }
147 }
148 if shift == 0 { None } else { Some(Some(y)) }
149 }
150}
151
152const fn even_multiple_iterator_to_bit_chunks<
153 I: Iterator<Item = T>,
154 T: PrimitiveUnsigned,
155 U: PrimitiveUnsigned,
156>(
157 xs: I,
158 in_chunk_size: u64,
159 out_chunk_size: u64,
160) -> EvenMultipleIteratorToBitChunks<I, T, U> {
161 EvenMultipleIteratorToBitChunks {
162 xs,
163 x_width: in_chunk_size,
164 y_width: out_chunk_size,
165 done: false,
166 phantom: PhantomData,
167 }
168}
169
170#[doc(hidden)]
171#[derive(Clone, Debug)]
172pub struct IrregularIteratorToBitChunks<
173 I: Iterator<Item = T>,
174 T: PrimitiveUnsigned,
175 U: PrimitiveUnsigned,
176> {
177 xs: I,
178 x: T,
179 x_width: u64,
180 y_width: u64,
181 remaining_x_bits: u64,
182 in_inner_loop: bool,
183 phantom: PhantomData<*const U>,
184}
185
186impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>
187 IrregularIteratorToBitChunks<I, T, U>
188{
189 fn next_with_wrapping<F: Fn(T) -> U>(&mut self, wrap: F) -> Option<Option<U>> {
190 let mut y = U::ZERO;
191 let mut remaining_y_bits = self.y_width;
192 loop {
193 if !self.in_inner_loop {
194 if let Some(x) = self.xs.next() {
195 if x.significant_bits() > self.x_width {
196 return Some(None);
197 }
198 self.x = x;
199 } else {
200 break;
201 }
202 self.remaining_x_bits = self.x_width;
203 self.in_inner_loop = true;
204 }
205 while self.remaining_x_bits != 0 {
206 let y_index = self.y_width - remaining_y_bits;
207 if self.remaining_x_bits <= remaining_y_bits {
208 y |= wrap(self.x) << y_index;
209 remaining_y_bits -= self.remaining_x_bits;
210 self.remaining_x_bits = 0;
211 } else {
212 y |= wrap(self.x).mod_power_of_2(remaining_y_bits) << y_index;
213 self.x >>= remaining_y_bits;
214 self.remaining_x_bits -= remaining_y_bits;
215 remaining_y_bits = 0;
216 }
217 if remaining_y_bits == 0 {
218 return Some(Some(y));
219 }
220 }
221 self.in_inner_loop = false;
222 }
223 if y == U::ZERO { None } else { Some(Some(y)) }
224 }
225}
226
227const fn irregular_iterator_to_bit_chunks<
228 I: Iterator<Item = T>,
229 T: PrimitiveUnsigned,
230 U: PrimitiveUnsigned,
231>(
232 xs: I,
233 in_chunk_size: u64,
234 out_chunk_size: u64,
235) -> IrregularIteratorToBitChunks<I, T, U> {
236 IrregularIteratorToBitChunks {
237 xs,
238 x: T::ZERO,
239 x_width: in_chunk_size,
240 y_width: out_chunk_size,
241 remaining_x_bits: 0,
242 in_inner_loop: false,
243 phantom: PhantomData,
244 }
245}
246
247#[derive(Clone, Debug)]
252pub enum IteratorToBitChunks<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned> {
253 SameWidth(SameWidthIteratorToBitChunks<I, T, U>),
254 EvenFraction(EvenFractionIteratorToBitChunks<I, T, U>),
255 EvenMultiple(EvenMultipleIteratorToBitChunks<I, T, U>),
256 Irregular(IrregularIteratorToBitChunks<I, T, U>),
257}
258
259impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>
260 IteratorToBitChunks<I, T, U>
261{
262 pub(crate) fn next_with_wrapping<F: Fn(T) -> U>(&mut self, wrap: F) -> Option<Option<U>> {
263 match self {
264 Self::SameWidth(xs) => xs.next_with_wrapping(wrap),
265 Self::EvenFraction(xs) => xs.next_with_wrapping(wrap),
266 Self::EvenMultiple(xs) => xs.next_with_wrapping(wrap),
267 Self::Irregular(xs) => xs.next_with_wrapping(wrap),
268 }
269 }
270}
271
272impl<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned + WrappingFrom<T>> Iterator
273 for IteratorToBitChunks<I, T, U>
274{
275 type Item = Option<U>;
276
277 #[inline]
278 fn next(&mut self) -> Option<Option<U>> {
279 self.next_with_wrapping(U::wrapping_from)
280 }
281}
282
283pub fn iterator_to_bit_chunks<I: Iterator<Item = T>, T: PrimitiveUnsigned, U: PrimitiveUnsigned>(
350 xs: I,
351 in_chunk_size: u64,
352 out_chunk_size: u64,
353) -> IteratorToBitChunks<I, T, U> {
354 assert_ne!(in_chunk_size, 0);
355 assert_ne!(out_chunk_size, 0);
356 assert!(in_chunk_size <= T::WIDTH);
357 assert!(out_chunk_size <= U::WIDTH);
358 match in_chunk_size.cmp(&out_chunk_size) {
359 Equal => {
360 return IteratorToBitChunks::SameWidth(same_width_iterator_to_bit_chunks(
361 xs,
362 in_chunk_size,
363 ));
364 }
365 Less => {
366 if out_chunk_size.divisible_by(in_chunk_size) {
367 return IteratorToBitChunks::EvenMultiple(even_multiple_iterator_to_bit_chunks(
368 xs,
369 in_chunk_size,
370 out_chunk_size,
371 ));
372 }
373 }
374 Greater => {
375 let (multiple, remainder) = in_chunk_size.div_mod(out_chunk_size);
376 if remainder == 0 {
377 return IteratorToBitChunks::EvenFraction(even_fraction_iterator_to_bit_chunks(
378 xs,
379 multiple,
380 out_chunk_size,
381 ));
382 }
383 }
384 }
385 IteratorToBitChunks::Irregular(irregular_iterator_to_bit_chunks(
386 xs,
387 in_chunk_size,
388 out_chunk_size,
389 ))
390}
391
392#[derive(Clone, Debug, Eq, Hash, PartialEq)]
396pub struct RulerSequence<T: ExactFrom<u32>> {
397 i: u64,
398 phantom: PhantomData<*const T>,
399}
400
401impl<T: ExactFrom<u32>> Iterator for RulerSequence<T> {
402 type Item = T;
403
404 fn next(&mut self) -> Option<T> {
405 self.i += 1;
406 Some(T::exact_from(self.i.trailing_zeros()))
407 }
408}
409
410pub const fn ruler_sequence<T: ExactFrom<u32>>() -> RulerSequence<T> {
438 RulerSequence {
439 i: 0,
440 phantom: PhantomData,
441 }
442}
443
444#[derive(Clone, Debug)]
448pub struct BitDistributorSequence {
449 bit_distributor: BitDistributor,
450}
451
452impl Iterator for BitDistributorSequence {
453 type Item = usize;
454
455 fn next(&mut self) -> Option<usize> {
456 let i = self.bit_distributor.get_output(1);
457 self.bit_distributor.increment_counter();
458 Some(i)
459 }
460}
461
462pub fn bit_distributor_sequence(
518 x_output_type: BitDistributorOutputType,
519 y_output_type: BitDistributorOutputType,
520) -> BitDistributorSequence {
521 BitDistributorSequence {
522 bit_distributor: BitDistributor::new(&[y_output_type, x_output_type]),
523 }
524}