58 template <
typename State>
61 constexpr State threshold = 4;
63 if (result >= threshold)
65 for (
const auto &n : nh)
73void render(
const Grid &g, std::ostream &os)
75 static const std::array<const char *, 5> glyphs = {
" .",
" :",
" +",
" #",
"##"};
80 const auto v = g.at({
static_cast<ca_index_t>(i),
82 const std::size_t idx = v < 0 ? 0 : (v > 4 ? 4 :
static_cast<std::size_t
>(v));
90bool stabilised(
const Grid &g)
94 if (g.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}) >= 4)
100std::size_t cells_changed(
const Grid &before,
const Grid &after)
103 for (
ca_size_t i = 0; i < before.size(0); ++i)
104 for (
ca_size_t j = 0; j < before.size(1); ++j)
108 if (before.at(c) != after.at(c))
118 std::size_t drops = 80;
123 drops =
static_cast<std::size_t
>(std::stoul(argv[1]));
125 side =
static_cast<ca_size_t>(std::stoul(argv[2]));
127 catch (
const std::invalid_argument &)
129 std::cerr <<
"Invalid argument: expected non-negative integers for [drops] [side]\n";
132 catch (
const std::out_of_range &)
134 std::cerr <<
"Argument out of range for [drops] [side]\n";
139 std::cerr <<
"Invalid side: must be a positive integer\n";
143 Grid grid({side, side}, 0);
147 std::cout <<
"BTW Sandpile — " << side <<
"×" << side <<
", " << drops
148 <<
" drops at the centre\n\n";
150 std::size_t total_avalanche_cells = 0;
151 for (std::size_t
k = 0;
k < drops; ++
k)
154 const auto cr =
static_cast<ca_index_t>(side / 2);
155 const auto cc =
static_cast<ca_index_t>(side / 2);
156 grid.set({cr, cc}, grid.at({cr, cc}) + 1);
161 Grid pre =
engine.frame();
162 std::size_t sweeps = 0;
163 while (not stabilised(
engine.frame())
and sweeps < 4 * side * side)
168 const std::size_t aval = cells_changed(pre,
engine.frame());
169 total_avalanche_cells += aval;
172 if (
k % (drops < 20 ? 1 : 10) == 0 or
k + 1 == drops)
173 std::cout <<
"drop " <<
k + 1 <<
"/" << drops
174 <<
" — avalanche size " << aval
175 <<
" (cumulative " << total_avalanche_cells
176 <<
", " << sweeps <<
" sweeps)\n";
179 std::cout <<
"\nFinal frame:\n\n";
180 render(grid, std::cout);
181 std::cout <<
"\nAverage avalanche size: "
182 <<
static_cast<double>(total_avalanche_cells) /
static_cast<double>(drops)
Common typedefs and tag types for the Cellular Automata module.
Update-scheme aware engine.
Lattice that adds boundary-aware access on top of a storage.
Von Neumann (L1) neighborhood of radius R in N dimensions.
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.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Out-of-range neighbours behave as if the lattice ended.
In-place sequential update (no double buffer).
Phase 13 update-scheme aware engine.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Phase 13 update-scheme strategies for Aleph::CA.