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(),
}
}
}