Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contig check for single element arrays #1362

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/array_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::marker::PhantomData;
use alloc::format;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::imp_prelude::*;
Expand Down
3 changes: 2 additions & 1 deletion src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ where
#[cfg(test)]
mod formatting_with_omit {
use itertools::Itertools;
use std::fmt;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use super::*;
Expand Down
5 changes: 4 additions & 1 deletion src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::iter::IntoIterator;
use std::mem;
use std::ops::{Index, IndexMut};
use std::{hash, mem::size_of};
Expand Down Expand Up @@ -115,6 +116,7 @@ where

/// Return `true` if the array shapes and all elements of `self` and
/// `rhs` are equal. Return `false` otherwise.
#[allow(clippy::unconditional_recursion)] // false positive
impl<'a, A, B, S, S2, D> PartialEq<&'a ArrayBase<S2, D>> for ArrayBase<S, D>
where
A: PartialEq<B>,
Expand All @@ -129,6 +131,7 @@ where

/// Return `true` if the array shapes and all elements of `self` and
/// `rhs` are equal. Return `false` otherwise.
#[allow(clippy::unconditional_recursion)] // false positive
impl<'a, A, B, S, S2, D> PartialEq<ArrayBase<S2, D>> for &'a ArrayBase<S, D>
where
A: PartialEq<B>,
Expand Down
2 changes: 2 additions & 0 deletions src/data_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::mem;
use std::mem::ManuallyDrop;
use std::ptr::NonNull;
use alloc::slice;
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use crate::extension::nonnull;

Expand Down
1 change: 1 addition & 0 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::mem::{self, size_of};
use std::mem::MaybeUninit;
use std::ptr::NonNull;
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::{
Expand Down
7 changes: 0 additions & 7 deletions src/dimension/axes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ where

trait IncOps: Copy {
fn post_inc(&mut self) -> Self;
fn post_dec(&mut self) -> Self;
fn pre_dec(&mut self) -> Self;
}

Expand All @@ -155,12 +154,6 @@ impl IncOps for usize {
x
}
#[inline(always)]
fn post_dec(&mut self) -> Self {
let x = *self;
*self -= 1;
x
}
#[inline(always)]
fn pre_dec(&mut self) -> Self {
*self -= 1;
*self
Expand Down
1 change: 1 addition & 0 deletions src/dimension/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use num_traits::Zero;
use std::ops::{Index, IndexMut};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::{Dim, Dimension, Ix, Ix1, IxDyn, IxDynImpl};
Expand Down
31 changes: 18 additions & 13 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Index, IndexMut};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use super::axes_of;
Expand Down Expand Up @@ -284,21 +285,25 @@ pub trait Dimension:
return true;
}
if dim.ndim() == 1 {
return strides[0] as isize == -1;
adamreichold marked this conversation as resolved.
Show resolved Hide resolved
}
let order = strides._fastest_varying_stride_order();
let strides = strides.slice();

let dim_slice = dim.slice();
let mut cstride = 1;
for &i in order.slice() {
// a dimension of length 1 can have unequal strides
if dim_slice[i] != 1 && (strides[i] as isize).unsigned_abs() != cstride {
return false;
// fast case for ndim == 1:
// Either we have length <= 1, then stride is arbitrary,
// or we have stride == 1 or stride == -1, but +1 case is already handled above.
dim[0] <= 1 || strides[0] as isize == -1
} else {
let order = strides._fastest_varying_stride_order();
let strides = strides.slice();

let dim_slice = dim.slice();
let mut cstride = 1;
for &i in order.slice() {
// a dimension of length 1 can have unequal strides
if dim_slice[i] != 1 && (strides[i] as isize).unsigned_abs() != cstride {
return false;
}
cstride *= dim_slice[i];
}
cstride *= dim_slice[i];
true
}
true
}

/// Return the axis ordering corresponding to the fastest variation
Expand Down
2 changes: 2 additions & 0 deletions src/dimension/dynindeximpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::imp_prelude::*;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut, Index, IndexMut};
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
const CAP: usize = 4;

Expand Down
1 change: 1 addition & 0 deletions src/extension/nonnull.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ptr::NonNull;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Return a NonNull<T> pointer to the vector's data
Expand Down
1 change: 1 addition & 0 deletions src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// except according to those terms.

use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::mem::{forget, size_of};

Expand Down
1 change: 1 addition & 0 deletions src/impl_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// except according to those terms.

//! Methods for one-dimensional arrays.
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::mem::MaybeUninit;

Expand Down
1 change: 1 addition & 0 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use num_traits::{One, Zero};
use std::mem;
use std::mem::MaybeUninit;
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::dimension;
Expand Down
1 change: 1 addition & 0 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use std::mem::{size_of, ManuallyDrop};
use alloc::slice;
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use rawpointer::PointerExt;

Expand Down
1 change: 0 additions & 1 deletion src/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ mod arithmetic_ops {
use super::*;
use crate::imp_prelude::*;

use num_complex::Complex;
use std::ops::*;

fn clone_opf<A: Clone, B: Clone, C>(f: impl Fn(A, B) -> C) -> impl FnMut(&A, &B) -> C {
Expand Down
1 change: 1 addition & 0 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::mem;
use std::mem::MaybeUninit;
Expand Down
1 change: 1 addition & 0 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod windows;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ptr;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::Ix1;
Expand Down
1 change: 1 addition & 0 deletions src/linalg/impl_linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{LinalgScalar, Zip};

use std::any::TypeId;
use std::mem::MaybeUninit;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use num_complex::Complex;
Expand Down
2 changes: 1 addition & 1 deletion src/numeric/impl_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#[cfg(feature = "std")]
use num_traits::Float;
use num_traits::{self, FromPrimitive, Zero};
use num_traits::{FromPrimitive, Zero};
use std::ops::{Add, Div, Mul};

use crate::imp_prelude::*;
Expand Down
1 change: 1 addition & 0 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use crate::dimension::slices_intersect;
use crate::error::{ErrorKind, ShapeError};
use crate::{ArrayViewMut, DimAdd, Dimension, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::convert::TryFrom;
use std::fmt;
Expand Down
1 change: 1 addition & 0 deletions src/split_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) trait SplitAt {
}

pub(crate) trait SplitPreference : SplitAt {
#[allow(dead_code)] // used only when Rayon support is enabled
fn can_split(&self) -> bool;
fn split_preference(&self) -> (Axis, usize);
fn split(self) -> (Self, Self) where Self: Sized {
Expand Down
1 change: 1 addition & 0 deletions src/stacking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::dimension;
Expand Down
16 changes: 16 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,22 @@ fn test_contiguous() {
assert!(b.as_slice_memory_order().is_some());
}

#[test]
fn test_contiguous_single_element()
{
assert_matches!(array![1].as_slice_memory_order(), Some(&[1]));

let arr1 = array![1, 2, 3];
assert_matches!(arr1.slice(s![0..1]).as_slice_memory_order(), Some(&[1]));
assert_matches!(arr1.slice(s![1..2]).as_slice_memory_order(), Some(&[2]));
assert_matches!(arr1.slice(s![2..3]).as_slice_memory_order(), Some(&[3]));
assert_matches!(arr1.slice(s![0..0]).as_slice_memory_order(), Some(&[]));

let arr2 = array![[1, 2, 3], [4, 5, 6]];
assert_matches!(arr2.slice(s![.., 2..3]).as_slice_memory_order(), None);
assert_matches!(arr2.slice(s![1, 2..3]).as_slice_memory_order(), Some(&[6]));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2D case was working correctly before this fix too.

}

#[test]
fn test_contiguous_neg_strides() {
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
Expand Down
2 changes: 1 addition & 1 deletion tests/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)]

use ndarray::prelude::*;
use ndarray::{arr3, aview1, indices, s, Axis, Slice, Zip};
use ndarray::{arr3, indices, s, Slice, Zip};

use itertools::assert_equal;
use itertools::enumerate;
Expand Down
Loading