73#ifndef TPL_CA_GRAPH_AUTOMATON_H
74#define TPL_CA_GRAPH_AUTOMATON_H
109 static_assert(
CellState<T>,
"Graph_Lattice requires a CellState type");
117 static constexpr std::size_t
rank = 1;
147 const auto &
row = adjacency[i];
149 for (std::size_t
k = 0;
k <
row.size(); ++
k)
152 <<
"Graph_Lattice: neighbour id " <<
row[
k] <<
" of node " << i
153 <<
" is out of range [0, " <<
num_nodes_ <<
")";
175 :
num_nodes_(std::exchange(other.num_nodes_, 0)),
cells_(std::move(other.cells_)),
201 [[nodiscard]]
static constexpr std::size_t
dimension() noexcept {
return rank; }
208 <<
"Graph_Lattice::size: axis " << d <<
" out of range";
220 <<
"Graph_Lattice::at: id " << c[0] <<
" out of [0, " <<
num_nodes_ <<
")";
227 <<
"Graph_Lattice::set: id " << c[0] <<
" out of [0, " <<
num_nodes_ <<
")";
249 <<
"Graph_Lattice::at_node: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
257 <<
"Graph_Lattice::set_node: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
262 [[nodiscard]] std::size_t
degree(std::size_t n)
const
265 <<
"Graph_Lattice::degree: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
273 <<
"Graph_Lattice::neighbours: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
299 cells_.swap(other.cells_);
330template <
typename R,
typename T>
332and (
requires(
const R &
r,
const T &s, Neighbor_View<T> v) {
333 {
r(s, v) } -> std::convertible_to<T>;
337 {
r(s, v, ctx) } -> std::convertible_to<T>;
357template <
typename Lattice,
typename Rule>
366 "Graph_Synchronous_Engine requires a GraphRuleLike rule");
409 std::is_nothrow_move_constructible_v<Lattice>
and std::is_nothrow_move_constructible_v<Rule>)
430 noexcept(
swap(other)))
431 requires std::swappable<Rule>
449 noexcept(
cur_buf_.
swap(other.cur_buf_))
and std::is_nothrow_swappable_v<Rule>)
450 requires std::swappable<Rule>
460 if constexpr (not std::is_nothrow_swappable_v<Rule> or not
noexcept(
cur_buf_.
swap(other.cur_buf_)))
508 template <
typename F>
515 template <
typename F>
534 for (
ca_size_t node = 0; node < n; ++node)
536 const auto &neigh_ids =
cur_buf_.neighbours(node);
537 const std::size_t deg = neigh_ids.
size();
545 for (std::size_t
k = 0;
k < deg; ++
k)
567 for (std::size_t i = 0; i <
steps; ++i)
591 std::size_t
rows, std::size_t
cols,
bool periodic =
false)
594 auto idx = [
cols](std::size_t i, std::size_t j) {
return i *
cols + j; };
595 for (std::size_t i = 0; i <
rows; ++i)
596 for (std::size_t j = 0; j <
cols; ++j)
598 auto &
row = adj[idx(i, j)];
606 row.append(idx(i + 1, j));
608 row.append(idx(0, j));
611 row.append(idx(i, j - 1));
616 row.append(idx(i, j + 1));
618 row.append(idx(i, 0));
634 std::size_t n,
bool cycle =
false)
637 for (std::size_t i = 0; i < n; ++i)
641 else if (cycle
and n > 1)
642 adj[i].append(n - 1);
644 adj[i].append(i + 1);
645 else if (cycle
and n > 1)
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.
size_t size_t int32_t value
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void swap(Array &s) noexcept
Swap this with s
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Graph lattice: one cell per node + precomputed adjacency.
Graph_Lattice & operator=(Graph_Lattice &&other) noexcept
Exchange this lattice with another, which is left with the previous nodes of this one.
std::size_t degree(std::size_t n) const
Degree of node n.
Graph_Lattice()=default
Construct an empty graph lattice (0 nodes).
void set(const coord_type &c, const T &v)
ca_size_t size() const noexcept
T at_node(std::size_t n) const
Direct read at node id n.
Coord_Vec< 1 > coord_type
const Array< std::size_t > & neighbours(std::size_t n) const
Neighbour ids of node n in the order supplied at construction.
ca_size_t size(std::size_t d) const
extents_type extents() const noexcept
Graph_Lattice(const Array< Array< std::size_t > > &adjacency, const T &init=T{})
Build a graph lattice from an adjacency list.
void set_node(std::size_t n, const T &v)
Direct write at node id n.
std::array< ca_size_t, 1 > extents_type
T at(const coord_type &c) const
static constexpr std::size_t dimension() noexcept
void swap(Graph_Lattice &other) noexcept
O(1) swap with another graph lattice of the same type.
static constexpr std::size_t rank
Rank-1 lattice: node id is the only axis.
std::size_t max_degree() const noexcept
Maximum degree across the graph (computed in O(N)).
T at_safe(const coord_type &c) const
Boundary-aware read.
void fill(const T &value)
Set every cell to value.
Graph_Lattice(Graph_Lattice &&other) noexcept
Take the cells and the adjacency of another lattice, which is left with no nodes.
Graph_Lattice(const Graph_Lattice &other)=default
Copy another lattice: its cells and its adjacency.
Array< Array< std::size_t > > adjacency_
Graph_Lattice & operator=(const Graph_Lattice &other)=default
Copy another lattice into this one: its cells and its adjacency.
Synchronous double-buffered engine for graph CAs.
Graph_Synchronous_Engine(Graph_Synchronous_Engine &&other) noexcept(std::is_nothrow_move_constructible_v< Lattice > and std::is_nothrow_move_constructible_v< Rule >)
Take the simulation of another engine, which is left with empty lattices, no hooks and no steps run.
void step()
Apply the rule to every node once and swap buffers.
Array< state_type > nbuf_
Aleph::Array reused across cells.
void on_post_step(F &&f)
Register a hook fired after every step().
void run(const std::size_t steps)
Run several synchronous steps.
void swap_all_or_none(Graph_Synchronous_Engine &other)
Graph_Synchronous_Engine(Lattice initial, Rule r)
Build an engine on top of an existing graph lattice.
Graph_Synchronous_Engine(const Graph_Synchronous_Engine &other)=default
Copy another engine: its lattices, its rule, its hooks and its count of steps.
std::function< void(std::size_t, const Lattice &)> pre_hook_
Graph_Synchronous_Engine & operator=(Graph_Synchronous_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation of this one.
std::size_t steps_run() const noexcept
void swap(Graph_Synchronous_Engine &other) noexcept(noexcept(cur_buf_.swap(other.cur_buf_)) and std::is_nothrow_swappable_v< Rule >)
Exchange in O(1) the simulation of this engine and another.
const Lattice & frame() const noexcept
Graph_Synchronous_Engine & operator=(const Graph_Synchronous_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its hooks and its count of steps.
void on_pre_step(F &&f)
Register a hook fired before every step().
typename Lattice::state_type state_type
std::function< void(std::size_t, const Lattice &)> post_hook_
Lattice that adds boundary-aware access on top of a storage.
typename Storage::state_type state_type
void swap(Lattice &other) noexcept(noexcept(store_.swap(other.store_)))
O(1) swap.
ca_size_t size() const noexcept
constexpr size_t size() const noexcept
Returns the number of entries in the table.
A type usable as the value stored inside a cell.
Concept satisfied by graph rules.
@ R
Recovered (and immune).
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Array< Array< std::size_t > > make_grid_graph_adjacency(std::size_t rows, std::size_t cols, bool periodic=false)
Build the adjacency of a 2D 4-neighbour grid graph.
Array< Array< std::size_t > > make_path_graph_adjacency(std::size_t n, bool cycle=false)
Build the adjacency of a path graph with n nodes.
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.
State apply_rule(const Rule &r, const State &s, Neighbor_View< State > v, const Cell_Context< Rank > &ctx)
Invoke a rule, optionally forwarding the per-cell context.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
static std::atomic< bool > init
Per-cell context handed to rules that need to know "where" and "when" they are firing.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
C++20 concepts for the Cellular Automata module.