From 7b96aefc88559be3d5769988b6a2756d8089bdd2 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 + makefile | 4 -- src/lattice/iterator/direction.rs | 70 +++++++++++++++++++- src/lattice/iterator/double_ended_counter.rs | 5 +- src/lattice/iterator/mod.rs | 9 +-- src/lattice/iterator/parallel_iterator.rs | 2 +- src/lattice/iterator/producer.rs | 12 ++-- src/lattice/mod.rs | 5 +- 8 files changed, 89 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68f72243..ed34adf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,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/makefile b/makefile index 33ee8946..65d840ee 100644 --- a/makefile +++ b/makefile @@ -55,10 +55,6 @@ git_hooks := $(foreach file, $(notdir $(wildcard tools/git_hook/*)), .git/hooks/ .PHONY: all all: clippy -.PHONY: fix -fix: $(source_files) - - $(cargo) $(cargo_clippy) $(cargo_all_flag) $(rust_all_targets) $(fix_flags) - .PHONY: fix fix: $(source_files) diff --git a/src/lattice/iterator/direction.rs b/src/lattice/iterator/direction.rs index 1f3f05c2..25ef137a 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,9 +13,68 @@ 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::{IteratorOrientedDirection, OrientedDirection, IteratorElement}; +/// # use lattice_qcd_rs::error::ImplementationError; +/// # fn main() -> Result<(), Box> { +/// let mut iter = IteratorOrientedDirection::<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. +/// +/// This item is depreciated and used should used [`IteratorOrientedDirection`] instead +/// (whose output can be converted into an [`Direction`]). +/// +/// The reason of the depreciation is that I use a new system that use generic trait for +/// the iterators this is an old struct where everything was more or done the same way +/// but manually it means it does not benefit from the optimization and the correction +/// the others iterators benefits from. Also because of the way I count element on the +/// lattice it does not make sense any more. +/// (before direction were counted for d = 2 `(index 0, or: true)`, `(index 1, or: true)` - end of count, +/// negative orientation weren't taken into account. Now they are counted +/// `(axis : 0, or: true)`, `(axis: 0, or false)`, `(axis : 1, or: true)`, `(axis: 1, or false)`). +/// Now it make more sense to iterate over [`OrientedDirection`] +/// +/// /// # Example /// ``` /// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement}; @@ -47,9 +107,13 @@ 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. 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..8ab84156 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; @@ -76,12 +77,12 @@ pub trait RandomAccessIterator: Sealed { // } } -/// Trait used by [`producer::LatticeProducer`] for implementing +/// Trait used by [`producer::Prod`] for implementing /// [`rayon::iter::plumbing::Producer`]. /// It is separate trait than [`RandomAccessIterator`] to avoid more a [`Clone`] constrain. /// -/// [`Split::split_at`] return two self and not two [`producer::LatticeProducer`] the idea is that they -/// should be converted into [`producer::LatticeProducer`]. +/// [`Split::split_at`] return two self and not two [`producer::Prod`] the idea is that they +/// should be converted into [`producer::Prod`]. /// /// This trait is a super trait of [`Sealed`] which is private meaning that It can't be /// implemented outside of this trait. diff --git a/src/lattice/iterator/parallel_iterator.rs b/src/lattice/iterator/parallel_iterator.rs index 08c2ffca..1725a87b 100644 --- a/src/lattice/iterator/parallel_iterator.rs +++ b/src/lattice/iterator/parallel_iterator.rs @@ -49,7 +49,7 @@ impl ParIter { } /// Take a reference of self and return a reference to an [`Iterator`]. - /// This might not be very useful look instead at [`Self::as_iter_mut`] + /// This might not be very useful look instead at [`Self::iter_mut`] #[must_use] #[inline] pub const fn as_iter(&self) -> &T { diff --git a/src/lattice/iterator/producer.rs b/src/lattice/iterator/producer.rs index 32d2e454..57bddde3 100644 --- a/src/lattice/iterator/producer.rs +++ b/src/lattice/iterator/producer.rs @@ -1,4 +1,4 @@ -//! Contains [`LatticeProducer`] +//! Contains [`Prod`] //--------------------------------------- // uses @@ -10,8 +10,8 @@ use super::{ParIter, RandomAccessIterator, Split}; //--------------------------------------- // struct definition -/// [`rayon::iter::plumbing::Producer`] for the [`rayon::iter::IndexedParallelIterator`] [`super::ParIter`] based on -/// the [`DoubleEndedIterator`] [`LatticeIterator`]. +/// [`rayon::iter::plumbing::Producer`] for the [`rayon::iter::IndexedParallelIterator`] +/// [`super::ParIter`] based on the [`DoubleEndedIterator`] [`Prod`]. #[repr(transparent)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Prod(T); @@ -25,21 +25,21 @@ impl Prod { Self(iter) } - /// Convert self into a [`LatticeIterator`] + /// Convert self into a [`Prod`] #[inline] #[must_use] pub fn into_iterator(self) -> T { self.0 } - /// Convert as a reference of [`LatticeIterator`] + /// Convert as a reference of [`Prod`] #[inline] #[must_use] const fn as_iter(&self) -> &T { &self.0 } - /// Convert as a mutable reference of [`LatticeIterator`] + /// Convert as a mutable reference of [`Prod`] #[allow(clippy::iter_not_returning_iterator)] // yes in some cases (see impl of IntoIterator) #[inline] #[must_use] 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 {