| 18 | |
| 19 | template <typename I> |
| 20 | int new_to_old_offset(int new_offset, const icm::shape& old_shape, I begin, I end) |
| 21 | { |
| 22 | icm::small_vector<int> old_strides(old_shape.size(), 1); |
| 23 | icm::small_vector<int> new_strides(old_shape.size(), 1); |
| 24 | auto os = 1; |
| 25 | auto ns = 1; |
| 26 | auto distance = std::distance(begin, end); |
| 27 | for (auto i = old_shape.size() - 1; i > 0; --i) { |
| 28 | os *= old_shape[i]; |
| 29 | if (i < distance) { |
| 30 | ns *= (begin + i)->size(); |
| 31 | } else { |
| 32 | ns *= old_shape[i]; |
| 33 | } |
| 34 | old_strides[i - 1] = os; |
| 35 | new_strides[i - 1] = ns; |
| 36 | } |
| 37 | auto old_offset = 0; |
| 38 | I it = begin; |
| 39 | for (auto i = 0; i < old_shape.size(); ++i) { |
| 40 | if (it != end) { |
| 41 | auto ci = (*it)[new_offset / new_strides[i]]; |
| 42 | if (ci >= old_shape[i]) { |
| 43 | throw invalid_operation("Subscript index is out of array bounds."); |
| 44 | } |
| 45 | old_offset += old_strides[i] * ci; |
| 46 | ++it; |
| 47 | } else { |
| 48 | old_offset += old_strides[i] * (new_offset / new_strides[i]); |
| 49 | } |
| 50 | new_offset %= new_strides[i]; |
| 51 | } |
| 52 | return old_offset; |
| 53 | } |
| 54 | |
| 55 | template <typename T, typename I> |
| 56 | class single_strided_array |
no test coverage detected