Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_io_test.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
36#include <cstdint>
37#include <limits>
38#include <set>
39#include <sstream>
40#include <stdexcept>
41#include <string>
42#include <utility>
43
44#include <gtest/gtest.h>
45
46#include <ca-io.H>
47#include <tpl_ca_lattice.H>
48#include <tpl_ca_storage.H>
49
50using namespace Aleph::CA;
51
52namespace
53{
56 using Cell = std::pair<ca_index_t, ca_index_t>;
57
58 Grid make_grid(ca_size_t rows, ca_size_t cols,
59 std::initializer_list<Cell> alive)
60 {
61 Grid g({rows, cols}, 0);
62 for (const auto &c : alive)
63 g.set({c.first, c.second}, 1);
64 return g;
65 }
66
67 Grid make_values_grid()
68 {
69 Grid g({2, 3}, 0);
70 int v = 1;
71 for (ca_index_t r = 0; r < 2; ++r)
72 for (ca_index_t c = 0; c < 3; ++c)
73 g.set({r, c}, v++);
74 return g;
75 }
76
77 std::set<Cell> alive_cells(const Grid &g)
78 {
79 std::set<Cell> out;
80 for (ca_index_t r = 0; r < static_cast<ca_index_t>(g.size(0)); ++r)
81 for (ca_index_t c = 0; c < static_cast<ca_index_t>(g.size(1)); ++c)
82 if (g.at({r, c}) != 0)
83 out.insert({r, c});
84 return out;
85 }
86
87 void expect_same_frame(const Grid &a, const Grid &b)
88 {
89 ASSERT_EQ(a.size(0), b.size(0));
90 ASSERT_EQ(a.size(1), b.size(1));
91 for (ca_index_t r = 0; r < static_cast<ca_index_t>(a.size(0)); ++r)
92 for (ca_index_t c = 0; c < static_cast<ca_index_t>(a.size(1)); ++c)
93 EXPECT_EQ(a.at({r, c}), b.at({r, c})) << "at (" << r << ", " << c << ")";
94 }
95
96 std::string bytes(std::initializer_list<unsigned> values)
97 {
98 std::string out;
99 for (const unsigned v : values)
100 out.push_back(static_cast<char>(static_cast<unsigned char>(v)));
101 return out;
102 }
103}
104
105TEST(CAIO, RleFullFrameRoundTripPreservesExtentsAndCells)
106{
107 const Grid src = make_grid(4, 5, {{1, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}});
109 opts.name = "glider";
110 opts.comment = "full-frame snapshot";
111
112 const std::string rle = write_rle_string(src, opts);
113 EXPECT_NE(rle.find("#N glider"), std::string::npos);
114 EXPECT_NE(rle.find("#C full-frame snapshot"), std::string::npos);
115 EXPECT_NE(rle.find("x = 5, y = 4, rule = B3/S23"), std::string::npos);
116
117 const RLE_Pattern pattern = read_rle_string(rle);
118 EXPECT_EQ(pattern.width, 5u);
119 EXPECT_EQ(pattern.height, 4u);
120 EXPECT_EQ(pattern.rule, "B3/S23");
121 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
122 expect_same_frame(src, dst);
123}
124
125TEST(CAIO, RleTrimmedOutputMatchesCanonicalGliderShape)
126{
127 const Grid src = make_grid(6, 7, {{2, 3}, {3, 4}, {4, 2}, {4, 3}, {4, 4}});
129 opts.trim_dead_border = true;
130 opts.line_width = 0;
131
132 const std::string rle = write_rle_string(src, opts);
133 EXPECT_NE(rle.find("x = 3, y = 3, rule = B3/S23"), std::string::npos);
134 EXPECT_NE(rle.find("bob$2bo$3o!"), std::string::npos) << rle;
135
136 const Grid trimmed = make_lattice_from_pattern<Grid>(read_rle_string(rle));
137 EXPECT_EQ(alive_cells(trimmed),
138 (std::set<Cell>{{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}));
139}
140
141TEST(CAIO, RleParserAcceptsCommentsLongRunsAndConwayAliases)
142{
143 const std::string text =
144 "#N mixed dialect\n"
145 "#C A is accepted as alive and . as dead\n"
146 "x = 5, y = 3, rule = B36/S23\n"
147 "2A3b$5.$4bo!\n";
148 const RLE_Pattern pattern = read_rle_string(text);
149 EXPECT_EQ(pattern.name, "mixed dialect");
150 ASSERT_EQ(pattern.comments.size(), 1u);
151 EXPECT_EQ(pattern.rule, "B36/S23");
152 EXPECT_EQ(pattern.width, 5u);
153 EXPECT_EQ(pattern.height, 3u);
154 EXPECT_EQ(pattern.alive,
155 (Array<Coord_Vec<2>>{{0, 0}, {0, 1}, {2, 4}}));
156}
157
158TEST(CAIO, RleParserRejectsMalformedInput)
159{
160 EXPECT_THROW(read_rle_string("3o!\n"), std::domain_error);
161 EXPECT_THROW(read_rle_string("x = 2, y = 1\n3o!\n"), std::domain_error);
162 EXPECT_THROW(read_rle_string("x = 2, y = 1\n2"), std::domain_error);
163 EXPECT_THROW(read_rle_string("x = 2, y = 1\nbo?\n"), std::domain_error);
164}
165
166// Regression (fuzz_rle_parser out-of-memory): a 34-byte document declared a
167// huge row and a live run of 999999999 cells, so read_rle() tried to store
168// about 16 GB of coordinates. The live-cell budget rejects it before any
169// coordinate is stored.
170TEST(CAIO, RleParserBoundsTheNumberOfLiveCells)
171{
172 const std::string hostile = "x = 1000000000, y = 1\n999999999o!\n";
173 EXPECT_THROW((void) read_rle_string(hostile), std::length_error);
174
175 auto r = try_read_rle_string(hostile);
176 EXPECT_FALSE(r.has_value());
177
178 // The budget is cumulative across runs and rows, and configurable.
179 EXPECT_THROW((void) read_rle_string("x = 3, y = 2\n2o$2o!\n", 3), std::length_error);
180 EXPECT_EQ(read_rle_string("x = 3, y = 2\n2o$o!\n", 3).alive.size(), 3u);
181
182 // A sparse pattern may declare a huge area: only live cells are stored.
183 const RLE_Pattern sparse = read_rle_string("x = 1000000000, y = 1000000000\no!\n");
184 EXPECT_EQ(sparse.alive, (Array<Coord_Vec<2>>{{0, 0}}));
185}
186
187TEST(CAIO, PlaintextRoundTripPreservesFullFrame)
188{
189 const Grid src = make_grid(3, 4, {{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
191 opts.comment = "plaintext glider";
192 const std::string text = write_plaintext_string(src, opts);
193 EXPECT_NE(text.find("!plaintext glider"), std::string::npos);
194 EXPECT_NE(text.find(".O.."), std::string::npos);
195
196 const Binary_Cell_Pattern pattern = read_plaintext_string(text);
197 EXPECT_EQ(pattern.width, 4u);
198 EXPECT_EQ(pattern.height, 3u);
199 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
200 expect_same_frame(src, dst);
201}
202
203TEST(CAIO, PlaintextParserAcceptsStarsAndRejectsUnknownGlyphs)
204{
205 const Binary_Cell_Pattern pattern = read_plaintext_string("!demo\n*..\n.O.\n");
206 EXPECT_EQ(pattern.width, 3u);
207 EXPECT_EQ(pattern.height, 2u);
208 EXPECT_EQ(pattern.alive, (Array<Coord_Vec<2>>{{0, 0}, {1, 1}}));
209 EXPECT_THROW(read_plaintext_string(".X.\n"), std::domain_error);
210}
211
212TEST(CAIO, Life106NormalisesNegativeCoordinates)
213{
214 const std::string text =
215 "#Life 1.06\n"
216 "#D shifted pair\n"
217 "-2 -1\n"
218 "0 1\n";
219 std::istringstream in(text);
220 const Binary_Cell_Pattern pattern = read_life_106(in);
221 EXPECT_EQ(pattern.origin_col, -2);
222 EXPECT_EQ(pattern.origin_row, -1);
223 EXPECT_EQ(pattern.width, 3u);
224 EXPECT_EQ(pattern.height, 3u);
225 EXPECT_EQ(pattern.alive, (Array<Coord_Vec<2>>{{0, 0}, {2, 2}}));
226}
227
228TEST(CAIO, Life106WriterProducesReadableCoordinates)
229{
230 const Grid src = make_grid(3, 3, {{0, 2}, {2, 0}});
231 std::ostringstream out;
232 write_life_106(out, src);
233
234 std::istringstream in(out.str());
235 const Binary_Cell_Pattern pattern = read_life_106(in);
236 EXPECT_EQ(pattern.width, 3u);
237 EXPECT_EQ(pattern.height, 3u);
238 EXPECT_EQ(pattern.alive, (Array<Coord_Vec<2>>{{0, 2}, {2, 0}}));
239}
240
241TEST(CAIO, Life105RoundTripPreservesFullFrame)
242{
243 const Grid src = make_grid(3, 4, {{0, 0}, {1, 2}, {2, 3}});
244 std::ostringstream out;
245 write_life_105(out, src, "single block");
246 EXPECT_NE(out.str().find("#Life 1.05"), std::string::npos);
247 EXPECT_NE(out.str().find("#P 0 0"), std::string::npos);
248
249 std::istringstream in(out.str());
250 const Binary_Cell_Pattern pattern = read_life_105(in);
251 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
252 expect_same_frame(src, dst);
253}
254
255TEST(CAIO, Life105ParserMergesNegativeBlocks)
256{
257 const std::string text =
258 "#Life 1.05\n"
259 "#P -1 -1\n"
260 "*.\n"
261 ".*\n"
262 "#P 2 0\n"
263 "*\n";
264 std::istringstream in(text);
265 const Binary_Cell_Pattern pattern = read_life_105(in);
266 EXPECT_EQ(pattern.origin_col, -1);
267 EXPECT_EQ(pattern.origin_row, -1);
268 EXPECT_EQ(pattern.width, 4u);
269 EXPECT_EQ(pattern.height, 2u);
270 EXPECT_EQ(pattern.alive,
271 (Array<Coord_Vec<2>>{{0, 0}, {1, 1}, {1, 3}}));
272}
273
274TEST(CAIO, CsvRoundTripPreservesNumericSnapshot)
275{
276 const Grid src = make_values_grid();
277 CSV_Options opts;
278 opts.delimiter = ';';
279
280 std::ostringstream out;
281 write_csv(out, src, opts);
282 EXPECT_EQ(out.str(), "1;2;3\n4;5;6\n");
283
284 std::istringstream in(out.str());
285 const Grid_Snapshot<int> snap = read_csv_snapshot<int>(in, opts);
286 const Grid dst = make_lattice_from_snapshot<Grid>(snap);
287 expect_same_frame(src, dst);
288}
289
290TEST(CAIO, CsvReaderRejectsRaggedRowsAndBadScalars)
291{
292 {
293 std::istringstream in("1,2\n3\n");
294 EXPECT_THROW(read_csv_snapshot<int>(in), std::domain_error);
295 }
296 {
297 std::istringstream in("1,x\n");
298 EXPECT_THROW(read_csv_snapshot<int>(in), std::domain_error);
299 }
300}
301
302TEST(CAIO, JsonRoundTripPreservesNumericSnapshot)
303{
304 const Grid src = make_values_grid();
306 opts.pretty = true;
307 opts.rule = "demo";
308
309 std::ostringstream out;
310 write_json(out, src, opts);
311 EXPECT_NE(out.str().find("\"type\": \"Aleph::CA::frame\""), std::string::npos);
312 EXPECT_NE(out.str().find("\"rule\": \"demo\""), std::string::npos);
313
314 std::istringstream in(out.str());
315 const Grid_Snapshot<int> snap = read_json_snapshot<int>(in);
316 const Grid dst = make_lattice_from_snapshot<Grid>(snap);
317 expect_same_frame(src, dst);
318}
319
320TEST(CAIO, CsvAndJsonWriteByteStatesAsNumbers)
321{
322 ByteGrid frame({1, 3}, std::uint8_t{0});
323 frame.set({0, 0}, std::uint8_t{7});
324 frame.set({0, 1}, std::uint8_t{42});
325 frame.set({0, 2}, std::uint8_t{255});
326
327 std::ostringstream csv;
328 write_csv(csv, frame);
329 EXPECT_EQ(csv.str(), "7,42,255\n");
330
331 std::ostringstream json;
332 write_json(json, frame);
333 EXPECT_NE(json.str().find("\"cells\":[[7,42,255]]"), std::string::npos);
334}
335
336TEST(CAIO, JsonReaderRejectsShapeMismatch)
337{
338 std::istringstream in(
339 "{\"type\":\"Aleph::CA::frame\",\"rank\":2,\"height\":1,\"width\":2,"
340 "\"cells\":[[1,2],[3,4]]}");
341 EXPECT_THROW(read_json_snapshot<int>(in), std::domain_error);
342}
343
344TEST(CAIO, JsonFrameStreamWriterEmitsStepTaggedFrames)
345{
346 const Grid frame = make_grid(2, 2, {{0, 0}, {1, 1}});
347 std::ostringstream out;
348 {
350 stream.accept(0, frame);
351 stream.accept(4, frame);
352 stream.close();
353 }
354 EXPECT_NE(out.str().find("\"type\": \"Aleph::CA::frame-stream\""), std::string::npos);
355 EXPECT_NE(out.str().find("\"step\": 0"), std::string::npos);
356 EXPECT_NE(out.str().find("\"step\": 4"), std::string::npos);
357 EXPECT_NE(out.str().find("\"cells\":[[1,0],[0,1]]"), std::string::npos);
358}
359
360TEST(CAIO, PgmBinaryOutputIsBitStable)
361{
362 const Grid frame = make_grid(2, 3, {{0, 1}, {1, 2}});
363 std::ostringstream out(std::ios::binary);
364 write_pgm(out, frame);
365
366 std::string expected = "P5\n3 2\n255\n";
367 expected += bytes({255, 0, 255, 255, 255, 0});
368 EXPECT_EQ(out.str(), expected);
369}
370
371TEST(CAIO, PpmBinaryOutputUsesCustomPalette)
372{
373 const Grid frame = make_grid(1, 2, {{0, 1}});
374 std::ostringstream out(std::ios::binary);
375 write_ppm(out, frame, [](int v)
376 {
377 return v == 0 ? RGB8{1, 2, 3} : RGB8{250, 0, 7};
378 });
379
380 std::string expected = "P6\n2 1\n255\n";
381 expected += bytes({1, 2, 3, 250, 0, 7});
382 EXPECT_EQ(out.str(), expected);
383}
384
385// Coordinates that fit in ca_index_t one by one may not fit once they are
386// added up or subtracted. The readers computed them with the overflow of
387// C++, undefined behaviour; now they reject such patterns with
388// std::overflow_error, and the non-throwing readers report it.
389TEST(CAIO, Life106RejectsAnExtentThatDoesNotFit)
390{
391 // max - min is 2^63, one more than the largest ca_index_t
392 std::istringstream too_wide("#Life 1.06\n-4611686018427387904 0\n4611686018427387904 0\n");
393 EXPECT_THROW((void) read_life_106(too_wide), std::overflow_error);
394
395 std::istringstream too_tall("#Life 1.06\n0 -4611686018427387904\n0 4611686018427387904\n");
396 EXPECT_THROW((void) read_life_106(too_tall), std::overflow_error);
397
398 std::istringstream again("#Life 1.06\n-4611686018427387904 0\n4611686018427387904 0\n");
399 const auto attempt = try_read_life_106(again);
400 ASSERT_FALSE(attempt.has_value());
401 EXPECT_NE(attempt.error().find("extent"), std::string::npos);
402
403 // the widest pattern: its columns go from 0 to the largest ca_index_t
404 std::istringstream widest("#Life 1.06\n-4611686018427387904 0\n4611686018427387903 0\n");
405 const Binary_Cell_Pattern pattern = read_life_106(widest);
406 EXPECT_EQ(pattern.width,
407 static_cast<ca_size_t>(std::numeric_limits<ca_index_t>::max()) + 1);
408 EXPECT_EQ(pattern.height, 1u);
409 EXPECT_EQ(pattern.origin_col, -4611686018427387904LL);
410 ASSERT_EQ(pattern.alive.size(), 2u);
411 const std::set<std::pair<ca_index_t, ca_index_t>> cells = {
412 {pattern.alive(0)[0], pattern.alive(0)[1]}, {pattern.alive(1)[0], pattern.alive(1)[1]}};
413 const std::set<std::pair<ca_index_t, ca_index_t>> expected = {
414 {0, 0}, {0, std::numeric_limits<ca_index_t>::max()}};
415 EXPECT_EQ(cells, expected);
416}
417
418TEST(CAIO, Life105RejectsCellsBeyondTheLargestCoordinate)
419{
420 // the block starts at the largest column, so its second cell overflows
421 std::istringstream past_end("#Life 1.05\n#P 9223372036854775807 0\n**\n");
422 EXPECT_THROW((void) read_life_105(past_end), std::overflow_error);
423
424 std::istringstream past_bottom("#Life 1.05\n#P 0 9223372036854775807\n*\n*\n");
425 EXPECT_THROW((void) read_life_105(past_bottom), std::overflow_error);
426
427 std::istringstream again("#Life 1.05\n#P 9223372036854775807 0\n**\n");
428 EXPECT_FALSE(try_read_life_105(again).has_value());
429
430 // the last cells that fit
431 std::istringstream at_end("#Life 1.05\n#P 9223372036854775806 0\n**\n");
432 const Binary_Cell_Pattern pattern = read_life_105(at_end);
433 EXPECT_EQ(pattern.origin_col, std::numeric_limits<ca_index_t>::max() - 1);
434 EXPECT_EQ(pattern.width, 2u);
435 EXPECT_EQ(pattern.alive.size(), 2u);
436
437 // two blocks far apart: each cell fits, the extent does not
438 std::istringstream far_apart("#Life 1.05\n#P -9223372036854775807 0\n*\n#P 1 0\n*\n");
439 EXPECT_THROW((void) read_life_105(far_apart), std::overflow_error);
440
441 // negative blocks that fit
442 std::istringstream negative("#Life 1.05\n#P -9223372036854775807 -5\n*.*\n");
443 const Binary_Cell_Pattern left = read_life_105(negative);
444 EXPECT_EQ(left.origin_col, -std::numeric_limits<ca_index_t>::max());
445 EXPECT_EQ(left.origin_row, -5);
446 EXPECT_EQ(left.width, 3u);
447}
448
449// A run long enough to wrap the unsigned sum of the size checks went back
450// to a previous column or row, and stored the same cell twice.
451TEST(CAIO, RleRunsThatWouldWrapAreRejected)
452{
453 EXPECT_THROW((void) read_rle_string("x = 5, y = 1\n3o18446744073709551615b2o!"),
454 std::domain_error);
455 EXPECT_THROW((void) read_rle_string("x = 5, y = 1\n3o18446744073709551615o!"),
456 std::domain_error);
457 EXPECT_THROW((void) read_rle_string("x = 1, y = 2\no$18446744073709551615$o!"),
458 std::domain_error);
459
460 // a size whose cells would not have ca_index_t coordinates
461 EXPECT_THROW((void) read_rle_string("x = 9223372036854775809, y = 1\n!"),
462 std::overflow_error);
463 EXPECT_THROW((void) read_rle_string("x = 1, y = 9223372036854775809\n!"),
464 std::overflow_error);
465
466 // the largest size: its last cell is at the largest ca_index_t
467 const RLE_Pattern largest
468 = read_rle_string("x = 9223372036854775808, y = 9223372036854775808\n"
469 "9223372036854775807bo9223372036854775807$9223372036854775807bo!");
470 EXPECT_EQ(largest.width, static_cast<ca_size_t>(std::numeric_limits<ca_index_t>::max()) + 1);
471 EXPECT_EQ(largest.height, largest.width);
472 ASSERT_EQ(largest.alive.size(), 2u);
473 EXPECT_EQ(largest.alive(0)[0], 0);
474 EXPECT_EQ(largest.alive(0)[1], std::numeric_limits<ca_index_t>::max());
475 EXPECT_EQ(largest.alive(1)[0], std::numeric_limits<ca_index_t>::max());
476 EXPECT_EQ(largest.alive(1)[1], std::numeric_limits<ca_index_t>::max());
477}
478
479// The fit check added the origin to the size, and a huge size wrapped it.
480TEST(CAIO, LoadBinaryPatternRejectsSizesThatWouldWrap)
481{
482 Binary_Cell_Pattern pattern;
483 pattern.height = std::numeric_limits<ca_size_t>::max();
484 pattern.width = 1;
485
486 Grid grid({4, 4}, 0);
487 EXPECT_THROW(load_binary_pattern(pattern, grid, 1, 0, 1, 0), std::domain_error);
488
489 pattern.height = 1;
490 pattern.width = std::numeric_limits<ca_size_t>::max();
491 EXPECT_THROW(load_binary_pattern(pattern, grid, 1, 0, 0, 1), std::domain_error);
492
493 pattern.width = 4;
494 pattern.alive.append(Coord_Vec<2>{0, 3});
495 load_binary_pattern(pattern, grid, 1, 0, 3, 0);
496 EXPECT_EQ(grid.at({3, 3}), 1);
497 EXPECT_THROW(load_binary_pattern(pattern, grid, 1, 0, 4, 0), std::domain_error);
498}
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
File-format readers and writers for cellular-automata frames.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Streaming JSON writer for CA trajectories.
Definition ca-io.H:1615
void close()
Finish the stream.
Definition ca-io.H:1691
void accept(std::size_t step, const Lattice &frame)
Append one frame to the stream.
Definition ca-io.H:1669
Lattice that adds boundary-aware access on top of a storage.
Minimal std::expected-style result type for C++20.
#define TEST(name)
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
Definition ca-io.H:1841
void write_pgm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PGM (P5) image.
Definition ca-io.H:1513
void write_life_105(std::ostream &out, const Lattice &frame, const std::string &comment={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a Life 1.05 file.
Definition ca-io.H:1134
Binary_Cell_Pattern read_plaintext_string(const std::string &s)
Read a Life plaintext pattern from a string.
Definition ca-io.H:1020
void write_life_106(std::ostream &out, const Lattice &frame, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a Life 1.06 file.
Definition ca-io.H:1040
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
expected< RLE_Pattern, std::string > try_read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Non-throwing variant of read_rle_string.
Definition ca-io.H:1774
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
std::string write_plaintext_string(const Lattice &frame, const Plaintext_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Return a Life plaintext document.
Definition ca-io.H:954
void write_ppm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PPM (P6) image.
Definition ca-io.H:1565
Binary_Cell_Pattern read_life_105(std::istream &in)
Read a Life 1.05 pattern.
Definition ca-io.H:1170
void write_csv(std::ostream &out, const Lattice &frame, const CSV_Options &opts={})
Write a numeric CSV snapshot.
Definition ca-io.H:1276
std::string write_rle_string(const Lattice &frame, const RLE_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Return a frame encoded as Conway RLE.
Definition ca-io.H:691
Binary_Cell_Pattern read_life_106(std::istream &in)
Read a Life 1.06 pattern.
Definition ca-io.H:1068
void write_json(std::ostream &out, const Lattice &frame, const JSON_Write_Options &opts={})
Write a JSON snapshot.
Definition ca-io.H:1383
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
RLE_Pattern read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Read a Conway RLE pattern from a string.
Definition ca-io.H:839
void load_binary_pattern(const Binary_Cell_Pattern &pattern, Lattice &frame, const typename Lattice::state_type &alive=static_cast< typename Lattice::state_type >(1), const typename Lattice::state_type &dead=typename Lattice::state_type{}, const ca_index_t origin_row=0, const ca_index_t origin_col=0)
Fill a lattice from a binary pattern.
Definition ca-io.H:864
expected< Binary_Cell_Pattern, std::string > try_read_life_105(std::istream &in)
Non-throwing variant of read_life_105.
Definition ca-io.H:1819
Binary pattern shared by RLE, plaintext and Life formats.
Definition ca-io.H:104
std::string name
optional pattern name
Definition ca-io.H:110
ca_index_t origin_row
original row offset before normalisation
Definition ca-io.H:107
ca_size_t height
number of rows
Definition ca-io.H:106
ca_size_t width
number of columns
Definition ca-io.H:105
Array< std::string > comments
format comments without marker prefixes
Definition ca-io.H:111
ca_index_t origin_col
original column offset before normalisation
Definition ca-io.H:108
std::string rule
optional rule string, e.g. B3/S23
Definition ca-io.H:109
Array< Coord_Vec< 2 > > alive
live cells as {row, col}
Definition ca-io.H:112
Options controlling CSV output and input.
Definition ca-io.H:185
char delimiter
separator between cells
Definition ca-io.H:186
Dense numeric snapshot used by CSV and JSON readers.
Definition ca-io.H:128
Options controlling JSON snapshot output.
Definition ca-io.H:196
bool pretty
pretty-print with indentation
Definition ca-io.H:197
std::string rule
optional rule field
Definition ca-io.H:198
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
Options controlling Life plaintext output.
Definition ca-io.H:173
std::string comment
optional ! comments, split on newlines
Definition ca-io.H:176
RGB byte triplet used by PPM exporters.
Definition ca-io.H:85
Options controlling Conway RLE output.
Definition ca-io.H:159
bool trim_dead_border
crop to the live-cell bounding box
Definition ca-io.H:163
std::string name
optional #N line
Definition ca-io.H:161
Gsl_Compatible_Rng r
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).