From 516294b7d95baffa1508392dfc4b240264e254bf Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Wed, 26 May 2021 22:20:08 -0400 Subject: [PATCH 1/2] Add .last() and .last_mut() methods to ArrayBase --- src/impl_methods.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 28098f68f..f556f9b4e 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -280,6 +280,66 @@ where } } + /// Returns a reference to the last element of the array, or `None` if it + /// is empty. + /// + /// # Example + /// + /// ```rust + /// use ndarray::Array3; + /// + /// let mut a = Array3::::zeros([3, 4, 2]); + /// a[[2, 3, 1]] = 42.; + /// assert_eq!(a.last(), Some(&42.)); + /// + /// let b = Array3::::zeros([3, 0, 5]); + /// assert_eq!(b.last(), None); + /// ``` + pub fn last(&self) -> Option<&A> + where + S: Data, + { + if self.is_empty() { + None + } else { + let mut index = self.raw_dim(); + for ax in 0..index.ndim() { + index[ax] -= 1; + } + Some(unsafe { self.uget(index) }) + } + } + + /// Returns a mutable reference to the last element of the array, or `None` + /// if it is empty. + /// + /// # Example + /// + /// ```rust + /// use ndarray::Array3; + /// + /// let mut a = Array3::::zeros([3, 4, 2]); + /// *a.last_mut().unwrap() = 42.; + /// assert_eq!(a[[2, 3, 1]], 42.); + /// + /// let mut b = Array3::::zeros([3, 0, 5]); + /// assert_eq!(b.last_mut(), None); + /// ``` + pub fn last_mut(&mut self) -> Option<&mut A> + where + S: DataMut, + { + if self.is_empty() { + None + } else { + let mut index = self.raw_dim(); + for ax in 0..index.ndim() { + index[ax] -= 1; + } + Some(unsafe { self.uget_mut(index) }) + } + } + /// Return an iterator of references to the elements of the array. /// /// Elements are visited in the *logical order* of the array, which From 68f258c0d82d1f78f5428991246a33d279b58565 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Wed, 26 May 2021 22:24:04 -0400 Subject: [PATCH 2/2] Add examples to docs for .first() and .first_mut() --- src/impl_methods.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/impl_methods.rs b/src/impl_methods.rs index f556f9b4e..8b1ace803 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -256,6 +256,19 @@ where /// Returns a reference to the first element of the array, or `None` if it /// is empty. + /// + /// # Example + /// + /// ```rust + /// use ndarray::Array3; + /// + /// let mut a = Array3::::zeros([3, 4, 2]); + /// a[[0, 0, 0]] = 42.; + /// assert_eq!(a.first(), Some(&42.)); + /// + /// let b = Array3::::zeros([3, 0, 5]); + /// assert_eq!(b.first(), None); + /// ``` pub fn first(&self) -> Option<&A> where S: Data, @@ -269,6 +282,19 @@ where /// Returns a mutable reference to the first element of the array, or /// `None` if it is empty. + /// + /// # Example + /// + /// ```rust + /// use ndarray::Array3; + /// + /// let mut a = Array3::::zeros([3, 4, 2]); + /// *a.first_mut().unwrap() = 42.; + /// assert_eq!(a[[0, 0, 0]], 42.); + /// + /// let mut b = Array3::::zeros([3, 0, 5]); + /// assert_eq!(b.first_mut(), None); + /// ``` pub fn first_mut(&mut self) -> Option<&mut A> where S: DataMut,