Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_wolfram1d_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
45# include <array>
46# include <cerrno>
47# include <cstdint>
48# include <cstdlib>
49# include <iostream>
50# include <limits>
51# include <span>
52# include <string>
53
54# include <ca-traits.H>
55# include <tpl_ca_storage.H>
56# include <tpl_ca_lattice.H>
57# include <tpl_ca_neighborhood.H>
58# include <tpl_ca_rule.H>
59
60using namespace Aleph;
61using namespace Aleph::CA;
62
63namespace
64{
66 constexpr ca_size_t Max_Width = 100000;
67 constexpr std::size_t Max_Steps = 100000;
68
69 bool parse_rule_number(const char * text, int & out)
70 {
71 errno = 0;
72 char * end = nullptr;
73 const long value = std::strtol(text, &end, 10);
74 if (errno == ERANGE or end == text or *end != '\0' or
76 {
77 std::cerr << "Invalid rule: '" << text
78 << "' (expected an integer in [0, 255])\n";
79 return false;
80 }
81 out = static_cast<int>(value);
82 return true;
83 }
84
85 bool parse_positive_size(const char * text, const char * name,
86 std::size_t max_value, std::size_t & out)
87 {
88 if (text == nullptr or text[0] == '-')
89 {
90 std::cerr << "Invalid " << name << ": '"
91 << (text == nullptr ? "" : text)
92 << "' (expected a positive integer)\n";
93 return false;
94 }
95 errno = 0;
96 char * end = nullptr;
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
100 value > max_value)
101 {
102 std::cerr << "Invalid " << name << ": '" << text
103 << "' (expected an integer in [1, "
104 << max_value << "])\n";
105 return false;
106 }
107 out = static_cast<std::size_t>(value);
108 return true;
109 }
110
111 bool parse_positive_ca_size(const char * text, const char * name,
112 ca_size_t & out)
113 {
114 std::size_t parsed = 0;
115 if (not parse_positive_size(text, name, Max_Width, parsed))
116 return false;
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)
121 {
122 std::cerr << "Invalid " << name << ": '" << text
123 << "' (value is too large)\n";
124 return false;
125 }
126 out = static_cast<ca_size_t>(parsed);
127 return true;
128 }
129
130 Row step_row(const Row & cur, const Lookup_Rule<2, 2> & rule)
131 {
132 Row nxt(cur.extents());
134 Offset_Vec<1>{ 1 } }});
135 std::array<int, 2> buf { };
136 for (ca_index_t i = 0; i < static_cast<ca_index_t>(cur.size(0)); ++i)
137 {
139 std::span<int>(buf.data(), buf.size()));
140 nxt.set({ i },
141 rule(cur.at({ i }), Neighbor_View<int>(buf.data(), 2)));
142 }
143 return nxt;
144 }
145
146 void print_row(const Row & r)
147 {
148 std::string s;
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';
153 }
154
155 void run_rule(unsigned rule_no, ca_size_t width, std::size_t steps)
156 {
158 static_cast<std::uint8_t>(rule_no));
159 Row row({ width });
160 row.set({ static_cast<ca_index_t>(width / 2) }, 1);
161
162 std::cout << "----- Rule " << rule_no << " -----\n";
163 print_row(row);
164 for (std::size_t t = 1; t <= steps; ++t)
165 {
166 row = step_row(row, rule);
167 print_row(row);
168 }
169 }
170}
171
172int main(int argc, char ** argv)
173{
174 ca_size_t width = 61;
175 std::size_t steps = 30;
176 int focus_rule = -1;
177
179 return 1;
180 if (argc >= 3 and not parse_positive_ca_size(argv[2], "width", width))
181 return 1;
182 if (argc >= 4 and not parse_positive_size(argv[3], "steps",
184 return 1;
185
186 if (focus_rule >= 0 and focus_rule <= 255)
187 run_rule(static_cast<unsigned>(focus_rule), width, steps);
188 else
189 for (unsigned r = 0; r < 256; ++r)
190 run_rule(r, width, steps);
191
192 return 0;
193}
int main()
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t row
Definition ca-c-api.h:115
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...).
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::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Definition ca-traits.H:90
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
void gather_neighbors(const Nbh &nh, const L &lat, const typename L::coord_type &center, 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.
Definition ca-traits.H:63
Coord_Vec< N > Offset_Vec
Default offset vector (aliases Coord_Vec).
Definition ca-traits.H:79
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.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
Definition stat_utils.H:294
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
gsl_rng * r
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).