58#ifndef TPL_CA_MULTI_FIELD_ENGINE_H
59#define TPL_CA_MULTI_FIELD_ENGINE_H
78namespace ca_mf_engine_detail {
81template <
typename Lattice,
typename Neighborhood,
typename Buffers, std::size_t...
I>
83 const Neighborhood &nh,
86 std::index_sequence<I...>)
89 std::size_t neigh_sz = nh.size();
90 if (neigh_sz > std::tuple_size_v<std::tuple_element_t<0, Buffers>>)
91 neigh_sz = std::tuple_size_v<std::tuple_element_t<0, Buffers>>;
93 std::size_t cursor = 0;
94 nh.for_each_offset(center, [&](
const auto &off)
99 if (cursor < neigh_sz)
100 ((std::get<I>(bufs)[cursor] = lat.template at_safe<I>(c)), ...);
121template <
typename Lattice,
typename Rule,
typename Neighborhood>
125 "Multi_Field_Engine requires a MultiFieldLatticeLike lattice");
127 "Multi_Field_Engine requires a NeighborhoodLike neighbourhood");
128 static_assert(Neighborhood::rank_v ==
Lattice::rank,
"Neighbourhood rank must match lattice rank");
154 template <std::size_t...
I>
157 return std::tuple{std::array<typename Lattice::template field_type<I>,
161 template <std::size_t...
I>
164 ((
nxt_.template set<I>(c, std::get<I>(v))), ...);
184 requires std::default_initializable<Neighborhood>
204 std::is_nothrow_move_constructible_v<Lattice>
and std::is_nothrow_move_constructible_v<Rule>
205 and std::is_nothrow_move_constructible_v<Neighborhood>)
225 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
244 noexcept(
cur_.
swap(other.cur_))
and std::is_nothrow_swappable_v<Rule>
245 and std::is_nothrow_swappable_v<Neighborhood>)
246 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
255 constexpr bool may_throw =
256 not std::is_nothrow_swappable_v<Rule> or not std::is_nothrow_swappable_v<Neighborhood>
257 or not
noexcept(
cur_.
swap(other.cur_));
259 if constexpr (may_throw)
325 template <
typename F>
332 template <
typename F>
355 auto bufs =
make_buffers(std::make_index_sequence<field_count>{});
360 std::make_index_sequence<field_count>{});
364 = apply_multi_field_rule<Rule, fields_tuple, rank>(
rule_, cur_vals, views, ctx);
365 write_cell(c, new_vals, std::make_index_sequence<field_count>{});
383 for (std::size_t i = 0; i <
steps; ++i)
388 template <
typename F>
392 if constexpr (
rank == 1)
401 else if constexpr (
rank == 2)
413 else if constexpr (
rank == 3)
429 static_assert(
rank <= 3,
"Multi_Field_Engine currently supports rank <= 3");
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
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
Synchronous double-buffered engine for multi-field lattices.
void run(const std::size_t steps)
Run several steps.
Multi_Field_Engine(const Multi_Field_Engine &other)=default
Copy another engine: its lattices, its rule, its neighborhood, its hooks and its count of steps.
typename Lattice::coord_type coord_type
Multi_Field_Engine(Lattice initial, Rule r)
Build with a default-constructed neighbourhood.
Multi_Field_Engine(Lattice initial, Rule r, Neighborhood n)
Build a multi-field engine.
void step()
Apply the multi-field rule to every cell once and swap buffers.
void on_pre_step(F &&f)
Register a hook fired before every step().
void iterate_lattice(F &&f)
static constexpr std::size_t rank
void write_cell(const coord_type &c, const fields_tuple &v, std::index_sequence< I... >)
Multi_Field_Engine(Multi_Field_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.
static constexpr std::size_t neighbour_count
Multi_Field_Engine & operator=(Multi_Field_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation of this one.
const extents_type & extents() const noexcept
static constexpr std::size_t field_count
void on_post_step(F &&f)
Register a hook fired after every step().
typename Lattice::extents_type extents_type
Multi_Field_Engine & operator=(const Multi_Field_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks and its count ...
const Lattice & frame() const noexcept
static auto make_buffers(std::index_sequence< I... >)
std::size_t steps_run() const noexcept
void swap_all_or_none(Multi_Field_Engine &other)
std::function< void(std::size_t, const Lattice &)> hook_type
Hook signature: (step_index_just_completed, frame).
typename Lattice::field_types fields_tuple
void swap(Multi_Field_Engine &other) noexcept(noexcept(cur_.swap(other.cur_)) 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.
Neighborhood neighborhood_type
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
A coordinate type with N integral components.
Concept: a lattice that exposes the multi-field interface.
Connectivity pattern around a coordinate.
auto build_views(const Lattice &lat, const Neighborhood &nh, const typename Lattice::coord_type ¢er, Buffers &bufs, std::index_sequence< I... >)
Recurse over the Field_Indices and gather one Neighbor_View per field.
@ I
Infected (and infectious).
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.
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.
C++20 concepts for the Cellular Automata module.
Phase 14 multi-field cellular automata lattice (AoS / SoA).
Phase 14 multi-field local rules for Aleph::CA.
Neighborhoods catalogue for Aleph::CA.