From 2fa265ac108e1d6387fe6b571cf0a497447821eb Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sat, 24 Nov 2018 17:01:49 +0100 Subject: [PATCH] implemented a better system for logging and binary output --- archimedean.cpp | 16 +-- config.cpp | 3 + conformal.cpp | 9 +- debug.cpp | 2 +- geometry2.cpp | 4 +- goldberg.cpp | 56 +++----- heptagon.cpp | 11 -- hyper.h | 188 +++++++++++++++++++++++---- hyperpoint.cpp | 27 +--- hyperroid/app/src/main/jni/hyper.cpp | 25 ++-- mapeditor.cpp | 182 +++++++++++--------------- netgen.cpp | 5 +- rogueviz-graph.cpp | 2 +- rogueviz-kohonen.cpp | 96 ++++++-------- rogueviz-pentagonal.cpp | 4 +- rug.cpp | 37 +++--- surface.cpp | 33 ++--- util.cpp | 13 +- 18 files changed, 374 insertions(+), 339 deletions(-) diff --git a/archimedean.cpp b/archimedean.cpp index 2e3d014f..1d2fd4e3 100644 --- a/archimedean.cpp +++ b/archimedean.cpp @@ -2,7 +2,7 @@ namespace hr { namespace arcm { -#define SDEBUG(x) if(debug_geometry) { doindent(); x; fflush(stdout); } +#define SDEBUG(x) if(debug_geometry) { x; fflush(stdout); } static const int sfPH = 1; static const int sfLINE = 2; @@ -563,16 +563,16 @@ void create_adjacent(heptagon *h, int d) { if(euclid) alt = encodeId(pair_to_vec(int(T[0][2]), int(T[1][2]))); - SDEBUG( printf("look for: %p / %s\n", alt, display(T * C0)); ) + SDEBUG( println(hlog, "look for: ", alt, " / ", T * C0); ) for(auto& p2: altmap[alt]) if(intval(p2.second * C0, T * C0) < 1e-4) { - SDEBUG( printf("cell found: %p\n", p2.first); ) + SDEBUG( println(hlog, "cell found: ", p2.first); ) for(int d2=0; d2degree(); d2++) { heptspin hs(p2.first, d2); auto& t2 = current.get_triangle(p2.first, d2); transmatrix T1 = T * spin(M_PI + t2.first); - SDEBUG( printf("compare: %s", display(T1 * xpush0(1))); ) - SDEBUG( printf(":: %s\n", display(p2.second * xpush0(1))); ) + SDEBUG( print(hlog, "compare: ", T1 * xpush0(1)); ) + SDEBUG( println(hlog, ":: ", p2.second * xpush0(1)); ) if(intval(T1 * xpush0(1), p2.second * xpush0(1)) < 1e-4) { // T1 = p2.second @@ -591,7 +591,7 @@ void create_adjacent(heptagon *h, int d) { return; } } - SDEBUG( printf("but rotation not found\n")); + SDEBUG( println(hlog, "but rotation not found")); } auto& t2 = current.get_triangle(current.get_adj(hi)); @@ -765,7 +765,7 @@ int readArgs() { archimedean_tiling at; shift(); at.parse(args()); if(at.errors) { - printf("error: %s\n", at.errormsg.c_str()); + println(hlog, "error: ", at.errormsg); } else { set_geometry(gArchimedean); @@ -1012,7 +1012,7 @@ void show() { archimedean_tiling tested; tested.parse(s); if(tested.errors) { - printf("WARNING: %d errors on %s '%s'\n", tested.errors, s.c_str(), tested.errormsg.c_str()); + println(hlog, "WARNING: ", tested.errors, " errors on ", s, " '", tested.errormsg, "'"); } else { tested.coloring = col; diff --git a/config.cpp b/config.cpp index 2452fe37..871820d7 100644 --- a/config.cpp +++ b/config.cpp @@ -26,6 +26,9 @@ charstyle& getcs(int id) { return vid.cs; } +void hread(hstream& hs, charstyle& cs) { hread_raw(hs, cs); } +void hwrite(hstream& hs, const charstyle& cs) { hwrite_raw(hs, cs); } + string csnameid(int id) { if(id == 0) return XLAT("male"); if(id == 1) return XLAT("female"); diff --git a/conformal.cpp b/conformal.cpp index d805595a..5e3a9214 100644 --- a/conformal.cpp +++ b/conformal.cpp @@ -348,13 +348,6 @@ namespace conformal { llv = ticks; phase = 0; - - if(0) - for(int j=0; j<=Q; j++) { - hyperpoint cur = v[j]->at * C0; - printf("%4d/%3d. %p [%3d] %s\n", j, Q, v[j]->base, celldist(v[j]->base), display(cur)); - } - } void movetophase() { @@ -516,7 +509,7 @@ namespace conformal { hyperpoint hscr; applymodel(last, hscr); ld bwidth = -current_display->radius * hscr[0]; - printf("bwidth = %lf/%lf\n", bwidth, len); + print(hlog, "bwidth = ", bwidth, "/", len); drawsegment: SDL_Surface *gr = glbuf.render(); diff --git a/debug.cpp b/debug.cpp index 644e0e56..a581bc09 100644 --- a/debug.cpp +++ b/debug.cpp @@ -336,7 +336,7 @@ void debugScreen() { dialog::addSelItem("celldistAlt", eubinary ? its(celldistAlt(mouseover)) : "--", 0); dialog::addSelItem("temporary", its(mouseover->listindex), 0); if(GOLDBERG) - dialog::addSelItem("whirl", gp::disp(gp::get_local_info(mouseover).relative), 0); + dialog::addSelItem("whirl", sprint(gp::get_local_info(mouseover).relative), 0); if(archimedean) dialog::addSelItem("ID", its(arcm::id_of(mouseover->master)), 0); dialog::addBreak(50); diff --git a/geometry2.cpp b/geometry2.cpp index 54e918c1..9faa18f8 100644 --- a/geometry2.cpp +++ b/geometry2.cpp @@ -220,8 +220,8 @@ transmatrix calc_relative_matrix_help(cell *c, heptagon *h1) { gm = gm * invheptmove[sp]; } } - printf("OK\n"); - display(gm * where); + println(hlog, "OK"); + println(hlog, gm * where); return gm * where; } diff --git a/goldberg.cpp b/goldberg.cpp index 63921ee1..57452476 100644 --- a/goldberg.cpp +++ b/goldberg.cpp @@ -133,22 +133,6 @@ namespace hr { namespace gp { return goldberg_map[c.second&31][c.first&31]; } - const char *disp(loc at) { - static char bufs[16][16]; - static int bufid; - bufid++; bufid %= 16; - snprintf(bufs[bufid], 16, "[%2d,%2d]", at.first, at.second); - return bufs[bufid]; - } - - const char *dcw(cellwalker cw) { - static char bufs[16][32]; - static int bufid; - bufid++; bufid %= 16; - snprintf(bufs[bufid], 32, "[%p/%2d:%d:%d]", cw.at, cw.at?cw.at->type:-1, cw.spin, cw.mirrored); - return bufs[bufid]; - } - int spawn; cell*& peek(cellwalker cw) { @@ -177,7 +161,7 @@ namespace hr { namespace gp { if(peek(wcw)) { auto wcw1 = get_localwalk(wc1, dir1); if(wcw + wstep != wcw1) { - WHD( Xprintf("%s : %s / %s (pull error from %s :: %s)\n", disp(at1), dcw(wcw+wstep), dcw(wcw1), disp(at), dcw(wcw)); ) + WHD( println(hlog, at1, " : ", (wcw+wstep), " / ", wcw1, " (pull error from ", at, " :: ", wcw, ")") ); exit(1); } } @@ -185,7 +169,7 @@ namespace hr { namespace gp { } if(peek(wcw)) { set_localwalk(wc1, dir1, wcw + wstep); - WHD( Xprintf("%s : %s (pulled from %s :: %s)\n", disp(at1), dcw(wcw + wstep), disp(at), dcw(wcw)); ) + WHD( println(hlog, at1, " :", wcw+wstep, " (pulled from ", at, " :: ", wcw, ")")); return true; } return false; @@ -195,12 +179,12 @@ namespace hr { namespace gp { auto& wc = get_mapping(at); auto wcw = get_localwalk(wc, dir); auto& wc1 = get_mapping(at + eudir(dir)); - WHD( Xprintf(" md:%02d s:%d", wc.mindir, wc.cw.spin); ) - WHD( Xprintf(" connection %s/%d %s=%s ~ %s/%d ", disp(at), dir, dcw(wc.cw+dir), dcw(wcw), disp(at+eudir(dir)), dir1); ) + WHD( print(hlog, format(" md:%02d s:%d", wc.mindir, wc.cw.spin)); ) + WHD( print(hlog, " connection ", at, "/", dir, " ", wc.cw+dir, "=", wcw, " ~ ", at+eudir(dir), "/", dir1); ) if(!wc1.cw.at) { wc1.start = wc.start; if(peek(wcw)) { - WHD( Xprintf("(pulled) "); ) + WHD( print(hlog, "(pulled) "); ) set_localwalk(wc1, dir1, wcw + wstep); } else { @@ -208,17 +192,17 @@ namespace hr { namespace gp { wcw.at->c.setspin(wcw.spin, 0, false); set_localwalk(wc1, dir1, wcw + wstep); spawn++; - WHD( Xprintf("(created) "); ) + WHD( print(hlog, "(created) "); ) } } WHD( Xprintf("%s ", dcw(wc1.cw+dir1)); ) auto wcw1 = get_localwalk(wc1, dir1); if(peek(wcw)) { if(wcw+wstep != wcw1) { - WHD( Xprintf("FAIL: %s / %s\n", dcw(wcw), dcw(wcw1)); exit(1); ) + WHD( println(hlog, "FAIL: ", wcw, " / ", wcw1); exit(1); ) } else { - WHD(Xprintf("(was there)\n");) + WHD( println(hlog, "(was there)\n");) } } else { @@ -226,7 +210,7 @@ namespace hr { namespace gp { peek(wcw) = wcw1.at; wcw.at->c.setspin(wcw.spin, wcw1.spin, wcw.mirrored != wcw1.mirrored); if(wcw+wstep != wcw1) { - Xprintf("assertion failed\n"); + println(hlog, "assertion failed"); exit(1); } } @@ -241,7 +225,7 @@ namespace hr { namespace gp { auto& ac0 = get_mapping(at); ac0.cw = cellwalker(hs.at->c7, hs.spin, hs.mirrored); ac0.start = at; - WHD( Xprintf("%s : %s\n", disp(at), dcw(ac0.cw)); ) + WHD( println(hlog, at, " : ", dcw(ac0.cw)); ) return ac0; } @@ -249,7 +233,7 @@ namespace hr { namespace gp { WHD( Xprintf("EXTEND %p %d\n", c, d); ) if(c->master->c7 != c) { while(c->master->c7 != c) { - WHD( Xprintf("%p direction 0 corresponds to %p direction %d\n", c, c->move(0), c->c.spin(0)); ) + WHD( println(hlog, c, " direction 0 corresponds to ", c->move(0), " direction ", c->c.spinm(0)); ) d = c->c.spin(0); c = c->move(0); } @@ -335,11 +319,11 @@ namespace hr { namespace gp { for(int i=0; i auto iprintf(T... t) { for(int i=0; i void save(T& c) { fwrite(&c, sizeof(T), 1, f); } +struct hstream { + virtual void write_char(char c) = 0; + virtual void write_chars(const char* c, size_t q) { while(q--) write_char(*(c++)); } + virtual char read_char() = 0; + virtual void read_chars(char* c, size_t q) { while(q--) *(c++) = read_char(); } - inline char loadChar() { char c; int i=fread(&c, 1, 1, f); if(i!=1) return 0; else return c; } - template int load(T& c) { return fread(&c, sizeof(T), 1, f); } - inline int32_t loadInt() { int i; if(load(i) < 1) return -1; else return i; } - void saveString(const string& s); - string loadString(); + template void write(const T& t) { hwrite(*this, t); } + template void read(T& t) { hread(*this, t); } + template T get() { T t; hread(*this, t); return t; } + template T get_raw() { T t; hread_raw(*this, t); return t; } + }; + +template void hwrite_raw(hstream& hs, const T& c) { hs.write_chars((char*) &c, sizeof(T)); } +template void hread_raw(hstream& hs, T& c) { hs.read_chars((char*) &c, sizeof(T)); } + +template::value || std::is_enum::value>::type> void hwrite(hstream& hs, const T& c) { hwrite_raw(hs, c); } +template::value || std::is_enum::value>::type> void hread(hstream& hs, T& c) { hread_raw(hs, c); } + +inline void hwrite(hstream& hs, const string& s) { hs.write_char(isize(s)); for(char c: s) hs.write_char(c); } +inline void hread(hstream& hs, string& s) { s = ""; int l = (unsigned char) hs.read_char(); for(int i=0; i void hwrite(hstream& hs, const array& a) { for(auto &ae: a) hwrite(hs, ae); } +template void hread(hstream& hs, array& a) { for(auto &ae: a) hread(hs, ae); } + +template void hwrite(hstream& hs, const vector& a) { hwrite(hs, isize(a)); for(auto &ae: a) hwrite(hs, ae); } +template void hread(hstream& hs, vector& a) { a.resize(hs.get()); for(auto &ae: a) hread(hs, ae); } + +template void hwrite(hstream& hs, const map& a) { + hwrite(hs, isize(a)); for(auto &ae: a) hwrite(hs, ae.first, ae.second); } +template void hread(hstream& hs, map& a) { + a.clear(); + int N = hs.get(); + for(int i=0; i void hwrite(hstream& hs, const C& c, const C1& c1, const CS&... cs) { hwrite(hs, c); hwrite(hs, c1, cs...); } +template void hread(hstream& hs, C& c, C1& c1, CS&... cs) { hread(hs, c); hread(hs, c1, cs...); } + +struct hstream_exception : std::exception { hstream_exception() {} }; + +struct fhstream : hstream { + FILE *f; + virtual void write_char(char c) { write_chars(&c, 1); } + virtual void write_chars(char* c, size_t i) { if(fwrite(c, i, 1, f) != 1) throw hstream_exception(); } + virtual void read_chars(char* c, size_t i) { if(fread(c, i, 1, f) != 1) throw hstream_exception(); } + virtual char read_char() { char c; read_chars(&c, 1); return c; } + fhstream() { f = NULL; } + fhstream(const string pathname, const char *mode) { f = fopen(pathname.c_str(), mode); } + ~fhstream() { if(f) fclose(f); } + }; + +struct shstream : hstream { + string s; + int pos; + shstream() { pos = 0; } + virtual void write_char(char c) { s += c; } + virtual char read_char() { if(pos == isize(s)) throw hstream_exception(); return s[pos++]; } + }; + +inline void print(hstream& hs) {} + +template string sprint(const CS&... cs) { shstream hs; print(hs, cs...); return hs.s; } + +template void print(hstream& hs, const C& c, const C1& c1, const CS&... cs) { print(hs, c); print(hs, c1, cs...); } + +template void println(hstream& hs, const CS&... cs) { print(hs, cs...); hs.write_char('\n'); } + +// copied from: https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element + +namespace detail +{ + template + struct seq { }; + + template + struct gen_seq : gen_seq { }; + + template + struct gen_seq<0, Is...> : seq { }; + + template + void for_each(T&& t, F f, seq) + { + auto l = { (f(std::get(t)), 0)... }; ignore(l); + } +} + +template +void for_each_in_tuple(std::tuple const& t, F f) +{ + detail::for_each(t, f, detail::gen_seq()); +} + +inline void print(hstream& hs, const string& s) { hs.write_chars(s.c_str(), isize(s)); } +inline void print(hstream& hs, int i) { print(hs, its(i)); } +inline void print(hstream& hs, ld x) { print(hs, fts(x)); } + +struct comma_printer { + bool first; + hstream& hs; + template void operator() (const T& t) { if(first) first = false; else print(hs, ","); print(hs, t); } + comma_printer(hstream& hs) : first(true), hs(hs) {} + }; + +template void print(hstream& hs, const array& a) { print(hs, "("); comma_printer c(hs); for(const T& t: a) c(t); print(hs, ")"); } +template void print(hstream& hs, const vector& a) { print(hs, "("); comma_printer c(hs); for(const T& t: a) c(t); print(hs, ")"); } + +inline void print(hstream& hs, const hyperpoint h) { print(hs, (const array&)h); } +inline void print(hstream& hs, const transmatrix T) { + print(hs, "("); comma_printer c(hs); + for(int i=0; i<3; i++) + for(int j=0; j<3; j++) c(T[i][j]); + print(hs, ")"); } + +template void print(hstream& hs, const pair & t) { print(hs, "(", t.first, ",", t.second, ")"); } + +template void print(hstream& hs, const tuple & t) { + print(hs, "("); + comma_printer p(hs); + for_each_in_tuple(t, p); + print(hs, ")"); + } + +#ifndef SPECIAL_LOGGER +inline void special_log(char c) { putchar(c); } +#endif + +struct logger : hstream { + int indentation; + virtual void write_char(char c) { special_log(c); if(c == 10) for(int i=0; itype, ":", cw.spin, ":", cw.mirrored, "]"); + else print(hs, "[NULL]"); + } + +struct indenter { + dynamicval ind; + + indenter(int i = 2) : ind(hlog.indentation, hlog.indentation + (i)) {} + }; void appendHelp(string s); diff --git a/hyperpoint.cpp b/hyperpoint.cpp index d2ed0449..890fcecc 100644 --- a/hyperpoint.cpp +++ b/hyperpoint.cpp @@ -179,13 +179,6 @@ ld zlevel(const hyperpoint &h) { else return (h[2] < 0 ? -1 : 1) * sqrt(-intval(h, Hypc)); } -// display a hyperbolic point -char *display(const hyperpoint& H) { - static char buf[100]; - sprintf(buf, "%8.4f:%8.4f:%8.4f", double(H[0]), double(H[1]), double(H[2])); - return buf; - } - ld hypot_auto(ld x, ld y) { switch(cgclass) { case gcEuclid: @@ -434,23 +427,6 @@ void fixmatrix(transmatrix& T) { // show the matrix on screen -void display(const transmatrix& T) { - for(int y=0; y<3; y++) { - for(int x=0; x<3; x++) printf("%10.7f", double(T[y][x])); - printf(" -> %10.7f\n", double(squar(T[y][0]) + squar(T[y][1]) + sig(2) * squar(T[y][2]))); - // printf("\n"); - } - - for(int x=0; x<3; x++) printf("%10.7f", double(squar(T[0][x]) + squar(T[1][x]) + sig(2) * squar(T[2][x]))); - printf("\n"); - - for(int x=0; x<3; x++) { - int y = (x+1) % 3; - printf("%10.7f", double(T[0][x]*T[0][y] + T[1][x]*T[1][y] + sig(2) * T[2][x]*T[2][y])); - } - printf("\n\n"); - } - ld det(const transmatrix& T) { ld det = 0; for(int i=0; i<3; i++) @@ -461,8 +437,7 @@ ld det(const transmatrix& T) { } void inverse_error(const transmatrix& T) { - printf("Warning: inverting a singular matrix\n"); - display(T); + println(hlog, "Warning: inverting a singular matrix: ", T); } transmatrix inverse(const transmatrix& T) { diff --git a/hyperroid/app/src/main/jni/hyper.cpp b/hyperroid/app/src/main/jni/hyper.cpp index 0400128a..f37c4e7f 100644 --- a/hyperroid/app/src/main/jni/hyper.cpp +++ b/hyperroid/app/src/main/jni/hyper.cpp @@ -33,21 +33,18 @@ char android_log_buf[1000000]; int android_log_bufpos = 0; -#define XPRINTF -#pragma clang diagnostic ignored "-Wformat-security" -template -// __attribute__((__format__ (__printf__, 1, 2))) -void Xprintf(const char *fmt, T... t) { - snprintf(android_log_buf+android_log_bufpos, 1000000-android_log_bufpos, fmt, t...); - int last_cr = 0; - int i; - for(i=0; android_log_buf[i]; i++) if(android_log_buf[i] == 10) { - android_log_buf[i] = 0; - __android_log_print(ANDROID_LOG_VERBOSE, "RUG", "%s", android_log_buf+last_cr); - last_cr = i+1; + +#define SPECIAL_LOGGER + +void special_log(char c) { + if(c == 10 || android_log_bufpos == 999999) { + android_log_buf[android_log_bufpos] = 0; + __android_log_print(ANDROID_LOG_VERBOSE, "HRLOG", "%s", android_log_buf); + android_log_bufpos = 0; + } + else { + android_log_buf[android_log_bufpos++] = c; } - android_log_bufpos = 0; - while(i > last_cr) android_log_buf[android_log_bufpos++] = android_log_buf[last_cr++]; } #include diff --git a/mapeditor.cpp b/mapeditor.cpp index 70e5e201..bf4451c5 100644 --- a/mapeditor.cpp +++ b/mapeditor.cpp @@ -74,34 +74,9 @@ namespace mapeditor { #endif } -namespace mapstream { - FILE *f; - - void savePoint(const hyperpoint& h) { - for(int i=0; i<3; i++) { double x = h[i]; save(x); } - } - - hyperpoint loadPoint() { - hyperpoint h; - for(int i=0; i<3; i++) { double x; load(x); h[i] = x; } - return h; - } - - void saveString(const string& s) { - saveChar(isize(s)); - for(char c: s) saveChar(c); - } - - string loadString() { - string s; - int l = loadChar(); - for(int i=0; i cellids; vector cellbyid; vector relspin; @@ -115,31 +90,31 @@ namespace mapstream { } bool saveMap(const char *fname) { - f = fopen(fname, "wb"); - if(!f) return false; - int32_t i = VERNUM; save(i); - save(patterns::whichPattern); - save(geometry); + fhstream f(fname, "wb"); + if(!f.f) return false; + int32_t i = VERNUM; f.write(i); + f.write(patterns::whichPattern); + f.write(geometry); char nbtype = char(variation); - save(nbtype); + f.write(nbtype); if(GOLDBERG) { - save(gp::param.first); - save(gp::param.second); + f.write(gp::param.first); + f.write(gp::param.second); } if(geometry == gTorus) { - save(torusconfig::qty); - save(torusconfig::dx); - save(torusconfig::dy); + f.write(torusconfig::qty); + f.write(torusconfig::dx); + f.write(torusconfig::dy); } if(geometry == gFieldQuotient) { using namespace fieldpattern; - save(quotient_field_changed); + f.write(quotient_field_changed); if(quotient_field_changed) { - save(current_extra); - save(fgeomextras[current_extra].current_prime_id); + f.write(current_extra); + f.write(fgeomextras[current_extra].current_prime_id); } } - if(geometry == gArchimedean) saveString(arcm::current.symbol); + if(geometry == gArchimedean) f.write(arcm::current.symbol); addToQueue((bounded || euclid) ? currentmap->gamestart() : cwt.at->master->c7); for(int i=0; itype; j++) if(c->move(j) && cellids.count(c->move(j)) && cellids[c->move(j)] < i) { int32_t i = cellids[c->move(j)]; - save(i); - saveChar(c->c.spin(j)); - saveChar(j); + f.write(i); + f.write_char(c->c.spin(j)); + f.write_char(j); break; } } - saveChar(c->land); - saveChar(c->mondir); - saveChar(c->monst); - saveChar(c->wall); - // saveChar(c->barleft); - // saveChar(c->barright); - saveChar(c->item); - saveChar(c->mpdist); - // saveChar(c->bardir); - save(c->wparam); save(c->landparam); - saveChar(c->stuntime); saveChar(c->hitpoints); + f.write_char(c->land); + f.write_char(c->mondir); + f.write_char(c->monst); + f.write_char(c->wall); + // f.write_char(c->barleft); + // f.write_char(c->barright); + f.write_char(c->item); + f.write_char(c->mpdist); + // f.write_char(c->bardir); + f.write(c->wparam); f.write(c->landparam); + f.write_char(c->stuntime); f.write_char(c->hitpoints); for(int j=0; jtype; j++) { cell *c2 = c->move(j); if(c2 && c2->land != laNone) addToQueue(c2); } } printf("cells saved = %d\n", isize(cellbyid)); - int32_t n = -1; save(n); + int32_t n = -1; f.write(n); int32_t id = cellids.count(cwt.at) ? cellids[cwt.at] : -1; - save(id); + f.write(id); for(int i=0; id[l].list)) { usershapelayer& ds(us->d[l]); - save(i); save(usp.first); save(l); save(ds.sym); save(ds.rots); save(ds.color); - n = isize(ds.list); save(n); - savePoint(ds.shift); - savePoint(ds.spin); - for(int i=0; i= 7400) load(patterns::whichPattern); + int vernum = f.get(); + if(vernum >= 7400) f.read(patterns::whichPattern); if(vernum >= 10203) { - load(geometry); + f.read(geometry); char nbtype; - load(nbtype); + f.read(nbtype); variation = eVariation(nbtype); if(GOLDBERG) { - load(gp::param.first); - load(gp::param.second); + f.read(gp::param.first); + f.read(gp::param.second); } if(geometry == gTorus) { - load(torusconfig::qty); - load(torusconfig::dx); - load(torusconfig::dy); + f.read(torusconfig::qty); + f.read(torusconfig::dx); + f.read(torusconfig::dy); } if(geometry == gFieldQuotient) { using namespace fieldpattern; - load(quotient_field_changed); + f.read(quotient_field_changed); if(quotient_field_changed) { - load(current_extra); - load(fgeomextras[current_extra].current_prime_id); + f.read(current_extra); + f.read(fgeomextras[current_extra].current_prime_id); enableFieldChange(); } } if(geometry == gArchimedean) { string& symbol = arcm::current.symbol; - symbol = loadString(); + symbol = f.get(); arcm::current.parse(); if(arcm::current.errors > 0) { printf("Errors! %s\n", arcm::current.errormsg.c_str()); @@ -256,48 +230,48 @@ namespace mapstream { rspin = 0; } else { - int32_t parent = loadInt(); + int32_t parent = f.get(); if(parent<0 || parent >= isize(cellbyid)) break; - int dir = loadChar(); + int dir = f.read_char(); cell *c2 = cellbyid[parent]; dir = fixspin(dir, relspin[parent], c2->type); c = createMov(c2, dir); // printf("%p:%d,%d -> %p\n", c2, relspin[parent], dir, c); // spinval becomes xspinval - rspin = (c2->c.spin(dir) - loadChar() + MODFIXER) % c->type; + rspin = (c2->c.spin(dir) - f.read_char() + MODFIXER) % c->type; } cellbyid.push_back(c); relspin.push_back(rspin); - c->land = (eLand) loadChar(); - c->mondir = fixspin(rspin, loadChar(), c->type); - c->monst = (eMonster) loadChar(); - c->wall = (eWall) loadChar(); - // c->barleft = (eLand) loadChar(); - // c->barright = (eLand) loadChar(); - c->item = (eItem) loadChar(); - c->mpdist = loadChar(); + c->land = (eLand) f.read_char(); + c->mondir = fixspin(rspin, f.read_char(), c->type); + c->monst = (eMonster) f.read_char(); + c->wall = (eWall) f.read_char(); + // c->barleft = (eLand) f.read_char(); + // c->barright = (eLand) f.read_char(); + c->item = (eItem) f.read_char(); + c->mpdist = f.read_char(); c->bardir = NOBARRIERS; - // fixspin(rspin, loadChar(), c->type); + // fixspin(rspin, f.read_char(), c->type); if(vernum < 7400) { short z; - load(z); + f.read(z); c->wparam = z; } - else load(c->wparam); - load(c->landparam); + else f.read(c->wparam); + f.read(c->landparam); // backward compatibility if(vernum < 7400 && !isIcyLand(c->land)) c->landparam = HEAT(c); - c->stuntime = loadChar(); - c->hitpoints = loadChar(); + c->stuntime = f.read_char(); + c->hitpoints = f.read_char(); if(patterns::whichPattern) mapeditor::modelcell[patterns::getpatterninfo0(c).id] = c; } - int32_t whereami = loadInt(); + int32_t whereami = f.get(); if(whereami >= 0 && whereami < isize(cellbyid)) cwt.at = cellbyid[whereami]; else cwt.at = currentmap->gamestart(); @@ -330,24 +304,24 @@ namespace mapstream { cheater = 1; if(vernum >= 7400) while(true) { - int i = loadInt(); + int i = f.get(); if(i == -1) break; - int j = loadInt(), l = loadInt(); + int j = f.get(), l = f.get(); if(i<0 || i >= mapeditor::USERSHAPEGROUPS) break; if(l<0 || l >= USERLAYERS) break; initShape(i, j); usershapelayer& ds(usershapes[i][j]->d[l]); - load(ds.sym); load(ds.rots); load(ds.color); + f.read(ds.sym); f.read(ds.rots); f.read(ds.color); ds.list.clear(); - int siz = loadInt(); + int siz = f.get(); - ds.shift = loadPoint(); - ds.spin = loadPoint(); + ds.shift = f.get(); + ds.spin = f.get(); for(int i=0; i()); } buildpolys(); diff --git a/netgen.cpp b/netgen.cpp index 4392a4d3..6603b7fe 100644 --- a/netgen.cpp +++ b/netgen.cpp @@ -360,8 +360,7 @@ namespace hr { namespace netgen { if(0) if((i==0 && e == 0) || (i == zeroi && e == zeroe)) { for(int ofs=0; ofs(f, weights[i]); // save neurons - int N = isize(net); - mapstream::save(N); - for(int i=0; i(isize(net)); + for(int i=0; i(f, net[i].net[j]); // save shown samples map saved_id; - int ss = isize(sample_vdata_id); - mapstream::save(ss); + f.write(isize(sample_vdata_id)); int index = 0; for(auto p: sample_vdata_id) { int i = p.first; - for(int j=0; j(f, data[i].val[j]); + f.write(data[i].name); int id = p.second; saved_id[id] = index++; auto& vd = vdata[id]; - mapstream::save(vd.cp); + hwrite_raw(f, vd.cp); } // save edge types - int qet = isize(edgetypes); - mapstream::save(qet); + f.write(isize(edgetypes)); for(auto&et: edgetypes) { - mapstream::saveString(et->name); - saveFloat(et->visible_from); - mapstream::save(et->color); + f.write(et->name); + hwrite_raw(f, et->visible_from); + f.write(et->color); } // save edge infos - int qei = isize(edgeinfos); - mapstream::save(qei); + f.write(isize(edgeinfos)); for(auto& ei: edgeinfos) { for(int x=0; xtype == &*edgetypes[x]) mapstream::saveChar(x); - mapstream::save(saved_id[ei->i]); - mapstream::save(saved_id[ei->j]); - saveFloat(ei->weight); + if(ei->type == &*edgetypes[x]) f.write_char(x); + f.write(saved_id[ei->i]); + f.write(saved_id[ei->j]); + hwrite_raw(f, ei->weight); } - fclose(mapstream::f); } void load_compressed(string name) { // save everything in compressed form - mapstream::f = fopen(name.c_str(), "rb"); - if(!mapstream::f) { + fhstream f(name, "rb"); + if(!f.f) { printf("failed to open for load_compressed: %s\n", name.c_str()); return; } // load columns - mapstream::load(columns); + f.read(columns); colnames.resize(columns); - for(int i=0; i(); samples = 0; sominit(1, true); // load neurons - int N = mapstream::loadInt(); + int N = f.get(); if(cells != N) { fprintf(stderr, "Error: bad number of cells (N=%d c=%d)\n", N, cells); exit(1); } for(neuron& n: net) for(int k=0; k(); // load data - samples = mapstream::loadInt(); + samples = f.get(); data.resize(samples); int id = 0; for(auto& d: data) { alloc(d.val); - for(int j=0; j(); + f.read(d.name); int i = vdata.size(); sample_vdata_id[id] = i; vdata.emplace_back(); auto& v = vdata.back(); v.name = data[i].name; - mapstream::load(v.cp); + hread_raw(f, v.cp); createViz(i, cwt.at, Id); v.m->store(); id++; } // load edge types - int qet = mapstream::loadInt(); + int qet = f.get(); for(int i=0; ivisible_from = loadFloat(); - mapstream::load(et->color); + auto et = add_edgetype(f.get()); + et->visible_from = f.get_raw(); + f.read(et->color); } // load edge infos - int qei = mapstream::loadInt(); + int qei = f.get(); for(int i=0; i(); + int ej = f.get(); + float w = f.get_raw(); addedge(ei, ej, w, true, &*t); } - fclose(mapstream::f); analyze(); } diff --git a/rogueviz-pentagonal.cpp b/rogueviz-pentagonal.cpp index f4dbece6..900d6eba 100644 --- a/rogueviz-pentagonal.cpp +++ b/rogueviz-pentagonal.cpp @@ -173,11 +173,11 @@ void run_snub(int v, int w) { for(auto p: sideangles) sav.push_back(p.first); sort(sav.begin(), sav.end()); - printf("sideangles "); for(int i=0; i<3; i++) printf("%lf ", double(sav[i])); printf("\n"); + println(hlog, "sideangles ", sav); xts0 = tC0(ts[0]); - printf("original %s\n", display(xts0)); + println(hlog, "original ", xts0); cor = rel * gmatrix[cc] * C0; diff --git a/rug.cpp b/rug.cpp index 14afecaf..be5d4085 100644 --- a/rug.cpp +++ b/rug.cpp @@ -95,7 +95,7 @@ transmatrix orthonormalize(hyperpoint h1, hyperpoint h2) { hyperpoint azeq_to_hyperboloid(hyperpoint h) { if(abs(h[2])>1e-4) { - Xprintf("Error: h[2] = %lf\n", h[2]); + println(hlog, "Error: h[2] = ", h[2]); rug_failure = true; } if(euclid) { @@ -415,7 +415,7 @@ void buildTorusRug() { ld xfactor = 0, yfactor = 0; - Xprintf("factor = %lf\n", factor); + println(hlog, "factor = ", factor); if(factor <= 2.05) factor = 2.2; factor -= 1; @@ -439,10 +439,10 @@ void buildTorusRug() { ld xscale = hypot(xfactor * xh[0] * 2 * M_PI, yfactor * xh[1] * 2 * M_PI); ld yscale = hypot(xfactor * yh[0] * 2 * M_PI, yfactor * yh[1] * 2 * M_PI); - printf("xh = %s\n", display(xh)); - printf("yh = %s\n", display(yh)); - printf("factor = %lf %lf (%lf)\n", double(xfactor), double(yfactor), factor); - printf("scales = %fl %lf\n", double(xscale), double(yscale)); + println(hlog, "xh = ", xh); + println(hlog, "yh = ", yh); + println(hlog, "factor = ", make_tuple(xfactor, yfactor, factor)); + println(hlog, "scales = ", make_tuple(xscale, yscale)); modelscale = xscale / crossf; } @@ -536,7 +536,7 @@ void buildTorusRug() { qvalid = 0; for(auto p: points) if(!p->glue) qvalid++; - Xprintf("qvalid = %d\n", qvalid); + println(hlog, "qvalid = ", qvalid); if(rug_perspective) push_all_points(2, -model_distance); @@ -558,11 +558,11 @@ void verify() { ratios.push_back(l0 / l); } - Xprintf("%s", "Length verification:\n"); + println(hlog, "Length verification:"); sort(ratios.begin(), ratios.end()); for(int i=0; i otriangles = triangles; @@ -866,8 +866,7 @@ void subdivide() { calcLengths(); - Xprintf("result (%d,%d)\n", isize(points), isize(triangles)); - + println(hlog, "result ", make_tuple(isize(points), isize(triangles))); } ld slow_modeldist(const hyperpoint& h1, const hyperpoint& h2) { @@ -953,7 +952,7 @@ int detect_cusps() { max_edge_length = max(max_edge_length, e.len); anticusp_dist = anticusp_factor * max_edge_length; - int stats[3] = {0,0,0}; + array stats = {0,0,0}; map > code_to_point; for(auto p: points) if(p->valid) @@ -983,7 +982,7 @@ int detect_cusps() { printf("cusp stats: %d/%d/%d | %d/%d/%d\n", stats[0], stats[1], stats[2], stats2[0], stats2[1], stats2[2]); */ - Xprintf("cusp stats: %d/%d/%d\n", stats[0], stats[1], stats[2]); + println(hlog, "cusp stats: ", stats); return stats[2]; } @@ -1014,7 +1013,7 @@ void addNewPoints() { enqueue(&m); } } - if(qvalid != oqvalid) { Xprintf("adding new points %4d %4d %4d %.9lf %9d %9d\n", oqvalid, qvalid, isize(points), dist, dt, queueiter); } + if(qvalid != oqvalid) { println(hlog, "adding new points ", make_tuple(oqvalid, qvalid, isize(points), dist, dt, queueiter)); } } void physics() { diff --git a/surface.cpp b/surface.cpp index 07662834..9588b3d8 100644 --- a/surface.cpp +++ b/surface.cpp @@ -283,40 +283,41 @@ transmatrix create_M_matrix(hyperpoint zero, hyperpoint v1) { transmatrix M = build_matrix(v1, v2, Hypc); - display(M); + println(hlog, M); - printf("M matrix test: %lf %lf %lf %lf\n", hypot3(T*M*unit_vector[0]), hypot3(T*M*unit_vector[1]), hypot3(T*M*(unit_vector[0]+unit_vector[1])), - ((T*M*unit_vector[0]) | (T*M*unit_vector[1]))); + println(hlog, "M matrix test: ", + make_tuple(hypot3(T*M*unit_vector[0]), hypot3(T*M*unit_vector[1]), hypot3(T*M*(unit_vector[0]+unit_vector[1])), + ((T*M*unit_vector[0]) | (T*M*unit_vector[1])))); return M; } dexp_origin at_zero(hyperpoint zero, transmatrix start) { - printf("zero = %s\n", display(zero)); + println(hlog, "zero = ", zero); - printf("curvature at zero = %lf\n", compute_curvature(zero)); - printf("curvature at X1 = %lf\n", compute_curvature(zero + hpxyz(.3, 0, 0))); - printf("curvature at X2 = %lf\n", compute_curvature(zero + hpxyz(0, .3, 0))); - printf("curvature at X3 = %lf\n", compute_curvature(zero + hpxyz(.4, .3, 0))); + println(hlog, "curvature at zero = ", compute_curvature(zero)); + println(hlog, "curvature at X1 = ", compute_curvature(zero + hpxyz(.3, 0, 0))); + println(hlog, "curvature at X2 = ", compute_curvature(zero + hpxyz(0, .3, 0))); + println(hlog, "curvature at X3 = ", compute_curvature(zero + hpxyz(.4, .3, 0))); return {start, create_M_matrix(zero, unit_vector[0]), zero}; } dexp_origin at_other(dexp_origin& o1, hyperpoint h) { - printf("\n\nmapping %s...\n", display(h)); - display(o1.H); display(o1.M); + println(hlog, "\n\nmapping ", h, "..."); + println(hlog, o1.H, o1.M); auto dd = map_to_surface(h, o1); hyperpoint newzero = dd.params; - printf("error = %lf\n", dd.remaining_distance); + println(hlog, "error = ", dd.remaining_distance); transmatrix Spin = spintox(o1.H * h); transmatrix T = pushxto0(Spin * o1.H * h) * Spin; - printf("h is = %s\n", display(h)); - printf("T*c0 is = %s\n", display(T * C0)); - printf("T*h is = %s\n", display(T * o1.H * h)); + println(hlog, "h is = ", h); + println(hlog, "T*c0 is = ", T * C0); + println(hlog, "T*h is = ", T * o1.H * h); return {T * o1.H, create_M_matrix(newzero, dd.cont), newzero}; } @@ -388,7 +389,7 @@ void draw_kuen_map() { px = 0xFF000000 + (((int)(n*255/nmax)) * (kuen_branch(v,u) == 1 ? 0x10101 : 0x10001)); } } - printf("nmax = %lf\n", nmax); + println(hlog, "nmax = ", nmax); } for(auto p: rug::points) { @@ -635,7 +636,7 @@ void run_shape(eShape s) { rug::qvalid++; } - printf("minx = %lf maxx = %lf\n", minx, maxx); + println(hlog, "minx = ", minx, " maxx = ", maxx); ld shift = -(minx + maxx) / 2; for(auto p: rug::points) if(p->valid) diff --git a/util.cpp b/util.cpp index 2328d98f..c8007607 100644 --- a/util.cpp +++ b/util.cpp @@ -140,17 +140,6 @@ bool appears(const string& haystack, const string& needle) { return simplify(haystack).find(simplify(needle)) != string::npos; } -/* indenter */ - -int current_indentation; - -struct indenter { - indenter() { current_indentation += 2; } - ~indenter() { current_indentation -= 2; } - }; - -void doindent() { for(int i=0; i