Aleph-w 3.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
148 [[nodiscard]] static constexpr std::size_t dimension() noexcept
149 {
150 return N;
151 }
152
155 {
156 return total_cells_;
157 }
158
163 [[nodiscard]] ca_size_t size(std::size_t d) const
164 {
165 ah_out_of_range_error_if(d >= N) << "Dense_Cell_Storage::size: axis " << d << " out of range";
166 return ext_[d];
167 }
168
171 {
172 return ext_;
173 }
174
186 {
187 ca_size_t idx = 0;
188 for (std::size_t d = 0; d < N; ++d)
189 {
190 ah_out_of_range_error_if(c[d] < 0 or static_cast<ca_size_t>(c[d]) >= ext_[d])
191 << "Dense_Cell_Storage::linear_index: coord[" << d << "]=" << c[d] << " out of [0, "
192 << ext_[d] << ")";
193 idx = idx * ext_[d] + static_cast<ca_size_t>(c[d]);
194 }
195 return idx;
196 }
197
203 [[nodiscard]] T at(const coord_type &c) const
204 {
205 return buffer_[linear_index(c)];
206 }
207
213 void set(const coord_type &c, const T &v)
214 {
215 buffer_[linear_index(c)] = v;
216 }
217
220 requires(N == 1)
221 {
222 return at(coord_type{i});
223 }
224
227 requires(N == 2)
228 {
229 return at(coord_type{i, j});
230 }
231
234 requires(N == 3)
235 {
236 return at(coord_type{i, j, k});
237 }
238
240 void set(ca_index_t i, const T &v)
241 requires(N == 1)
242 {
243 set(coord_type{i}, v);
244 }
245
247 void set(ca_index_t i, ca_index_t j, const T &v)
248 requires(N == 2)
249 {
250 set(coord_type{i, j}, v);
251 }
252
254 void set(ca_index_t i, ca_index_t j, ca_index_t k, const T &v)
255 requires(N == 3)
256 {
257 set(coord_type{i, j, k}, v);
258 }
259
262 {
263 return total_cells_ == 0 ? nullptr : &buffer_[0];
264 }
265
268 {
269 return total_cells_ == 0 ? nullptr : &buffer_[0];
270 }
271
274
277
301 {
302 return view_type(data(), typename view_type::extents_type(ext_));
303 }
304
313
316 {
317 using std::swap;
318 swap(ext_, other.ext_);
319 swap(total_cells_, other.total_cells_);
320 buffer_.swap(other.buffer_);
321 }
322
324 void fill(const T &value)
325 {
326 for (ca_size_t i = 0; i < total_cells_; ++i)
327 buffer_[i] = value;
328 }
329};
330
332template <typename T, std::size_t N>
334{
335 a.swap(b);
336}
337
338} // namespace CA
339} // namespace Aleph
340
341#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:584
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:468
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:138
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
T at(ca_index_t i) const
1D shorthand for at({i}).
static ca_size_t product(const extents_type &e)
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).
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()=default
Construct an empty storage (zero extents, no cells allocated).
const T * data() const noexcept
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
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
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.