MCPcopy Index your code
hub / github.com/endbasic/endbasic / flat_index

Method flat_index

core/src/mem.rs:36–56  ·  view source on GitHub ↗

Computes the flat index into `values` for the given `subscripts`, with bounds checking.

(&self, subscripts: &[i32])

Source from the content-addressed store, hash-verified

34impl 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

Callers 3

deref_array_integerFunction · 0.80
get_array_varMethod · 0.80
resolve_array_indexMethod · 0.80

Calls 1

iterMethod · 0.80

Tested by

no test coverage detected