Computes the flat index into `values` for the given `subscripts`, with bounds checking.
(&self, subscripts: &[i32])
| 34 | impl ArrayData { |
| 35 | /// Computes the flat index into `values` for the given `subscripts`, with bounds checking. |
| 36 | pub(crate) fn flat_index(&self, subscripts: &[i32]) -> Result<usize, String> { |
| 37 | debug_assert_eq!( |
| 38 | subscripts.len(), |
| 39 | self.dimensions.len(), |
| 40 | "Invalid number of subscripts; guaranteed valid by the compiler" |
| 41 | ); |
| 42 | |
| 43 | let mut offset = 0; |
| 44 | let mut multiplier = 1; |
| 45 | for (s, d) in subscripts.iter().zip(&self.dimensions) { |
| 46 | let Ok(s) = usize::try_from(*s) else { |
| 47 | return Err(format!("Subscript {} cannot be negative", s)); |
| 48 | }; |
| 49 | if s >= *d { |
| 50 | return Err(format!("Subscript {} exceeds limit of {}", s, d)); |
| 51 | } |
| 52 | offset += s * multiplier; |
| 53 | multiplier *= d; |
| 54 | } |
| 55 | Ok(offset) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// A typed scalar value, used both in the compile-time constant pool and as a |
no test coverage detected