| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Common | ||
| 6 | * \copydoc GridFormat::MDIndex | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_MD_INDEX_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_MD_INDEX_HPP_ | ||
| 10 | |||
| 11 | #include <cassert> | ||
| 12 | #include <ostream> | ||
| 13 | #include <utility> | ||
| 14 | #include <concepts> | ||
| 15 | #include <iterator> | ||
| 16 | #include <algorithm> | ||
| 17 | #include <initializer_list> | ||
| 18 | #include <functional> | ||
| 19 | #include <numeric> | ||
| 20 | |||
| 21 | #include <gridformat/common/concepts.hpp> | ||
| 22 | #include <gridformat/common/ranges.hpp> | ||
| 23 | #include <gridformat/common/reserved_vector.hpp> | ||
| 24 | #include <gridformat/common/iterator_facades.hpp> | ||
| 25 | #include <gridformat/common/md_layout.hpp> | ||
| 26 | |||
| 27 | namespace GridFormat { | ||
| 28 | |||
| 29 | //! \addtogroup Common | ||
| 30 | //! \{ | ||
| 31 | |||
| 32 | //! Represents a multi-dimensional index. | ||
| 33 | class MDIndex { | ||
| 34 | static constexpr std::size_t buffered_dimensions = 5; | ||
| 35 | using Indices = ReservedVector<std::size_t, buffered_dimensions>; | ||
| 36 | |||
| 37 | public: | ||
| 38 | 18361 | MDIndex() = default; | |
| 39 | |||
| 40 | //! Construct from a range of indices | ||
| 41 | template<Concepts::MDRange<1> R> | ||
| 42 | 21309 | explicit MDIndex(R&& indices) { | |
| 43 |
3/5✓ Branch 1 taken 1 times.
✓ Branch 2 taken 18810 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
21309 | _indices.reserve(Ranges::size(indices)); |
| 44 |
2/4✓ Branch 1 taken 18811 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 18811 times.
✗ Branch 5 not taken.
|
21309 | std::ranges::copy(indices, std::back_inserter(_indices)); |
| 45 | 21309 | } | |
| 46 | |||
| 47 | //! Construct from a vector of indices | ||
| 48 | template<std::integral T> | ||
| 49 | 30 | explicit MDIndex(const std::initializer_list<T>& indices) { | |
| 50 |
1/2✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
30 | _indices.reserve(Ranges::size(indices)); |
| 51 |
2/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
30 | std::ranges::copy(indices, std::back_inserter(_indices)); |
| 52 | 30 | } | |
| 53 | |||
| 54 | //! Zero-initialize a md-index with a given size | ||
| 55 | 105315 | explicit MDIndex(std::integral auto size) { | |
| 56 |
1/2✓ Branch 1 taken 105315 times.
✗ Branch 2 not taken.
|
105315 | _indices.resize(size, std::size_t{0}); |
| 57 | 105315 | } | |
| 58 | |||
| 59 | //! Zero-initialize a md-index with a given layout | ||
| 60 | 105315 | explicit MDIndex(const MDLayout& layout) | |
| 61 | 105315 | : MDIndex(layout.dimension()) | |
| 62 | 105315 | {} | |
| 63 | |||
| 64 | 413781 | auto begin() const { return _indices.begin(); } | |
| 65 | 38885 | auto begin() { return _indices.begin(); } | |
| 66 | |||
| 67 | 58675 | auto end() const { return _indices.end(); } | |
| 68 | 25923 | auto end() { return _indices.end(); } | |
| 69 | |||
| 70 | 427741 | std::size_t size() const { | |
| 71 | 427741 | return _indices.size(); | |
| 72 | } | ||
| 73 | |||
| 74 | 6244190 | std::size_t get(unsigned int codim) const { | |
| 75 | 6244190 | return _indices[codim]; | |
| 76 | } | ||
| 77 | |||
| 78 | 2094600 | void set(unsigned int codim, std::size_t index) { | |
| 79 | 2094600 | _indices[codim] = index; | |
| 80 | 2094600 | } | |
| 81 | |||
| 82 | 667696 | bool operator==(const MDIndex& other) const { | |
| 83 | 667696 | return std::ranges::equal(_indices, other._indices); | |
| 84 | } | ||
| 85 | |||
| 86 | 12962 | MDIndex& operator+=(const MDIndex& other) { | |
| 87 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 12962 times.
|
12962 | if (size() != other.size()) |
| 88 | ✗ | throw ValueError("MDIndex size mismatch"); | |
| 89 | 12962 | std::transform( | |
| 90 | begin(), | ||
| 91 | end(), | ||
| 92 | other.begin(), | ||
| 93 | begin(), | ||
| 94 | std::plus<std::size_t>{} | ||
| 95 | ); | ||
| 96 | 12962 | return *this; | |
| 97 | } | ||
| 98 | |||
| 99 | 1 | MDIndex operator+(const MDIndex& other) const { | |
| 100 | 1 | MDIndex result(*this); | |
| 101 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result += other; |
| 102 | 1 | return result; | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | 1 | friend std::ostream& operator<<(std::ostream& s, const MDIndex& md_index) { | |
| 106 | 1 | s << "("; | |
| 107 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (md_index._indices.size() > 0) { |
| 108 | 1 | s << md_index._indices[0]; | |
| 109 |
3/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
1 | std::ranges::for_each(md_index._indices | std::views::drop(1), [&] (const auto idx) { |
| 110 | 1 | s << "," << idx; | |
| 111 | 1 | }); | |
| 112 | } | ||
| 113 | 1 | s << ")"; | |
| 114 | 1 | return s; | |
| 115 | } | ||
| 116 | |||
| 117 | private: | ||
| 118 | Indices _indices; | ||
| 119 | }; | ||
| 120 | |||
| 121 | |||
| 122 | //! A range over multi-dimensional indices within a given layout | ||
| 123 | class MDIndexRange { | ||
| 124 | class Iterator | ||
| 125 | : public ForwardIteratorFacade<Iterator, MDIndex, const MDIndex&> { | ||
| 126 | public: | ||
| 127 | Iterator() = default; | ||
| 128 | 105315 | Iterator(const MDLayout& layout, bool is_end = false) | |
| 129 | 105315 | : _layout{&layout} | |
| 130 | 105315 | , _current{layout} | |
| 131 |
1/2✓ Branch 1 taken 105315 times.
✗ Branch 2 not taken.
|
105315 | , _last_dim{layout.dimension() - 1} { |
| 132 |
2/2✓ Branch 0 taken 59054 times.
✓ Branch 1 taken 46261 times.
|
105315 | if (is_end) |
| 133 |
1/2✓ Branch 1 taken 59054 times.
✗ Branch 2 not taken.
|
59054 | _current.set(_last_dim, layout.extent(_last_dim)); |
| 134 | 105315 | } | |
| 135 | |||
| 136 | private: | ||
| 137 | friend IteratorAccess; | ||
| 138 | |||
| 139 | 413793 | const MDIndex& _dereference() const { | |
| 140 | 413793 | return _current; | |
| 141 | } | ||
| 142 | |||
| 143 | 667680 | bool _is_equal(const Iterator& other) const { | |
| 144 |
3/4✓ Branch 0 taken 667680 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 46547 times.
✓ Branch 4 taken 621133 times.
|
667680 | return _layout == other._layout && _current == other._current; |
| 145 | } | ||
| 146 | |||
| 147 | 476903 | void _increment() { | |
| 148 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 476903 times.
|
476903 | assert(!_is_end()); |
| 149 | 476903 | unsigned int codim = 0; | |
| 150 | while (true) { | ||
| 151 | 651616 | _increment_at(codim); | |
| 152 |
2/2✓ Branch 2 taken 220934 times.
✓ Branch 3 taken 430682 times.
|
651616 | if (_current.get(codim) >= _layout->extent(codim)) { |
| 153 |
2/2✓ Branch 0 taken 46221 times.
✓ Branch 1 taken 174713 times.
|
220934 | if (codim == _last_dim) |
| 154 | 46221 | break; | |
| 155 | 174713 | _current.set(codim, 0); | |
| 156 | 174713 | codim++; | |
| 157 | } else { | ||
| 158 | 430682 | break; | |
| 159 | } | ||
| 160 | } | ||
| 161 | 476903 | } | |
| 162 | |||
| 163 | 651616 | void _increment_at(unsigned int codim) { | |
| 164 | 651616 | _current.set(codim, _current.get(codim) + 1); | |
| 165 | 651616 | } | |
| 166 | |||
| 167 | 476903 | bool _is_end() const { | |
| 168 | 476903 | return _current.get(_last_dim) >= _layout->extent(_last_dim); | |
| 169 | } | ||
| 170 | |||
| 171 | const MDLayout* _layout; | ||
| 172 | MDIndex _current; | ||
| 173 | std::size_t _last_dim; | ||
| 174 | }; | ||
| 175 | |||
| 176 | public: | ||
| 177 | 1876 | explicit MDIndexRange(MDLayout layout) | |
| 178 | 1876 | : _layout{std::move(layout)} | |
| 179 | 1876 | {} | |
| 180 | |||
| 181 | explicit MDIndexRange(const std::vector<std::size_t>& dimensions) | ||
| 182 | : MDIndexRange(MDLayout{dimensions}) | ||
| 183 | {} | ||
| 184 | |||
| 185 | 3 | explicit MDIndexRange(const std::initializer_list<std::size_t>& dimensions) | |
| 186 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | : MDIndexRange(MDLayout{dimensions}) |
| 187 | 3 | {} | |
| 188 | |||
| 189 | 46261 | auto begin() const { return Iterator{_layout}; } | |
| 190 | 59054 | auto end() const { return Iterator{_layout, true}; } | |
| 191 | |||
| 192 | const MDLayout& layout() const { | ||
| 193 | return _layout; | ||
| 194 | } | ||
| 195 | |||
| 196 | 28539 | std::size_t size() const { | |
| 197 | 28539 | return _layout.number_of_entries(); | |
| 198 | } | ||
| 199 | |||
| 200 | 834 | std::size_t size(unsigned int codim) const { | |
| 201 | 834 | return _layout.extent(codim); | |
| 202 | } | ||
| 203 | |||
| 204 | private: | ||
| 205 | MDLayout _layout; | ||
| 206 | }; | ||
| 207 | |||
| 208 | |||
| 209 | #ifndef DOXYGEN | ||
| 210 | namespace Detail { | ||
| 211 | |||
| 212 | template<std::ranges::range R> | ||
| 213 | 36728 | std::size_t flat_index_from_sub_sizes(const MDIndex& index, R&& sub_sizes) { | |
| 214 |
2/4✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 36728 times.
|
36728 | assert(index.size() == Ranges::size(sub_sizes)); |
| 215 |
2/4✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36728 times.
✗ Branch 5 not taken.
|
36728 | auto offsets = std::views::iota(std::size_t{0}, index.size()) |
| 216 |
2/4✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36728 times.
✗ Branch 5 not taken.
|
73456 | | std::views::transform([&] (const std::integral auto dim) { |
| 217 | 92045 | return index.get(dim)*sub_sizes[dim]; | |
| 218 | }); | ||
| 219 |
3/6✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36728 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 36728 times.
✗ Branch 8 not taken.
|
36728 | return std::accumulate( |
| 220 | std::ranges::begin(offsets), | ||
| 221 | std::ranges::end(offsets), | ||
| 222 | 0 | ||
| 223 | 36728 | ); | |
| 224 | } | ||
| 225 | |||
| 226 | } // namespace Detail | ||
| 227 | #endif // DOXYGEN | ||
| 228 | |||
| 229 | //! Compute the flat index from a multidimensional index and layout | ||
| 230 | 36728 | inline std::size_t flat_index(const MDIndex& index, const MDLayout& layout) { | |
| 231 |
3/6✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36728 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 36728 times.
|
36728 | assert(index.size() == layout.dimension()); |
| 232 |
2/4✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36728 times.
✗ Branch 5 not taken.
|
36728 | auto sub_sizes = std::views::iota(std::size_t{0}, index.size()) |
| 233 |
1/2✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
|
36728 | | std::views::transform([&] (const std::integral auto dim) { |
| 234 |
2/2✓ Branch 1 taken 55317 times.
✓ Branch 2 taken 36728 times.
|
92045 | if (dim < layout.dimension() - 1) |
| 235 | 55317 | return layout.number_of_entries(dim + 1); | |
| 236 | 36728 | return std::size_t{1}; | |
| 237 |
1/2✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
|
36728 | }); |
| 238 |
1/2✓ Branch 1 taken 36728 times.
✗ Branch 2 not taken.
|
73456 | return Detail::flat_index_from_sub_sizes(index, sub_sizes); |
| 239 | } | ||
| 240 | |||
| 241 | //! \} group Common | ||
| 242 | |||
| 243 | } // namespace GridFormat | ||
| 244 | |||
| 245 | #endif // GRIDFORMAT_COMMON_MD_INDEX_HPP_ | ||
| 246 |