Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_io_expected_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
44#include <cstddef>
45#include <iostream>
46#include <sstream>
47#include <string>
48
49#include <ca-io.H>
50
51using namespace Aleph;
52using namespace Aleph::CA;
53
54namespace
55{
56
57void load_rle(const std::string &label, const std::string &text)
58{
59 // No try/catch: the error is a value we inspect.
60 auto result = try_read_rle_string(text);
61 std::cout << " " << label << ": ";
62 if (result)
63 std::cout << "ok — " << result->width << "x" << result->height << " grid, "
64 << result->alive.size() << " live cell(s)\n";
65 else
66 std::cout << "rejected — " << result.error() << '\n';
67}
68
69} // namespace
70
71int main()
72{
73 std::cout << "Parsing RLE patterns with try_read_rle_string (no exceptions):\n";
74
75 load_rle("glider", "x = 3, y = 3, rule = B3/S23\nbob$2bo$3o!\n");
76 load_rle("blinker", "x = 3, y = 1, rule = B3/S23\n3o!\n");
77 load_rle("garbage", "this is not a pattern @@@");
78 load_rle("truncated", "x = 5, y = 5, rule = B3/S23\n");
79
80 // A pipeline that loads from one of several formats, recovering on failure.
81 std::cout << "\nFalling back across formats with or_else:\n";
82 const std::string maybe_life = "#Life 1.06\n0 0\n1 0\n2 0\n";
83
84 std::istringstream rle_in(maybe_life);
85 auto cells =
86 try_read_rle(rle_in) // try RLE first...
87 .transform([](const RLE_Pattern &p) { return p.alive.size(); })
88 .or_else(
89 [&](const std::string &) -> expected<std::size_t, std::string>
90 {
91 // ...not RLE; fall back to Life 1.06.
92 std::istringstream life_in(maybe_life);
93 return try_read_life_106(life_in).transform(
94 [](const Binary_Cell_Pattern &p) { return p.alive.size(); });
95 });
96
97 if (cells)
98 std::cout << " parsed " << *cells << " live cell(s) after fallback\n";
99 else
100 std::cout << " every format failed: " << cells.error() << '\n';
101
102 return 0;
103}
File-format readers and writers for cellular-automata frames.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
Minimal std::expected-style result type for C++20.
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
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
Definition ca-io.H:1768
expected< RLE_Pattern, std::string > try_read_rle_string(const std::string &s)
Non-throwing variant of read_rle_string.
Definition ca-io.H:1702
expected< RLE_Pattern, std::string > try_read_rle(std::istream &in)
Non-throwing variant of read_rle.
Definition ca-io.H:1680
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Binary pattern shared by RLE, plaintext and Life formats.
Definition ca-io.H:104
Array< Coord_Vec< 2 > > alive
live cells as {row, col}
Definition ca-io.H:112