X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=binloader.cpp;h=a0304561c0c5ee7dfb0be31e6465a7a6315b0611;hb=f95dfda2d14615a9816ba1abb32bd240a470e228;hp=2ed60cbe78250ff34eba9f7c358968f90c4ddb97;hpb=756a91677c1565b1bd555722ea795f911106104f;p=remoteglot-book diff --git a/binloader.cpp b/binloader.cpp index 2ed60cb..a030456 100644 --- a/binloader.cpp +++ b/binloader.cpp @@ -19,11 +19,26 @@ using namespace std; -Arena arena; +static int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2) +{ + int shared_len = min(n1, n2); + int s = memcmp(s1, s2, shared_len); + if (s != 0) { + return s; + } + + if (n1 < n2) { + return -1; + } else if (n1 > n2) { + return 1; + } else { + return 0; + } +} enum Result { WHITE = 0, DRAW, BLACK }; struct Element { - char *bpfen; + char *bpfen; // includes prev_board_hash int bpfen_len; char move[8]; // Na1xc3+ Result result; @@ -33,15 +48,8 @@ struct Element { long start_position; bool operator< (const Element& other) const { - int shared_len = min(bpfen_len, other.bpfen_len); - int s = memcmp(bpfen, other.bpfen, shared_len); - if (s < 0) { - return true; - } else if (s > 0) { - return false; - } else { - return bpfen_len < other.bpfen_len; - } + int s = memcmp_different_len(bpfen, bpfen_len, other.bpfen, other.bpfen_len); + return s < 0; } };