Skip to content

Commit 0a1522b

Browse files
Shankmukh Kiran SagarShankmukh Kiran Sagar
authored andcommitted
feat: add contains() method to check if index is within array bounds
1 parent 27af864 commit 0a1522b

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
@@ -815,6 +815,24 @@ impl<A, D: Dimension> ArrayRef<A, D>
815815
{
816816
unsafe { self.get_ptr(index).map(|ptr| &*ptr) }
817817
}
818+
819+
/// Return `true` if the index is within the array bounds.
820+
///
821+
/// ```
822+
/// use ndarray::arr2;
823+
///
824+
/// let a = arr2(&[[1., 2.],
825+
/// [3., 4.]]);
826+
///
827+
/// assert!(a.contains((0, 1)));
828+
/// assert!(!a.contains((2, 0)));
829+
/// assert!(!a.contains((0, 2)));
830+
/// ```
831+
pub fn contains<I>(&self, index: I) -> bool
832+
where I: NdIndex<D>
833+
{
834+
index.index_checked(self._dim(), self._strides()).is_some()
835+
}
818836
}
819837

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

0 commit comments

Comments
 (0)