lifelib.cpp 11.6 KB
Newer Older
1 2
#include "pattern2.h"
#include "streamlife.h"
3
#include "soup/hashsoup.h"
4
#include "ssplit.h"
5 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 56 57 58
#include <new>

extern "C"
{

    void* CreateLifetree(int maxmem, int nlayers) {
        /*
        * Since we don't know at compile-time how many layers our lifetree
        * has, we have explicit template instantiations for powers of two
        * and round up to the next one.
        *
        * Negative integers are used to denote 'special' algorithms such
        * as streamlife.
        */

        if (nlayers == -1) {
            return new(std::nothrow) apg::streamtree<uint32_t, 1>(maxmem);
        } else if (nlayers > 32) {
            return new(std::nothrow) apg::lifetree<uint32_t, 64>(maxmem);
        } else if (nlayers > 16) {
            return new(std::nothrow) apg::lifetree<uint32_t, 32>(maxmem);
        } else if (nlayers > 8) {
            return new(std::nothrow) apg::lifetree<uint32_t, 16>(maxmem);
        } else if (nlayers > 4) {
            return new(std::nothrow) apg::lifetree<uint32_t, 8>(maxmem);
        } else if (nlayers > 2) {
            return new(std::nothrow) apg::lifetree<uint32_t, 4>(maxmem);
        } else if (nlayers > 1) {
            return new(std::nothrow) apg::lifetree<uint32_t, 2>(maxmem);
        } else {
            return new(std::nothrow) apg::lifetree<uint32_t, 1>(maxmem);
        }
    }

    void DeleteLifetree(void *ptr, int nlayers) {
        if (nlayers == -1) {
            delete reinterpret_cast<apg::streamtree<uint32_t, 1>*>(ptr);
        } else if (nlayers > 32) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 64>*>(ptr);
        } else if (nlayers > 16) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 32>*>(ptr);
        } else if (nlayers > 8) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 16>*>(ptr);
        } else if (nlayers > 4) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 8>*>(ptr);
        } else if (nlayers > 2) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 4>*>(ptr);
        } else if (nlayers > 1) {
            delete reinterpret_cast<apg::lifetree<uint32_t, 2>*>(ptr);
        } else {
            delete reinterpret_cast<apg::lifetree<uint32_t, 1>*>(ptr);
        }
    }

59 60 61 62 63
    int GetDiameterOfPattern(void *ptr) {
        auto ppat = reinterpret_cast<apg::pattern*>(ptr);
        return ppat->logdiam();
    }

64 65 66 67
    void DeletePattern(void *ptr) {
        delete reinterpret_cast<apg::pattern*>(ptr);
    }

68 69 70 71 72 73 74 75 76 77 78 79
    void SavePatternRLE(void *ptr, const char *filename, const char *header, const char *footer) {

        std::cerr << "filename: '" << filename << "'\n";
        std::cerr << "header: '" << header << "'\n";
        std::cerr << "footer: '" << footer << "'\n";
        std::cerr << "--------" << std::endl;

        std::ofstream out(filename);
        auto ppat = reinterpret_cast<apg::pattern*>(ptr);
        out << header;
        ppat->write_rle(out);
        out << footer << std::endl;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    void SavePatternMC(void *ptr, const char *filename, const char *header, const char *footer) {

        std::cerr << "filename: '" << filename << "'\n";
        std::cerr << "header: '" << header << "'\n";
        std::cerr << "footer: '" << footer << "'\n";
        std::cerr << "--------" << std::endl;

        std::ofstream out(filename);
        auto ppat = reinterpret_cast<apg::pattern*>(ptr);
        auto lab = ppat->getlab();
        lab->write_macrocell_header(out);
        out << header;
        lab->write_macrocell_headerless(out, ppat->gethnode(), ppat->getrule());
        out << footer << std::endl;
96 97
    }

98 99 100 101 102 103 104
    void* BooleanPatternImmutable(void* pat1, void* pat2, int op) {
        auto ppat1 = reinterpret_cast<apg::pattern*>(pat1);
        auto ppat2 = reinterpret_cast<apg::pattern*>(pat2);
        auto lab = ppat1->getlab();
        return new(std::nothrow) apg::pattern(lab, lab->boolean_universe(ppat1->gethnode(), ppat1->coerce(*ppat2), op), ppat1->getrule());
    }

105 106 107 108 109 110 111
    void BooleanPatternMutable(void* pat1, void* pat2, int op) {
        auto ppat1 = reinterpret_cast<apg::pattern*>(pat1);
        auto ppat2 = reinterpret_cast<apg::pattern*>(pat2);
        auto lab = ppat1->getlab();
        ppat1->changehnode(lab->boolean_universe(ppat1->gethnode(), ppat1->coerce(*ppat2), op));
    }

112 113 114 115 116
    void* CreatePatternFromFile(void* lt, const char *filename) {
        auto lab = reinterpret_cast<apg::lifetree_abstract<uint32_t>*>(lt);
        return new(std::nothrow) apg::pattern(lab, std::string(filename));
    }

117
    void* CreateRectangle(void* lt, int x, int y, int width, int height, const char *rule) {
118 119 120 121 122
        auto lab = reinterpret_cast<apg::lifetree_abstract<uint32_t>*>(lt);
        auto hnode = lab->rectangle(x, y, width, height);
        return new(std::nothrow) apg::pattern(lab, hnode, std::string(rule));
    }

123 124 125 126 127
    void* CreatePatternFromRLE(void* lt, const char *rle, const char *rule) {
        auto lab = reinterpret_cast<apg::lifetree_abstract<uint32_t>*>(lt);
        return new(std::nothrow) apg::pattern(lab, std::string(rle), std::string(rule));
    }

128 129 130 131 132 133 134
    void* Hashsoup(void* lt, const char *rule, const char *symmetry, const char *seed) {
        auto lab = reinterpret_cast<apg::lifetree_abstract<uint32_t>*>(lt);
        std::vector<apg::bitworld> vbw;
        vbw.push_back(apg::hashsoup(std::string(seed), std::string(symmetry)));
        return new(std::nothrow) apg::pattern(lab, vbw, std::string(rule));
    }

135
    void* AdvancePattern(void* pat, int numgens, uint64_t exponent) {
136 137 138 139
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return new(std::nothrow) apg::pattern(ppat->advance2(numgens, exponent));
    }

140
    void* GetSolidForPattern(void* pat, uint64_t state, uint64_t exponent) {
141 142 143
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto lab = ppat->getlab();
        uint64_t depth = ppat->gethnode().depth;
144
        return new(std::nothrow) apg::pattern(lab, lab->solid(depth, state << exponent), ppat->getrule());
145 146
    }

147 148 149 150 151 152 153
    void* GetSemisolidForPattern(void* pat, int flags) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto lab = ppat->getlab();
        uint64_t depth = ppat->gethnode().depth;
        return new(std::nothrow) apg::pattern(lab, lab->semisolid(depth, flags), ppat->getrule());
    }

154
    void* ShiftPattern(void* pat, int x, int y, uint64_t exponent) {
155 156 157 158
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return new(std::nothrow) apg::pattern(ppat->shift(x, y, exponent));
    }

159 160 161 162 163
    void* TransformPattern(void* pat, const char *tfm) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return new(std::nothrow) apg::pattern(ppat->transform(std::string(tfm), 0, 0));
    }

164 165
    void* MakeSpaceshipStream(void* pat, const char *infile) {
        std::vector<int64_t> gstream;
166 167 168 169 170 171 172 173

        if (infile[0] == '[') {
            std::istringstream in(infile);
            apg::onlyints(gstream, in);
        } else {
            std::ifstream in(infile);
            apg::onlyints(gstream, in);
        }
174 175 176 177
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return new(std::nothrow) apg::pattern(ppat->stream(gstream));
    }

178 179 180 181 182 183 184 185 186 187 188 189 190
    void* MatchLive(void* pat, void* pat1) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto ppat1 = reinterpret_cast<apg::pattern*>(pat1);
        return new(std::nothrow) apg::pattern(ppat->match(*ppat1));
    }

    void* MatchLiveAndDead(void* pat, void* pat1, void* pat0) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto ppat1 = reinterpret_cast<apg::pattern*>(pat1);
        auto ppat0 = reinterpret_cast<apg::pattern*>(pat0);
        return new(std::nothrow) apg::pattern(ppat->match(*ppat1, *ppat0));
    }

191 192 193 194 195 196 197 198 199 200 201 202
    void* FindPeriodOrAdvance(void* pat, int exponent) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        apg::pattern x = ppat->pdetect(1ull << exponent);

        if (ppat->dt != 0) {
            ppat->ascertain_period();
            return nullptr;
        } else {
            return new(std::nothrow) apg::pattern(x);
        }
    }

203 204 205 206 207
    int GetPopulationOfPattern(void* pat, int modprime) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->popcount(modprime);
    }

208 209 210 211 212
    void GetPatternBox(void* pat, int64_t* bbox) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        ppat->getrect(bbox);
    }

213 214 215 216 217 218
    void GetRuleOfPattern(void* pat, char* buffer) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        std::string s = ppat->getrule();
        s.copy(buffer, 2048);
    }

219 220 221 222 223 224
    void GetApgcodeOfPattern(void* pat, char* buffer) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        std::string s = ppat->apgcode();
        s.copy(buffer, 2048);
    }

225 226 227 228 229
    void GetCompiledVersion(char* buffer) {
        std::string s(LIFELIB_VERSION);
        s.copy(buffer, 2048);
    }

230 231 232 233 234
    void GetCoords(void* pat, int64_t* coords) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        ppat->get_coords(coords);
    }

235 236 237 238 239 240 241
    void GetCells(void* pat, int ncells, int64_t* coords, uint64_t* states) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        for (int i = 0; i < ncells; i++) {
            states[i] = ppat->getcell(coords[2*i], coords[2*i+1]);
        }
    }

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    void GetSubpops(void* pat, int n, int pixelsize, uint64_t* pops) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto hnode = ppat->gethnode();
        auto lab = ppat->getlab();
        uint32_t target_depth = n + pixelsize - 4;
        while (hnode.depth < target_depth) { hnode = lab->pyramid_up(hnode); }

        uint64_t dimension = (1ull << n);
        for (uint64_t y = 0; y < dimension; y++) {
            for (uint64_t x = 0; x < dimension; x++) {
                pops[(y << n) + x] = lab->getpop_recurse(lab->subnode(hnode, x, y, n), 1073750017, ((uint64_t) -1));
            }
        }
    }

257 258 259 260 261 262 263 264 265
    void SetCells(void* pat, int ncells, int64_t* coords, uint64_t* states) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto lab = ppat->getlab();
        auto newnode = lab->bror_recurse(lab->fromcells(ncells, coords, 0));
        ppat->changehnode(lab->boolean_universe(ppat->gethnode(), newnode, 3));
        auto newnode2 = lab->fromcells(ncells, coords, states);
        ppat->changehnode(lab->boolean_universe(ppat->gethnode(), newnode2, 1));
    }

266 267 268 269 270 271
    uint64_t GetPatternBound(void* pat, int direction, int pixelsize) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        auto lab = ppat->getlab();
        return lab->bound_recurse(ppat->gethnode(), direction, pixelsize);
    }

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    int64_t GetDXOfPattern(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->dx;
    }

    int64_t GetDYOfPattern(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->dy;
    }

    int64_t GetDTOfPattern(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->dt;
    }

287 288 289 290 291
    uint64_t GetOriginState(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->getcell(0, 0);
    }

292 293 294 295 296 297 298 299 300 301
    uint64_t GetBeszelIndex(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->gethnode().index;
    }

    uint64_t GetUlqomaIndex(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->gethnode().index2;
    }

302 303 304 305
    bool PatternNonempty(void* pat) {
        auto ppat = reinterpret_cast<apg::pattern*>(pat);
        return ppat->gethnode().nonempty();
    }
306

307 308 309 310 311
    bool PatternEquality(void* pat1, void* pat2) {
        auto ppat1 = reinterpret_cast<apg::pattern*>(pat1);
        auto ppat2 = reinterpret_cast<apg::pattern*>(pat2);
        return ((*ppat1) == (*ppat2));
    }
312
}