MCPcopy Create free account
hub / github.com/ciphermodelabs/ciphercore / slice_index

Function slice_index

ciphercore-base/src/slices.rs:24–64  ·  view source on GitHub ↗

Assumes that `slice` is a correct slice, in particular that `get_slice_shape(shape, slice)` does not return an error.

(shape: ArrayShape, slice: Slice, index: ArrayShape)

Source from the content-addressed store, hash-verified

22/// Assumes that `slice` is a correct slice, in particular that
23/// `get_slice_shape(shape, slice)` does not return an error.
24pub fn slice_index(shape: ArrayShape, slice: Slice, index: ArrayShape) -> Result<ArrayShape> {
25 let clean_slice = get_clean_slice(shape.clone(), slice)?;
26 let mut result_index: Vec<u64> = vec![];
27 let mut j = 0;
28 for i in 0..shape.len() {
29 if i < clean_slice.len() {
30 match clean_slice[i] {
31 SliceElement::SingleIndex(ind) => {
32 let real_ind = if ind >= 0 { ind } else { ind + shape[i] as i64 };
33 if real_ind < 0 {
34 panic!("Should not be here!");
35 }
36 result_index.push(real_ind as u64);
37 }
38 SliceElement::SubArray(_, _, _) => {
39 if j >= index.len() {
40 return Err(runtime_error!("Index is too short"));
41 }
42 result_index.push(slice_1d_index(shape[i], clean_slice[i].clone(), index[j])?);
43 j += 1;
44 }
45 SliceElement::Ellipsis => {
46 panic!("Should not be here!");
47 }
48 }
49 } else {
50 if j >= index.len() {
51 return Err(runtime_error!("Index is too short"));
52 }
53 result_index.push(index[j]);
54 j += 1;
55 }
56 }
57 if j == 0 && index.len() == 1 && index[0] == 0 {
58 return Ok(result_index);
59 }
60 if j != index.len() {
61 return Err(runtime_error!("Index is too long"));
62 }
63 Ok(result_index)
64}
65
66fn slice_1d_index(dimension: u64, slice_element: SliceElement, index: u64) -> Result<u64> {
67 let (begin, _, step) = normalize_subarray(dimension, slice_element)?;

Callers 1

evaluate_nodeMethod · 0.85

Calls 4

get_clean_sliceFunction · 0.85
slice_1d_indexFunction · 0.85
cloneMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected