Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_triangular_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#include <cstdio>
43
44#include <tpl_ca_engine.H>
45#include <tpl_ca_neighborhood.H>
46#include <tpl_ca_rule.H>
47#include <tpl_ca_storage.H>
49
50using namespace Aleph;
51using namespace Aleph::CA;
52
53namespace {
54
55struct Spread_Once_Functor
56{
57 template <typename State>
58 [[nodiscard]] constexpr State operator()(const State &current,
59 const std::size_t alive) const noexcept
60 {
61 return (current != State{} or alive >= 1) ? static_cast<State>(1)
62 : static_cast<State>(0);
63 }
64};
65
67
68void print(const L &lat, std::size_t step)
69{
70 std::size_t alive = 0;
71 for (ca_size_t i = 0; i < lat.rows(); ++i)
72 for (ca_size_t j = 0; j < lat.cols(); ++j)
73 if (lat.at_tri(Tri_Coord{static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
74 != 0)
75 ++alive;
76 std::printf("\nStep %2zu alive=%zu\n", step, alive);
77 for (ca_size_t i = 0; i < lat.rows(); ++i)
78 {
79 for (ca_size_t j = 0; j < lat.cols(); ++j)
80 {
81 const Tri_Coord c{static_cast<ca_index_t>(i),
82 static_cast<ca_index_t>(j)};
83 const char glyph
84 = lat.at_tri(c) != 0 ? (tri_is_up(c) ? '^' : 'v') : '.';
85 std::printf("%c ", glyph);
86 }
87 std::putchar('\n');
88 }
89}
90
91} // namespace
92
93int main()
94{
95 L seed({8, 8}, 0);
96 // Seed a single up-triangle at (3, 3) and a down-triangle far from it.
97 seed.set_tri(Tri_Coord{3, 3}, 1);
98 seed.set_tri(Tri_Coord{6, 0}, 1);
99
100 std::printf(
101 "Aleph::CA Phase-6 example: triangular spread-once on an 8x8 lattice\n");
102 print(seed, 0);
103
106 seed, Rule{Spread_Once_Functor{}}, Triangular_Neighborhood{});
107 for (std::size_t s = 1; s <= 6; ++s)
108 {
109 eng.step();
110 print(eng.frame(), s);
111 }
112
113 std::printf("\n--- TikZ render of step 6 ---\n");
115 eng.frame(),
116 [](int v) { return v != 0 ? "blue" : "white"; },
117 /*side=*/0.6)
118 .c_str(),
119 stdout);
120 return 0;
121}
int main()
Rule whose next state depends on (current, alive_count).
Definition tpl_ca_rule.H:99
Synchronous double-buffered engine.
void step()
Apply the rule to every cell once and swap buffers.
Triangular lattice with parity helpers over a 2D storage.
Three edge-neighbours over a 2D lattice with parity coupling.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
constexpr bool tri_is_up(const Tri_Coord &c) noexcept
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
void render_triangular_lattice_tikz(std::ostream &os, const Lattice &lat, Palette &&palette, const double side=1.0)
Render a triangular frame as a TikZ picture.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
Definition tpl_net.H:887
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
Coordinates of a triangular cell as (i, j) integers.
ValueArg< size_t > seed
Definition testHash.C:53
Synchronous double-buffered engine for cellular automata.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Triangular lattice with up/down parity helpers and pixel mapping suitable for visualisation.