Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Jan-Lukas Wynen 5 : #include "scipp/core/view_index.h" 6 : 7 : #include "scipp/core/except.h" 8 : 9 : namespace scipp::core { 10 : 11 2624820 : ViewIndex::ViewIndex(const Dimensions &target_dimensions, 12 2624820 : const Strides &strides) { 13 2624820 : scipp::index rewind = 0; 14 2624820 : scipp::index dim_write = 0; 15 7667674 : for (scipp::index dim_read = target_dimensions.ndim() - 1; dim_read >= 0; 16 : --dim_read) { 17 5042854 : const auto stride = strides[dim_read]; 18 5042854 : const auto delta = stride - rewind; 19 5042854 : const auto size = target_dimensions.size(dim_read); 20 5042854 : rewind = size * stride; 21 5042854 : if (delta != 0 || stride == 0) { 22 3308328 : m_shape[dim_write] = size; 23 3308328 : m_delta[dim_write] = delta; 24 3308328 : m_strides[dim_write] = stride; 25 3308328 : ++dim_write; 26 : } else { 27 : // The memory for this dimension is contiguous with the previous dim, 28 : // so we flatten them into one dimension. 29 : // This cannot happen in the innermost dim because if stride != 0, 30 : // delta != 0 because rewind == 0. 31 1734526 : m_shape[dim_write - 1] *= size; 32 : } 33 : } 34 2624820 : m_ndim = dim_write; 35 2624820 : } 36 : 37 : } // namespace scipp::core