1 2 3 4 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 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 441 442 443 444 445 446 447 448 449 450 451 452 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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
|
// generic useful stuff for any C++ program
#ifndef _TOOLS_H
#define _TOOLS_H
#ifdef NULL
#undef NULL
#endif
#define NULL 0
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef signed long long int llong;
typedef unsigned long long int ullong;
#ifdef _DEBUG
#define ASSERT(c) assert(c)
#else
#define ASSERT(c) if(c) {}
#endif
#if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1400)
#define RESTRICT __restrict
#else
#define RESTRICT
#endif
inline void *operator new(size_t size)
{
void *p = malloc(size);
if(!p) abort();
return p;
}
inline void *operator new[](size_t size)
{
void *p = malloc(size);
if(!p) abort();
return p;
}
inline void operator delete(void *p) { if(p) free(p); }
inline void operator delete[](void *p) { if(p) free(p); }
inline void *operator new(size_t, void *p) { return p; }
inline void *operator new[](size_t, void *p) { return p; }
inline void operator delete(void *, void *) {}
inline void operator delete[](void *, void *) {}
#ifdef swap
#undef swap
#endif
template<class T>
static inline void swap(T &a, T &b)
{
T t = a;
a = b;
b = t;
}
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
template<class T>
static inline T max(T a, T b)
{
return a > b ? a : b;
}
template<class T>
static inline T min(T a, T b)
{
return a < b ? a : b;
}
template<class T, class U>
static inline T clamp(T a, U b, U c)
{
return max(T(b), min(a, T(c)));
}
#define rnd(x) ((int)(randomMT()&0xFFFFFF)%(x))
#define rndscale(x) (float((randomMT()&0xFFFFFF)*double(x)/double(0xFFFFFF)))
#define detrnd(s, x) ((int)(((((uint)(s))*1103515245+12345)>>16)%(x)))
#define loop(v,m) for(int v = 0; v<int(m); v++)
#define loopi(m) loop(i,m)
#define loopj(m) loop(j,m)
#define loopk(m) loop(k,m)
#define loopl(m) loop(l,m)
#define loopirev(v) for(int i = v-1; i>=0; i--)
#define DELETEP(p) if(p) { delete p; p = 0; }
#define DELETEA(p) if(p) { delete[] p; p = 0; }
#define PI (3.1415927f)
#define PI2 (2*PI)
#define SQRT2 (1.4142136f)
#define SQRT3 (1.7320508f)
#define RAD (PI / 180.0f)
#ifdef WIN32
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_LN2
#define M_LN2 0.693147180559945309417
#endif
#ifndef __GNUC__
#pragma warning (3: 4189) // local variable is initialized but not referenced
#pragma warning (disable: 4244) // conversion from 'int' to 'float', possible loss of data
#pragma warning (disable: 4267) // conversion from 'size_t' to 'int', possible loss of data
#pragma warning (disable: 4355) // 'this' : used in base member initializer list
#pragma warning (disable: 4996) // 'strncpy' was declared deprecated
#endif
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define PATHDIV '\\'
#else
#define __cdecl
#define _vsnprintf vsnprintf
#define PATHDIV '/'
#endif
#ifdef __GNUC__
#define PRINTFARGS(fmt, args) __attribute__((format(printf, fmt, args)))
#else
#define PRINTFARGS(fmt, args)
#endif
// easy safe strings
#define MAXSTRLEN 260
typedef char string[MAXSTRLEN];
inline void vformatstring(char *d, const char *fmt, va_list v, int len = MAXSTRLEN) { _vsnprintf(d, len, fmt, v); d[len-1] = 0; }
inline char *copystring(char *d, const char *s, size_t len = MAXSTRLEN) { strncpy(d, s, len); d[len-1] = 0; return d; }
inline char *concatstring(char *d, const char *s, size_t len = MAXSTRLEN) { size_t used = strlen(d); return used < len ? copystring(d+used, s, len-used) : d; }
struct stringformatter
{
char *buf;
stringformatter(char *buf): buf((char *)buf) {}
void operator()(const char *fmt, ...) PRINTFARGS(2, 3)
{
va_list v;
va_start(v, fmt);
vformatstring(buf, fmt, v);
va_end(v);
}
};
#define formatstring(d) stringformatter((char *)d)
#define defformatstring(d) string d; formatstring(d)
#define defvformatstring(d,last,fmt) string d; { va_list ap; va_start(ap, last); vformatstring(d, fmt, ap); va_end(ap); }
#define loopv(v) for(int i = 0; i<(v).length(); i++)
#define loopvj(v) for(int j = 0; j<(v).length(); j++)
#define loopvk(v) for(int k = 0; k<(v).length(); k++)
#define loopvrev(v) for(int i = (v).length()-1; i>=0; i--)
template <class T>
struct databuf
{
enum
{
OVERREAD = 1<<0,
OVERWROTE = 1<<1
};
T *buf;
int len, maxlen;
uchar flags;
databuf() : buf(NULL), len(0), maxlen(0), flags(0) {}
template<class U>
databuf(T *buf, U maxlen) : buf(buf), len(0), maxlen((int)maxlen), flags(0) {}
const T &get()
{
static T overreadval = 0;
if(len<maxlen) return buf[len++];
flags |= OVERREAD;
return overreadval;
}
databuf subbuf(int sz)
{
sz = min(sz, maxlen-len);
len += sz;
return databuf(&buf[len-sz], sz);
}
void put(const T &val)
{
if(len<maxlen) buf[len++] = val;
else flags |= OVERWROTE;
}
void put(const T *vals, int numvals)
{
if(maxlen-len<numvals) flags |= OVERWROTE;
memcpy(&buf[len], (const void *)vals, min(maxlen-len, numvals)*sizeof(T));
len += min(maxlen-len, numvals);
}
int get(T *vals, int numvals)
{
int read = min(maxlen-len, numvals);
if(read<numvals) flags |= OVERREAD;
memcpy(vals, (void *)&buf[len], read*sizeof(T));
len += read;
return read;
}
void offset(int n)
{
n = min(n, maxlen);
buf += n;
maxlen -= n;
len = max(len-n, 0);
}
bool empty() const { return len==0; }
int length() const { return len; }
int remaining() const { return maxlen-len; }
bool overread() const { return (flags&OVERREAD)!=0; }
bool overwrote() const { return (flags&OVERWROTE)!=0; }
void forceoverread()
{
len = maxlen;
flags |= OVERREAD;
}
};
typedef databuf<char> charbuf;
typedef databuf<uchar> ucharbuf;
struct packetbuf : ucharbuf
{
ENetPacket *packet;
int growth;
packetbuf(ENetPacket *packet) : ucharbuf(packet->data, packet->dataLength), packet(packet), growth(0) {}
packetbuf(int growth, int pflags = 0) : growth(growth)
{
packet = enet_packet_create(NULL, growth, pflags);
buf = (uchar *)packet->data;
maxlen = packet->dataLength;
}
~packetbuf() { cleanup(); }
void reliable() { packet->flags |= ENET_PACKET_FLAG_RELIABLE; }
void resize(int n)
{
enet_packet_resize(packet, n);
buf = (uchar *)packet->data;
maxlen = packet->dataLength;
}
void checkspace(int n)
{
if(len + n > maxlen && packet && growth > 0) resize(max(len + n, maxlen + growth));
}
ucharbuf subbuf(int sz)
{
checkspace(sz);
return ucharbuf::subbuf(sz);
}
void put(const uchar &val)
{
checkspace(1);
ucharbuf::put(val);
}
void put(const uchar *vals, int numvals)
{
checkspace(numvals);
ucharbuf::put(vals, numvals);
}
ENetPacket *finalize()
{
resize(len);
return packet;
}
void cleanup()
{
if(growth > 0 && packet && !packet->referenceCount) { enet_packet_destroy(packet); packet = NULL; buf = NULL; len = maxlen = 0; }
}
};
template<class T>
static inline float heapscore(const T &n) { return n; }
template<class T>
static inline bool compareless(const T &x, const T &y) { return x < y; }
template<class T, class F>
static inline void insertionsort(T *start, T *end, F fun)
{
for(T *i = start+1; i < end; i++)
{
if(fun(*i, i[-1]))
{
T tmp = *i;
*i = i[-1];
T *j = i-1;
for(; j > start && fun(tmp, j[-1]); --j)
*j = j[-1];
*j = tmp;
}
}
}
template<class T, class F>
static inline void insertionsort(T *buf, int n, F fun)
{
insertionsort(buf, buf+n, fun);
}
template<class T>
static inline void insertionsort(T *buf, int n)
{
insertionsort(buf, buf+n, compareless<T>);
}
template<class T, class F>
static inline void quicksort(T *start, T *end, F fun)
{
while(end-start > 10)
{
T *mid = &start[(end-start)/2], *i = start+1, *j = end-2, pivot;
if(fun(*start, *mid)) /* start < mid */
{
if(fun(end[-1], *start)) { pivot = *start; *start = end[-1]; end[-1] = *mid; } /* end < start < mid */
else if(fun(end[-1], *mid)) { pivot = end[-1]; end[-1] = *mid; } /* start <= end < mid */
else { pivot = *mid; } /* start < mid <= end */
}
else if(fun(*start, end[-1])) { pivot = *start; *start = *mid; } /*mid <= start < end */
else if(fun(*mid, end[-1])) { pivot = end[-1]; end[-1] = *start; *start = *mid; } /* mid < end <= start */
else { pivot = *mid; swap(*start, end[-1]); } /* end <= mid <= start */
*mid = end[-2];
do
{
while(fun(*i, pivot)) if(++i >= j) goto partitioned;
while(fun(pivot, *--j)) if(i >= j) goto partitioned;
swap(*i, *j);
}
while(++i < j);
partitioned:
end[-2] = *i;
*i = pivot;
if(i-start < end-(i+1))
{
quicksort(start, i, fun);
start = i+1;
}
else
{
quicksort(i+1, end, fun);
end = i;
}
}
insertionsort(start, end, fun);
}
template<class T, class F>
static inline void quicksort(T *buf, int n, F fun)
{
quicksort(buf, buf+n, fun);
}
template<class T>
static inline void quicksort(T *buf, int n)
{
quicksort(buf, buf+n, compareless<T>);
}
template<class T> struct isclass
{
template<class C> static char test(void (C::*)(void));
template<class C> static int test(...);
enum { yes = sizeof(test<T>(0)) == 1 ? 1 : 0, no = yes^1 };
};
static inline uint hthash(const char *key)
{
uint h = 5381;
for(int i = 0, k; (k = key[i]); i++) h = ((h<<5)+h)^k; // bernstein k=33 xor
return h;
}
static inline bool htcmp(const char *x, const char *y)
{
return !strcmp(x, y);
}
static inline uint hthash(int key)
{
return key;
}
static inline bool htcmp(int x, int y)
{
return x==y;
}
#ifndef STANDALONE
static inline uint hthash(GLuint key)
{
return key;
}
static inline bool htcmp(GLuint x, GLuint y)
{
return x==y;
}
#endif
template <class T> struct vector
{
static const int MINSIZE = 8;
T *buf;
int alen, ulen;
vector() : buf(NULL), alen(0), ulen(0)
{
}
vector(const vector &v) : buf(NULL), alen(0), ulen(0)
{
*this = v;
}
~vector() { shrink(0); if(buf) delete[] (uchar *)buf; }
vector<T> &operator=(const vector<T> &v)
{
shrink(0);
if(v.length() > alen) growbuf(v.length());
loopv(v) add(v[i]);
return *this;
}
T &add(const T &x)
{
if(ulen==alen) growbuf(ulen+1);
new (&buf[ulen]) T(x);
return buf[ulen++];
}
T &add()
{
if(ulen==alen) growbuf(ulen+1);
new (&buf[ulen]) T;
return buf[ulen++];
}
T &dup()
{
if(ulen==alen) growbuf(ulen+1);
new (&buf[ulen]) T(buf[ulen-1]);
return buf[ulen++];
}
void move(vector<T> &v)
{
if(!ulen)
{
swap(buf, v.buf);
swap(ulen, v.ulen);
swap(alen, v.alen);
}
else
{
growbuf(ulen+v.ulen);
if(v.ulen) memcpy(&buf[ulen], (void *)v.buf, v.ulen*sizeof(T));
ulen += v.ulen;
v.ulen = 0;
}
}
bool inrange(size_t i) const { return i<size_t(ulen); }
bool inrange(int i) const { return i>=0 && i<ulen; }
T &pop() { return buf[--ulen]; }
T &last() { return buf[ulen-1]; }
void drop() { ulen--; buf[ulen].~T(); }
bool empty() const { return ulen==0; }
int capacity() const { return alen; }
int length() const { return ulen; }
T &operator[](int i) { ASSERT(i>=0 && i<ulen); return buf[i]; }
const T &operator[](int i) const { ASSERT(i >= 0 && i<ulen); return buf[i]; }
void disown() { buf = NULL; alen = ulen = 0; }
void shrink(int i) { ASSERT(i<=ulen); if(isclass<T>::no) ulen = i; else while(ulen>i) drop(); }
void setsize(int i) { ASSERT(i<=ulen); ulen = i; }
void deletecontents() { while(!empty()) delete pop(); }
void deletearrays() { while(!empty()) delete[] pop(); }
T *getbuf() { return buf; }
const T *getbuf() const { return buf; }
bool inbuf(const T *e) const { return e >= buf && e < &buf[ulen]; }
template<class F>
void sort(F fun, int i = 0, int n = -1)
{
quicksort(&buf[i], n < 0 ? ulen-i : n, fun);
}
void sort() { sort(compareless<T>); }
void growbuf(int sz)
{
int olen = alen;
if(!alen) alen = max(MINSIZE, sz);
else while(alen < sz) alen *= 2;
if(alen <= olen) return;
uchar *newbuf = new uchar[alen*sizeof(T)];
if(olen > 0)
{
memcpy(newbuf, (void *)buf, olen*sizeof(T));
delete[] (uchar *)buf;
}
buf = (T *)newbuf;
}
databuf<T> reserve(int sz)
{
if(ulen+sz > alen) growbuf(ulen+sz);
return databuf<T>(&buf[ulen], sz);
}
void advance(int sz)
{
ulen += sz;
}
void addbuf(const databuf<T> &p)
{
advance(p.length());
}
T *pad(int n)
{
T *buf = reserve(n).buf;
advance(n);
return buf;
}
void put(const T &v) { add(v); }
void put(const T *v, int n)
{
databuf<T> buf = reserve(n);
buf.put(v, n);
addbuf(buf);
}
void remove(int i, int n)
{
for(int p = i+n; p<ulen; p++) buf[p-n] = buf[p];
ulen -= n;
}
T remove(int i)
{
T e = buf[i];
for(int p = i+1; p<ulen; p++) buf[p-1] = buf[p];
ulen--;
return e;
}
T removeunordered(int i)
{
T e = buf[i];
ulen--;
if(ulen>0) buf[i] = buf[ulen];
return e;
}
template<class U>
int find(const U &o)
{
loopi(ulen) if(buf[i]==o) return i;
return -1;
}
void removeobj(const T &o)
{
loopi(ulen) if(buf[i]==o) remove(i--);
}
void replacewithlast(const T &o)
{
if(!ulen) return;
loopi(ulen-1) if(buf[i]==o)
{
buf[i] = buf[ulen-1];
}
ulen--;
}
T &insert(int i, const T &e)
{
add(T());
for(int p = ulen-1; p>i; p--) buf[p] = buf[p-1];
buf[i] = e;
return buf[i];
}
T *insert(int i, const T *e, int n)
{
if(ulen+n>alen) growbuf(ulen+n);
loopj(n) add(T());
for(int p = ulen-1; p>=i+n; p--) buf[p] = buf[p-n];
loopj(n) buf[i+j] = e[j];
return &buf[i];
}
void reverse()
{
loopi(ulen/2) swap(buf[i], buf[ulen-1-i]);
}
static int heapparent(int i) { return (i - 1) >> 1; }
static int heapchild(int i) { return (i << 1) + 1; }
void buildheap()
{
for(int i = ulen/2; i >= 0; i--) downheap(i);
}
int upheap(int i)
{
float score = heapscore(buf[i]);
while(i > 0)
{
int pi = heapparent(i);
if(score >= heapscore(buf[pi])) break;
swap(buf[i], buf[pi]);
i = pi;
}
return i;
}
T &addheap(const T &x)
{
add(x);
return buf[upheap(ulen-1)];
}
int downheap(int i)
{
float score = heapscore(buf[i]);
for(;;)
{
int ci = heapchild(i);
if(ci >= ulen) break;
float cscore = heapscore(buf[ci]);
if(score > cscore)
{
if(ci+1 < ulen && heapscore(buf[ci+1]) < cscore) { swap(buf[ci+1], buf[i]); i = ci+1; }
else { swap(buf[ci], buf[i]); i = ci; }
}
else if(ci+1 < ulen && heapscore(buf[ci+1]) < score) { swap(buf[ci+1], buf[i]); i = ci+1; }
else break;
}
return i;
}
T removeheap()
{
T e = removeunordered(0);
if(ulen) downheap(0);
return e;
}
template<class K>
int htfind(const K &key)
{
loopi(ulen) if(htcmp(key, buf[i])) return i;
return -1;
}
};
template<class T> struct hashset
{
typedef T elem;
typedef const T const_elem;
enum { CHUNKSIZE = 64 };
struct chain { T elem; chain *next; };
struct chainchunk { chain chains[CHUNKSIZE]; chainchunk *next; };
int size;
int numelems;
chain **chains;
chainchunk *chunks;
chain *unused;
hashset(int size = 1<<10)
: size(size)
{
numelems = 0;
chunks = NULL;
unused = NULL;
chains = new chain *[size];
loopi(size) chains[i] = NULL;
}
~hashset()
{
DELETEA(chains);
deletechunks();
}
chain *insert(uint h)
{
if(!unused)
{
chainchunk *chunk = new chainchunk;
chunk->next = chunks;
chunks = chunk;
loopi(CHUNKSIZE-1) chunk->chains[i].next = &chunk->chains[i+1];
chunk->chains[CHUNKSIZE-1].next = unused;
unused = chunk->chains;
}
chain *c = unused;
unused = unused->next;
c->next = chains[h];
chains[h] = c;
numelems++;
return c;
}
#define HTFIND(key, success, fail) \
uint h = hthash(key)&(this->size-1); \
for(chain *c = this->chains[h]; c; c = c->next) \
{ \
if(htcmp(key, c->elem)) return (success); \
} \
return (fail);
template<class K>
T *access(const K &key)
{
HTFIND(key, &c->elem, NULL);
}
template<class K, class E>
T &access(const K &key, const E &elem)
{
HTFIND(key, c->elem, insert(h)->elem = elem);
}
template<class K>
T &operator[](const K &key)
{
HTFIND(key, c->elem, insert(h)->elem);
}
template<class K>
T &find(const K &key, T ¬found)
{
HTFIND(key, c->elem, notfound);
}
template<class K>
const T &find(const K &key, const T ¬found)
{
HTFIND(key, c->elem, notfound);
}
template<class K>
bool remove(const K &key)
{
uint h = hthash(key)&(size-1);
for(chain **p = &chains[h], *c = chains[h]; c; p = &c->next, c = c->next)
{
if(htcmp(key, c->elem))
{
*p = c->next;
c->elem.~T();
new (&c->elem) T;
c->next = unused;
unused = c;
numelems--;
return true;
}
}
return false;
}
void deletechunks()
{
for(chainchunk *nextchunk; chunks; chunks = nextchunk)
{
nextchunk = chunks->next;
delete chunks;
}
}
void clear()
{
if(!numelems) return;
loopi(size) chains[i] = NULL;
numelems = 0;
unused = NULL;
deletechunks();
}
static inline chain *getnext(void *i) { return ((chain *)i)->next; }
static inline T &getdata(void *i) { return ((chain *)i)->elem; }
};
template<class K, class T> struct hashtableentry
{
K key;
T data;
hashtableentry() {}
hashtableentry(const K &key, const T &data) : key(key), data(data) {}
};
template<class U, class K, class T>
static inline bool htcmp(const U *x, const hashtableentry<K, T> &y)
{
return htcmp(x, y.key);
}
template<class U, class K, class T>
static inline bool htcmp(const U &x, const hashtableentry<K, T> &y)
{
return htcmp(x, y.key);
}
template<class K, class T> struct hashtable : hashset<hashtableentry<K, T> >
{
typedef hashtableentry<K, T> entry;
typedef struct hashset<entry>::chain chain;
typedef K key;
typedef T value;
hashtable(int size = 1<<10) : hashset<entry>(size) {}
entry &insert(const K &key, uint h)
{
chain *c = hashset<entry>::insert(h);
c->elem.key = key;
return c->elem;
}
T *access(const K &key)
{
HTFIND(key, &c->elem.data, NULL);
}
T &access(const K &key, const T &data)
{
HTFIND(key, c->elem.data, insert(key, h).data = data);
}
T &operator[](const K &key)
{
HTFIND(key, c->elem.data, insert(key, h).data);
}
T &find(const K &key, T ¬found)
{
HTFIND(key, c->elem.data, notfound);
}
const T &find(const K &key, const T ¬found)
{
HTFIND(key, c->elem.data, notfound);
}
static inline chain *getnext(void *i) { return ((chain *)i)->next; }
static inline K &getkey(void *i) { return ((chain *)i)->elem.key; }
static inline T &getdata(void *i) { return ((chain *)i)->elem.data; }
};
#define enumerates(ht,t,e,b) loopi((ht).size) for(hashset<t>::chain *enumc = (ht).chains[i]; enumc;) { t &e = enumc->elem; enumc = enumc->next; b; }
#define enumeratekt(ht,k,e,t,f,b) loopi((ht).size) for(hashtable<k,t>::chain *enumc = (ht).chains[i]; enumc;) { const hashtable<k,t>::key &e = enumc->elem.key; t &f = enumc->elem.data; enumc = enumc->next; b; }
#define enumerate(ht,t,e,b) loopi((ht).size) for(void *enumc = (ht).chains[i]; enumc;) { t &e = (ht).getdata(enumc); enumc = (ht).getnext(enumc); b; }
struct unionfind
{
struct ufval
{
int rank, next;
ufval() : rank(0), next(-1) {}
};
vector<ufval> ufvals;
int find(int k)
{
if(k>=ufvals.length()) return k;
while(ufvals[k].next>=0) k = ufvals[k].next;
return k;
}
int compressfind(int k)
{
if(ufvals[k].next<0) return k;
return ufvals[k].next = compressfind(ufvals[k].next);
}
void unite (int x, int y)
{
while(ufvals.length() <= max(x, y)) ufvals.add();
x = compressfind(x);
y = compressfind(y);
if(x==y) return;
ufval &xval = ufvals[x], &yval = ufvals[y];
if(xval.rank < yval.rank) xval.next = y;
else
{
yval.next = x;
if(xval.rank==yval.rank) yval.rank++;
}
}
};
template <class T, int SIZE> struct ringbuf
{
int index, len;
T data[SIZE];
ringbuf() { clear(); }
void clear()
{
index = len = 0;
}
bool empty() const { return !len; }
const int length() const { return len; }
T &add()
{
T &t = data[index];
index++;
if(index >= SIZE) index -= SIZE;
if(len < SIZE) len++;
return t;
}
T &add(const T &e) { return add() = e; }
T &operator[](int i)
{
i += index - len;
return data[i < 0 ? i + SIZE : i%SIZE];
}
const T &operator[](int i) const
{
i += index - len;
return data[i < 0 ? i + SIZE : i%SIZE];
}
};
template <class T, int SIZE> struct queue
{
int head, tail, len;
T data[SIZE];
queue() { clear(); }
void clear() { head = tail = len = 0; }
int length() const { return len; }
bool empty() const { return !len; }
bool full() const { return len == SIZE; }
T &added() { return data[tail > 0 ? tail-1 : SIZE-1]; }
T &added(int offset) { return data[tail-offset > 0 ? tail-offset-1 : tail-offset-1 + SIZE]; }
T &adding() { return data[tail]; }
T &adding(int offset) { return data[tail+offset >= SIZE ? tail+offset - SIZE : tail+offset]; }
T &add()
{
ASSERT(len < SIZE);
T &t = data[tail];
tail = (tail + 1)%SIZE;
len++;
return t;
}
T &removing() { return data[head]; }
T &removing(int offset) { return data[head+offset >= SIZE ? head+offset - SIZE : head+offset]; }
T &remove()
{
ASSERT(len > 0);
T &t = data[head];
head = (head + 1)%SIZE;
len--;
return t;
}
};
inline char *newstring(size_t l) { return new char[l+1]; }
inline char *newstring(const char *s, size_t l) { return copystring(newstring(l), s, l+1); }
inline char *newstring(const char *s) { return newstring(s, strlen(s)); }
inline char *newstringbuf(const char *s) { return newstring(s, MAXSTRLEN-1); }
const int islittleendian = 1;
#ifdef SDL_BYTEORDER
#define endianswap16 SDL_Swap16
#define endianswap32 SDL_Swap32
#define endianswap64 SDL_Swap64
#else
inline ushort endianswap16(ushort n) { return (n<<8) | (n>>8); }
inline uint endianswap32(uint n) { return (n<<24) | (n>>24) | ((n>>8)&0xFF00) | ((n<<8)&0xFF0000); }
inline ullong endianswap64(ullong n) { return endianswap32(uint(n >> 32)) | ((ullong)endianswap32(uint(n)) << 32); }
#endif
template<class T> inline T endianswap(T n) { union { T t; uint i; } conv; conv.t = n; conv.i = endianswap32(conv.i); return conv.t; }
template<> inline ushort endianswap<ushort>(ushort n) { return endianswap16(n); }
template<> inline short endianswap<short>(short n) { return endianswap16(n); }
template<> inline uint endianswap<uint>(uint n) { return endianswap32(n); }
template<> inline int endianswap<int>(int n) { return endianswap32(n); }
template<> inline ullong endianswap<ullong>(ullong n) { return endianswap64(n); }
template<> inline llong endianswap<llong>(llong n) { return endianswap64(n); }
template<> inline double endianswap<double>(double n) { union { double t; uint i; } conv; conv.t = n; conv.i = endianswap64(conv.i); return conv.t; }
template<class T> inline void endianswap(T *buf, int len) { for(T *end = &buf[len]; buf < end; buf++) *buf = endianswap(*buf); }
template<class T> inline T endiansame(T n) { return n; }
template<class T> inline void endiansame(T *buf, int len) {}
#ifdef SDL_BYTEORDER
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define lilswap endiansame
#define bigswap endianswap
#else
#define lilswap endianswap
#define bigswap endiansame
#endif
#else
template<class T> inline T lilswap(T n) { return *(const uchar *)&islittleendian ? n : endianswap(n); }
template<class T> inline void lilswap(T *buf, int len) { if(!*(const uchar *)&islittleendian) endianswap(buf, len); }
template<class T> inline T bigswap(T n) { return *(const uchar *)&islittleendian ? endianswap(n) : n; }
template<class T> inline void bigswap(T *buf, int len) { if(*(const uchar *)&islittleendian) endianswap(buf, len); }
#endif
/* workaround for some C platforms that have these two functions as macros - not used anywhere */
#ifdef getchar
#undef getchar
#endif
#ifdef putchar
#undef putchar
#endif
struct stream
{
#ifdef WIN32
#ifdef __GNUC__
typedef off64_t offset;
#else
typedef __int64 offset;
#endif
#else
typedef off_t offset;
#endif
virtual ~stream() {}
virtual void close() = 0;
virtual bool end() = 0;
virtual offset tell() { return -1; }
virtual offset rawtell() { return tell(); }
virtual bool seek(offset pos, int whence = SEEK_SET) { return false; }
virtual offset size();
virtual offset rawsize() { return size(); }
virtual int read(void *buf, int len) { return 0; }
virtual int write(const void *buf, int len) { return 0; }
virtual int getchar() { uchar c; return read(&c, 1) == 1 ? c : -1; }
virtual bool putchar(int n) { uchar c = n; return write(&c, 1) == 1; }
virtual bool getline(char *str, int len);
virtual bool putstring(const char *str) { int len = (int)strlen(str); return write(str, len) == len; }
virtual bool putline(const char *str) { return putstring(str) && putchar('\n'); }
virtual int printf(const char *fmt, ...) PRINTFARGS(2, 3);
virtual uint getcrc() { return 0; }
template<class T> int put(const T *v, int n) { return write(v, n*sizeof(T))/sizeof(T); }
template<class T> bool put(T n) { return write(&n, sizeof(n)) == sizeof(n); }
template<class T> bool putlil(T n) { return put<T>(lilswap(n)); }
template<class T> bool putbig(T n) { return put<T>(bigswap(n)); }
template<class T> int get(T *v, int n) { return read(v, n*sizeof(T))/sizeof(T); }
template<class T> T get() { T n; return read(&n, sizeof(n)) == sizeof(n) ? n : 0; }
template<class T> T getlil() { return lilswap(get<T>()); }
template<class T> T getbig() { return bigswap(get<T>()); }
#ifndef STANDALONE
SDL_RWops *rwops();
#endif
};
template<class T>
struct streambuf
{
stream *s;
streambuf(stream *s) : s(s) {}
T get() { return s->get<T>(); }
int get(T *vals, int numvals) { return s->get(vals, numvals); }
void put(const T &val) { s->put(&val, 1); }
void put(const T *vals, int numvals) { s->put(vals, numvals); }
int length() { return s->size(); }
};
enum
{
CT_PRINT = 1<<0,
CT_SPACE = 1<<1,
CT_DIGIT = 1<<2,
CT_ALPHA = 1<<3,
CT_LOWER = 1<<4,
CT_UPPER = 1<<5,
CT_UNICODE = 1<<6
};
extern const uchar cubectype[256];
static inline int iscubeprint(uchar c) { return cubectype[c]&CT_PRINT; }
static inline int iscubespace(uchar c) { return cubectype[c]&CT_SPACE; }
static inline int iscubealpha(uchar c) { return cubectype[c]&CT_ALPHA; }
static inline int iscubealnum(uchar c) { return cubectype[c]&(CT_ALPHA|CT_DIGIT); }
static inline int iscubelower(uchar c) { return cubectype[c]&CT_LOWER; }
static inline int iscubeupper(uchar c) { return cubectype[c]&CT_UPPER; }
static inline int cube2uni(uchar c)
{
extern const int cube2unichars[256];
return cube2unichars[c];
}
static inline uchar uni2cube(int c)
{
extern const int uni2cubeoffsets[8];
extern const uchar uni2cubechars[];
return uint(c) <= 0x7FF ? uni2cubechars[uni2cubeoffsets[c>>8] + (c&0xFF)] : 0;
}
extern int decodeutf8(uchar *dst, int dstlen, const uchar *src, int srclen, int *carry = NULL);
extern int encodeutf8(uchar *dstbuf, int dstlen, const uchar *srcbuf, int srclen, int *carry = NULL);
extern char *makerelpath(const char *dir, const char *file, const char *prefix = NULL, const char *cmd = NULL);
extern char *path(char *s);
extern char *path(const char *s, bool copy);
extern const char *parentdir(const char *directory);
extern bool fileexists(const char *path, const char *mode);
extern bool createdir(const char *path);
extern size_t fixpackagedir(char *dir);
extern const char *sethomedir(const char *dir);
extern const char *addpackagedir(const char *dir);
extern const char *findfile(const char *filename, const char *mode);
extern stream *openrawfile(const char *filename, const char *mode);
extern stream *openzipfile(const char *filename, const char *mode);
extern stream *openfile(const char *filename, const char *mode);
extern stream *opentempfile(const char *filename, const char *mode);
extern stream *opengzfile(const char *filename, const char *mode, stream *file = NULL, int level = Z_BEST_COMPRESSION);
extern stream *openutf8file(const char *filename, const char *mode, stream *file = NULL);
extern char *loadfile(const char *fn, int *size, bool utf8 = true);
extern bool listdir(const char *dir, bool rel, const char *ext, vector<char *> &files);
extern int listfiles(const char *dir, const char *ext, vector<char *> &files);
extern int listzipfiles(const char *dir, const char *ext, vector<char *> &files);
extern void seedMT(uint seed);
extern uint randomMT();
extern int guessnumcpus();
extern void putint(ucharbuf &p, int n);
extern void putint(packetbuf &p, int n);
extern void putint(vector<uchar> &p, int n);
extern int getint(ucharbuf &p);
extern void putuint(ucharbuf &p, int n);
extern void putuint(packetbuf &p, int n);
extern void putuint(vector<uchar> &p, int n);
extern int getuint(ucharbuf &p);
extern void putfloat(ucharbuf &p, float f);
extern void putfloat(packetbuf &p, float f);
extern void putfloat(vector<uchar> &p, float f);
extern float getfloat(ucharbuf &p);
extern void sendstring(const char *t, ucharbuf &p);
extern void sendstring(const char *t, packetbuf &p);
extern void sendstring(const char *t, vector<uchar> &p);
extern void getstring(char *t, ucharbuf &p, int len);
template<size_t N> static inline void getstring(char (&t)[N], ucharbuf &p) { getstring(t, p, int(N)); }
extern void filtertext(char *dst, const char *src, bool whitespace = true, int len = sizeof(string)-1);
#endif
|