diff --git a/src/arrayformat.rs b/src/arrayformat.rs index d13add8f7..32d43e4fb 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -55,7 +55,7 @@ fn format_array(view: &ArrayBase, f: &mut fmt::Formatter, write!(f, "]")?; } write!(f, ",")?; - write!(f, "\n")?; + writeln!(f)?; for _ in 0..ndim - n { write!(f, " ")?; } diff --git a/src/data_traits.rs b/src/data_traits.rs index bd4522968..46993fa21 100644 --- a/src/data_traits.rs +++ b/src/data_traits.rs @@ -236,7 +236,7 @@ unsafe impl Data for OwnedArcRepr { Self::ensure_unique(&mut self_); let data = OwnedRepr(Arc::try_unwrap(self_.data.0).ok().unwrap()); ArrayBase { - data: data, + data, ptr: self_.ptr, dim: self_.dim, strides: self_.strides, diff --git a/src/dimension/axes.rs b/src/dimension/axes.rs index f087eb5c5..c1483ac9b 100644 --- a/src/dimension/axes.rs +++ b/src/dimension/axes.rs @@ -1,4 +1,3 @@ - use crate::{Dimension, Axis, Ix, Ixs}; /// Create a new Axes iterator @@ -7,7 +6,7 @@ pub fn axes_of<'a, D>(d: &'a D, strides: &'a D) -> Axes<'a, D> { Axes { dim: d, - strides: strides, + strides, start: 0, end: d.ndim(), } @@ -56,6 +55,9 @@ impl AxisDescription { /// Return stride #[inline(always)] pub fn stride(self) -> Ixs { self.2 } + /// Returns True if axis is of length 0 + #[inline(always)] + pub fn is_empty(self) -> bool { self.len() == 0 } } copy_and_clone!(['a, D] Axes<'a, D>); @@ -128,4 +130,3 @@ impl IncOps for usize { *self } } - diff --git a/src/dimension/axis.rs b/src/dimension/axis.rs index f09ee95d8..bf6380b13 100644 --- a/src/dimension/axis.rs +++ b/src/dimension/axis.rs @@ -21,7 +21,7 @@ pub struct Axis(pub usize); impl Axis { /// Return the index of the axis. #[inline(always)] - pub fn index(&self) -> usize { self.0 } + pub fn index(self) -> usize { self.0 } } copy_and_clone!{Axis} @@ -39,4 +39,3 @@ macro_rules! derive_cmp { derive_cmp!{PartialEq for Axis, eq -> bool} derive_cmp!{PartialOrd for Axis, partial_cmp -> Option} - diff --git a/src/dimension/dim.rs b/src/dimension/dim.rs index 4adb71658..c508a68d9 100644 --- a/src/dimension/dim.rs +++ b/src/dimension/dim.rs @@ -45,7 +45,7 @@ impl Dim { /// Private constructor and accessors for Dim pub(crate) fn new(index: I) -> Dim { Dim { - index: index, + index, } } #[inline(always)] @@ -181,4 +181,3 @@ impl_op!(Sub, sub, SubAssign, sub_assign, sub); impl_single_op!(Sub, sub, SubAssign, sub_assign, sub); impl_op!(Mul, mul, MulAssign, mul_assign, mul); impl_scalar_op!(Mul, mul, MulAssign, mul_assign, mul); - diff --git a/src/dimension/dimension_trait.rs b/src/dimension/dimension_trait.rs index f8357f069..4cccd482c 100644 --- a/src/dimension/dimension_trait.rs +++ b/src/dimension/dimension_trait.rs @@ -356,7 +356,7 @@ macro_rules! impl_insert_axis_array( debug_assert!(axis.index() <= $n); let mut out = [1; $n + 1]; out[0..axis.index()].copy_from_slice(&self.slice()[0..axis.index()]); - out[axis.index()+1..$n+1].copy_from_slice(&self.slice()[axis.index()..$n]); + out[axis.index()+1..=$n].copy_from_slice(&self.slice()[axis.index()..$n]); Dim(out) } ); diff --git a/src/dimension/dynindeximpl.rs b/src/dimension/dynindeximpl.rs index 4af929c86..97c1e09e9 100644 --- a/src/dimension/dynindeximpl.rs +++ b/src/dimension/dynindeximpl.rs @@ -1,4 +1,3 @@ - use std::ops::{ Index, IndexMut, @@ -59,9 +58,8 @@ impl IxDynRepr { pub fn copy_from(x: &[T]) -> Self { if x.len() <= CAP { let mut arr = [T::zero(); CAP]; - for i in 0..x.len() { - arr[i] = x[i]; - } + // copy x elements to arr + arr[..x.len()].clone_from_slice(&x[..]); IxDynRepr::Inline(x.len() as _, arr) } else { Self::from(x) @@ -134,7 +132,7 @@ impl IxDynImpl { if len < CAP { let mut out = [1; CAP]; out[0..i].copy_from_slice(&self[0..i]); - out[i+1..len+1].copy_from_slice(&self[i..len]); + out[i+1..=len].copy_from_slice(&self[i..len]); IxDynRepr::Inline((len + 1) as u32, out) } else { let mut out = Vec::with_capacity(len + 1); @@ -218,7 +216,7 @@ impl<'a> IntoIterator for &'a IxDynImpl { type IntoIter = <&'a [Ix] as IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - self[..].into_iter() + self[..].iter() } } @@ -233,7 +231,7 @@ impl IxDyn { /// Create a new dimension value with `n` axes, all zeros #[inline] pub fn zeros(n: usize) -> IxDyn { - const ZEROS: &'static [usize] = &[0; 4]; + const ZEROS: &[usize] = &[0; 4]; if n <= ZEROS.len() { Dim(&ZEROS[..n]) } else { diff --git a/src/dimension/mod.rs b/src/dimension/mod.rs index aebcfc662..a3d8f1c0e 100644 --- a/src/dimension/mod.rs +++ b/src/dimension/mod.rs @@ -262,13 +262,11 @@ pub trait DimensionExt { /// Get the dimension at `axis`. /// /// *Panics* if `axis` is out of bounds. - #[inline] fn axis(&self, axis: Axis) -> Ix; /// Set the dimension at `axis`. /// /// *Panics* if `axis` is out of bounds. - #[inline] fn set_axis(&mut self, axis: Axis, value: Ix); } @@ -520,12 +518,11 @@ fn slice_min_max(axis_len: usize, slice: Slice) -> Option<(usize, usize)> { let (start, end, step) = to_abs_slice(axis_len, slice); if start == end { None + } + else if step > 0 { + Some((start, end - 1 - (end - start - 1) % (step as usize))) } else { - if step > 0 { - Some((start, end - 1 - (end - start - 1) % (step as usize))) - } else { - Some((start + (end - start - 1) % (-step as usize), end - 1)) - } + Some((start + (end - start - 1) % (-step as usize), end - 1)) } } diff --git a/src/impl_1d.rs b/src/impl_1d.rs index 8e4e11ae4..d31f23a01 100644 --- a/src/impl_1d.rs +++ b/src/impl_1d.rs @@ -27,4 +27,3 @@ impl ArrayBase } } } - diff --git a/src/impl_clone.rs b/src/impl_clone.rs index af009feb4..013f070ca 100644 --- a/src/impl_clone.rs +++ b/src/impl_clone.rs @@ -13,8 +13,8 @@ impl Clone for ArrayBase { unsafe { let (data, ptr) = self.data.clone_with_ptr(self.ptr); ArrayBase { - data: data, - ptr: ptr, + data, + ptr, dim: self.dim.clone(), strides: self.strides.clone(), } diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 1c2b84439..883424393 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -246,7 +246,7 @@ impl ArrayBase unsafe { Self::from_shape_vec_unchecked(shape, v) } } else { let dim = shape.dim.clone(); - let v = to_vec_mapped(indexes::indices_iter_f(dim).into_iter(), f); + let v = to_vec_mapped(indexes::indices_iter_f(dim), f); unsafe { Self::from_shape_vec_unchecked(shape, v) } } } @@ -342,8 +342,8 @@ impl ArrayBase ArrayBase { ptr: v.as_mut_ptr(), data: DataOwned::new(v), - strides: strides, - dim: dim + strides, + dim } } diff --git a/src/impl_methods.rs b/src/impl_methods.rs index de74af795..e81206599 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -206,7 +206,7 @@ where { let data = self.data.into_shared(); ArrayBase { - data: data, + data, ptr: self.ptr, dim: self.dim, strides: self.strides, @@ -353,9 +353,9 @@ where let mut new_dim = Do::zeros(out_ndim); let mut new_strides = Do::zeros(out_ndim); izip!(self.dim.slice(), self.strides.slice(), indices) - .filter_map(|(d, s, slice_or_index)| match slice_or_index { - &SliceOrIndex::Slice {..} => Some((d, s)), - &SliceOrIndex::Index(_) => None, + .filter_map(|(d, s, slice_or_index)| match *slice_or_index { + SliceOrIndex::Slice {..} => Some((d, s)), + SliceOrIndex::Index(_) => None, }) .zip(izip!(new_dim.slice_mut(), new_strides.slice_mut())) .for_each(|((d, s), (new_d, new_s))| { @@ -391,11 +391,11 @@ where indices .iter() .enumerate() - .for_each(|(axis, slice_or_index)| match slice_or_index { - &SliceOrIndex::Slice { start, end, step } => { + .for_each(|(axis, slice_or_index)| match *slice_or_index { + SliceOrIndex::Slice { start, end, step } => { self.slice_axis_inplace(Axis(axis), Slice { start, end, step }) } - &SliceOrIndex::Index(index) => { + SliceOrIndex::Index(index) => { let i_usize = abs_index(self.len_of(Axis(axis)), index); self.collapse_axis(Axis(axis), i_usize) } @@ -1101,8 +1101,7 @@ where /* empty shape has len 1 */ let len = self.dim.slice().iter().cloned().min().unwrap_or(1); let stride = self.strides() - .iter() - .fold(0, |sum, s| sum + s); + .iter().sum(); (len, stride) } @@ -1166,9 +1165,8 @@ where /// contiguous in memory, it has custom strides, etc. pub fn is_standard_layout(&self) -> bool { fn is_standard_layout(dim: &D, strides: &D) -> bool { - match D::NDIM { - Some(1) => return strides[0] == 1 || dim[0] <= 1, - _ => { } + if let Some(1) = D::NDIM { + return strides[0] == 1 || dim[0] <= 1; } if dim.slice().iter().any(|&d| d == 0) { return true; @@ -1379,7 +1377,7 @@ where dim: shape, } } else { - let v = self.iter().map(|x| x.clone()).collect::>(); + let v = self.iter().cloned().collect::>(); unsafe { ArrayBase::from_shape_vec_unchecked(shape, v) } @@ -1425,8 +1423,8 @@ where return Ok(ArrayBase { data: self.data, ptr: self.ptr, - dim: dim, - strides: strides, + dim, + strides, }); } } @@ -1737,7 +1735,7 @@ where Some(slc) => { let ptr = slc.as_ptr() as *mut A; let end = unsafe { - ptr.offset(slc.len() as isize) + ptr.add(slc.len()) }; self.ptr >= ptr && self.ptr <= end } diff --git a/src/impl_raw_views.rs b/src/impl_raw_views.rs index 5cb8b13b6..84cca12b7 100644 --- a/src/impl_raw_views.rs +++ b/src/impl_raw_views.rs @@ -15,7 +15,7 @@ where RawArrayView { data: RawViewRepr::new(), ptr: ptr as *mut A, - dim: dim, + dim, strides: strides, } } @@ -118,9 +118,9 @@ where pub(crate) unsafe fn new_(ptr: *mut A, dim: D, strides: D) -> Self { RawArrayViewMut { data: RawViewRepr::new(), - ptr: ptr, - dim: dim, - strides: strides, + ptr, + dim, + strides, } } diff --git a/src/impl_views.rs b/src/impl_views.rs index e4ac909db..d13882593 100644 --- a/src/impl_views.rs +++ b/src/impl_views.rs @@ -486,8 +486,8 @@ impl<'a, A, D> ArrayView<'a, A, D> ArrayView { data: ViewRepr::new(), ptr: ptr as *mut A, - dim: dim, - strides: strides, + dim, + strides, } } @@ -533,9 +533,9 @@ impl<'a, A, D> ArrayViewMut<'a, A, D> } ArrayViewMut { data: ViewRepr::new(), - ptr: ptr, - dim: dim, - strides: strides, + ptr, + dim, + strides, } } @@ -586,4 +586,3 @@ impl<'a, A, D> ArrayViewMut<'a, A, D> AxisIterMut::new(self, Axis(0)) } } - diff --git a/src/indexes.rs b/src/indexes.rs index 3c83ad510..640063fa5 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -32,7 +32,7 @@ pub fn indices(shape: E) -> Indices let dim = shape.into_dimension(); Indices { start: E::Dim::zeros(dim.ndim()), - dim: dim, + dim, } } @@ -90,7 +90,7 @@ impl IntoIterator for Indices let sz = self.dim.size(); let index = if sz != 0 { Some(self.start) } else { None }; IndicesIter { - index: index, + index, dim: self.dim, } } @@ -135,7 +135,7 @@ impl NdProducer for Indices { #[doc(hidden)] fn raw_dim(&self) -> Self::Dim { - self.dim.clone() + self.dim } #[doc(hidden)] @@ -168,7 +168,7 @@ impl NdProducer for Indices { unsafe fn uget_ptr(&self, i: &Self::Dim) -> Self::Ptr { let mut index = *i; index += &self.start; - IndexPtr { index: index } + IndexPtr { index } } #[doc(hidden)] @@ -214,7 +214,7 @@ pub fn indices_iter_f(shape: E) -> IndicesIterF IndicesIterF { has_remaining: dim.size_checked() != Some(0), index: zero, - dim: dim, + dim, } } diff --git a/src/iterators/chunks.rs b/src/iterators/chunks.rs index a56914e3d..4e098a682 100644 --- a/src/iterators/chunks.rs +++ b/src/iterators/chunks.rs @@ -1,4 +1,3 @@ - use crate::imp_prelude::*; use crate::IntoDimension; use crate::{NdProducer, Layout}; @@ -59,8 +58,8 @@ impl<'a, A, D: Dimension> ExactChunks<'a, A, D> { ExactChunks { base: a, - chunk: chunk, - inner_strides: inner_strides, + chunk, + inner_strides, } } } @@ -142,8 +141,8 @@ impl<'a, A, D: Dimension> ExactChunksMut<'a, A, D> { ExactChunksMut { base: a, - chunk: chunk, - inner_strides: inner_strides, + chunk, + inner_strides, } } } diff --git a/src/iterators/mod.rs b/src/iterators/mod.rs index b9aab1aa9..c9c67275a 100644 --- a/src/iterators/mod.rs +++ b/src/iterators/mod.rs @@ -61,7 +61,7 @@ impl Baseiter { #[inline] pub unsafe fn new(ptr: *mut A, len: D, stride: D) -> Baseiter { Baseiter { - ptr: ptr, + ptr, index: len.first_index(), dim: len, strides: stride, @@ -94,23 +94,19 @@ impl Iterator for Baseiter { let ndim = self.dim.ndim(); debug_assert_ne!(ndim, 0); let mut accum = init; - loop { - if let Some(mut index) = self.index.clone() { - let stride = self.strides.last_elem() as isize; - let elem_index = index.last_elem(); - let len = self.dim.last_elem(); - let offset = D::stride_offset(&index, &self.strides); - unsafe { - let row_ptr = self.ptr.offset(offset); - for i in 0..(len - elem_index) { - accum = g(accum, row_ptr.offset(i as isize * stride)); - } + while let Some(mut index) = self.index.clone() { + let stride = self.strides.last_elem() as isize; + let elem_index = index.last_elem(); + let len = self.dim.last_elem(); + let offset = D::stride_offset(&index, &self.strides); + unsafe { + let row_ptr = self.ptr.offset(offset); + for i in 0..(len - elem_index) { + accum = g(accum, row_ptr.offset(i as isize * stride)); } - index.set_last_elem(len - 1); + } + index.set_last_elem(len - 1); self.index = self.dim.next_for(index); - } else { - break; - }; } accum } @@ -671,7 +667,7 @@ impl AxisIterCore { AxisIterCore { index: 0, len: shape, - stride: stride, + stride, inner_dim: v.dim.remove_axis(axis), inner_strides: v.strides.remove_axis(axis), ptr: v.ptr, @@ -1084,8 +1080,8 @@ fn chunk_iter_parts(v: ArrayView, axis: Axis, size: usize let iter = AxisIterCore { index: 0, len: iter_len, - stride: stride, - inner_dim: inner_dim, + stride, + inner_dim, inner_strides: v.strides, ptr: v.ptr, }; @@ -1097,9 +1093,9 @@ impl<'a, A, D: Dimension> AxisChunksIter<'a, A, D> { pub(crate) fn new(v: ArrayView<'a, A, D>, axis: Axis, size: usize) -> Self { let (iter, n_whole_chunks, last_dim) = chunk_iter_parts(v, axis, size); AxisChunksIter { - iter: iter, - n_whole_chunks: n_whole_chunks, - last_dim: last_dim, + iter, + n_whole_chunks, + last_dim, life: PhantomData, } } @@ -1186,9 +1182,9 @@ impl<'a, A, D: Dimension> AxisChunksIterMut<'a, A, D> { pub(crate) fn new(v: ArrayViewMut<'a, A, D>, axis: Axis, size: usize) -> Self { let (iter, len, last_dim) = chunk_iter_parts(v.into_view(), axis, size); AxisChunksIterMut { - iter: iter, + iter, n_whole_chunks: len, - last_dim: last_dim, + last_dim, life: PhantomData, } } diff --git a/src/iterators/windows.rs b/src/iterators/windows.rs index 5846832de..4e302bcc1 100644 --- a/src/iterators/windows.rs +++ b/src/iterators/windows.rs @@ -1,4 +1,3 @@ - use crate::imp_prelude::*; use super::ElementsBase; use crate::IntoDimension; @@ -37,7 +36,7 @@ impl<'a, A, D: Dimension> Windows<'a, A, D> { unsafe { Windows { base: ArrayView::from_shape_ptr(size.clone().strides(a.strides), a.ptr), - window: window, + window, strides: window_strides, } } diff --git a/src/layout/layoutfmt.rs b/src/layout/layoutfmt.rs index 6312a3417..08c2f636e 100644 --- a/src/layout/layoutfmt.rs +++ b/src/layout/layoutfmt.rs @@ -1,4 +1,3 @@ - // Copyright 2017 bluss and ndarray developers. // // Licensed under the Apache License, Version 2.0 ArrayBase let mut sum = A::zero(); for i in 0..self.len() { unsafe { - sum = sum.clone() + self.uget(i).clone() * rhs.uget(i).clone(); + sum = sum + *self.uget(i) * *rhs.uget(i); } } sum @@ -495,7 +495,7 @@ fn mat_mul_general(alpha: A, } } else { // It's a no-op if `c` has zero length. - if c.len() == 0 { + if c.is_empty() { return; } diff --git a/src/linspace.rs b/src/linspace.rs index 51ede80a7..fe6ab6b4f 100644 --- a/src/linspace.rs +++ b/src/linspace.rs @@ -80,7 +80,7 @@ pub fn linspace(a: F, b: F, n: usize) -> Linspace }; Linspace { start: a, - step: step, + step, index: 0, len: n, } @@ -101,7 +101,7 @@ pub fn range(a: F, b: F, step: F) -> Linspace let steps = F::ceil(len / step); Linspace { start: a, - step: step, + step, len: steps.to_usize().unwrap(), index: 0, } diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 2806afba3..7515c1125 100644 --- a/src/macro_utils.rs +++ b/src/macro_utils.rs @@ -1,4 +1,3 @@ - /// Derive Copy and Clone using the parameters (and bounds) as specified in [] macro_rules! copy_and_clone { ([$($parm:tt)*] $type_:ty) => { @@ -60,7 +59,7 @@ macro_rules! expand_if { #[cfg(debug_assertions)] macro_rules! debug_bounds_check { ($self_:ident, $index:expr) => { - if let None = $index.index_checked(&$self_.dim, &$self_.strides) { + if $index.index_checked(&$self_.dim, &$self_.strides).is_none() { panic!("ndarray: index {:?} is out of bounds for array of shape {:?}", $index, $self_.shape()); } @@ -71,4 +70,3 @@ macro_rules! debug_bounds_check { macro_rules! debug_bounds_check { ($self_:ident, $index:expr) => { }; } - diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index f8c1f04a3..95cea501a 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -145,7 +145,7 @@ impl ArrayBase { let n = A::from_usize(self.len_of(axis)).expect("Converting axis length to `A` must not fail."); let sum = self.sum_axis(axis); - sum / &aview0(&n) + sum / aview0(&n) } /// Return variance along `axis`. @@ -288,4 +288,3 @@ impl ArrayBase }).is_done() } } - diff --git a/src/slice.rs b/src/slice.rs index cb86900ba..2dceb4054 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -110,16 +110,16 @@ copy_and_clone!{SliceOrIndex} impl SliceOrIndex { /// Returns `true` if `self` is a `Slice` value. pub fn is_slice(&self) -> bool { - match self { - &SliceOrIndex::Slice { .. } => true, + match *self { + SliceOrIndex::Slice { .. } => true, _ => false, } } /// Returns `true` if `self` is an `Index` value. pub fn is_index(&self) -> bool { - match self { - &SliceOrIndex::Index(_) => true, + match *self { + SliceOrIndex::Index(_) => true, _ => false, } } @@ -317,8 +317,8 @@ where #[doc(hidden)] pub unsafe fn new_unchecked(indices: T, out_dim: PhantomData) -> SliceInfo { SliceInfo { - out_dim: out_dim, - indices: indices, + out_dim, + indices, } } } @@ -339,7 +339,7 @@ where } Ok(SliceInfo { out_dim: PhantomData, - indices: indices, + indices, }) } } diff --git a/src/stacking.rs b/src/stacking.rs index a2dc55852..883655e8c 100644 --- a/src/stacking.rs +++ b/src/stacking.rs @@ -34,7 +34,7 @@ pub fn stack<'a, A, D>(axis: Axis, arrays: &[ArrayView<'a, A, D>]) where A: Copy, D: RemoveAxis { - if arrays.len() == 0 { + if arrays.is_empty() { return Err(from_kind(ErrorKind::Unsupported)); } let mut res_dim = arrays[0].raw_dim();