lifetree_abstract.h 21.9 KB
Newer Older
Adam P. Goucher's avatar
Adam P. Goucher committed
1 2 3 4
#pragma once

#include "hashtrees/hypertree.h"
#include "bitworld.h"
5
#include "sanirule.h"
Adam P. Goucher's avatar
Adam P. Goucher committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <map>
#include <utility>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>

namespace apg {

    uint64_t transform_uint64(uint64_t x, uint8_t perm) {

        uint64_t c[4];

        c[0] = x & 0x000000000f0f0f0full;
        c[1] = (x >> 4) & 0x000000000f0f0f0full;
        c[2] = (x >> 32) & 0x000000000f0f0f0full;
        c[3] = (x >> 36) & 0x000000000f0f0f0full;

        uint64_t y = c[perm & 3] | (c[(perm >> 2) & 3] << 4) | (c[(perm >> 4) & 3] << 32) | (c[(perm >> 6) & 3] << 36);

        c[0] = y & 0x0000333300003333ull;
        c[1] = (y >> 2) & 0x0000333300003333ull;
        c[2] = (y >> 16) & 0x0000333300003333ull;
        c[3] = (y >> 18) & 0x0000333300003333ull;

        y = c[perm & 3] | (c[(perm >> 2) & 3] << 2) | (c[(perm >> 4) & 3] << 16) | (c[(perm >> 6) & 3] << 18);

        c[0] = y & 0x0055005500550055ull;
        c[1] = (y >> 1) & 0x0055005500550055ull;
        c[2] = (y >> 8) & 0x0055005500550055ull;
        c[3] = (y >> 9) & 0x0055005500550055ull;

        y = c[perm & 3] | (c[(perm >> 2) & 3] << 1) | (c[(perm >> 4) & 3] << 8) | (c[(perm >> 6) & 3] << 9);

        return y;
    }

    template<typename I>
    struct lifemeta {

        I res;
        I aux;

    };

    template<typename I>
    class lifetree_abstract {

        public:

56 57
        virtual ~lifetree_abstract() {}

Adam P. Goucher's avatar
Adam P. Goucher committed
58 59 60 61 62 63 64 65 66 67
        uint64_t gc_threshold;

        virtual uint64_t newihandle(hypernode<I> hnode) = 0;
        virtual void delhandle(uint64_t ihandle) = 0;
        virtual void delhandle(std::string handle) = 0;
        virtual void sethandle(uint64_t ihandle, hypernode<I> hnode) = 0;
        virtual void sethandle(std::string handle, hypernode<I> hnode) = 0;
        virtual hypernode<I> gethandle(uint64_t ihandle) = 0;
        virtual hypernode<I> gethandle(std::string handle) = 0;
        virtual uint64_t counthandles() = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
68
        virtual uint64_t countlayers() = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
69 70 71
        virtual void force_gc() = 0;
        virtual bool threshold_gc(uint64_t threshold) = 0;
        virtual uint64_t getcell_recurse(hypernode<I> hnode, uint64_t x, uint64_t y) = 0;
72
        virtual void write_macrocell(std::ostream &outstream, hypernode<I> hnode, std::string rule, int multistate) = 0;
73
        virtual void write_macrocell_header(std::ostream &outstream) = 0;
74
        virtual void write_macrocell_headerless(std::ostream &outstream, hypernode<I> hnode, std::string rule, int multistate) = 0;
75
        virtual void getcells_recurse(hypernode<I> hnode, uint64_t x, uint64_t y, std::map<std::pair<uint64_t, uint64_t>, uint64_t> &cells) = 0;
76
        virtual void write_rle(std::ostream &outstream, hypernode<I> hnode, std::string rule) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
77 78 79
        virtual hypernode<I> _string32(std::string s) = 0;
        virtual std::string _string32(hypernode<I> hnode) = 0;

80 81 82 83 84 85 86 87
        void write_macrocell(std::ostream &outstream, hypernode<I> hnode, std::string rule) {
            write_macrocell(outstream, hnode, rule, 2);
        }

        void write_macrocell_headerless(std::ostream &outstream, hypernode<I> hnode, std::string rule) {
            write_macrocell_headerless(outstream, hnode, rule, 2);
        }

Adam P. Goucher's avatar
Adam P. Goucher committed
88 89 90 91
        bool threshold_gc() {
            return threshold_gc(gc_threshold);
        }

92
        // virtual kiventry<nicearray<I, 4>, I, lifemeta<I> >* ind2ptr_nonleaf(uint32_t depth, I index) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
93 94 95 96
        virtual I make_nonleaf(uint32_t depth, nicearray<I, 4> contents) = 0;
        virtual hypernode<I> make_nonleaf_hn(uint32_t depth, nicearray<I, 4> contents) = 0;
        virtual I getpop_recurse(hypernode<I> hnode, I modprime, uint64_t layermask) = 0;
        virtual hypernode<I> solid(uint32_t depth) = 0;
97
        virtual hypernode<I> solid(uint32_t depth, uint64_t state) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
98 99 100 101

        virtual hypernode<I> getchild(hypernode<I> hnode, uint32_t n) = 0;
        virtual uint64_t leafpart(I index, uint32_t part) = 0;

102 103
        virtual hypernode<I> pyramid_down(hypernode<I> hnode) = 0;
        virtual hypernode<I> pyramid_up(hypernode<I> hnode) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
104

105
        virtual hypernode<I> subnode(hypernode<I> hnode, uint64_t x, uint64_t y, uint64_t n) = 0;
106
        virtual hypernode<I> onecell_recurse(hypernode<I> hnode) = 0;
107

Adam P. Goucher's avatar
Adam P. Goucher committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        virtual hypernode<I> demorton_recurse(std::map<uint64_t, uint64_t>::iterator &it,
                                              std::map<uint64_t, uint64_t>::iterator &pe,
                                              uint64_t lm, uint64_t upto, uint32_t depth) = 0;

        hypernode<I> demorton(std::map<uint64_t, uint64_t> &mmap, uint64_t lm) {
            std::map<uint64_t, uint64_t>::iterator it = mmap.begin();
            std::map<uint64_t, uint64_t>::iterator pe = mmap.end();
            return pyramid_down(demorton_recurse(it, pe, lm, 0, 31));
        }

        hypernode<I> demorton(const bitworld &bw, uint64_t lm) {
            std::map<uint64_t, uint64_t> mmap;
            bw.mortonmap(&mmap);
            return demorton(mmap, lm);
        }

        virtual void bitworld_recurse(hypernode<I> hnode, bitworld* bw, uint32_t layer, int32_t x, int32_t y) = 0;

        bitworld flatlayer(hypernode<I> hnode, uint32_t layer, int32_t x, int32_t y) {
            bitworld bw;
            bitworld_recurse(hnode, &bw, layer, x, y);
            return bw;
        }

        bitworld flatlayer(hypernode<I> hnode, uint32_t layer) {
            return flatlayer(hnode, layer, -(1 << (hnode.depth)), -(1 << (hnode.depth)));
        }

        virtual hypernode<I> shift_recurse(hypernode<I> hnode, uint64_t x, uint64_t y, uint64_t exponent,
                                            std::map<std::pair<I, uint32_t>, I> *memmap) = 0;

        hypernode<I> shift_recurse(hypernode<I> hnode, uint64_t x, uint64_t y, uint64_t exponent) {
            std::map<std::pair<I, uint32_t>, I> memmap;
            return shift_recurse(hnode, x, y, exponent, &memmap);
        }

        hypernode<I> shift_toroidal(hypernode<I> hnode, int64_t x, int64_t y, uint64_t exponent) {
            nicearray<I, 4> cc = {hnode.index, hnode.index, hnode.index, hnode.index};
            hypernode<I> xcc = make_nonleaf_hn(hnode.depth + 1, cc);

            int64_t sx = x;
            int64_t sy = y;
            uint64_t sz = exponent;

            if ((sx == 0) && (sy == 0)) { return hnode; }

            while (((sx & 1) == 0) && ((sy & 1) == 0)) {
                sx = sx / 2;
                sy = sy / 2;
                sz = sz + 1;
            }

            // We cast to unsigned integers, which is okay provided our
            // universe is no larger than (2 ^ 64)-by-(2 ^ 64):
            uint64_t ux = (uint64_t) (0 - sx);
            uint64_t uy = (uint64_t) (0 - sy);

            return shift_recurse(xcc, ux, uy, sz);
        }

        hypernode<I> pyramid_up(hypernode<I> hnode_initial, uint32_t target_depth) {
            hypernode<I> hnode = hnode_initial;
            while (target_depth > hnode.depth) {
                // Do this iteratively:
                hnode = pyramid_up(hnode);
            }
            return hnode;
        }

        virtual hypernode<I> convolve_recurse(hypernode<I> lnode, hypernode<I> rnode, bool exclusive,
                                        std::map<std::pair<std::pair<I, I>, uint32_t>, I> *memmap1,
                                        std::map<std::pair<std::pair<I, I>, uint32_t>, I> *memmap2) = 0;

        hypernode<I> convolve_recurse(hypernode<I> lnode, hypernode<I> rnode, bool exclusive) {
            std::map<std::pair<std::pair<I, I>, uint32_t>, I> memmap1;
            std::map<std::pair<std::pair<I, I>, uint32_t>, I> memmap2;
            return convolve_recurse(lnode, rnode, exclusive, &memmap1, &memmap2);
        }

        hypernode<I> convolve_universe(hypernode<I> lnode, hypernode<I> rnode, bool exclusive) {
            hypernode<I> lx = lnode;
            hypernode<I> rx = rnode;
            while (lx.depth < rx.depth) { lx = pyramid_up(lx); }
            while (lx.depth > rx.depth) { rx = pyramid_up(rx); }
            hypernode<I> hnode = convolve_recurse(lx, rx, exclusive);
            hnode = pyramid_down(hnode);
            return hnode;
        }

        virtual uint64_t digest_recurse(hypernode<I> hnode,
                                        std::map<std::pair<I, uint32_t>, uint64_t> *memmap) = 0;

        virtual hypernode<I> boolean_recurse(hypernode<I> lnode, hypernode<I> rnode, int operation,
                                        std::map<std::pair<std::pair<I, I>, uint32_t>, I> *memmap) = 0;

        hypernode<I> boolean_recurse(hypernode<I> lnode, hypernode<I> rnode, int operation) {
            std::map<std::pair<std::pair<I, I>, uint32_t>, I> memmap;
            return boolean_recurse(lnode, rnode, operation, &memmap);
        }

208 209 210 211 212 213 214 215 216 217 218 219
        hypernode<I> breach(hypernode<I> hnode) {
            if (hnode.index2 == 0) {
                return hnode;
            } else if (hnode.index == 0) {
                return hypernode<I>(hnode.index2, hnode.depth);
            } else {
                hypernode<I> i1(hnode.index,  hnode.depth);
                hypernode<I> i2(hnode.index2, hnode.depth);
                return boolean_recurse(i1, i2, 1);
            }
        }

220 221 222 223 224 225 226 227
        virtual hypernode<I> tensor_recurse(hypernode<I> hnode, lifetree_abstract<I> *lab, uint32_t delta, std::vector<I> &v,
                                    std::map<std::pair<I, uint32_t>, I> *memmap) = 0;

        hypernode<I> tensor_recurse(hypernode<I> hnode, lifetree_abstract<I> *lab, uint32_t delta, std::vector<I> &v) {
            std::map<std::pair<I, uint32_t>, I> memmap;
            return tensor_recurse(hnode, lab, delta, v, &memmap);
        }

Adam P. Goucher's avatar
Adam P. Goucher committed
228
        hypernode<I> boolean_universe(hypernode<I> lnode, hypernode<I> rnode, int operation) {
229 230 231 232 233 234 235

            if (operation == 4) {
                hypernode<I> oncell = pyramid_down(breach(rnode));
                std::vector<I> v;
                v.push_back(0);
                v.push_back(breach(rnode).index);
                return tensor_recurse(lnode, this, oncell.depth + 4, v);
236 237 238 239
            } else if (operation == 5) {
                return convolve_universe(lnode, rnode, false);
            } else if (operation == 6) {
                return convolve_universe(lnode, rnode, true);
240 241
            }

Adam P. Goucher's avatar
Adam P. Goucher committed
242 243 244 245 246 247 248 249 250
            hypernode<I> lx = lnode;
            hypernode<I> rx = rnode;
            while (lx.depth < rx.depth) { lx = pyramid_up(lx); }
            while (lx.depth > rx.depth) { rx = pyramid_up(rx); }
            hypernode<I> hnode = boolean_recurse(lx, rx, operation);
            hnode = pyramid_down(hnode);
            return hnode;
        }

251
        hypernode<I> fromplanes(std::vector<bitworld> planes) {
Adam P. Goucher's avatar
Adam P. Goucher committed
252 253 254 255 256
            /*
            * Convert a string from headerless RLE to a hypernode in the
            * current universe.
            */
            hypernode<I> hnode(0, 1);
257
            for (unsigned int i = 0; i < planes.size(); i++) {
258
                hnode = boolean_universe(hnode, demorton(planes[i], 1ull << i), 1);
Adam P. Goucher's avatar
Adam P. Goucher committed
259 260 261 262
            }
            return hnode;
        }

263 264 265 266
        hypernode<I> fromcells(uint64_t n, int64_t *coords, uint64_t *states) {
            return fromplanes(cells2vec(n, coords, states));
        }

267
        hypernode<I> fromrle(std::string s, RuleMapper *rm) {
268
            if (s != "") {
269
                return fromplanes(rle2vec(s, rm));
270 271 272 273 274
            } else {
                return hypernode<I>(0, 1);
            }
        }

275 276
        hypernode<I> fromrle(std::string s) { return fromrle(s, 0); }

Adam P. Goucher's avatar
Adam P. Goucher committed
277 278 279
        virtual int rulestring_to_integer(std::string rulestring) = 0;
        virtual hypernode<I> iterate_recurse(hypernode<I> hnode, uint64_t mantissa, uint64_t exponent, int rule, int history) = 0;

280
        hypernode<I> advance(hypernode<I> hnode_initial, uint64_t mantissa, uint64_t exponent, int rule, bool history) {
Adam P. Goucher's avatar
Adam P. Goucher committed
281 282 283 284 285 286 287
            /*
            * Advance the universe by (mantissa * (2 ** exponent)) timesteps,
            * returning an index to the resulting hypernode. This resizes the
            * universe as necessary (without changing the 'origin', taken to
            * be the centre of the hypernode).
            */
            // std::cerr << "Exponent = " << exponent << " ; mantissa = " << mantissa << std::endl;
288
            int family = uli_get_family(rule) + history;
Adam P. Goucher's avatar
Adam P. Goucher committed
289 290
            hypernode<I> hnode = pyramid_up(pyramid_up(hnode_initial));
            hnode = pyramid_up(hnode, exponent + 2);
291
            hnode = iterate_recurse(hnode, mantissa, exponent, rule, family);
Adam P. Goucher's avatar
Adam P. Goucher committed
292 293 294 295 296 297 298 299 300
            hnode = pyramid_down(hnode);
            return hnode;
        }

        hypernode<I> advance(hypernode<I> hnode_initial, uint64_t mantissa, uint64_t exponent, std::string rule, bool history) {
            int ruleint = rulestring_to_integer(rule);
            if (ruleint == -1) {
                return hnode_initial;
            } else {
301
                return advance(hnode_initial, mantissa, exponent, ruleint, history);
Adam P. Goucher's avatar
Adam P. Goucher committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
            }
        }

        hypernode<I> advance(hypernode<I> hnode_initial, uint64_t mantissa, uint64_t exponent, std::string rule) {
            // std::cerr << mantissa << " * 2^" << exponent << std::endl;
            std::string newrule = rule;
            bool history = false;
            if (newrule.length() >= 8 && newrule.substr(newrule.length()-7) == "History") {
                newrule = newrule.substr(0, newrule.length()-7);
                history = true;
                if (newrule[newrule.length()-1] == '_') {
                    newrule = newrule.substr(0, newrule.length()-1);
                }
            }
            return advance(hnode_initial, mantissa, exponent, newrule, history);
        }

        hypernode<I> advance(hypernode<I> hnode_initial, std::string rule, uint64_t steps, uint64_t stepexp) {
            hypernode<I> hnode = hnode_initial;
            if (steps) {
                uint64_t numsteps = steps;
                uint64_t exponent = stepexp;

                while ((numsteps & 7) && exponent) {
                    numsteps = numsteps << 1;
                    exponent -= 1;
                }

Adam P. Goucher's avatar
Adam P. Goucher committed
330 331 332 333 334 335
                uint64_t vm = uli_valid_mantissa(rulestring_to_integer(rule));
                uint64_t mantissa = 8;
                while ((mantissa != 1) && ((numsteps % mantissa) || ((vm & (1 << mantissa)) == 0))) { mantissa -= 1; }
                if ((vm & (1 << mantissa)) == 0) {
                    std::cerr << "Rule " << rule << " cannot be iterated " << steps;
                    std::cerr << " x 2^" << stepexp << " generations." << std::endl;
Adam P. Goucher's avatar
Adam P. Goucher committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
                    exit(1);
                }

                uint64_t multiplier = numsteps / mantissa;

                while (multiplier) {
                    if (multiplier & 1) {
                        hnode = advance(hnode, mantissa, exponent, rule);
                    }
                    multiplier = multiplier >> 1;
                    exponent += 1;
                }
            } else {
                hnode = pyramid_down(hnode);
            }
            return hnode;
        }

        hypernode<I> advance(hypernode<I> hnode_initial, std::string rule, uint64_t steps) {
            return advance(hnode_initial, rule, steps, 0);
        }

358
        virtual uint64_t bound_recurse(hypernode<I> hnode, int direction, std::map<std::pair<I, uint32_t>, uint64_t> *memmap, uint32_t pixelsize) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
359

360
        uint64_t bound_recurse(hypernode<I> hnode, int direction, int pixelsize) {
Adam P. Goucher's avatar
Adam P. Goucher committed
361
            std::map<std::pair<I, uint32_t>, uint64_t> memmap;
362 363 364 365 366
            return bound_recurse(hnode, direction, &memmap, pixelsize);
        }

        uint64_t bound_recurse(hypernode<I> hnode, int direction) {
            return bound_recurse(hnode, direction, 0);
Adam P. Goucher's avatar
Adam P. Goucher committed
367 368 369 370 371 372 373 374 375 376 377
        }

        bool getbbox(hypernode<I> hnode, int64_t *bbox) {

            if (hnode.index == 0) { return false; }

            int64_t left = bound_recurse(hnode, 0);
            int64_t top = bound_recurse(hnode, 1);
            int64_t right = bound_recurse(hnode, 2);
            int64_t bottom = bound_recurse(hnode, 3);

378 379
            bbox[0] = left - (8ll << hnode.depth);
            bbox[1] = top - (8ll << hnode.depth);
Adam P. Goucher's avatar
Adam P. Goucher committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
            bbox[2] = 1 + right - left;
            bbox[3] = 1 + bottom - top;

            return true;
        }

        uint64_t digest_universe(hypernode<I> hnode) {
            std::map<std::pair<I, uint32_t>, uint64_t> memmap;
            uint64_t h = digest_recurse(hnode, &memmap);
            h += (bound_recurse(hnode, 0) * 5);
            h += (bound_recurse(hnode, 1) * 17);
            h += (bound_recurse(hnode, 2) * 257);
            h += (bound_recurse(hnode, 3) * 65537);
            return h;
        }

        virtual hypernode<I> copy_recurse(hypernode<I> hnode, lifetree_abstract<I> *lab, std::map<std::pair<I, uint32_t>, I> *memmap) = 0;

        hypernode<I> copy_recurse(hypernode<I> hnode, lifetree_abstract<I> *lab) {
            std::map<std::pair<I, uint32_t>, I> memmap;
            return copy_recurse(hnode, lab, &memmap);
        }

403
        virtual hypernode<I> brand_recurse(hypernode<I> hnode, std::map<std::pair<I, uint32_t>, I> *memmap, bool disjunctive) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
404 405 406

        hypernode<I> brand_recurse(hypernode<I> hnode) {
            std::map<std::pair<I, uint32_t>, I> memmap;
407 408 409 410 411 412
            return brand_recurse(hnode, &memmap, false);
        }

        hypernode<I> bror_recurse(hypernode<I> hnode) {
            std::map<std::pair<I, uint32_t>, I> memmap;
            return brand_recurse(hnode, &memmap, true);
Adam P. Goucher's avatar
Adam P. Goucher committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        }

        virtual hypernode<I> transform_recurse(hypernode<I> hnode, uint8_t perm, std::map<std::pair<I, uint32_t>, I> *memmap) = 0;

        hypernode<I> transform_recurse(hypernode<I> hnode, uint8_t perm) {
            std::map<std::pair<I, uint32_t>, I> memmap;
            return transform_recurse(hnode, perm, &memmap);
        }

        hypernode<I> shift_universe(hypernode<I> hnode_initial, int64_t x, int64_t y, uint64_t exponent) {
            hypernode<I> hnode = hnode_initial;
            if ((x != 0) || (y != 0)) {
                int64_t absx = (x < 0) ? (-x) : x;
                int64_t absy = (y < 0) ? (-y) : y;
                uint64_t diameter = (absx > absy) ? absx : absy;
                uint64_t lzcount = __builtin_clzll(diameter);
                hnode = pyramid_up(hnode_initial, (64 - lzcount) + exponent);
                hnode = pyramid_up(hnode);
                hnode = shift_toroidal(hnode, x, y, exponent);
            }
            hnode = pyramid_down(hnode);
            return hnode;
        }

        hypernode<I> shift_universe(hypernode<I> hnode_initial, int64_t x, int64_t y) {
            return shift_universe(hnode_initial, x, y, 0);
        }

441 442 443 444 445 446 447 448 449 450 451 452
        hypernode<I> semisolid(uint64_t depth, uint64_t flags) {

            hypernode<I> hnode = solid(depth);
            I z = 0;
            nicearray<I, 4> cc = {((flags & 1) ? hnode.index : z),
                                  ((flags & 2) ? hnode.index : z),
                                  ((flags & 4) ? hnode.index : z),
                                  ((flags & 8) ? hnode.index : z)};
            return make_nonleaf_hn(hnode.depth + 1, cc);

        }

Adam P. Goucher's avatar
Adam P. Goucher committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
        hypernode<I> rectangle(uint64_t width, uint64_t height) {
            if (width == 0 || height == 0) {
                return hypernode<I>(0, 1);
            } else {
                uint64_t diameter = (width > height) ? width : height;
                uint64_t lzcount = __builtin_clzll(diameter);
                hypernode<I> hnode = solid((lzcount < 60) ? (60 - lzcount) : 1);
                I z = 0;
                nicearray<I, 4> cc = {z, z, z, hnode.index};
                hnode = make_nonleaf_hn(hnode.depth + 1, cc);
                hnode = boolean_universe(hnode, shift_universe(hnode, width, 0, 0), 3);
                hnode = boolean_universe(hnode, shift_universe(hnode, 0, height, 0), 3);
                return hnode;
            }
        }

        hypernode<I> rectangle(int64_t x, int64_t y, uint64_t width, uint64_t height) {
            /*
            * Return a hnode containing a solid rectangle with top-left corner
            * (x, y) and dimensions width * height.
            */
            hypernode<I> hnode = rectangle(width, height);
            hnode = shift_universe(hnode, x, y, 0);
            return hnode;
        }

        hypernode<I> transform_and_shift(hypernode<I> hnode, uint8_t perm, int64_t x, int64_t y) {
            /*
            * Apply an isometry of the plane which sends (0, 0) to (x, y) with
            * an optional rotation and/or reflection.
            */
484 485 486 487

            uint8_t invperm = (1 << ((perm & 12) >> 1)) | (2 << ((perm & 48) >> 3)) | (3 << ((perm & 192) >> 5));
            int64_t nx = (invperm & 64) ? x : (x + 1);
            int64_t ny = (invperm & 128) ? y : (y + 1);
Adam P. Goucher's avatar
Adam P. Goucher committed
488 489 490 491
            hypernode<I> hn = (perm == 228) ? hnode : transform_recurse(hnode, perm);
            return shift_universe(hn, nx, ny, 0);
        }

492
        virtual hypernode<I> read_macrocell(std::istream &instream, std::string &rule) = 0;
Adam P. Goucher's avatar
Adam P. Goucher committed
493 494 495

        hypernode<I> load_macrocell(std::string filename, std::string &rule) {
            std::ifstream f(filename);
496
            return read_macrocell(f, rule);
Adam P. Goucher's avatar
Adam P. Goucher committed
497 498 499 500 501 502 503
        }

        hypernode<I> load_macrocell(std::string filename, std::string &rule, int64_t x, int64_t y) {
            return shift_universe(load_macrocell(filename, rule), x, y);
        }

        hypernode<I> pattern_match(hypernode<I> u, hypernode<I> c0, hypernode<I> c1) {
504
            hypernode<I> v = pyramid_up(pyramid_up(pyramid_up(pyramid_up(u, c0.depth), c1.depth)));
Adam P. Goucher's avatar
Adam P. Goucher committed
505 506 507 508 509 510 511 512 513 514 515 516 517
            hypernode<I> sol = solid(v.depth);
            hypernode<I> sol2 = solid(v.depth-1);
            hypernode<I> rc0 = pyramid_up(transform_recurse(c0, 27), v.depth);
            hypernode<I> rc1 = pyramid_up(transform_recurse(c1, 27), v.depth);
            hypernode<I> match0 = convolve_recurse(v, rc0, false);
            hypernode<I> match1 = convolve_recurse(boolean_recurse(sol, v, 3), rc1, false);
            hypernode<I> badcells = boolean_recurse(match0, match1, 1);
            hypernode<I> goodcells = boolean_recurse(pyramid_up(pyramid_up(sol2)), badcells, 3);
            return pyramid_down(shift_toroidal(brand_recurse(goodcells), 1, 1, 0));
        }

    };
}