use crate::array::*;
#[allow(unused_imports)] use crate::finite_function::*;
use crate::indexed_coproduct::*;
use crate::semifinite::*;
use core::iter::IntoIterator;
use num_traits::{One, Zero};
pub struct IndexedCoproductSemifiniteFunctionIterator<K: ArrayKind, T> {
pointers: K::Type<K::I>,
values: SemifiniteFunction<K, T>,
index: K::I,
}
impl<K: ArrayKind, T> Iterator for IndexedCoproductSemifiniteFunctionIterator<K, T>
where
K::Type<K::I>: NaturalArray<K>,
K::Type<T>: Array<K, T>,
K::I: Into<usize>,
{
type Item = SemifiniteFunction<K, T>;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.pointers.len() - K::I::one() {
return None;
}
let start = self.pointers.get(self.index.clone());
let end = self.pointers.get(self.index.clone() + K::I::one());
let values = self.values.0.get_range(start.clone()..end.clone());
let ff = SemifiniteFunction::new(K::Type::<T>::from_slice(values));
self.index = self.index.clone() + K::I::one();
Some(ff)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let n = (self.pointers.len() - K::I::one()).into();
(n, Some(n)) }
}
impl<K: ArrayKind, T> ExactSizeIterator for IndexedCoproductSemifiniteFunctionIterator<K, T>
where
K::Type<K::I>: NaturalArray<K>,
K::Type<T>: Array<K, T>,
K::I: Into<usize>,
{
fn len(&self) -> usize {
(self.pointers.len() - K::I::one()).into()
}
}
impl<K: ArrayKind, T> IntoIterator for IndexedCoproduct<K, SemifiniteFunction<K, T>>
where
K::Type<K::I>: NaturalArray<K>,
K::Type<T>: Array<K, T>,
K::I: Into<usize>,
{
type Item = SemifiniteFunction<K, T>;
type IntoIter = IndexedCoproductSemifiniteFunctionIterator<K, T>;
fn into_iter(self) -> Self::IntoIter {
IndexedCoproductSemifiniteFunctionIterator {
pointers: self.sources.table.into().cumulative_sum(),
values: self.values,
index: K::I::zero(),
}
}
}
use crate::array::vec::VecKind;
impl<T> IndexedCoproduct<VecKind, SemifiniteFunction<VecKind, T>> {
pub fn iter(&self) -> impl Iterator<Item = &[T]> {
let pointers = self.sources.table.cumulative_sum();
(0..pointers.len() - 1).map(move |i| {
let start = pointers.get(i);
let end = pointers.get(i + 1);
&self.values.0[start..end]
})
}
}