119 template <
typename State>
122 std::size_t alive = 0;
123 for (
const auto &v : neighbours)
126 return f_(current, alive);
169 template <
typename State>
172 using sum_type = std::common_type_t<State, std::intmax_t>;
174 for (
const auto &v : neighbours)
176 return static_cast<State
>(
f_(
sum));
184namespace ca_rule_detail {
188 for (std::size_t i = 0; i <
exp; ++i)
211template <std::
size_t NumStates, std::
size_t NumNeighbors>
265 template <
typename State>
269 <<
"Lookup_Rule::operator(): expected " <<
NumNeighbors <<
" neighbours, got "
270 << neighbours.size();
274 if constexpr (std::is_signed_v<State>)
276 <<
"Lookup_Rule::operator(): " << label <<
" state is negative";
278 const std::size_t idx =
static_cast<std::size_t
>(
value);
280 << idx <<
" outside [0, " <<
NumStates <<
")";
285 for (
const auto &v : neighbours)
287 return static_cast<State
>(
table_[idx]);
307 for (std::size_t pattern = 0; pattern < 8; ++pattern)
309 const std::size_t left = (pattern >> 2) & 1;
310 const std::size_t self = (pattern >> 1) & 1;
311 const std::size_t right = (pattern >> 0) & 1;
312 const std::size_t
lookup_idx = (self << 2) | (left << 1) | right;
347template <
typename F,
typename Engine = std::mt19937_64>
399 template <
typename State>
402 requires std::invocable<F &, const State &, Neighbor_View<State>>
404 return f_(current, neighbours);
422 template <
typename State, std::
size_t Rank>
427 if constexpr (std::is_invocable_r_v<State,
F &,
const State &,
432 return f_(current, neighbours,
eng);
436 return f_(current, neighbours);
454template <
typename...
Rules>
475 template <
typename State>
480 [&](
const auto &...
r)
482 ((
acc =
r(
acc, neighbours)), ...);
504 template <
typename State>
507 const bool alive_now = current != State{};
510 return alive_next ?
static_cast<State
>(1) :
static_cast<State
>(0);
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
size_t size_t int32_t value
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Sequential composition of one or more rules.
std::tuple< Rules... > rules_
constexpr Composite_Rule(Rules... rs)
Construct a sequential composition of rules.
constexpr State operator()(const State ¤t, Neighbor_View< State > neighbours) const
Apply every composed rule to the same neighbour view.
Precomputed transition table for (self, neighbours...).
static constexpr std::size_t num_states_v
Number of distinct cell states accepted by this rule.
State operator()(const State ¤t, Neighbor_View< State > neighbours) const
Compute the next state using the precomputed table.
static constexpr std::size_t num_neighbours_v
Number of neighbour entries required by operator().
static constexpr std::size_t table_size_v
Number of entries in the transition table.
constexpr const table_type & raw_table() const noexcept
Return the underlying transition table.
constexpr Lookup_Rule()=default
Build a zero-initialised lookup rule.
std::array< std::size_t, table_size_v > table_type
Transition table type storing next states for every input tuple.
constexpr Lookup_Rule(table_type t)
Build a lookup rule from a precomputed transition table.
Rule whose next state depends on (current, alive_count).
constexpr State operator()(const State ¤t, Neighbor_View< State > neighbours) const
Compute the next state from current value and neighbours.
constexpr Outer_Totalistic_Rule(F func)
Construct an outer-totalistic rule from a functor.
Reproducible stochastic rule wrapper (Phase 8).
constexpr std::uint64_t master_seed() const noexcept
constexpr Probabilistic_Rule(F func, std::uint64_t master_seed)
Construct a stochastic wrapper with an explicit master seed.
State operator()(const State ¤t, Neighbor_View< State > neighbours) const
Legacy invocation: forward (state, neighbours) to F.
constexpr Probabilistic_Rule(F func)
Construct a stochastic wrapper from a functor.
Engine engine_type
Underlying engine type used when the wrapped functor accepts an Engine& reference.
std::uint64_t master_seed_
void set_master_seed(std::uint64_t s) noexcept
Replace the master seed without rebuilding the rule.
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Reproducible invocation with engine context.
Rule whose next state depends on the sum of every cell in the neighbourhood (including the centre).
constexpr State operator()(const State ¤t, Neighbor_View< State > neighbours) const
Compute the next state from the sum of centre and neighbours.
constexpr Totalistic_Rule(F func)
Construct a totalistic rule from a functor.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
constexpr std::size_t static_pow_rule(std::size_t base, std::size_t exp) noexcept
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Outer_Totalistic_Rule< Game_Of_Life_Functor > Game_Of_Life_Rule
Outer-totalistic rule type implementing Conway's Game of Life.
constexpr Lookup_Rule< 2, 2 > make_wolfram_elementary_rule(std::uint8_t rule_no) noexcept
Build the elementary 1D Wolfram rule rule_no (0..255) as a Lookup_Rule<2, 2> over neighbourhood {-1,...
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Functor implementing Conway's Game of Life canonical rule (B3/S23).
constexpr State operator()(const State ¤t, const std::size_t alive) const noexcept
Evaluate the B3/S23 transition.