ARTS 2.5.4 (git: 4c0d3b4d)
grids.h
Go to the documentation of this file.
1#ifndef grids_h
2#define grids_h
3
4#include <array>
5#include <iostream>
6#include <vector>
7
9template <typename... Inds>
10constexpr std::size_t mul(Inds... inds) noexcept {
11 return (std::size_t(inds) * ...);
12}
13
15template <std::size_t N>
16constexpr std::size_t mul(const std::array<std::size_t, N>& arr) noexcept {
17 if constexpr (N == 0) {
18 return 0;
19 } else {
20 std::size_t out = 1;
21 for (auto i : arr) out *= i;
22 return out;
23 }
24}
25
27template <typename... Inds>
28constexpr std::array<std::size_t, sizeof...(Inds)> gridsize_from_index(Inds... inds) noexcept {
29 std::array<std::size_t, sizeof...(Inds)> out{};
30 std::array<std::size_t, sizeof...(Inds)> arr{std::size_t(inds)...};
31 std::size_t i = sizeof...(Inds)-1;
32 std::size_t s = 1;
33 while (i < sizeof...(Inds)) {
34 out[i] = s;
35 s *= arr[i];
36 i--;
37 }
38 return out;
39}
40
42template <std::size_t N>
43constexpr std::size_t index_from_gridsize(std::array<std::size_t, N> gridsize,
44 std::array<std::size_t, N> inds) noexcept {
45 std::size_t pos=0;
46 for (std::size_t i=0; i<N; i++) pos += gridsize[i] * inds[i];
47 return pos;
48}
49
51template <typename b, std::size_t n>
52class Grid {
54 std::vector<b> ptr;
55
57 std::array<std::size_t, n> gridsize;
58
60 [[nodiscard]] std::size_t size() const noexcept { return ptr.size(); }
61
62public:
64 static constexpr std::size_t N = n;
65
67 using base = b;
68
69 static_assert(N, "Must have size");
70
72 template <typename... Inds>
73 Grid(Inds... inds) noexcept : ptr(mul(inds...)), gridsize(gridsize_from_index(inds...)) {
74 static_assert(sizeof...(Inds) == N,
75 "Must have same size for initialization");
76 }
77
79 Grid(Grid&& g) noexcept : ptr(std::move(g.ptr)), gridsize(std::move(g.gridsize)) {}
80
82 Grid& operator=(Grid&& g) noexcept {
83 ptr = std::move(g.ptr);
84 gridsize = std::move(g.gridsize);
85 return *this;
86 }
87
89 template <typename... Inds>
90 base& operator()(Inds... inds) noexcept {
91 return ptr[index_from_gridsize(gridsize, std::array<std::size_t, N>{std::size_t(inds)...})];
92 }
93
95 template <typename... Inds>
96 const base& operator()(Inds... inds) const noexcept {
97 return ptr[index_from_gridsize(gridsize, std::array<std::size_t, N>{std::size_t(inds)...})];
98 }
99
101 base& operator[](std::size_t ind) noexcept {
102 static_assert(N == 1);
103 return ptr[ind];
104 }
105
107 const base& operator[](std::size_t ind) const noexcept {
108 static_assert(N == 1);
109 return ptr[ind];
110 }
111
113 friend std::ostream& operator<<(std::ostream& os, const Grid& g) {
114 const std::size_t nel = g.size();
115 for (std::size_t i = 0; i < nel; i++) {
116 os << g.ptr[i];
117 if constexpr (N > 1) {
118 if (i not_eq 0 and i not_eq nel and i % g.gridsize[N-2] == 0)
119 os << '\n';
120 else if (i not_eq nel)
121 os << ' ';
122 } else if (i not_eq nel) {
123 os << ' ';
124 }
125 }
126 return os;
127 }
128}; // Grid
129
131template <typename b, std::size_t... Sizes>
134 std::array<b, mul(Sizes...)> ptr;
135
136public:
138 static constexpr std::size_t N = sizeof...(Sizes);
139
141 using base = b;
142
143 static_assert(N, "Must have size");
144
146 template <typename... Inds>
147 constexpr FixedGrid(Inds... inds [[maybe_unused]]) noexcept : ptr({}) {
148 static_assert(sizeof...(Inds) == N, "Must have same size for initialization");
149 }
150
152 constexpr FixedGrid() noexcept : ptr({}) {static_assert(mul(Sizes...), "Must have size");}
153
155 constexpr FixedGrid(FixedGrid&& g) noexcept : ptr(std::move(g.ptr)) {}
156
158 constexpr FixedGrid& operator=(FixedGrid&& g) noexcept {
159 ptr = std::move(g.ptr);
160 return *this;
161 }
162
164 template <typename... Inds>
165 constexpr base& operator()(Inds... inds) noexcept {
166 return ptr[index_from_gridsize(gridsize_from_index(Sizes...), std::array<std::size_t, N>{std::size_t(inds)...})];
167 }
168
170 template <typename... Inds>
171 constexpr const base& operator()(Inds... inds) const noexcept {
172 return ptr[index_from_gridsize(gridsize_from_index(Sizes...), std::array<std::size_t, N>{std::size_t(inds)...})];
173 }
174
176 constexpr base& operator[](std::size_t ind) noexcept {
177 static_assert(N == 1);
178 return ptr[ind];
179 }
180
182 constexpr const base& operator[](std::size_t ind) const noexcept {
183 static_assert(N == 1);
184 return ptr[ind];
185 }
186
188 friend std::ostream& operator<<(std::ostream& os, const FixedGrid& g) {
189 constexpr std::size_t nel = mul(Sizes...);
190 constexpr std::size_t last_of = (std::array<std::size_t, N>{std::size_t(Sizes)...}).back();
191 for (std::size_t i = 0; i < nel; i++) {
192 os << g.ptr[i];
193 if (i not_eq 0 and i not_eq nel and i % last_of == 0)
194 os << '\n';
195 else if (i not_eq nel)
196 os << ' ';
197 }
198 return os;
199 }
200}; // FixedGrid
201
202#endif // grids_h
Row-major fixed grid creation.
Definition: grids.h:132
std::array< b, mul(Sizes...)> ptr
Definition: grids.h:134
b base
Definition: grids.h:141
constexpr FixedGrid & operator=(FixedGrid &&g) noexcept
Definition: grids.h:158
constexpr FixedGrid() noexcept
Definition: grids.h:152
constexpr FixedGrid(Inds... inds) noexcept
Definition: grids.h:147
constexpr FixedGrid(FixedGrid &&g) noexcept
Definition: grids.h:155
constexpr base & operator[](std::size_t ind) noexcept
Definition: grids.h:176
constexpr const base & operator[](std::size_t ind) const noexcept
Definition: grids.h:182
constexpr const base & operator()(Inds... inds) const noexcept
Definition: grids.h:171
static constexpr std::size_t N
Definition: grids.h:138
constexpr base & operator()(Inds... inds) noexcept
Definition: grids.h:165
friend std::ostream & operator<<(std::ostream &os, const FixedGrid &g)
Definition: grids.h:188
Row-major grid creation.
Definition: grids.h:52
base & operator[](std::size_t ind) noexcept
Definition: grids.h:101
Grid(Inds... inds) noexcept
Definition: grids.h:73
static constexpr std::size_t N
Definition: grids.h:64
std::vector< b > ptr
Definition: grids.h:54
const base & operator()(Inds... inds) const noexcept
Definition: grids.h:96
friend std::ostream & operator<<(std::ostream &os, const Grid &g)
Definition: grids.h:113
Grid(Grid &&g) noexcept
Definition: grids.h:79
std::size_t size() const noexcept
Definition: grids.h:60
std::array< std::size_t, n > gridsize
Definition: grids.h:57
b base
Definition: grids.h:67
Grid & operator=(Grid &&g) noexcept
Definition: grids.h:82
const base & operator[](std::size_t ind) const noexcept
Definition: grids.h:107
base & operator()(Inds... inds) noexcept
Definition: grids.h:90
constexpr std::size_t index_from_gridsize(std::array< std::size_t, N > gridsize, std::array< std::size_t, N > inds) noexcept
Definition: grids.h:43
constexpr std::size_t mul(Inds... inds) noexcept
Definition: grids.h:10
constexpr std::array< std::size_t, sizeof...(Inds)> gridsize_from_index(Inds... inds) noexcept
Definition: grids.h:28
#define b
#define N
Definition: rng.cc:164