Skip to content

Commit 6faac1e

Browse files
authored
Merge pull request #1013 from jturner314/array-last-method
Add .last() and .last_mut() methods
2 parents 81662ae + 68f258c commit 6faac1e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/impl_methods.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ where
256256

257257
/// Returns a reference to the first element of the array, or `None` if it
258258
/// is empty.
259+
///
260+
/// # Example
261+
///
262+
/// ```rust
263+
/// use ndarray::Array3;
264+
///
265+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
266+
/// a[[0, 0, 0]] = 42.;
267+
/// assert_eq!(a.first(), Some(&42.));
268+
///
269+
/// let b = Array3::<f64>::zeros([3, 0, 5]);
270+
/// assert_eq!(b.first(), None);
271+
/// ```
259272
pub fn first(&self) -> Option<&A>
260273
where
261274
S: Data,
@@ -269,6 +282,19 @@ where
269282

270283
/// Returns a mutable reference to the first element of the array, or
271284
/// `None` if it is empty.
285+
///
286+
/// # Example
287+
///
288+
/// ```rust
289+
/// use ndarray::Array3;
290+
///
291+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
292+
/// *a.first_mut().unwrap() = 42.;
293+
/// assert_eq!(a[[0, 0, 0]], 42.);
294+
///
295+
/// let mut b = Array3::<f64>::zeros([3, 0, 5]);
296+
/// assert_eq!(b.first_mut(), None);
297+
/// ```
272298
pub fn first_mut(&mut self) -> Option<&mut A>
273299
where
274300
S: DataMut,
@@ -280,6 +306,66 @@ where
280306
}
281307
}
282308

309+
/// Returns a reference to the last element of the array, or `None` if it
310+
/// is empty.
311+
///
312+
/// # Example
313+
///
314+
/// ```rust
315+
/// use ndarray::Array3;
316+
///
317+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
318+
/// a[[2, 3, 1]] = 42.;
319+
/// assert_eq!(a.last(), Some(&42.));
320+
///
321+
/// let b = Array3::<f64>::zeros([3, 0, 5]);
322+
/// assert_eq!(b.last(), None);
323+
/// ```
324+
pub fn last(&self) -> Option<&A>
325+
where
326+
S: Data,
327+
{
328+
if self.is_empty() {
329+
None
330+
} else {
331+
let mut index = self.raw_dim();
332+
for ax in 0..index.ndim() {
333+
index[ax] -= 1;
334+
}
335+
Some(unsafe { self.uget(index) })
336+
}
337+
}
338+
339+
/// Returns a mutable reference to the last element of the array, or `None`
340+
/// if it is empty.
341+
///
342+
/// # Example
343+
///
344+
/// ```rust
345+
/// use ndarray::Array3;
346+
///
347+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
348+
/// *a.last_mut().unwrap() = 42.;
349+
/// assert_eq!(a[[2, 3, 1]], 42.);
350+
///
351+
/// let mut b = Array3::<f64>::zeros([3, 0, 5]);
352+
/// assert_eq!(b.last_mut(), None);
353+
/// ```
354+
pub fn last_mut(&mut self) -> Option<&mut A>
355+
where
356+
S: DataMut,
357+
{
358+
if self.is_empty() {
359+
None
360+
} else {
361+
let mut index = self.raw_dim();
362+
for ax in 0..index.ndim() {
363+
index[ax] -= 1;
364+
}
365+
Some(unsafe { self.uget_mut(index) })
366+
}
367+
}
368+
283369
/// Return an iterator of references to the elements of the array.
284370
///
285371
/// Elements are visited in the *logical order* of the array, which

0 commit comments

Comments
 (0)