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_ <<
")";
171 <<
"Graph_Lattice::size: axis " << d <<
" out of range";
183 <<
"Graph_Lattice::at: id " << c[0] <<
" out of [0, " <<
num_nodes_ <<
")";
190 <<
"Graph_Lattice::set: id " << c[0] <<
" out of [0, " <<
num_nodes_ <<
")";
212 <<
"Graph_Lattice::at_node: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
220 <<
"Graph_Lattice::set_node: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
228 <<
"Graph_Lattice::degree: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
236 <<
"Graph_Lattice::neighbours: id " << n <<
" out of [0, " <<
num_nodes_ <<
")";
293template <
typename R,
typename T>
296 {
r(s, v) } -> std::convertible_to<T>;
300 {
r(s, v, ctx) } -> std::convertible_to<T>;
320template <
typename Lattice,
typename Rule>
329 "Graph_Synchronous_Engine requires a GraphRuleLike rule");
363 template <
typename F>
370 template <
typename F>
389 for (
ca_size_t node = 0; node < n; ++node)
400 for (std::size_t
k = 0;
k <
deg; ++
k)
422 for (std::size_t i = 0; i <
steps; ++i)
449 auto idx = [
cols](std::size_t i, std::size_t j) {
return i *
cols + j; };
450 for (std::size_t i = 0; i <
rows; ++i)
451 for (std::size_t j = 0; j <
cols; ++j)
453 auto &
row = adj[idx(i, j)];
461 row.append(idx(i + 1, j));
463 row.append(idx(0, j));
466 row.append(idx(i, j - 1));
471 row.append(idx(i, j + 1));
473 row.append(idx(i, 0));
489 std::size_t n,
bool cycle =
false)
492 for (std::size_t i = 0; i < n; ++i)
497 adj[i].append(n - 1);
499 adj[i].append(i + 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.
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.
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.
Array< Array< std::size_t > > adjacency_
Synchronous double-buffered engine for graph CAs.
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.
Graph_Synchronous_Engine(Lattice initial, Rule r)
Build an engine on top of an existing graph lattice.
std::function< void(std::size_t, const Lattice &)> pre_hook_
std::size_t steps_run() const noexcept
const Lattice & frame() const noexcept
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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
@ 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.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
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.