70#ifndef TPL_CA_HASHLIFE_H
71#define TPL_CA_HASHLIFE_H
84#include <unordered_map>
113 [[nodiscard]]
constexpr bool apply(
const bool current,
const std::uint8_t n)
const noexcept
124 static_cast<std::uint16_t
>(1u << 3),
static_cast<std::uint16_t
>((1u << 2) | (1u << 3))};
128 static_cast<std::uint16_t
>((1u << 3) | (1u << 6)),
129 static_cast<std::uint16_t
>((1u << 2) | (1u << 3))};
133 static_cast<std::uint16_t
>((1u << 3) | (1u << 6) | (1u << 7) | (1u << 8)),
134 static_cast<std::uint16_t
>((1u << 3) | (1u << 4) | (1u << 6) | (1u << 7) | (1u << 8))};
150 for (
int i = 0; i <= 8; ++i)
151 if (((
r.birth_mask >> i) & 1u) != 0)
152 s.push_back(
static_cast<char>(
'0' + i));
155 for (
int i = 0; i <= 8; ++i)
156 if (((
r.survival_mask >> i) & 1u) != 0)
157 s.push_back(
static_cast<char>(
'0' + i));
168[[nodiscard]]
inline std::optional<Outer_Totalistic_Binary_Rule>
parse_rule(
const std::string &s)
170 std::uint16_t birth = 0, survival = 0;
172 while (i < s.size()
and std::isspace(
static_cast<unsigned char>(s[i])))
174 if (i < s.size()
and (s[i] ==
'B' or s[i] ==
'b'))
177 while (i < s.size()
and std::isdigit(
static_cast<unsigned char>(s[i])))
179 birth |=
static_cast<std::uint16_t
>(1u << (s[i] -
'0'));
182 if (i < s.size()
and s[i] ==
'/')
184 if (i < s.size()
and (s[i] ==
'S' or s[i] ==
's'))
186 while (i < s.size()
and std::isdigit(
static_cast<unsigned char>(s[i])))
188 survival |=
static_cast<std::uint16_t
>(1u << (s[i] -
'0'));
194 while (i < s.size()
and std::isdigit(
static_cast<unsigned char>(s[i])))
196 survival |=
static_cast<std::uint16_t
>(1u << (s[i] -
'0'));
199 if (i >= s.size() or s[i] !=
'/')
202 while (i < s.size()
and std::isdigit(
static_cast<unsigned char>(s[i])))
204 birth |=
static_cast<std::uint16_t
>(1u << (s[i] -
'0'));
211namespace ca_hashlife_detail {
248 auto mix = [](std::size_t a, std::size_t b)
noexcept -> std::size_t
250 return a ^ (b + 0x9e3779b97f4a7c15ULL + (a << 6) + (a >> 2));
252 std::size_t
h = (
static_cast<std::size_t
>(
k.level) << 8) |
k.bits;
253 h = mix(
h,
reinterpret_cast<std::uintptr_t
>(
k.nw));
254 h = mix(
h,
reinterpret_cast<std::uintptr_t
>(
k.ne));
255 h = mix(
h,
reinterpret_cast<std::uintptr_t
>(
k.sw));
256 h = mix(
h,
reinterpret_cast<std::uintptr_t
>(
k.se));
321 [[nodiscard]] std::int64_t
width() const noexcept
327 [[nodiscard]] std::int64_t
height() const noexcept
354 <<
"Hashlife_Engine: cache_capacity (" <<
cache_capacity_ <<
") below minimum 1024";
407 pool_.swap(other.pool_);
408 table_.swap(other.table_);
441 void set_alive(
const std::int64_t x,
const std::int64_t
y,
const bool alive =
true)
445 const std::int64_t half = std::int64_t{1} << (
root_->
level - 1);
446 if (x >= -half
and x < half and y >= -half
and y < half)
450 const std::int64_t half = std::int64_t{1} << (
root_->
level - 1);
456 [[nodiscard]]
bool alive_at(
const std::int64_t x,
const std::int64_t
y)
const
458 const std::int64_t half = std::int64_t{1} << (
root_->
level - 1);
459 if (x < -half or x >= half or y < -half or y >= half)
479 const std::uint8_t min_level =
480 std::max<std::uint8_t>(
static_cast<std::uint8_t
>(
k + 2), 4);
488 const std::uint64_t
steps = std::uint64_t{1} << (L - 2);
506 std::uint64_t
run(std::uint64_t generations)
508 std::uint64_t advanced = 0;
509 while (generations > 0)
511 const unsigned k =
static_cast<unsigned>(63 - std::countl_zero(generations));
512 const std::uint64_t step =
advance(
k);
514 if (step >= generations)
536 BBox bb{std::numeric_limits<std::int64_t>::max(), std::numeric_limits<std::int64_t>::max(),
537 std::numeric_limits<std::int64_t>::min(), std::numeric_limits<std::int64_t>::min()};
540 const std::int64_t half = std::int64_t{1} << (
root_->
level - 1);
581 template <
typename F>
586 const std::int64_t half = std::int64_t{1} << (
root_->
level - 1);
605 bool header_seen =
false;
606 std::int64_t origin_x = 0;
607 std::int64_t origin_y = 0;
608 std::int64_t cur_x = 0;
609 std::int64_t cur_y = 0;
610 std::int64_t
run = 0;
613 while (not ended
and std::getline(in, line))
615 if (line.empty() or line[0] ==
'#')
623 origin_x = -(x_size / 2);
624 origin_y = -(y_size / 2);
630 for (
const char c : line)
632 if (std::isdigit(
static_cast<unsigned char>(c)))
633 run =
run * 10 + (c -
'0');
634 else if (c ==
'b' or c ==
'.')
636 cur_x += (
run == 0 ? 1 :
run);
639 else if (c ==
'o' or c ==
'A')
641 const std::int64_t
r = (
run == 0 ? 1 :
run);
642 for (std::int64_t i = 0; i <
r; ++i)
649 cur_y += (
run == 0 ? 1 :
run);
665 std::istringstream iss(s);
677 void save_rle(std::ostream &
out,
const std::string &comment = {})
const
679 if (not comment.empty())
681 std::istringstream cs(comment);
683 while (std::getline(cs,
l))
684 out <<
"#C " <<
l <<
'\n';
686 const BBox bb =
bbox();
700 if (cells.
size() > 1)
703 out <<
"x = " << bb.width() <<
", y = " << bb.height() <<
", rule = " <<
format_rule(
rule_)
706 std::ostringstream body;
707 auto emit_run = [&body](
const std::int64_t n,
const char glyph)
716 std::int64_t prev_y = bb.y_min;
717 std::int64_t cur_x = bb.x_min;
719 while (i < cells.
size())
721 const std::int64_t
y = cells(i).first;
722 const std::int64_t x = cells(i).second;
725 emit_run(
y - prev_y,
'$');
731 emit_run(x - cur_x,
'b');
734 std::int64_t alive_run = 0;
736 while (j < cells.
size()
and cells(j).first ==
y and cells(j).second == cur_x + alive_run)
741 emit_run(alive_run,
'o');
748 const std::string body_str = body.str();
750 while (pos < body_str.size())
752 std::size_t end = std::min<std::size_t>(pos + 70, body_str.size());
753 while (end < body_str.size()
and std::isdigit(
static_cast<unsigned char>(body_str[end])))
755 if (end < body_str.size()
and (body_str[end] ==
'b' or body_str[end] ==
'o'))
757 out << body_str.substr(pos, end - pos) <<
'\n';
765 std::ostringstream oss;
780 std::unordered_map<Node_Key, Node *, ca_hashlife_detail::Node_Key_Hash>
table_;
794 const Node_Key key{1,
bits,
nullptr,
nullptr,
nullptr,
nullptr};
800 n.population =
static_cast<std::uint64_t
>(std::popcount(
bits));
809 const Node_Key key{
static_cast<std::uint8_t
>(nw->
level + 1), 0, nw, ne, sw, se};
846 for (
auto &n :
pool_)
857 const std::uint8_t L = n->
level;
860 Node *nw =
make_leaf(
static_cast<std::uint8_t
>((n->
bits & 1u) ? (1u << 3) : 0u));
861 Node *ne =
make_leaf(
static_cast<std::uint8_t
>((n->
bits & 2u) ? (1u << 2) : 0u));
862 Node *sw =
make_leaf(
static_cast<std::uint8_t
>((n->
bits & 4u) ? (1u << 1) : 0u));
863 Node *se =
make_leaf(
static_cast<std::uint8_t
>((n->
bits & 8u) ? (1u << 0) : 0u));
871 return make_node(new_nw, new_ne, new_sw, new_se);
885 const std::uint64_t central =
886 n->nw->se->se->population
887 + n->ne->sw->sw->population
888 + n->sw->ne->ne->population
889 + n->se->nw->nw->population;
890 return central == n->population;
912 const std::uint8_t a = n->
nw->
bits;
913 const std::uint8_t b = n->
ne->
bits;
914 const std::uint8_t c = n->
sw->
bits;
915 const std::uint8_t d = n->
se->
bits;
919 g |=
static_cast<std::uint16_t
>((a & 0x3u)) << 0;
920 g |=
static_cast<std::uint16_t
>((b & 0x3u)) << 2;
921 g |=
static_cast<std::uint16_t
>((a >> 2) & 0x3u) << 4;
922 g |=
static_cast<std::uint16_t
>((b >> 2) & 0x3u) << 6;
923 g |=
static_cast<std::uint16_t
>((c & 0x3u)) << 8;
924 g |=
static_cast<std::uint16_t
>((d & 0x3u)) << 10;
925 g |=
static_cast<std::uint16_t
>((c >> 2) & 0x3u) << 12;
926 g |=
static_cast<std::uint16_t
>((d >> 2) & 0x3u) << 14;
928 auto get = [g](
const int r,
const int c)
noexcept ->
bool
930 return ((g >> (
r * 4 + c)) & 1u) != 0;
932 auto count = [&get](
const int r,
const int c)
noexcept -> std::uint8_t
934 return static_cast<std::uint8_t
>(get(
r - 1, c - 1) + get(
r - 1, c) + get(
r - 1, c + 1)
935 + get(
r, c - 1) + get(
r, c + 1) + get(
r + 1, c - 1)
936 + get(
r + 1, c) + get(
r + 1, c + 1));
939 std::uint8_t result = 0;
959 Node *result =
nullptr;
1001 result =
make_node(q_nw, q_ne, q_sw, q_se);
1013 const int bit_idx =
static_cast<int>(
y) * 2 +
static_cast<int>(x);
1014 std::uint8_t new_bits = n->
bits;
1016 new_bits |=
static_cast<std::uint8_t
>(1u << bit_idx);
1018 new_bits &=
static_cast<std::uint8_t
>(~(1u << bit_idx));
1021 const std::int64_t half = std::int64_t{1} << (n->
level - 1);
1043 [[nodiscard]]
static bool get_in_node(
const Node *n,
const std::int64_t x,
const std::int64_t
y)
noexcept
1047 const int bit_idx =
static_cast<int>(
y) * 2 +
static_cast<int>(x);
1048 return ((n->bits >> bit_idx) & 1u) != 0;
1050 const std::int64_t half = std::int64_t{1} << (n->level - 1);
1056 template <
typename F>
1073 const std::int64_t half = std::int64_t{1} << (n->
level - 1);
1084 auto pos = line.find(key);
1085 if (pos == std::string::npos)
1087 pos = line.find(
'=', pos);
1088 if (pos == std::string::npos)
1091 while (pos < line.size()
and std::isspace(
static_cast<unsigned char>(line[pos])))
1094 if (pos < line.size()
and line[pos] ==
'-')
1100 while (pos < line.size()
and std::isdigit(
static_cast<unsigned char>(line[pos])))
1102 v = v * 10 + (line[pos] -
'0');
1105 return neg ? -v : v;
1110 auto pos = line.find(
"rule");
1111 if (pos == std::string::npos)
1112 pos = line.find(
"Rule");
1113 if (pos == std::string::npos)
1114 return std::nullopt;
1115 pos = line.find(
'=', pos);
1116 if (pos == std::string::npos)
1117 return std::nullopt;
1119 while (pos < line.size()
and std::isspace(
static_cast<unsigned char>(line[pos])))
1122 while (pos < line.size()
and not std::isspace(
static_cast<unsigned char>(line[pos]))
1123 and line[pos] !=
',')
1125 token.push_back(line[pos]);
1129 return std::nullopt;
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & base()
Return a reference to the first element of array.
void swap(Array &s) noexcept
Swap this with s
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Hashlife engine for outer-totalistic binary cellular automata.
Rule rule() const noexcept
Current rule.
void swap(Hashlife_Engine &other) noexcept
Exchange in O(1) the universe, the rule and the caches of this engine and another.
Hashlife_Engine & operator=(const Hashlife_Engine &)=delete
Copying is disabled, as in the copy constructor.
void load_rle_string(const std::string &s)
Convenience overload that reads the pattern from a string.
BBox bbox() const
Tight bounding box of the alive cells (empty if population() == 0).
std::size_t cache_capacity() const noexcept
Maximum allowed canonical-node count before the result cache is dropped.
Hashlife_Engine & operator=(Hashlife_Engine &&other) noexcept
Exchange this engine with another, which is left with the previous universe of this one.
Hashlife_Engine(Rule rule=Conway_Life, std::size_t cache_capacity=std::size_t{1}<< 22)
Build an engine with the given rule and result-cache capacity.
static constexpr std::uint8_t initial_level_
8 × 8 starting universe
static bool is_centered_well(const Node *n) noexcept
Standard Hashlife "centered well" predicate: every quadrant of n has its population concentrated in t...
void set_rule(const Rule &r)
Replace the rule. Invalidates the result cache.
static std::optional< Outer_Totalistic_Binary_Rule > parse_rule_field(const std::string &line)
Node * set_in_node(const Node *n, const std::int64_t x, const std::int64_t y, const bool alive)
Node * vertical_centered(Node *n, Node *s)
void clear_result_cache()
static bool get_in_node(const Node *n, const std::int64_t x, const std::int64_t y) noexcept
void save_rle(std::ostream &out, const std::string &comment={}) const
Write the alive cells in RLE format.
std::uint64_t run(std::uint64_t generations)
Advance by exactly generations steps.
void for_each_alive(F &&f) const
Iterate over all alive cells, calling f(x, y) for each.
Hashlife_Engine(Hashlife_Engine &&other)
Take the universe and the caches of another engine, which is left with an empty universe of the same ...
Stats stats() const noexcept
Diagnostic counters and current root level.
void set_alive(const std::int64_t x, const std::int64_t y, const bool alive=true)
Set or clear the cell at world coordinates (x, y).
std::uint64_t advance(const unsigned k)
Advance the universe by 2^k generations.
Node * expand(const Node *n)
void visit_alive(Node *n, std::int64_t x, std::int64_t y, F &&f) const
Node * empty_node(const std::uint8_t level)
std::size_t result_misses_
Node * horizontal_centered(Node *w, Node *e)
std::unordered_map< Node_Key, Node *, ca_hashlife_detail::Node_Key_Hash > table_
Node * evolve_level_2(Node *n)
static std::int64_t parse_header_value(const std::string &line, const char key)
std::string save_rle_string(const std::string &comment={}) const
Convenience overload returning the RLE serialisation as a string.
Node * make_node(Node *nw, Node *ne, Node *sw, Node *se)
std::size_t result_cache_clears_
std::size_t cache_capacity_
Hashlife_Engine(const Hashlife_Engine &)=delete
Copying is disabled.
Array< Node * > empty_by_level_
Aleph::Array of canonical empties per level.
void clear()
Reset the universe to empty (preserves rule, capacity and node cache).
Node * make_leaf(const std::uint8_t bits)
void load_rle(std::istream &in)
Load an RLE pattern from in (centred at the origin).
std::int64_t generation() const noexcept
Number of generations elapsed since construction (or the last clear).
bool alive_at(const std::int64_t x, const std::int64_t y) const
Read the cell at world coordinates (x, y).
std::uint64_t population() const noexcept
Number of alive cells.
Node * center_of_4(Node *nw, Node *ne, Node *sw, Node *se)
std::optional< Outer_Totalistic_Binary_Rule > parse_rule(const std::string &s)
Parse a B.../S... (or Wolfram S/B) rule string.
std::string format_rule(const Outer_Totalistic_Binary_Rule &r)
Format a rule as a Conway-style Bxxx/Sxxx string.
constexpr Outer_Totalistic_Binary_Rule Conway_Life
Conway's Game of Life: B3/S23.
constexpr Outer_Totalistic_Binary_Rule HighLife
Nathan Thompson's HighLife: B36/S23 (replicators).
constexpr Outer_Totalistic_Binary_Rule Day_And_Night
Bays' Day & Night: B3678/S34678 (self-complementary).
constexpr Outer_Totalistic_Binary_Rule Seeds
Seeds: B2/S (every live cell dies, two neighbours produce a birth).
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Inclusive bounding box (returned by bbox).
std::int64_t height() const noexcept
Height of the box (0 if empty).
bool is_empty() const noexcept
True iff there are no alive cells.
std::int64_t width() const noexcept
Width of the box (0 if empty).
Diagnostics returned by stats.
std::size_t result_cache_clears
times the result cache was invalidated
std::size_t result_misses
misses on the per-node result cache
std::size_t result_hits
hits on the per-node result cache
std::size_t canonical_nodes
number of distinct canonical nodes
std::uint8_t root_level
current universe level (side 2^level)
Outer-totalistic binary rule encoded as two 9-bit bitmasks.
std::uint16_t birth_mask
bit i ⇒ dead cell with i live neighbours becomes alive
constexpr bool apply(const bool current, const std::uint8_t n) const noexcept
True iff a cell with current state and n live neighbours is alive next.
constexpr bool operator==(const Outer_Totalistic_Binary_Rule &) const noexcept=default
std::uint16_t survival_mask
bit i ⇒ live cell with i live neighbours stays alive
std::size_t operator()(const Node_Key &k) const noexcept
Hash key used to canonicalise nodes via std::unordered_map.
bool operator==(const Node_Key &o) const noexcept
Node * result
memoised next-generation result (level level - 1)
std::uint8_t level
1 ⇒ 2x2 leaf; k ⇒ 2^k × 2^k node
std::uint64_t population
number of alive cells covered by this node
std::uint8_t bits
for level-1 leaves: NW NE SW SE bits
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.