(dimension: u64, slice_element: SliceElement)
| 125 | } |
| 126 | |
| 127 | fn get_slice_shape_1d(dimension: u64, slice_element: SliceElement) -> Result<Option<u64>> { |
| 128 | match slice_element { |
| 129 | SliceElement::SingleIndex(mut ind) => { |
| 130 | if ind < 0 { |
| 131 | ind += dimension as i64; |
| 132 | } |
| 133 | if ind < 0 || ind >= dimension as i64 { |
| 134 | Err(runtime_error!("Slice is out of bounds (SingleIndex)")) |
| 135 | } else { |
| 136 | Ok(None) |
| 137 | } |
| 138 | } |
| 139 | SliceElement::SubArray(_, _, _) => { |
| 140 | let (begin, end, step) = normalize_subarray(dimension, slice_element)?; |
| 141 | let mut current = begin; |
| 142 | let mut counter = 0; |
| 143 | loop { |
| 144 | if (step > 0 && current >= end) || (step < 0 && current <= end) { |
| 145 | break; |
| 146 | } |
| 147 | if current < 0 || current >= dimension as i64 { |
| 148 | return Err(runtime_error!( |
| 149 | "Slicing index is out of bounds: {} not in [{}, {})", |
| 150 | current, |
| 151 | 0, |
| 152 | dimension |
| 153 | )); |
| 154 | } |
| 155 | counter += 1; |
| 156 | current += step; |
| 157 | } |
| 158 | if counter == 0 { |
| 159 | return Err(runtime_error!("Empty slice")); |
| 160 | } |
| 161 | Ok(Some(counter)) |
| 162 | } |
| 163 | SliceElement::Ellipsis => { |
| 164 | panic!("Should not be here!"); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[cfg(test)] |
| 170 | mod tests { |
no test coverage detected