67 constexpr std::size_t Max_Steps = 100000;
69 bool parse_rule_number(
const char * text,
int &
out)
73 const long value = std::strtol(text, &end, 10);
74 if (errno == ERANGE or end == text or *end !=
'\0' or
75 value < 0 or value > 255)
77 std::cerr <<
"Invalid rule: '" << text
78 <<
"' (expected an integer in [0, 255])\n";
85 bool parse_positive_size(
const char * text,
const char * name,
86 std::size_t max_value, std::size_t &
out)
88 if (text ==
nullptr or text[0] ==
'-')
90 std::cerr <<
"Invalid " << name <<
": '"
91 << (text ==
nullptr ?
"" : text)
92 <<
"' (expected a positive integer)\n";
97 const unsigned long long value = std::strtoull(text, &end, 10);
98 if (errno == ERANGE or end == text or *end !=
'\0' or
value == 0 or
99 value > std::numeric_limits<std::size_t>::max() or
102 std::cerr <<
"Invalid " << name <<
": '" << text
103 <<
"' (expected an integer in [1, "
107 out =
static_cast<std::size_t
>(
value);
111 bool parse_positive_ca_size(
const char * text,
const char * name,
114 std::size_t parsed = 0;
115 if (not parse_positive_size(text, name, Max_Width, parsed))
117 const auto ca_max = std::numeric_limits<ca_size_t>::max();
118 const auto idx_max =
static_cast<std::size_t
>(
119 std::numeric_limits<ca_index_t>::max());
120 if (parsed > ca_max or parsed > idx_max)
122 std::cerr <<
"Invalid " << name <<
": '" << text
123 <<
"' (value is too large)\n";
132 Row nxt(cur.extents());
135 std::array<int, 2> buf { };
136 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(cur.size(0)); ++i)
139 std::span<int>(buf.data(), buf.size()));
146 void print_row(
const Row &
r)
149 s.reserve(
r.size(0));
150 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(
r.size(0)); ++i)
151 s.push_back(
r.at({ i }) ?
'#' :
' ');
152 std::cout << s <<
'\n';
155 void run_rule(
unsigned rule_no,
ca_size_t width, std::size_t
steps)
158 static_cast<std::uint8_t
>(rule_no));
162 std::cout <<
"----- Rule " << rule_no <<
" -----\n";
164 for (std::size_t t = 1; t <=
steps; ++t)
166 row = step_row(
row, rule);
172int main(
int argc,
char ** argv)
175 std::size_t
steps = 30;
178 if (argc >= 2
and not parse_rule_number(argv[1], focus_rule))
180 if (argc >= 3
and not parse_positive_ca_size(argv[2],
"width", width))
182 if (argc >= 4
and not parse_positive_size(argv[3],
"steps",
186 if (focus_rule >= 0
and focus_rule <= 255)
187 run_rule(
static_cast<unsigned>(focus_rule), width,
steps);
189 for (
unsigned r = 0;
r < 256; ++
r)
190 run_rule(
r, width,
steps);
size_t size_t int32_t value
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
User-supplied list of offsets for arbitrary connectivity.
Lattice that adds boundary-aware access on top of a storage.
Precomputed transition table for (self, neighbours...).
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.
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.
Coord_Vec< N > Offset_Vec
Default offset vector (aliases Coord_Vec).
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.
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
Out-of-range neighbours behave as if the lattice ended.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).