55#ifndef TPL_CA_ENGINE_H
56#define TPL_CA_ENGINE_H
73namespace ca_engine_detail {
80template <std::
size_t W, std::
size_t H>
89template <
typename Coord,
typename F>
93 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
101template <
typename Coord,
typename F>
105 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
106 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
115template <
typename Coord,
typename F>
119 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
120 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
130template <std::
size_t W, std::
size_t H,
typename Coord,
typename F>
137 const ca_size_t i_end = ti + H < n0 ? ti + H : n0;
138 const ca_size_t j_end = tj + W < n1 ? tj + W : n1;
150template <
typename Coord,
typename F>
154 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
155 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
156 for (
ca_index_t k = 0; k < static_cast<ca_index_t>(n2); ++
k)
182template <
typename Lattice,
typename Rule,
typename Neighborhood,
typename Order = RowMajor>
187 "Synchronous_Engine requires a NeighborhoodLike neighborhood");
189 static_assert(Neighborhood::rank_v ==
Lattice::rank,
"Neighborhood rank must match lattice rank");
258 requires std::default_initializable<Neighborhood>
278 std::is_nothrow_move_constructible_v<Lattice>
and std::is_nothrow_move_constructible_v<Rule>
279 and std::is_nothrow_move_constructible_v<Neighborhood>)
300 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
319 noexcept(
cur_buf_.
swap(other.cur_buf_))
and std::is_nothrow_swappable_v<Rule>
320 and std::is_nothrow_swappable_v<Neighborhood>)
321 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
330 constexpr bool may_throw =
331 not std::is_nothrow_swappable_v<Rule> or not std::is_nothrow_swappable_v<Neighborhood>
334 if constexpr (may_throw)
411 [[nodiscard]]
const Rule &
rule() const noexcept
420 [[nodiscard]] Rule &
rule() noexcept
454 template <
typename F>
465 template <
typename F>
484 using namespace ca_engine_detail;
498 std::array<state_type, neighbour_count> nbuf{};
502 const std::size_t neigh_sz = std::min(
nh_.size(), nbuf.size());
512 if constexpr (
rank == 1)
516 else if constexpr (
rank == 2)
518 if constexpr (is_tile_v<Order>)
519 iterate_2d_tile<Order::width, Order::height, coord_type>(
cur_buf_.
size(0),
522 else if constexpr (std::is_same_v<Order, ColumnMajor>)
527 else if constexpr (
rank == 3)
532 static_assert(
rank <= 3,
"Synchronous_Engine currently supports rank <= 3");
553 for (std::size_t i = 0; i <
steps; ++i)
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
void set(const coord_type &c, const state_type &v)
Strict write: throws if c is out of range.
typename Storage::state_type state_type
typename Storage::extents_type extents_type
const extents_type & extents() const noexcept
void swap(Lattice &other) noexcept(noexcept(store_.swap(other.store_)))
O(1) swap.
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Synchronous double-buffered engine.
const extents_type & extents() const noexcept
Return the frame extents.
static constexpr std::size_t rank
Lattice dimension.
static constexpr std::size_t neighbour_count
Number of neighbours.
void run(const std::size_t steps)
Run several synchronous steps.
std::function< void(std::size_t, const Lattice &)> hook_type
Hook signature: (step_index_just_completed, frame).
Synchronous_Engine & operator=(Synchronous_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation of this one.
typename Lattice::extents_type extents_type
Extents type.
const Rule & rule() const noexcept
Read-only access to the rule (used by checkpointing).
Rule & rule() noexcept
Mutable access to the rule (used by checkpointing to restore the master RNG seed of stochastic rules)...
Lattice & write_lattice() noexcept
Mutable access to the current lattice buffer.
void on_post_step(F &&f)
Register a hook fired after every step().
std::size_t steps_run() const noexcept
Return the number of completed steps.
Neighborhood neighborhood_type
Neighborhood type.
typename Lattice::coord_type coord_type
Coordinate type.
void step()
Apply the rule to every cell once and swap buffers.
Synchronous_Engine(Lattice initial, Rule r, Neighborhood n)
Build an engine on top of an existing initial lattice.
Synchronous_Engine & operator=(const Synchronous_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks and its count ...
void on_pre_step(F &&f)
Register a hook fired before every step().
Order order_type
Iteration order.
const Lattice & frame() const noexcept
Return the current frame.
void swap(Synchronous_Engine &other) noexcept(noexcept(cur_buf_.swap(other.cur_buf_)) and std::is_nothrow_swappable_v< Rule > and std::is_nothrow_swappable_v< Neighborhood >)
Exchange in O(1) the simulation of this engine and another.
Synchronous_Engine(Lattice initial, Rule r)
Build an engine with a default-constructed neighbourhood.
Synchronous_Engine(const Synchronous_Engine &other)=default
Copy another engine: its lattices, its rule, its neighborhood, its hooks and its count of steps.
typename Lattice::state_type state_type
Cell state type.
void set_step_count(const std::size_t s) noexcept
Override the completed-step counter.
Synchronous_Engine(Synchronous_Engine &&other) noexcept(std::is_nothrow_move_constructible_v< Lattice > and std::is_nothrow_move_constructible_v< Rule > and std::is_nothrow_move_constructible_v< Neighborhood >)
Take the simulation of another engine, which is left with empty lattices, no hooks and no steps run.
void swap_all_or_none(Synchronous_Engine &other)
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
A coordinate type with N integral components.
Detect whether a lattice exposes a refresh_halo() method (the hallmark of a Ghost_Lattice or any othe...
Storage + topology that carries the cell values.
Connectivity pattern around a coordinate.
Local transition function (any supported signature).
void iterate_3d_row_major(const ca_size_t n0, const ca_size_t n1, const ca_size_t n2, F &&f)
3D row-major iteration.
void iterate_1d(ca_size_t n0, F &&f)
1D row-major iteration callback f(coord).
void iterate_2d_column_major(ca_size_t n0, ca_size_t n1, F &&f)
2D column-major iteration.
void iterate_2d_tile(const ca_size_t n0, const ca_size_t n1, F &&f)
2D tiled iteration with tile size H along axis 0 and W along axis 1.
void iterate_2d_row_major(ca_size_t n0, ca_size_t n1, F &&f)
2D row-major iteration.
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.
void gather_neighbors(const Nbh &nh, const L &lat, const typename L::coord_type ¢er, std::span< T > out)
Populate out[0..nh.size()) with neighbour values of center.
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.
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Iterate a 2D lattice in W x H tiles.
Detect whether O is a Tile<W, H> instantiation.
C++20 concepts for the Cellular Automata module.
Neighborhoods catalogue for Aleph::CA.