From 250c208c7f04d8be10e1f5607b58d8214af7f7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Wed, 17 Jan 2024 10:11:48 +0100 Subject: [PATCH] add type alias in direction module and depreciated IteratorDirection --- CHANGELOG.md | 3 + src/lattice/iterator/direction.rs | 79 ++++++++++++++++---- src/lattice/iterator/double_ended_counter.rs | 5 +- src/lattice/iterator/mod.rs | 3 +- src/lattice/mod.rs | 5 +- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3005ab0..652f1041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,9 @@ - added [`LatticeCyclic::par_iter_links`] and [`LatticeCyclic::par_iter_points`] to get parallel iterator on the links and points respectively. - Added [`CardinalDirection`]. - Added [`Axis`]. +- Added type alias [`lattice::IteratorOrientedDirection`] for [`DoubleEndedCounter`]. +- Depreciate [`IteratorDirection`], use [`IteratorOrientedDirection`]. + # v0.2.1 diff --git a/src/lattice/iterator/direction.rs b/src/lattice/iterator/direction.rs index 1f3f05c2..4560d8ec 100644 --- a/src/lattice/iterator/direction.rs +++ b/src/lattice/iterator/direction.rs @@ -3,6 +3,7 @@ //! # example //! see [`IteratorDirection`] +#![allow(deprecated)] use std::iter::FusedIterator; use rayon::iter::{ @@ -12,15 +13,59 @@ use rayon::iter::{ #[cfg(feature = "serde-serialize")] use serde::{Deserialize, Serialize}; -use super::{super::Direction, IteratorElement, RandomAccessIterator}; +use super::{super::Direction, DoubleEndedCounter, IteratorElement}; +use crate::lattice::OrientedDirection; -/// Iterator over [`Direction`] with the same sign. +/// Iterator over [`OrientedDirection`]. /// # Example /// ``` -/// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement}; +/// # use lattice_qcd_rs::lattice::{IteratorDirection, OrientedDirection, IteratorElement}; /// # use lattice_qcd_rs::error::ImplementationError; /// # fn main() -> Result<(), Box> { -/// let mut iter = IteratorDirection::<4, true>::new(None) +/// let mut iter = IteratorDirection::<4, true>::new(); +/// +/// let iter_val = iter.next(); +/// // debug +/// println!("{iter_val:?}, {:?}", OrientedDirection::<4, true>::new(0)); +/// +/// assert_eq!( +/// iter_val.ok_or(ImplementationError::OptionWithUnexpectedNone)?, +/// OrientedDirection::<4, true>::new(0) +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)? +/// ); +/// assert_eq!( +/// iter.next() +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?, +/// OrientedDirection::<4, true>::new(1) +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)? +/// ); +/// assert_eq!( +/// iter.next() +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?, +/// OrientedDirection::<4, true>::new(2) +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)? +/// ); +/// assert_eq!( +/// iter.next() +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?, +/// OrientedDirection::<4, true>::new(3) +/// .ok_or(ImplementationError::OptionWithUnexpectedNone)? +/// ); +/// assert_eq!(iter.next(), None); +/// assert_eq!(iter.next(), None); +/// # Ok(()) +/// # } +/// ``` +pub type IteratorOrientedDirection = + DoubleEndedCounter>; + +/// Iterator over [`Direction`] with the orientation. +/// # Example +/// ``` +/// # use lattice_qcd_rs::lattice::{IteratorDirectionOriented, Direction, IteratorElement}; +/// # use lattice_qcd_rs::error::ImplementationError; +/// # fn main() -> Result<(), Box> { +/// let mut iter = IteratorDirectionOriented::<4, true>::new(None) /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// assert_eq!( /// iter.next() @@ -47,9 +92,10 @@ use super::{super::Direction, IteratorElement, RandomAccessIterator}; /// # Ok(()) /// # } /// ``` -// TODO +// TODO remove ? #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Hash)] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] +#[deprecated(since = "0.3.0", note = "use `IteratorOrientedDirection` instead with a map and a conversion")] pub struct IteratorDirection { /// Front element of the iterator. The state need to be increased before /// being returned by the next [`Iterator::next`] call. @@ -66,10 +112,10 @@ impl /// after `element`. Giving `None` results in the first element being the direction with index 0 /// # Example /// ``` - /// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement}; + /// # use lattice_qcd_rs::lattice::{IteratorDirectionOriented, Direction, IteratorElement}; /// # use lattice_qcd_rs::error::ImplementationError; /// # fn main() -> Result<(), Box> { - /// let mut iter = IteratorDirection::<4, true>::new(None) + /// let mut iter = IteratorDirectionOriented::<4, true>::new(None) /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// assert_eq!( /// iter.next() @@ -83,7 +129,7 @@ impl /// ); /// let element = /// Direction::<4>::new(0, false).ok_or(ImplementationError::OptionWithUnexpectedNone)?; - /// let mut iter = IteratorDirection::<4, false>::new(Some(element)) + /// let mut iter = IteratorDirectionOriented::<4, false>::new(Some(element)) /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// assert_eq!( /// iter.next() @@ -99,13 +145,13 @@ impl /// # } /// ``` /// ``` - /// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement}; + /// # use lattice_qcd_rs::lattice::{IteratorDirectionOriented, Direction, IteratorElement}; /// # use lattice_qcd_rs::error::ImplementationError; /// # fn main() -> Result<(), Box> { - /// let iter = IteratorDirection::<0, true>::new(None); + /// let iter = IteratorDirectionOriented::<0, true>::new(None); /// // 0 is invalid /// assert!(iter.is_none()); - /// let iter = IteratorDirection::<4, true>::new(Some( + /// let iter = IteratorDirectionOriented::<4, true>::new(Some( /// Direction::new(2, false).ok_or(ImplementationError::OptionWithUnexpectedNone)?, /// )); /// // the sign of the direction is invalid @@ -122,16 +168,17 @@ impl } } - /// create a new iterator. The first call to [`IteratorDirection::next()`] gives the element + /// create a new iterator. The first call to [`IteratorDirectionOriented::next()`] gives the element /// just after the one given or the first element if [`IteratorElement::FirstElement`] /// is given. /// # Example /// ``` - /// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement}; + /// # use lattice_qcd_rs::lattice::{IteratorDirectionOriented, Direction, IteratorElement}; /// # use lattice_qcd_rs::error::ImplementationError; /// # fn main() -> Result<(), Box> { - /// let mut iter = IteratorDirection::<4, true>::new_from_element(IteratorElement::FirstElement) - /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; + /// let mut iter = + /// IteratorDirectionOriented::<4, true>::new_from_element(IteratorElement::FirstElement) + /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// assert_eq!( /// iter.next() /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?, @@ -145,7 +192,7 @@ impl /// let element = /// Direction::<4>::new(0, false).ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// let mut iter = - /// IteratorDirection::<4, false>::new_from_element(IteratorElement::Element(element)) + /// IteratorDirectionOriented::<4, false>::new_from_element(IteratorElement::Element(element)) /// .ok_or(ImplementationError::OptionWithUnexpectedNone)?; /// assert_eq!( /// iter.next() diff --git a/src/lattice/iterator/double_ended_counter.rs b/src/lattice/iterator/double_ended_counter.rs index c282fc50..0d9b9e61 100644 --- a/src/lattice/iterator/double_ended_counter.rs +++ b/src/lattice/iterator/double_ended_counter.rs @@ -110,9 +110,10 @@ impl RandomAccessIterator for DoubleEndedCounter { type Item = D; fn iter_len(&self) -> usize { - self.front() + // this time it is end - front because we use index and not length + self.end() .direction_to_index() - .saturating_sub(self.end().direction_to_index()) + .saturating_sub(self.front().direction_to_index()) } fn increase_front_element_by(&self, advance_by: usize) -> IteratorElement { diff --git a/src/lattice/iterator/mod.rs b/src/lattice/iterator/mod.rs index e1e099c6..9b7f5e91 100644 --- a/src/lattice/iterator/mod.rs +++ b/src/lattice/iterator/mod.rs @@ -28,7 +28,8 @@ mod producer; //--------------------------------------- // uses -pub use self::direction::IteratorDirection; +#[allow(deprecated)] +pub use self::direction::{IteratorDirection, IteratorOrientedDirection}; pub use self::double_ended_counter::DoubleEndedCounter; pub use self::element::IteratorElement; pub use self::lattice_iterator::LatticeIterator; diff --git a/src/lattice/mod.rs b/src/lattice/mod.rs index 39a40cdd..146dc390 100644 --- a/src/lattice/mod.rs +++ b/src/lattice/mod.rs @@ -27,9 +27,11 @@ pub use self::direction::{ Axis, Direction, DirectionConversionError, DirectionEnum, DirectionList, OrientedDirection, }; // TODO remove IteratorElement from public interface ? +#[allow(deprecated)] pub use self::iterator::{ IteratorDirection, IteratorElement, IteratorLatticeLinkCanonical, IteratorLatticePoint, - LatticeIterator, ParIter, ParIterLatticeLinkCanonical, ParIterLatticePoint, + IteratorOrientedDirection, LatticeIterator, ParIter, ParIterLatticeLinkCanonical, + ParIterLatticePoint, }; pub use self::lattice_cyclic::LatticeCyclic; use crate::private::Sealed; @@ -524,6 +526,7 @@ impl LatticeLinkCanonical { self.dir = dir.to_positive(); } + /// Transform an index to an canonical link inside a given lattice if it exists #[inline] #[must_use] fn index_to_canonical_link(lattice: &LatticeCyclic, index: usize) -> Option {