Skip to content

Commit 3d16b96

Browse files
committed
add type alias in direction module and depreciated IteratorDirection
1 parent e7b08cc commit 3d16b96

File tree

7 files changed

+74
-17
lines changed

7 files changed

+74
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
- added [`LatticeCyclic::par_iter_links`] and [`LatticeCyclic::par_iter_points`] to get parallel iterator on the links and points respectively.
4646
- Added [`CardinalDirection`].
4747
- Added [`Axis`].
48+
- Added type alias [`lattice::IteratorOrientedDirection`] for [`DoubleEndedCounter<OrientedDirection>`].
49+
- Depreciate [`IteratorDirection`], use [`IteratorOrientedDirection`].
50+
4851

4952
# v0.2.1
5053

src/lattice/iterator/direction.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! # example
44
//! see [`IteratorDirection`]
55
6+
#![allow(deprecated)]
67
use std::iter::FusedIterator;
78

89
use rayon::iter::{
@@ -12,9 +13,53 @@ use rayon::iter::{
1213
#[cfg(feature = "serde-serialize")]
1314
use serde::{Deserialize, Serialize};
1415

15-
use super::{super::Direction, IteratorElement, RandomAccessIterator};
16+
use super::{super::Direction, DoubleEndedCounter, IteratorElement};
17+
use crate::lattice::OrientedDirection;
1618

17-
/// Iterator over [`Direction`] with the same sign.
19+
/// Iterator over [`OrientedDirection`].
20+
/// # Example
21+
/// ```
22+
/// # use lattice_qcd_rs::lattice::{IteratorOrientedDirection, OrientedDirection, IteratorElement};
23+
/// # use lattice_qcd_rs::error::ImplementationError;
24+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
25+
/// let mut iter = IteratorOrientedDirection::<4, true>::new();
26+
///
27+
/// let iter_val = iter.next();
28+
/// // debug
29+
/// println!("{iter_val:?}, {:?}", OrientedDirection::<4, true>::new(0));
30+
///
31+
/// assert_eq!(
32+
/// iter_val.ok_or(ImplementationError::OptionWithUnexpectedNone)?,
33+
/// OrientedDirection::<4, true>::new(0)
34+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?
35+
/// );
36+
/// assert_eq!(
37+
/// iter.next()
38+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?,
39+
/// OrientedDirection::<4, true>::new(1)
40+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?
41+
/// );
42+
/// assert_eq!(
43+
/// iter.next()
44+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?,
45+
/// OrientedDirection::<4, true>::new(2)
46+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?
47+
/// );
48+
/// assert_eq!(
49+
/// iter.next()
50+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?,
51+
/// OrientedDirection::<4, true>::new(3)
52+
/// .ok_or(ImplementationError::OptionWithUnexpectedNone)?
53+
/// );
54+
/// assert_eq!(iter.next(), None);
55+
/// assert_eq!(iter.next(), None);
56+
/// # Ok(())
57+
/// # }
58+
/// ```
59+
pub type IteratorOrientedDirection<const D: usize, const ORIENTATION: bool> =
60+
DoubleEndedCounter<OrientedDirection<D, ORIENTATION>>;
61+
62+
/// Iterator over [`Direction`] with the orientation.
1863
/// # Example
1964
/// ```
2065
/// # use lattice_qcd_rs::lattice::{IteratorDirection, Direction, IteratorElement};
@@ -47,9 +92,13 @@ use super::{super::Direction, IteratorElement, RandomAccessIterator};
4792
/// # Ok(())
4893
/// # }
4994
/// ```
50-
// TODO
95+
// TODO remove ?
5196
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Hash)]
5297
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
98+
#[deprecated(
99+
since = "0.3.0",
100+
note = "use `IteratorOrientedDirection` instead with a map and a conversion"
101+
)]
53102
pub struct IteratorDirection<const D: usize, const IS_POSITIVE_DIRECTION: bool> {
54103
/// Front element of the iterator. The state need to be increased before
55104
/// being returned by the next [`Iterator::next`] call.

src/lattice/iterator/double_ended_counter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ impl<D: DirectionIndexing> RandomAccessIterator for DoubleEndedCounter<D> {
110110
type Item = D;
111111

112112
fn iter_len(&self) -> usize {
113-
self.front()
113+
// this time it is end - front because we use index and not length
114+
self.end()
114115
.direction_to_index()
115-
.saturating_sub(self.end().direction_to_index())
116+
.saturating_sub(self.front().direction_to_index())
116117
}
117118

118119
fn increase_front_element_by(&self, advance_by: usize) -> IteratorElement<Self::Item> {

src/lattice/iterator/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ mod producer;
2828
//---------------------------------------
2929
// uses
3030

31-
pub use self::direction::IteratorDirection;
31+
#[allow(deprecated)]
32+
pub use self::direction::{IteratorDirection, IteratorOrientedDirection};
3233
pub use self::double_ended_counter::DoubleEndedCounter;
3334
pub use self::element::IteratorElement;
3435
pub use self::lattice_iterator::LatticeIterator;
@@ -76,12 +77,12 @@ pub trait RandomAccessIterator: Sealed {
7677
// }
7778
}
7879

79-
/// Trait used by [`producer::LatticeProducer`] for implementing
80+
/// Trait used by [`producer::Prod`] for implementing
8081
/// [`rayon::iter::plumbing::Producer`].
8182
/// It is separate trait than [`RandomAccessIterator`] to avoid more a [`Clone`] constrain.
8283
///
83-
/// [`Split::split_at`] return two self and not two [`producer::LatticeProducer`] the idea is that they
84-
/// should be converted into [`producer::LatticeProducer`].
84+
/// [`Split::split_at`] return two self and not two [`producer::Prod`] the idea is that they
85+
/// should be converted into [`producer::Prod`].
8586
///
8687
/// This trait is a super trait of [`Sealed`] which is private meaning that It can't be
8788
/// implemented outside of this trait.

src/lattice/iterator/parallel_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<T> ParIter<T> {
4949
}
5050

5151
/// Take a reference of self and return a reference to an [`Iterator`].
52-
/// This might not be very useful look instead at [`Self::as_iter_mut`]
52+
/// This might not be very useful look instead at [`Self::iter_mut`]
5353
#[must_use]
5454
#[inline]
5555
pub const fn as_iter(&self) -> &T {

src/lattice/iterator/producer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Contains [`LatticeProducer`]
1+
//! Contains [`Prod`]
22
33
//---------------------------------------
44
// uses
@@ -10,8 +10,8 @@ use super::{ParIter, RandomAccessIterator, Split};
1010
//---------------------------------------
1111
// struct definition
1212

13-
/// [`rayon::iter::plumbing::Producer`] for the [`rayon::iter::IndexedParallelIterator`] [`super::ParIter`] based on
14-
/// the [`DoubleEndedIterator`] [`LatticeIterator`].
13+
/// [`rayon::iter::plumbing::Producer`] for the [`rayon::iter::IndexedParallelIterator`]
14+
/// [`super::ParIter`] based on the [`DoubleEndedIterator`] [`Prod`].
1515
#[repr(transparent)]
1616
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
1717
pub struct Prod<T>(T);
@@ -25,21 +25,21 @@ impl<T> Prod<T> {
2525
Self(iter)
2626
}
2727

28-
/// Convert self into a [`LatticeIterator`]
28+
/// Convert self into a [`Prod`]
2929
#[inline]
3030
#[must_use]
3131
pub fn into_iterator(self) -> T {
3232
self.0
3333
}
3434

35-
/// Convert as a reference of [`LatticeIterator`]
35+
/// Convert as a reference of [`Prod`]
3636
#[inline]
3737
#[must_use]
3838
const fn as_iter(&self) -> &T {
3939
&self.0
4040
}
4141

42-
/// Convert as a mutable reference of [`LatticeIterator`]
42+
/// Convert as a mutable reference of [`Prod`]
4343
#[allow(clippy::iter_not_returning_iterator)] // yes in some cases (see impl of IntoIterator)
4444
#[inline]
4545
#[must_use]

src/lattice/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ pub use self::direction::{
2727
Axis, Direction, DirectionConversionError, DirectionEnum, DirectionList, OrientedDirection,
2828
};
2929
// TODO remove IteratorElement from public interface ?
30+
#[allow(deprecated)]
3031
pub use self::iterator::{
3132
IteratorDirection, IteratorElement, IteratorLatticeLinkCanonical, IteratorLatticePoint,
32-
LatticeIterator, ParIter, ParIterLatticeLinkCanonical, ParIterLatticePoint,
33+
IteratorOrientedDirection, LatticeIterator, ParIter, ParIterLatticeLinkCanonical,
34+
ParIterLatticePoint,
3335
};
3436
pub use self::lattice_cyclic::LatticeCyclic;
3537
use crate::private::Sealed;
@@ -524,6 +526,7 @@ impl<const D: usize> LatticeLinkCanonical<D> {
524526
self.dir = dir.to_positive();
525527
}
526528

529+
/// Transform an index to an canonical link inside a given lattice if it exists
527530
#[inline]
528531
#[must_use]
529532
fn index_to_canonical_link(lattice: &LatticeCyclic<D>, index: usize) -> Option<Self> {

0 commit comments

Comments
 (0)