Skip to content

Commit 770aa1e

Browse files
authored
make dims_to_flat and i_dims_to_flat accept an iter (#725)
1 parent c14536b commit 770aa1e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/shape.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
borrow::Cow,
2+
borrow::{Borrow, Cow},
33
fmt,
44
hash::Hash,
55
ops::{Deref, DerefMut, Index, RangeBounds},
@@ -173,19 +173,27 @@ impl Shape {
173173
}
174174
index.reverse();
175175
}
176-
pub(crate) fn dims_to_flat(&self, index: &[usize]) -> Option<usize> {
176+
pub(crate) fn dims_to_flat(
177+
&self,
178+
index: impl IntoIterator<Item = impl Borrow<usize>>,
179+
) -> Option<usize> {
177180
let mut flat = 0;
178-
for (&dim, &i) in self.dims.iter().zip(index) {
181+
for (&dim, i) in self.dims.iter().zip(index) {
182+
let i = *i.borrow();
179183
if i >= dim {
180184
return None;
181185
}
182186
flat = flat * dim + i;
183187
}
184188
Some(flat)
185189
}
186-
pub(crate) fn i_dims_to_flat(&self, index: &[isize]) -> Option<usize> {
190+
pub(crate) fn i_dims_to_flat(
191+
&self,
192+
index: impl IntoIterator<Item = impl Borrow<isize>>,
193+
) -> Option<usize> {
187194
let mut flat = 0;
188-
for (&dim, &i) in self.dims.iter().zip(index) {
195+
for (&dim, i) in self.dims.iter().zip(index) {
196+
let i = *i.borrow();
189197
if i < 0 || i >= dim as isize {
190198
return None;
191199
}

0 commit comments

Comments
 (0)