|
| 1 | +//! Contains [`DoubleEndedCounter`] |
| 2 | +
|
| 3 | +//--------------------------------------- |
| 4 | +// uses |
| 5 | + |
| 6 | +use std::{ |
| 7 | + fmt::{self, Display}, |
| 8 | + iter::FusedIterator, |
| 9 | +}; |
| 10 | + |
| 11 | +#[cfg(feature = "serde-serialize")] |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use utils_lib::{Getter, Sealed}; |
| 14 | + |
| 15 | +use super::{IteratorElement, RandomAccessIterator, Split}; |
| 16 | +use crate::lattice::direction::DirectionIndexing; |
| 17 | + |
| 18 | +//--------------------------------------- |
| 19 | +// struct definition |
| 20 | + |
| 21 | +/// An iterator that track the front and the back in order to be able to implement |
| 22 | +/// [`DoubleEndedIterator`]. |
| 23 | +/// |
| 24 | +/// By itself it is not use a lot in the library it is used as a properties and use |
| 25 | +/// to track the front and the back. [`Iterator`] traits are not (yet ?) implemented |
| 26 | +/// on this type. |
| 27 | +#[derive(Sealed, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 28 | +#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] |
| 29 | +#[derive(Getter)] |
| 30 | +pub struct DoubleEndedCounter<T> { |
| 31 | + /// Front element of the iterator. The state need to be increased before |
| 32 | + /// being returned by the next [`Iterator::next`] call. |
| 33 | + #[get(Const, Pub)] |
| 34 | + #[get_mut(Pub)] |
| 35 | + pub(super) front: IteratorElement<T>, |
| 36 | + /// End element of the iterator. |
| 37 | + /// It needs to be decreased before the next [`DoubleEndedIterator::next_back`] call. |
| 38 | + #[get(Const, Pub)] |
| 39 | + #[get_mut(Pub)] |
| 40 | + pub(super) end: IteratorElement<T>, |
| 41 | +} |
| 42 | + |
| 43 | +impl<T> DoubleEndedCounter<T> { |
| 44 | + /// Create a new [`Self`] with [`IteratorElement::FirstElement`] as `front` and |
| 45 | + /// [`IteratorElement::LastElement`] as `end` |
| 46 | + /// # Example |
| 47 | + /// ```ignore |
| 48 | + /// use lattice_qcd_rs::lattice::iter::{DoubleEndedCounter,IteratorElement}; |
| 49 | + /// let counter = DoubleEndedCounter::<()>::new(); |
| 50 | + /// assert_eq!(counter.front(), IteratorElement::FirstElement); |
| 51 | + /// assert_eq!(counter.end(), IteratorElement::LastElement); |
| 52 | + /// ``` |
| 53 | + /// TODO restrict to valid iter ? |
| 54 | + pub const fn new() -> Self { |
| 55 | + Self { |
| 56 | + front: IteratorElement::FirstElement, |
| 57 | + end: IteratorElement::LastElement, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Create a new self with a given `front` and `end` element |
| 62 | + pub(super) const fn new_with_front_end( |
| 63 | + front: IteratorElement<T>, |
| 64 | + end: IteratorElement<T>, |
| 65 | + ) -> Self { |
| 66 | + Self { front, end } |
| 67 | + } |
| 68 | + |
| 69 | + // possible with_first, with_last |
| 70 | +} |
| 71 | + |
| 72 | +//--------------------------------------- |
| 73 | +// common traits |
| 74 | + |
| 75 | +/// It is [`Self::new`], |
| 76 | +impl<T> Default for DoubleEndedCounter<T> { |
| 77 | + #[inline] |
| 78 | + fn default() -> Self { |
| 79 | + Self::new() |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<T: Display> Display for DoubleEndedCounter<T> { |
| 84 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 85 | + write!(f, "front: {}, end: {}", self.front(), self.end()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +//--------------------------------------- |
| 90 | +// conversion |
| 91 | + |
| 92 | +impl<T> From<DoubleEndedCounter<T>> for [IteratorElement<T>; 2] { |
| 93 | + #[inline] |
| 94 | + fn from(value: DoubleEndedCounter<T>) -> Self { |
| 95 | + [value.front, value.end] |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<T> From<DoubleEndedCounter<T>> for (IteratorElement<T>, IteratorElement<T>) { |
| 100 | + #[inline] |
| 101 | + fn from(value: DoubleEndedCounter<T>) -> Self { |
| 102 | + (value.front, value.end) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +//--------------------------------------- |
| 107 | +// impl of RandomAccessIterator |
| 108 | + |
| 109 | +impl<D: DirectionIndexing> RandomAccessIterator for DoubleEndedCounter<D> { |
| 110 | + type Item = D; |
| 111 | + |
| 112 | + fn iter_len(&self) -> usize { |
| 113 | + self.front() |
| 114 | + .direction_to_index() |
| 115 | + .saturating_sub(self.end().direction_to_index()) |
| 116 | + } |
| 117 | + |
| 118 | + fn increase_front_element_by(&self, advance_by: usize) -> IteratorElement<Self::Item> { |
| 119 | + let index = match self.front() { |
| 120 | + IteratorElement::FirstElement => 0, |
| 121 | + IteratorElement::Element(ref element) => element.direction_to_index() + 1, |
| 122 | + IteratorElement::LastElement => { |
| 123 | + // early return |
| 124 | + return IteratorElement::LastElement; |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + let new_index = index + advance_by; |
| 129 | + IteratorElement::index_to_element(new_index, |index| { |
| 130 | + Self::Item::direction_from_index(index) |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + fn decrease_end_element_by(&self, back_by: usize) -> IteratorElement<Self::Item> { |
| 135 | + let index = match self.end() { |
| 136 | + IteratorElement::FirstElement => { |
| 137 | + // early return |
| 138 | + return IteratorElement::FirstElement; |
| 139 | + } |
| 140 | + IteratorElement::Element(ref element) => element.direction_to_index() + 1, |
| 141 | + IteratorElement::LastElement => Self::Item::number_of_directions() + 1, |
| 142 | + }; |
| 143 | + |
| 144 | + let new_index = index.saturating_sub(back_by); |
| 145 | + IteratorElement::index_to_element(new_index, |index| { |
| 146 | + Self::Item::direction_from_index(index) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl<I> Split for DoubleEndedCounter<I> |
| 152 | +where |
| 153 | + Self: RandomAccessIterator<Item = I>, |
| 154 | + I: Clone, |
| 155 | +{ |
| 156 | + #[inline] |
| 157 | + fn split_at(self, index: usize) -> (Self, Self) { |
| 158 | + let splinting = self.increase_front_element_by(index); |
| 159 | + ( |
| 160 | + Self::new_with_front_end(self.front, splinting.clone()), |
| 161 | + Self::new_with_front_end(splinting, self.end), |
| 162 | + ) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +//--------------------------------------- |
| 167 | +// impl of Iterator traits |
| 168 | + |
| 169 | +/// TODO DOC |
| 170 | +impl<T> Iterator for DoubleEndedCounter<T> |
| 171 | +where |
| 172 | + Self: RandomAccessIterator<Item = T>, |
| 173 | + T: Clone, |
| 174 | +{ |
| 175 | + type Item = T; |
| 176 | + |
| 177 | + #[inline] |
| 178 | + fn next(&mut self) -> Option<Self::Item> { |
| 179 | + self.nth(0) |
| 180 | + } |
| 181 | + |
| 182 | + #[inline] |
| 183 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 184 | + let size = self.iter_len(); |
| 185 | + (size, Some(size)) |
| 186 | + } |
| 187 | + |
| 188 | + #[inline] |
| 189 | + fn count(self) -> usize |
| 190 | + where |
| 191 | + Self: Sized, |
| 192 | + { |
| 193 | + self.iter_len() |
| 194 | + } |
| 195 | + |
| 196 | + #[inline] |
| 197 | + fn last(mut self) -> Option<Self::Item> |
| 198 | + where |
| 199 | + Self: Sized, |
| 200 | + { |
| 201 | + self.nth(self.iter_len().saturating_sub(1)) |
| 202 | + } |
| 203 | + |
| 204 | + #[inline] |
| 205 | + fn nth(&mut self, n: usize) -> Option<Self::Item> { |
| 206 | + let len = self.iter_len(); |
| 207 | + if len <= n { |
| 208 | + if len != 0 { |
| 209 | + // we need to change the state of the iterator other wise it could |
| 210 | + // produce element we should have otherwise skipped. |
| 211 | + //*self.front_mut() = self.end().clone(); |
| 212 | + *self.front_mut() = IteratorElement::LastElement; |
| 213 | + } |
| 214 | + return None; |
| 215 | + } |
| 216 | + let next_element = self.increase_front_element_by(n + 1); |
| 217 | + *self.front_mut() = next_element.clone(); |
| 218 | + next_element.into() |
| 219 | + } |
| 220 | + |
| 221 | + #[inline] |
| 222 | + fn max(self) -> Option<Self::Item> |
| 223 | + where |
| 224 | + Self: Sized, |
| 225 | + Self::Item: Ord, |
| 226 | + { |
| 227 | + self.last() |
| 228 | + } |
| 229 | + |
| 230 | + #[inline] |
| 231 | + fn min(mut self) -> Option<Self::Item> |
| 232 | + where |
| 233 | + Self: Sized, |
| 234 | + Self::Item: Ord, |
| 235 | + { |
| 236 | + self.next() |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +/// TODO DOC |
| 241 | +impl<T> DoubleEndedIterator for DoubleEndedCounter<T> |
| 242 | +where |
| 243 | + Self: RandomAccessIterator<Item = T>, |
| 244 | + T: Clone, |
| 245 | +{ |
| 246 | + #[inline] |
| 247 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 248 | + self.nth_back(0) |
| 249 | + } |
| 250 | + |
| 251 | + #[inline] |
| 252 | + fn nth_back(&mut self, n: usize) -> Option<Self::Item> { |
| 253 | + let len = self.iter_len(); |
| 254 | + if len <= n { |
| 255 | + if len != 0 { |
| 256 | + // we need to change the state of the iterator other wise it could |
| 257 | + // produce element we should have otherwise skipped. |
| 258 | + //*self.end_mut() = self.front().clone(); |
| 259 | + *self.end_mut() = IteratorElement::FirstElement; |
| 260 | + } |
| 261 | + return None; |
| 262 | + } |
| 263 | + let previous_element = self.decrease_end_element_by(n + 1); |
| 264 | + *self.end_mut() = previous_element.clone(); |
| 265 | + previous_element.into() |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +impl<T> FusedIterator for DoubleEndedCounter<T> where Self: RandomAccessIterator + Iterator {} |
| 270 | + |
| 271 | +impl<T> ExactSizeIterator for DoubleEndedCounter<T> |
| 272 | +where |
| 273 | + Self: RandomAccessIterator + Iterator, |
| 274 | +{ |
| 275 | + #[inline] |
| 276 | + fn len(&self) -> usize { |
| 277 | + self.iter_len() |
| 278 | + } |
| 279 | +} |
0 commit comments