Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_storage.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
55#ifndef TPL_CA_STORAGE_H
56#define TPL_CA_STORAGE_H
57
58#include <cstddef>
59#include <utility>
60
61#include <ah-errors.H>
62#include <ah-mdspan.H>
63#include <tpl_array.H>
64
65#include <ca-traits.H>
66#include <tpl_ca_concepts.H>
67
68namespace Aleph {
69namespace CA {
70
96template <typename T, std::size_t N>
98{
99 static_assert(N >= 1, "Dense_Cell_Storage requires N >= 1");
100 static_assert(CellState<T>, "Dense_Cell_Storage requires a CellState type");
101
102public:
103 using state_type = T;
105 using extents_type = std::array<ca_size_t, N>;
106
108 static constexpr std::size_t rank = N;
109
110private:
114
116 {
117 ca_size_t p = 1;
118 for (std::size_t d = 0; d < N; ++d)
119 {
120 // Store limit before passing to macro to avoid preprocessor
121 // misparse of <ca_size_t> inside a macro argument (clang-cl).
122 const ca_size_t lim = std::numeric_limits<ca_size_t>::max();
123 ah_overflow_error_if(e[d] != 0 and p > lim / e[d])
124 << "Dense_Cell_Storage: extent product overflows ca_size_t at dimension " << d;
125 p *= e[d];
126 }
127 return p;
128 }
129
130public:
133
146
151 Dense_Cell_Storage(const Dense_Cell_Storage &other) = default;
152
161 : ext_(std::exchange(other.ext_, extents_type{})),
162 total_cells_(std::exchange(other.total_cells_, 0)), buffer_(std::move(other.buffer_))
163 {}
164
171
178 {
179 swap(other);
180 return *this;
181 }
182
184 [[nodiscard]] static constexpr std::size_t dimension() noexcept
185 {
186 return N;
187 }
188
190 [[nodiscard]] ca_size_t size() const noexcept
191 {
192 return total_cells_;
193 }
194
199 [[nodiscard]] ca_size_t size(std::size_t d) const
200 {
201 ah_out_of_range_error_if(d >= N) << "Dense_Cell_Storage::size: axis " << d << " out of range";
202 return ext_[d];
203 }
204
206 [[nodiscard]] const extents_type &extents() const noexcept
207 {
208 return ext_;
209 }
210
221 [[nodiscard]] ca_size_t linear_index(const coord_type &c) const
222 {
223 ca_size_t idx = 0;
224 for (std::size_t d = 0; d < N; ++d)
225 {
226 ah_out_of_range_error_if(c[d] < 0 or static_cast<ca_size_t>(c[d]) >= ext_[d])
227 << "Dense_Cell_Storage::linear_index: coord[" << d << "]=" << c[d] << " out of [0, "
228 << ext_[d] << ")";
229 idx = idx * ext_[d] + static_cast<ca_size_t>(c[d]);
230 }
231 return idx;
232 }
233
239 [[nodiscard]] T at(const coord_type &c) const
240 {
241 return buffer_[linear_index(c)];
242 }
243
249 void set(const coord_type &c, const T &v)
250 {
251 buffer_[linear_index(c)] = v;
252 }
253
255 [[nodiscard]] T at(ca_index_t i) const
256 requires(N == 1)
257 {
258 return at(coord_type{i});
259 }
260
262 [[nodiscard]] T at(ca_index_t i, ca_index_t j) const
263 requires(N == 2)
264 {
265 return at(coord_type{i, j});
266 }
267
269 [[nodiscard]] T at(ca_index_t i, ca_index_t j, ca_index_t k) const
270 requires(N == 3)
271 {
272 return at(coord_type{i, j, k});
273 }
274
276 void set(ca_index_t i, const T &v)
277 requires(N == 1)
278 {
279 set(coord_type{i}, v);
280 }
281
283 void set(ca_index_t i, ca_index_t j, const T &v)
284 requires(N == 2)
285 {
286 set(coord_type{i, j}, v);
287 }
288
290 void set(ca_index_t i, ca_index_t j, ca_index_t k, const T &v)
291 requires(N == 3)
292 {
293 set(coord_type{i, j, k}, v);
294 }
295
297 [[nodiscard]] T *data() noexcept
298 {
299 return total_cells_ == 0 ? nullptr : &buffer_[0];
300 }
301
303 [[nodiscard]] const T *data() const noexcept
304 {
305 return total_cells_ == 0 ? nullptr : &buffer_[0];
306 }
307
310
313
336 [[nodiscard]] view_type view() noexcept
337 {
338 return view_type(data(), typename view_type::extents_type(ext_));
339 }
340
345 [[nodiscard]] const_view_type view() const noexcept
346 {
348 }
349
351 void swap(Dense_Cell_Storage &other) noexcept
352 {
353 using std::swap;
354 swap(ext_, other.ext_);
355 swap(total_cells_, other.total_cells_);
356 buffer_.swap(other.buffer_);
357 }
358
360 void fill(const T &value)
361 {
362 for (ca_size_t i = 0; i < total_cells_; ++i)
363 buffer_[i] = value;
364 }
365};
366
368template <typename T, std::size_t N>
370{
371 a.swap(b);
372}
373
374} // namespace CA
375} // namespace Aleph
376
377#endif // TPL_CA_STORAGE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
Non-owning multidimensional array view (Aleph::mdspan).
size_t size_t int32_t value
Definition ca-c-api.h:116
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
Row-major dense storage for N-dimensional cellular automata.
void set(ca_index_t i, ca_index_t j, ca_index_t k, const T &v)
3D shorthand for set({i, j, k}, v).
Dense_Cell_Storage(const extents_type &extents, const T &init=T{})
Construct a storage with the given extents and initial value.
Aleph::mdspan< const T, Aleph::dextents< ca_size_t, N > > const_view_type
Non-owning, zero-copy read-only N-dimensional view of this storage's buffer.
const_view_type view() const noexcept
Dense_Cell_Storage(Dense_Cell_Storage &&other) noexcept
Take the cells of another storage, which is left empty.
T at(ca_index_t i) const
1D shorthand for at({i}).
static ca_size_t product(const extents_type &e)
Dense_Cell_Storage(const Dense_Cell_Storage &other)=default
Copy another storage: its extents and its cells, which the copy keeps apart from those of other.
ca_size_t size() const noexcept
extents_type ext_
per-axis extents
void set(ca_index_t i, ca_index_t j, const T &v)
2D shorthand for set({i, j}, v).
void fill(const T &value)
Set every cell to value.
static constexpr std::size_t dimension() noexcept
ca_size_t size(std::size_t d) const
Extent of axis d.
ca_size_t linear_index(const coord_type &c) const
Map a multi-index to a linear offset.
void swap(Dense_Cell_Storage &other) noexcept
O(1) swap with another storage of the same type.
view_type view() noexcept
Return a mutable Aleph::mdspan view of this storage.
const extents_type & extents() const noexcept
void set(ca_index_t i, const T &v)
1D shorthand for set({i}, v).
Dense_Cell_Storage & operator=(Dense_Cell_Storage &&other) noexcept
Exchange this storage with another, which is left with the previous cells of this one.
Aleph::mdspan< T, Aleph::dextents< ca_size_t, N > > view_type
Non-owning, zero-copy N-dimensional view of this storage's buffer.
std::array< ca_size_t, N > extents_type
ca_size_t total_cells_
product of ext
T at(ca_index_t i, ca_index_t j, ca_index_t k) const
3D shorthand for at({i, j, k}).
T at(const coord_type &c) const
Read access through a multi-index.
void set(const coord_type &c, const T &v)
Write access through a multi-index.
T at(ca_index_t i, ca_index_t j) const
2D shorthand for at({i, j}).
Array< T > buffer_
contiguous row-major storage
static constexpr std::size_t rank
Number of axes known at compile time.
Dense_Cell_Storage & operator=(const Dense_Cell_Storage &other)=default
Copy another storage into this one: its extents and its cells, which this one keeps apart from those ...
Dense_Cell_Storage()=default
Construct an empty storage (zero extents, no cells allocated).
const T * data() const noexcept
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
Definition ah-mdspan.H:303
Non-owning view of a contiguous buffer as a multidimensional grid.
Definition ah-mdspan.H:576
Extents extents_type
Definition ah-mdspan.H:578
A type usable as the value stored inside a cell.
#define N
Definition fib.C:294
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
void swap(Bit_Cell_Storage< N > &a, Bit_Cell_Storage< N > &b) noexcept
Free-function swap so the storage plays nicely with std::swap.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
static std::atomic< bool > init
Definition hash-fct.C:54
static int * k
Dynamic array container with automatic resizing.
C++20 concepts for the Cellular Automata module.