Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_bit_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
49#ifndef TPL_CA_BIT_STORAGE_H
50#define TPL_CA_BIT_STORAGE_H
51
52#include <cstddef>
53#include <limits>
54#include <utility>
55
56#include <ah-errors.H>
57#include <bitArray.H>
58
59#include <ca-traits.H>
60#include <tpl_ca_concepts.H>
61
62namespace Aleph {
63namespace CA {
64
74template <std::size_t N>
76{
77 static_assert(N >= 1, "Bit_Cell_Storage requires N >= 1");
78
79public:
80 using state_type = bool;
82 using extents_type = std::array<ca_size_t, N>;
83
85 static constexpr std::size_t rank = N;
86
87private:
91
93 {
94 ca_size_t p = 1;
95 for (std::size_t d = 0; d < N; ++d)
96 {
97 if (e[d] == 0)
98 return 0;
99 ah_overflow_error_if(p > std::numeric_limits<ca_size_t>::max() / e[d])
100 << "Bit_Cell_Storage::product: extents product overflows ca_size_t";
101 p *= e[d];
102 }
103 return p;
104 }
105
106public:
108 Bit_Cell_Storage() = default;
109
115 explicit Bit_Cell_Storage(const extents_type &extents, const bool init = false)
117 {}
118
123 Bit_Cell_Storage(const Bit_Cell_Storage &other) = default;
124
134 : ext_(std::exchange(other.ext_, extents_type{})),
135 total_cells_(std::exchange(other.total_cells_, 0)), buffer_(std::move(other.buffer_))
136 {}
137
144
151 {
152 swap(other);
153 return *this;
154 }
155
157 [[nodiscard]] static constexpr std::size_t dimension() noexcept
158 {
159 return N;
160 }
161
163 [[nodiscard]] ca_size_t size() const noexcept
164 {
165 return total_cells_;
166 }
167
173 [[nodiscard]] ca_size_t size(std::size_t d) const
174 {
175 ah_out_of_range_error_if(d >= N) << "Bit_Cell_Storage::size: axis " << d << " out of range";
176 return ext_[d];
177 }
178
182 [[nodiscard]] const extents_type &extents() const noexcept
183 {
184 return ext_;
185 }
186
193 [[nodiscard]] ca_size_t linear_index(const coord_type &c) const
194 {
195 ca_size_t idx = 0;
196 for (std::size_t d = 0; d < N; ++d)
197 {
198 ah_out_of_range_error_if(c[d] < 0 or static_cast<ca_size_t>(c[d]) >= ext_[d])
199 << "Bit_Cell_Storage::linear_index: coord[" << d << "]=" << c[d] << " out of [0, "
200 << ext_[d] << ")";
201 idx = idx * ext_[d] + static_cast<ca_size_t>(c[d]);
202 }
203 return idx;
204 }
205
211 [[nodiscard]] bool at(const coord_type &c) const
212 {
213 return buffer_.read_bit(linear_index(c)) != 0;
214 }
215
221 void set(const coord_type &c, bool v)
222 {
223 buffer_.write_bit(linear_index(c), v ? 1u : 0u);
224 }
225
231 [[nodiscard]] bool at(ca_index_t i) const
232 requires(N == 1)
233 {
234 return at(coord_type{i});
235 }
236
243 [[nodiscard]] bool at(ca_index_t i, ca_index_t j) const
244 requires(N == 2)
245 {
246 return at(coord_type{i, j});
247 }
248
256 [[nodiscard]] bool at(ca_index_t i, ca_index_t j, ca_index_t k) const
257 requires(N == 3)
258 {
259 return at(coord_type{i, j, k});
260 }
261
267 void set(ca_index_t i, bool v)
268 requires(N == 1)
269 {
270 set(coord_type{i}, v);
271 }
272
279 void set(ca_index_t i, ca_index_t j, const bool v)
280 requires(N == 2)
281 {
282 set(coord_type{i, j}, v);
283 }
284
292 void set(ca_index_t i, ca_index_t j, ca_index_t k, const bool v)
293 requires(N == 3)
294 {
295 set(coord_type{i, j, k}, v);
296 }
297
302 void swap(Bit_Cell_Storage &other) noexcept
303 {
304 using std::swap;
305 swap(ext_, other.ext_);
306 swap(total_cells_, other.total_cells_);
307 buffer_.swap(other.buffer_);
308 }
309
314 void fill(const bool value)
315 {
316 const unsigned int v = value ? 1u : 0u;
317 for (ca_size_t i = 0; i < total_cells_; ++i)
318 buffer_.write_bit(i, v);
319 }
320};
321
327template <std::size_t N>
328inline void swap(Bit_Cell_Storage<N> &a, Bit_Cell_Storage<N> &b) noexcept
329{
330 a.swap(b);
331}
332
333} // namespace CA
334} // namespace Aleph
335
336#endif // TPL_CA_BIT_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
Space-efficient bit array implementation.
size_t size_t int32_t value
Definition ca-c-api.h:116
Common typedefs and tag types for the Cellular Automata module.
Contiguous array of bits.
Definition bitArray.H:201
int read_bit(const size_t i) const
Read bit i.
Definition bitArray.H:409
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
Definition bitArray.H:424
void swap(BitArray &array) noexcept
Definition bitArray.H:541
Bit-packed row-major storage for N-dimensional binary CAs.
Bit_Cell_Storage & operator=(Bit_Cell_Storage &&other) noexcept
Exchange this storage with another, which is left with the previous cells of this one.
Bit_Cell_Storage(const extents_type &extents, const bool init=false)
Construct a storage with the given extents and initial value.
void fill(const bool value)
Set every cell to value.
bool at(ca_index_t i) const
Read 1D cell at index i.
static ca_size_t product(const extents_type &e)
ca_size_t total_cells_
product of ext
const extents_type & extents() const noexcept
Return the full extents array.
Bit_Cell_Storage(Bit_Cell_Storage &&other) noexcept
Take the cells of another storage, which is left empty.
ca_size_t size() const noexcept
extents_type ext_
per-axis extents
static constexpr std::size_t dimension() noexcept
BitArray buffer_
bit-packed row-major storage
void swap(Bit_Cell_Storage &other) noexcept
Swap contents with another Bit_Cell_Storage instance.
bool at(ca_index_t i, ca_index_t j, ca_index_t k) const
Read 3D cell at (i, j, k).
static constexpr std::size_t rank
Number of axes known at compile time.
Bit_Cell_Storage(const Bit_Cell_Storage &other)=default
Copy another storage: its extents and its bits, which the copy keeps apart from those of other.
void set(ca_index_t i, bool v)
Write 1D cell at index i.
void set(ca_index_t i, ca_index_t j, const bool v)
Write 2D cell at (i, j).
void set(ca_index_t i, ca_index_t j, ca_index_t k, const bool v)
Write 3D cell at (i, j, k).
bool at(ca_index_t i, ca_index_t j) const
Read 2D cell at (i, j).
ca_size_t linear_index(const coord_type &c) const
Map a multi-index to a linear bit offset (row-major).
ca_size_t size(std::size_t d) const
Return the extent (number of cells) along axis d.
Bit_Cell_Storage & operator=(const Bit_Cell_Storage &other)=default
Copy another storage into this one: its extents and its bits, which this one keeps apart from those o...
std::array< ca_size_t, N > extents_type
void set(const coord_type &c, bool v)
Write the cell at coordinate c.
Bit_Cell_Storage()=default
Construct an empty storage (zero extents).
bool at(const coord_type &c) const
Read the cell at coordinate c.
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
#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.
static std::atomic< bool > init
Definition hash-fct.C:54
static int * k
C++20 concepts for the Cellular Automata module.