145 <<
"Grid_Snapshot::at: coordinate (" <<
row <<
", " <<
col <<
") out of " <<
height <<
"x"
219template <
typename State>
243template <
typename State>
260namespace ca_io_detail {
262template <
typename Lattice>
266 static_assert(
Lattice::rank == 2,
"CA IO currently supports 2D frames");
273 while (b < s.size()
and std::isspace(
static_cast<unsigned char>(s[b])))
275 std::size_t e = s.size();
276 while (e > b
and std::isspace(
static_cast<unsigned char>(s[e - 1])))
278 return s.substr(b, e - b);
284 c =
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
288inline bool blank(
const std::string &s)
290 for (
const char c : s)
291 if (not std::isspace(
static_cast<unsigned char>(c)))
301 for (
const char c : t)
304 << what <<
": invalid unsigned size '" << s <<
"'";
307 << what <<
": size overflows ca_size_t";
319 if (t[i] ==
'-' or t[i] ==
'+')
326 for (; i < t.size(); ++i)
330 << what <<
": invalid coordinate '" << s <<
"'";
333 << what <<
": coordinate overflows ca_index_t";
350 (b < 0
and a < std::numeric_limits<ca_index_t>::min() - b))
351 << what <<
": coordinate overflows ca_index_t";
358 std::istringstream in(comment);
360 while (std::getline(in, line))
369 std::ostringstream
out;
370 for (
const char c : s)
390 if (
static_cast<unsigned char>(c) < 0x20)
391 out <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
392 <<
static_cast<unsigned>(
static_cast<unsigned char>(c)) << std::dec
393 << std::setfill(
' ');
408 token = std::to_string(n);
409 token.push_back(glyph);
419 const std::string &body,
420 const std::size_t line_width)
424 const std::size_t width = line_width == 0 ? std::numeric_limits<std::size_t>::max() : line_width;
425 while (pos < body.size())
427 const std::size_t start = pos;
428 while (pos < body.size()
and std::isdigit(
static_cast<unsigned char>(body[pos])))
432 const std::size_t len = pos - start;
438 out.write(body.data() +
static_cast<std::ptrdiff_t
>(start),
static_cast<std::streamsize
>(len));
440 if (body[pos - 1] ==
'!')
450inline std::string
header_value(
const std::string &line,
const std::string &key)
452 std::istringstream parts(line);
454 while (std::getline(parts, part,
','))
456 const auto eq = part.find(
'=');
457 if (
eq == std::string::npos)
468 const bool have_extent,
489 << what <<
": pattern extent needs coordinates beyond ca_index_t";
493 pattern.
height = row_span + 1;
494 pattern.
width = col_span + 1;
497 for (
const auto &c : absolute)
511 << format <<
": trailing characters in scalar '" << token <<
"'";
518 while (pos < s.size()
and std::isspace(
static_cast<unsigned char>(s[pos])))
520 const std::size_t start = pos;
521 if (pos < s.size()
and (s[pos] ==
'-' or s[pos] ==
'+'))
523 while (pos < s.size()
and std::isdigit(
static_cast<unsigned char>(s[pos])))
525 if (pos < s.size()
and s[pos] ==
'.')
528 while (pos < s.size()
and std::isdigit(
static_cast<unsigned char>(s[pos])))
531 if (pos < s.size()
and (s[pos] ==
'e' or s[pos] ==
'E'))
534 if (pos < s.size()
and (s[pos] ==
'-' or s[pos] ==
'+'))
536 while (pos < s.size()
and std::isdigit(
static_cast<unsigned char>(s[pos])))
540 return parse_scalar<T>(s.substr(start, pos - start),
"JSON snapshot");
545 const std::string needle =
"\"" + key +
"\"";
546 const std::size_t key_pos = s.find(needle);
547 ah_domain_error_if(key_pos == std::string::npos) <<
"JSON snapshot: missing field '" << key <<
"'";
548 std::size_t colon = s.find(
':', key_pos + needle.size());
550 <<
"JSON snapshot: missing ':' after '" << key <<
"'";
552 while (colon < s.size()
and std::isspace(
static_cast<unsigned char>(s[colon])))
554 const std::size_t start = colon;
555 while (colon < s.size()
and std::isdigit(
static_cast<unsigned char>(s[colon])))
558 <<
"JSON snapshot: field '" << key <<
"' is not an unsigned integer";
570 if constexpr (std::is_integral_v<T>
and sizeof(
T) == 1
and not std::is_same_v<T, bool>)
571 out << static_cast<int>(
value);
597template <
typename Lattice>
601 const typename Lattice::state_type &dead =
typename Lattice::state_type{})
603 ca_io_detail::require_rank2<Lattice>(
"write_rle");
604 using coord_t =
typename Lattice::coord_type;
606 for (
const auto &line : ca_io_detail::split_comment_lines(opts.name))
607 out <<
"#N " << line <<
'\n';
608 for (
const auto &line : ca_io_detail::split_comment_lines(opts.comment))
609 out <<
"#C " << line <<
'\n';
611 const ca_size_t
rows = frame.
size(0);
612 const ca_size_t
cols = frame.
size(1);
613 ca_size_t r0 = 0, c0 = 0, r1 =
rows, c1 =
cols;
615 if (opts.trim_dead_border)
618 ca_size_t min_r =
rows, min_c =
cols, max_r = 0, max_c = 0;
619 for (ca_size_t
r = 0;
r <
rows; ++
r)
620 for (ca_size_t c = 0; c <
cols; ++c)
621 if (frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}) != dead)
624 min_r = std::min(min_r,
r);
625 min_c = std::min(min_c, c);
626 max_r = std::max(max_r,
r);
627 max_c = std::max(max_c, c);
638 r0 = c0 = r1 = c1 = 0;
644 out <<
"x = " << width <<
", y = " << height <<
", rule = " << opts.rule <<
'\n';
647 if (height == 0 or width == 0)
653 for (ca_size_t
r = r0;
r < r1; ++
r)
658 for (ca_size_t c = c0; c < c1; ++c)
662 if (alive == run_alive)
666 ca_io_detail::append_rle_token(body, run, run_alive ?
'o' :
'b');
671 ca_io_detail::append_rle_token(body, run, run_alive ?
'o' :
'b');
673 ca_io_detail::append_rle_token(body, 1,
'$');
678 ca_io_detail::write_wrapped_rle_body(
out, body, opts.line_width);
679 ca_io_detail::ensure_stream_ok(
out,
"write_rle");
690template <
typename Lattice>
694 const typename Lattice::state_type &dead =
typename Lattice::state_type{})
696 std::ostringstream
out;
697 write_rle(
out, frame, opts, dead);
732 const ca_size_t max_live_cells = Rle_Default_Max_Live_Cells)
736 bool header_seen =
false;
742 while (not ended
and std::getline(in, line))
748 if (line.size() >= 2
and (line[1] ==
'N' or line[1] ==
'n'))
749 pattern.
name = ca_io_detail::trim_copy(line.substr(2));
750 else if (line.size() >= 2
and (line[1] ==
'C' or line[1] ==
'c'))
757 const std::string x = ca_io_detail::header_value(line,
"x");
758 const std::string
y = ca_io_detail::header_value(line,
"y");
760 <<
"read_rle: missing x/y header in line '" << line <<
"'";
761 pattern.
width = ca_io_detail::parse_size_token(x,
"read_rle x");
762 pattern.
height = ca_io_detail::parse_size_token(
y,
"read_rle y");
764 pattern.
height > ca_io_detail::Max_Pattern_Extent)
765 <<
"read_rle: pattern size needs coordinates beyond ca_index_t";
766 pattern.
rule = ca_io_detail::header_value(line,
"rule");
771 for (
const char ch : line)
773 if (std::isdigit(
static_cast<unsigned char>(ch)))
777 <<
"read_rle: run length overflows ca_size_t";
778 run = run * 10 + digit;
780 else if (ch ==
'b' or ch ==
'.')
785 <<
"read_rle: dead run exceeds row width";
789 else if (ch ==
'o' or ch ==
'A')
793 <<
"read_rle: live run exceeds declared dimensions";
795 <<
"read_rle: more than " << max_live_cells <<
" live cells";
798 static_cast<ca_index_t>(cur_col + k)});
806 <<
"read_rle: row advance exceeds declared height";
820 <<
"read_rle: invalid body character '" << ch <<
"'";
840 const std::string &s,
const ca_size_t max_live_cells = Rle_Default_Max_Live_Cells)
842 std::istringstream in(s);
843 return read_rle(in, max_live_cells);
863template <
typename Lattice>
869 const ca_index_t origin_row = 0,
870 const ca_index_t origin_col = 0)
872 ca_io_detail::require_rank2<Lattice>(
"load_binary_pattern");
873 using coord_t =
typename Lattice::coord_type;
875 <<
"load_binary_pattern: negative destination origin";
877 static_cast<ca_size_t
>(origin_row) > frame.
size(0) - pattern.
height or
879 static_cast<ca_size_t
>(origin_col) > frame.
size(1) - pattern.
width)
880 <<
"load_binary_pattern: pattern " << pattern.
height <<
"x" << pattern.
width
881 <<
" does not fit in frame " << frame.
size(0) <<
"x" << frame.
size(1);
884 for (
const auto &
cell : pattern.alive)
885 frame.set(coord_t{origin_row +
cell[0], origin_col +
cell[1]}, alive);
899template <
typename Lattice>
905 ca_io_detail::require_rank2<Lattice>(
"make_lattice_from_pattern");
906 Lattice frame({pattern.
height, pattern.
width}, dead);
907 load_binary_pattern(pattern, frame, alive, dead);
923template <
typename Lattice>
927 const typename Lattice::state_type &dead =
typename Lattice::state_type{})
929 ca_io_detail::require_rank2<Lattice>(
"write_plaintext");
930 using coord_t =
typename Lattice::coord_type;
931 for (
const auto &line : ca_io_detail::split_comment_lines(opts.comment))
932 out <<
'!' << line <<
'\n';
934 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
936 for (ca_size_t c = 0; c < frame.
size(1); ++c)
937 out << (frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}) == dead
942 ca_io_detail::ensure_stream_ok(
out,
"write_plaintext");
953template <
typename Lattice>
957 const typename Lattice::state_type &dead =
typename Lattice::state_type{})
959 std::ostringstream
out;
960 write_plaintext(
out, frame, opts, dead);
981 while (std::getline(in, line))
994 for (
const char c : line)
995 if (c ==
'O' or c ==
'o' or c ==
'*' or c ==
'.')
1000 <<
"read_plaintext: invalid character '" << c <<
"'";
1022 std::istringstream in(s);
1039template <
typename Lattice>
1044 ca_io_detail::require_rank2<Lattice>(
"write_life_106");
1045 using coord_t =
typename Lattice::coord_type;
1046 out <<
"#Life 1.06\n";
1047 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1048 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1049 if (frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}) != dead)
1050 out << c <<
' ' <<
r <<
'\n';
1051 ca_io_detail::ensure_stream_ok(
out,
"write_life_106");
1072 bool header_seen =
false;
1074 ca_index_t min_r = 0, min_c = 0, max_r = 0, max_c = 0;
1077 while (std::getline(in, line))
1079 const std::string t = ca_io_detail::trim_copy(line);
1084 if (t ==
"#Life 1.06")
1086 else if (t.size() >= 2
and (t[1] ==
'D' or t[1] ==
'd'))
1090 std::istringstream ls(t);
1091 std::string xs, ys, extra;
1092 ls >> xs >> ys >> extra;
1093 ah_domain_error_if(not extra.empty()) <<
"read_life_106: too many fields in '" << line <<
"'";
1094 const ca_index_t col = ca_io_detail::parse_index_token(xs,
"read_life_106 x");
1095 const ca_index_t row = ca_io_detail::parse_index_token(ys,
"read_life_106 y");
1099 min_r = max_r =
row;
1100 min_c = max_c =
col;
1105 min_r = std::min(min_r,
row);
1106 min_c = std::min(min_c,
col);
1107 max_r = std::max(max_r,
row);
1108 max_c = std::max(max_c,
col);
1113 ca_io_detail::finalise_absolute_pattern(pattern, absolute, have, min_r, min_c, max_r, max_c,
1133template <
typename Lattice>
1136 const std::string &comment = {},
1137 const typename Lattice::state_type &dead =
typename Lattice::state_type{})
1139 ca_io_detail::require_rank2<Lattice>(
"write_life_105");
1140 using coord_t =
typename Lattice::coord_type;
1141 out <<
"#Life 1.05\n";
1142 for (
const auto &line : ca_io_detail::split_comment_lines(comment))
1143 out <<
"#D " << line <<
'\n';
1145 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1147 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1148 out << (frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}) == dead
1153 ca_io_detail::ensure_stream_ok(
out,
"write_life_105");
1175 bool header_seen =
false;
1176 bool in_block =
false;
1177 ca_index_t block_col = 0, block_row = 0, local_row = 0;
1178 ca_index_t min_r = 0, min_c = 0, max_r = 0, max_c = 0;
1179 bool have_extent =
false;
1183 if (not have_extent)
1185 min_r = max_r =
row;
1186 min_c = max_c =
col;
1191 min_r = std::min(min_r,
row);
1192 min_c = std::min(min_c,
col);
1193 max_r = std::max(max_r,
row);
1194 max_c = std::max(max_c,
col);
1198 while (std::getline(in, line))
1200 const std::string t = ca_io_detail::trim_copy(line);
1205 if (t ==
"#Life 1.05")
1210 if (t.size() >= 2
and (t[1] ==
'D' or t[1] ==
'd'))
1215 if (t.size() >= 2
and (t[1] ==
'P' or t[1] ==
'p'))
1217 std::istringstream ps(t.substr(2));
1218 std::string xs, ys, extra;
1219 ps >> xs >> ys >> extra;
1221 <<
"read_life_105: malformed #P line '" << line <<
"'";
1222 block_col = ca_io_detail::parse_index_token(xs,
"read_life_105 #P x");
1223 block_row = ca_io_detail::parse_index_token(ys,
"read_life_105 #P y");
1233 for (
const char c : line)
1235 if (c ==
'*' or c ==
'O' or c ==
'o' or c ==
'.')
1237 const ca_index_t row = ca_io_detail::checked_coord_add(block_row, local_row,
1239 const ca_index_t col = ca_io_detail::checked_coord_add(block_col, local_col,
1242 if (c ==
'*' or c ==
'O' or c ==
'o')
1249 <<
"read_life_105: invalid row character '" << c <<
"'";
1256 ca_io_detail::finalise_absolute_pattern(pattern, absolute, have_extent, min_r, min_c, max_r, max_c,
1275template <
typename Lattice>
1278 ca_io_detail::require_rank2<Lattice>(
"write_csv");
1279 using coord_t =
typename Lattice::coord_type;
1280 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1282 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1285 out << opts.delimiter;
1286 ca_io_detail::write_text_scalar(
1287 out, frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}));
1291 ca_io_detail::ensure_stream_ok(
out,
"write_csv");
1310template <
typename T>
1314 Grid_Snapshot<T> snap;
1317 while (std::getline(in, line))
1319 if (opts.skip_empty_lines
and ca_io_detail::blank(line))
1323 std::istringstream ls(line);
1324 while (std::getline(ls, token, opts.delimiter))
1325 values.
append(ca_io_detail::parse_scalar<T>(token,
"CSV snapshot"));
1326 if (line.empty()
and not opts.skip_empty_lines)
1329 snap.width = values.
size();
1331 <<
"read_csv_snapshot: row " <<
row <<
" has width " << values.
size() <<
" instead of "
1333 for (
const T &v : values)
1334 snap.cells.append(v);
1351template <
typename Lattice,
typename T>
1354 ca_io_detail::require_rank2<Lattice>(
"make_lattice_from_snapshot");
1360 frame.
set(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)},
1361 static_cast<state_t
>(snap.
at(
r, c)));
1382template <
typename Lattice>
1385 ca_io_detail::require_rank2<Lattice>(
"write_json");
1386 using coord_t =
typename Lattice::coord_type;
1388 const auto nl = opts.pretty ?
"\n" :
"";
1389 const auto i1 = opts.pretty ?
" " :
"";
1390 const auto i2 = opts.pretty ?
" " :
"";
1391 const auto sp = opts.pretty ?
" " :
"";
1394 out << i1 <<
"\"type\":" << sp <<
"\"Aleph::CA::frame\"," << nl;
1395 out << i1 <<
"\"rank\":" << sp <<
"2," << nl;
1396 out << i1 <<
"\"height\":" << sp << frame.
size(0) <<
',' << nl;
1397 out << i1 <<
"\"width\":" << sp << frame.
size(1);
1398 if (not opts.rule.empty())
1399 out <<
',' << nl << i1 <<
"\"rule\":" << sp <<
'"' << ca_io_detail::json_escape(opts.rule) <<
'"';
1400 out <<
',' << nl << i1 <<
"\"cells\":" << sp <<
'[' << nl;
1401 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1404 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1410 ca_io_detail::write_text_scalar(
1411 out, frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}));
1413 if (opts.pretty
and frame.
size(1) != 0)
1416 if (
r + 1 < frame.
size(0))
1420 out << i1 <<
']' << nl <<
"}" << nl;
1421 ca_io_detail::ensure_stream_ok(
out,
"write_json");
1438template <
typename T>
1441 std::ostringstream ss;
1443 const std::string s = ss.str();
1445 snap.
height = ca_io_detail::parse_json_size_field(s,
"height");
1446 snap.
width = ca_io_detail::parse_json_size_field(s,
"width");
1448 const std::size_t key = s.find(
"\"cells\"");
1449 ah_domain_error_if(key == std::string::npos) <<
"JSON snapshot: missing cells field";
1450 std::size_t pos = s.find(
'[', key);
1451 ah_domain_error_if(pos == std::string::npos) <<
"JSON snapshot: missing cells array";
1457 while (pos < s.size()
and (std::isspace(
static_cast<unsigned char>(s[pos])) or s[pos] ==
','))
1470 while (pos < s.size()
and std::isspace(
static_cast<unsigned char>(s[pos])))
1483 snap.
cells.append(ca_io_detail::parse_json_number<T>(s, pos));
1487 <<
"JSON snapshot: row " <<
rows <<
" has width " <<
cols <<
" instead of " << snap.
width;
1492 <<
"JSON snapshot: got " <<
rows <<
" rows instead of " << snap.
height;
1512template <
typename Lattice,
typename Mapper>
1517 requires requires(Mapper &&
m,
const typename Lattice::state_type &v) {
1518 {
m(v) } -> std::convertible_to<std::uint8_t>;
1521 ca_io_detail::require_rank2<Lattice>(
"write_pgm");
1522 using coord_t =
typename Lattice::coord_type;
1524 <<
"write_pgm: only 8-bit max_value in [1,255] is supported";
1525 out <<
"P5\n" << frame.
size(1) <<
' ' << frame.
size(0) <<
'\n' << opts.max_value <<
'\n';
1526 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1527 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1529 const std::uint8_t v =
static_cast<std::uint8_t
>(
1530 mapper(frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)})));
1531 out.write(
reinterpret_cast<const char *
>(&v), 1);
1533 ca_io_detail::ensure_stream_ok(
out,
"write_pgm");
1543template <
typename Lattice>
1546 write_pgm(
out, frame, Binary_Gray_Mapper<typename Lattice::state_type>{}, opts);
1564template <
typename Lattice,
typename Mapper>
1569 requires requires(Mapper &&
m,
const typename Lattice::state_type &v) {
1570 {
m(v) } -> std::convertible_to<RGB8>;
1573 ca_io_detail::require_rank2<Lattice>(
"write_ppm");
1574 using coord_t =
typename Lattice::coord_type;
1576 <<
"write_ppm: only 8-bit max_value in [1,255] is supported";
1577 out <<
"P6\n" << frame.
size(1) <<
' ' << frame.
size(0) <<
'\n' << opts.max_value <<
'\n';
1578 for (ca_size_t
r = 0;
r < frame.
size(0); ++
r)
1579 for (ca_size_t c = 0; c < frame.
size(1); ++c)
1582 mapper(frame.
at(coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}));
1583 const char bytes[3] = {
static_cast<char>(rgb.r),
static_cast<char>(rgb.g),
1584 static_cast<char>(rgb.b)};
1585 out.write(bytes, 3);
1587 ca_io_detail::ensure_stream_ok(
out,
"write_ppm");
1597template <
typename Lattice>
1600 write_ppm(
out, frame, Binary_RGB_Mapper<typename Lattice::state_type>{}, opts);
1616 std::ostream *out_ =
nullptr;
1618 bool closed_ =
false;
1631 : out_(&
out), opts_(
std::move(opts))
1633 *out_ <<
"{\n \"type\": \"Aleph::CA::frame-stream\",\n \"frames\": [\n";
1634 ca_io_detail::ensure_stream_ok(*out_,
"JSON_Frame_Stream_Writer");
1647 if (out_ !=
nullptr and not closed_)
1668 template <
typename Lattice>
1671 ca_io_detail::require_rank2<Lattice>(
"JSON_Frame_Stream_Writer::accept");
1672 ah_runtime_error_if(closed_) <<
"JSON_Frame_Stream_Writer::accept: stream already closed";
1676 *out_ <<
" {\n \"step\": " << step <<
",\n \"frame\": ";
1681 ca_io_detail::ensure_stream_ok(*out_,
"JSON_Frame_Stream_Writer::accept");
1695 *out_ <<
"\n ]\n}\n";
1697 ca_io_detail::ensure_stream_ok(*out_,
"JSON_Frame_Stream_Writer::close");
1710namespace ca_io_detail {
1723 catch (
const std::exception &e)
1751 std::istream &in,
const ca_size_t max_live_cells = Rle_Default_Max_Live_Cells)
1753 return ca_io_detail::try_parse([&]
1755 return read_rle(in, max_live_cells);
1775 const std::string &s,
const ca_size_t max_live_cells = Rle_Default_Max_Live_Cells)
1777 return ca_io_detail::try_parse([&]
1780 },
"try_read_rle_string");
1799 return ca_io_detail::try_parse([&]
1802 },
"try_read_plaintext");
1821 return ca_io_detail::try_parse([&]
1824 },
"try_read_life_105");
1843 return ca_io_detail::try_parse([&]
1846 },
"try_read_life_106");
1870 return ca_io_detail::try_parse([&]
1872 return read_csv_snapshot<T>(in, opts);
1873 },
"try_read_csv_snapshot");
C++20/23 compatibility layer: feature detection + Aleph:: polyfills.
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
size_t size_t int32_t value
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void clear() noexcept
Empties the container.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Streaming JSON writer for CA trajectories.
JSON_Frame_Stream_Writer(const JSON_Frame_Stream_Writer &)=delete
void close()
Finish the stream.
JSON_Frame_Stream_Writer(std::ostream &out, JSON_Write_Options opts={})
Start a JSON frame stream.
~JSON_Frame_Stream_Writer()
Finish the JSON document if it is still open.
void accept(std::size_t step, const Lattice &frame)
Append one frame to the stream.
Lattice that adds boundary-aware access on top of a storage.
void set(const coord_type &c, const state_type &v)
Strict write: throws if c is out of range.
typename Storage::state_type state_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
void fill(const state_type &value)
Set every cell to value.
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Minimal std::expected-style result type for C++20.
Wrapper for the error value used to initialize an expected.
Storage + topology that carries the cell values.
std::string trim_copy(const std::string &s)
std::string rle_token(ca_size_t n, char glyph)
void finalise_absolute_pattern(Binary_Cell_Pattern &pattern, const Array< Coord_Vec< 2 > > &absolute, const bool have_extent, const ca_index_t min_row, const ca_index_t min_col, const ca_index_t max_row, const ca_index_t max_col, const char *what)
auto try_parse(F &&parse, const char *context) -> expected< std::invoke_result_t< F & >, std::string >
Invoke a parser, translating any thrown exception into the error channel of expected.
ca_index_t checked_coord_add(const ca_index_t a, const ca_index_t b, const char *what)
a + b for coordinates, or std::overflow_error if it does not fit.
void ensure_stream_ok(const std::ostream &out, const char *fn)
bool blank(const std::string &s)
T parse_scalar(const std::string &token, const char *format)
ca_index_t parse_index_token(const std::string &s, const char *what)
Array< std::string > split_comment_lines(const std::string &comment)
constexpr ca_size_t Max_Pattern_Extent
Largest width or height of a pattern: its cells have coordinates from 0 to Max_Coordinate.
ca_size_t parse_json_size_field(const std::string &s, const std::string &key)
void write_text_scalar(std::ostream &out, const T &value)
std::string header_value(const std::string &line, const std::string &key)
std::string json_escape(const std::string &s)
ca_size_t parse_size_token(const std::string &s, const char *what)
T parse_json_number(const std::string &s, std::size_t &pos)
std::string lower_copy(std::string s)
void require_rank2(const char *fn)
void append_rle_token(std::string &body, ca_size_t n, char glyph)
void write_wrapped_rle_body(std::ostream &out, const std::string &body, const std::size_t line_width)
constexpr ca_index_t Max_Coordinate
Largest coordinate of a cell.
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
Lattice make_lattice_from_snapshot(const Grid_Snapshot< T > &snap)
Construct a lattice from a numeric snapshot.
void write_pgm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PGM (P5) image.
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.
void write_plaintext(std::ostream &out, const Lattice &frame, const Plaintext_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a Life plaintext (.cells) pattern.
Binary_Cell_Pattern read_plaintext_string(const std::string &s)
Read a Life plaintext pattern from a string.
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.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
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.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
constexpr ca_size_t Rle_Default_Max_Live_Cells
Default upper bound on the live cells read_rle() stores.
expected< Binary_Cell_Pattern, std::string > try_read_plaintext(std::istream &in)
Non-throwing variant of read_plaintext.
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.
void write_rle(std::ostream &out, const Lattice &frame, const RLE_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a 2D frame in Conway RLE format.
void write_ppm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PPM (P6) image.
Binary_Cell_Pattern read_life_105(std::istream &in)
Read a Life 1.05 pattern.
expected< RLE_Pattern, std::string > try_read_rle(std::istream &in, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Non-throwing variant of read_rle.
void write_csv(std::ostream &out, const Lattice &frame, const CSV_Options &opts={})
Write a numeric CSV snapshot.
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.
RLE_Pattern read_rle(std::istream &in, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Read a Conway RLE pattern.
Grid_Snapshot< T > read_json_snapshot(std::istream &in)
Read a JSON numeric snapshot produced by write_json.
Binary_Cell_Pattern read_life_106(std::istream &in)
Read a Life 1.06 pattern.
Binary_Cell_Pattern read_plaintext(std::istream &in)
Read a Life plaintext (.cells) pattern.
void write_json(std::ostream &out, const Lattice &frame, const JSON_Write_Options &opts={})
Write a JSON snapshot.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
expected< Grid_Snapshot< T >, std::string > try_read_csv_snapshot(std::istream &in, const CSV_Options &opts={})
Non-throwing variant of read_csv_snapshot.
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.
Lattice make_lattice_from_pattern(const Binary_Cell_Pattern &pattern, const typename Lattice::state_type &alive=static_cast< typename Lattice::state_type >(1), const typename Lattice::state_type &dead=typename Lattice::state_type{})
Construct a lattice with the exact dimensions of a pattern.
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.
Grid_Snapshot< T > read_csv_snapshot(std::istream &in, const CSV_Options &opts={})
Read a numeric CSV snapshot.
expected< Binary_Cell_Pattern, std::string > try_read_life_105(std::istream &in)
Non-throwing variant of read_life_105.
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Binary pattern shared by RLE, plaintext and Life formats.
std::string name
optional pattern name
ca_index_t origin_row
original row offset before normalisation
ca_size_t height
number of rows
ca_size_t width
number of columns
Array< std::string > comments
format comments without marker prefixes
ca_index_t origin_col
original column offset before normalisation
std::string rule
optional rule string, e.g. B3/S23
Array< Coord_Vec< 2 > > alive
live cells as {row, col}
Default binary grayscale mapper.
std::uint8_t alive_value
grayscale for live cells
std::uint8_t dead_value
grayscale for dead cells
State dead
state considered dead
std::uint8_t operator()(const State &v) const
Map a cell to one byte.
Default binary RGB mapper.
RGB8 dead_colour
colour for dead cells
State dead
state considered dead
RGB8 alive_colour
colour for live cells
RGB8 operator()(const State &v) const
Map a cell to RGB.
Options controlling CSV output and input.
char delimiter
separator between cells
bool skip_empty_lines
reader ignores empty/whitespace-only rows
Dense numeric snapshot used by CSV and JSON readers.
ca_size_t width
number of columns
ca_size_t height
number of rows
Array< T > cells
row-major payload
T at(ca_size_t row, ca_size_t col) const
Return the value at (row, col).
Options controlling JSON snapshot output.
bool pretty
pretty-print with indentation
std::string rule
optional rule field
Options controlling NetPBM output.
std::uint16_t max_value
NetPBM maximum sample value.
Options controlling Life plaintext output.
char dead
glyph for dead cells
char alive
glyph for live cells
std::string comment
optional ! comments, split on newlines
RGB byte triplet used by PPM exporters.
std::uint8_t g
green channel
friend constexpr bool operator==(RGB8, RGB8) noexcept=default
std::uint8_t b
blue channel
std::uint8_t r
red channel
Options controlling Conway RLE output.
std::string rule
rule field emitted in the header
std::size_t line_width
body wrap width; 0 disables wrapping
std::string comment
optional #C lines, split on newlines
bool trim_dead_border
crop to the live-cell bounding box
std::string name
optional #N line
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
C++20 concepts for the Cellular Automata module.