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 2625639 : ViewIndex::ViewIndex(const Dimensions &target_dimensions, 12 2625639 : const Strides &strides) { 13 2625639 : scipp::index rewind = 0; 14 2625639 : scipp::index dim_write = 0; 15 7669914 : for (scipp::index dim_read = target_dimensions.ndim() - 1; dim_read >= 0; 16 : --dim_read) { 17 5044275 : const auto stride = strides[dim_read]; 18 5044275 : const auto delta = stride - rewind; 19 5044275 : const auto size = target_dimensions.size(dim_read); 20 5044275 : rewind = size * stride; 21 5044275 : if (delta != 0 || stride == 0) { 22 3309517 : m_shape[dim_write] = size; 23 3309517 : m_delta[dim_write] = delta; 24 3309517 : m_strides[dim_write] = stride; 25 3309517 : ++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 1734758 : m_shape[dim_write - 1] *= size; 32 : } 33 : } 34 2625639 : m_ndim = dim_write; 35 2625639 : } 36 : 37 : } // namespace scipp::core