Skip to content

Commit 424f759

Browse files
Shankmukh Kiran SagarShanmukh kiran Sagar
authored andcommitted
feat: add contains() method to check if index is within array bounds
1 parent 0612fb7 commit 424f759

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/impl_methods.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,24 @@ impl<A, D: Dimension> ArrayRef<A, D>
823823
{
824824
unsafe { self.get_ptr(index).map(|ptr| &*ptr) }
825825
}
826+
827+
/// Return `true` if the index is within the array bounds.
828+
///
829+
/// ```
830+
/// use ndarray::arr2;
831+
///
832+
/// let a = arr2(&[[1., 2.],
833+
/// [3., 4.]]);
834+
///
835+
/// assert!(a.contains((0, 1)));
836+
/// assert!(!a.contains((2, 0)));
837+
/// assert!(!a.contains((0, 2)));
838+
/// ```
839+
pub fn contains<I>(&self, index: I) -> bool
840+
where I: NdIndex<D>
841+
{
842+
index.index_checked(self._dim(), self._strides()).is_some()
843+
}
826844
}
827845

828846
impl<A, D: Dimension> RawRef<A, D>

0 commit comments

Comments
 (0)