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 Simon Heybrock 5 : #pragma once 6 : 7 : #include <boost/container/small_vector.hpp> 8 : 9 : #include "scipp-core_export.h" 10 : #include "scipp/common/index.h" 11 : #include "scipp/core/dimensions.h" 12 : 13 : namespace scipp::core { 14 : 15 : /// Container for strides in memory. 16 : /// 17 : /// Given a contiguous buffer in memory viewed as a multi-dimensional array, 18 : /// `strides[d]` is the distance in memory between consecutive array elements 19 : /// in dimension `d`. 20 : /// @see scipp::flat_index_from_strides 21 : /// 22 : /// Strides objects do not store information on the dimensions. 23 : /// They should therefore be accompanied by an instance of Dimensions. 24 : /// The storage order in Strides is arbitrary but should be the same as in 25 : /// Dimensions, i.e. the slowest moving index should be first. 26 : class SCIPP_CORE_EXPORT Strides { 27 : public: 28 13645600 : Strides() = default; 29 : explicit Strides(const scipp::span<const scipp::index> &strides); 30 : Strides(std::initializer_list<scipp::index> strides); 31 : explicit Strides(const Dimensions &dims); 32 : 33 : bool operator==(const Strides &other) const noexcept; 34 : bool operator!=(const Strides &other) const noexcept; 35 : 36 6330736 : [[nodiscard]] scipp::index size() const noexcept { return m_strides.size(); } 37 : 38 : void push_back(const scipp::index i); 39 : void clear(); 40 : void resize(const scipp::index size); 41 : 42 11538832 : scipp::index operator[](const scipp::index i) const { 43 23077664 : return m_strides.at(i); 44 : } 45 : 46 5603176 : scipp::index &operator[](const scipp::index i) { return m_strides.at(i); } 47 : 48 851810 : [[nodiscard]] auto begin() const { return m_strides.begin(); } 49 : [[nodiscard]] auto begin() { return m_strides.begin(); } 50 : [[nodiscard]] auto end() const { return m_strides.end(); } 51 : [[nodiscard]] auto end() { return m_strides.end(); } 52 : 53 : void erase(scipp::index i); 54 : 55 : private: 56 : boost::container::small_vector<scipp::index, NDIM_STACK> m_strides; 57 : }; 58 : 59 : Strides SCIPP_CORE_EXPORT transpose(const Strides &strides, Dimensions from, 60 : scipp::span<const Dim> order); 61 : 62 : } // namespace scipp::core 63 : 64 : namespace scipp { 65 : using core::Strides; 66 : }