Aleph-w 3.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:
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
120 [[nodiscard]] static constexpr std::size_t dimension() noexcept
121 {
122 return N;
123 }
124
127 {
128 return total_cells_;
129 }
130
136 [[nodiscard]] ca_size_t size(std::size_t d) const
137 {
138 ah_out_of_range_error_if(d >= N) << "Bit_Cell_Storage::size: axis " << d << " out of range";
139 return ext_[d];
140 }
141
146 {
147 return ext_;
148 }
149
157 {
158 ca_size_t idx = 0;
159 for (std::size_t d = 0; d < N; ++d)
160 {
161 ah_out_of_range_error_if(c[d] < 0 or static_cast<ca_size_t>(c[d]) >= ext_[d])
162 << "Bit_Cell_Storage::linear_index: coord[" << d << "]=" << c[d] << " out of [0, "
163 << ext_[d] << ")";
164 idx = idx * ext_[d] + static_cast<ca_size_t>(c[d]);
165 }
166 return idx;
167 }
168
174 [[nodiscard]] bool at(const coord_type &c) const
175 {
176 return buffer_.read_bit(linear_index(c)) != 0;
177 }
178
184 void set(const coord_type &c, bool v)
185 {
186 buffer_.write_bit(linear_index(c), v ? 1u : 0u);
187 }
188
194 [[nodiscard]] bool at(ca_index_t i) const
195 requires(N == 1)
196 {
197 return at(coord_type{i});
198 }
199
206 [[nodiscard]] bool at(ca_index_t i, ca_index_t j) const
207 requires(N == 2)
208 {
209 return at(coord_type{i, j});
210 }
211
220 requires(N == 3)
221 {
222 return at(coord_type{i, j, k});
223 }
224
230 void set(ca_index_t i, bool v)
231 requires(N == 1)
232 {
233 set(coord_type{i}, v);
234 }
235
242 void set(ca_index_t i, ca_index_t j, const bool v)
243 requires(N == 2)
244 {
245 set(coord_type{i, j}, v);
246 }
247
255 void set(ca_index_t i, ca_index_t j, ca_index_t k, const bool v)
256 requires(N == 3)
257 {
258 set(coord_type{i, j, k}, v);
259 }
260
266 {
267 using std::swap;
268 swap(ext_, other.ext_);
269 swap(total_cells_, other.total_cells_);
270 buffer_.swap(other.buffer_);
271 }
272
277 void fill(const bool value)
278 {
279 const unsigned int v = value ? 1u : 0u;
280 for (ca_size_t i = 0; i < total_cells_; ++i)
281 buffer_.write_bit(i, v);
282 }
283};
284
290template <std::size_t N>
291inline void swap(Bit_Cell_Storage<N> &a, Bit_Cell_Storage<N> &b) noexcept
292{
293 a.swap(b);
294}
295
296} // namespace CA
297} // namespace Aleph
298
299#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:584
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:468
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:389
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
Definition bitArray.H:404
void swap(BitArray &array) noexcept
Definition bitArray.H:497
Bit-packed row-major storage for N-dimensional binary CAs.
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.
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.
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.
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.
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
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
static std::atomic< bool > init
Definition hash-fct.C:54
static int * k
C++20 concepts for the Cellular Automata module.