Skip to content

Commit

Permalink
add type alias in direction module and depreciated IteratorDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
ABouttefeux committed Feb 24, 2024
1 parent e7b08cc commit 7b96aef
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrientedDirection>`].
- Depreciate [`IteratorDirection`], use [`IteratorOrientedDirection`].


# v0.2.1

Expand Down
4 changes: 0 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 67 additions & 3 deletions src/lattice/iterator/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! # example
//! see [`IteratorDirection`]
#![allow(deprecated)]
use std::iter::FusedIterator;

use rayon::iter::{
Expand All @@ -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<dyn std::error::Error>> {
/// 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<const D: usize, const ORIENTATION: bool> =
DoubleEndedCounter<OrientedDirection<D, ORIENTATION>>;

/// 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};
Expand Down Expand Up @@ -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<const D: usize, const IS_POSITIVE_DIRECTION: bool> {
/// Front element of the iterator. The state need to be increased before
/// being returned by the next [`Iterator::next`] call.
Expand Down
5 changes: 3 additions & 2 deletions src/lattice/iterator/double_ended_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ impl<D: DirectionIndexing> RandomAccessIterator for DoubleEndedCounter<D> {
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<Self::Item> {
Expand Down
9 changes: 5 additions & 4 deletions src/lattice/iterator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/lattice/iterator/parallel_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<T> ParIter<T> {
}

/// 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 {
Expand Down
12 changes: 6 additions & 6 deletions src/lattice/iterator/producer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Contains [`LatticeProducer`]
//! Contains [`Prod`]
//---------------------------------------
// uses
Expand All @@ -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>(T);
Expand All @@ -25,21 +25,21 @@ impl<T> Prod<T> {
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
}

Check warning on line 40 in src/lattice/iterator/producer.rs

View check run for this annotation

Codecov / codecov/patch

src/lattice/iterator/producer.rs#L38-L40

Added lines #L38 - L40 were not covered by tests

/// 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]
Expand Down
5 changes: 4 additions & 1 deletion src/lattice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -524,6 +526,7 @@ impl<const D: usize> LatticeLinkCanonical<D> {
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<D>, index: usize) -> Option<Self> {
Expand Down

0 comments on commit 7b96aef

Please sign in to comment.