-
Notifications
You must be signed in to change notification settings - Fork 72
/
util.h
1130 lines (975 loc) · 32.3 KB
/
util.h
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
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>
#include <assert.h>
#include "iqm.h"
#define ASSERT(c) if(c) {}
#ifdef NULL
#undef NULL
#endif
#define NULL 0
#ifdef _WIN32
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
#define strcasecmp _stricmp
#endif
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef signed long long int llong;
typedef unsigned long long int ullong;
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 delete(void *p, size_t) { if(p) free(p); }
inline void operator delete[](void *p, size_t) { 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;
}
#define clamp(a,b,c) (max(b, min(a, c)))
#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)
#ifdef WIN32
#ifdef M_PI
#undef M_PI
#endif
#define M_PI 3.14159265
#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 PATHDIV '\\'
#else
#define __cdecl
#define _vsnprintf vsnprintf
#define PATHDIV '/'
#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)
{
size_t slen = min(strlen(s)+1, len);
memcpy(d, s, slen);
d[slen-1] = 0;
return d;
}
inline char *concatstring(char *d, const char *s) { size_t len = strlen(d); return copystring(d+len, s, MAXSTRLEN-len); }
template<size_t N> inline void formatstring(char (&d)[N], const char *fmt, ...)
{
va_list v;
va_start(v, fmt);
vformatstring(d, fmt, v, int(N));
va_end(v);
}
#define defformatstring(d,...) string d; formatstring(d, __VA_ARGS__)
#define defvformatstring(d,last,fmt) string d; { va_list ap; va_start(ap, last); vformatstring(d, fmt, ap); va_end(ap); }
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) { size_t l = strlen(s); char *d = newstring(l); memcpy(d, s, l+1); return d; }
#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 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() { setsize(0); if(buf) delete[] (uchar *)buf; }
vector<T> &operator=(const vector<T> &v)
{
setsize(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++];
}
bool inrange(uint i) const { return i<uint(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 setsize(int i) { ASSERT(i <= ulen); ulen = i; }
void swap(vector<T> &v)
{
::swap(buf, v.buf);
::swap(ulen, v.ulen);
::swap(alen, v.alen);
}
T *getbuf() { return buf; }
const T *getbuf() const { return buf; }
bool inbuf(const T *e) const { return e >= buf && e < &buf[ulen]; }
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, buf, olen*sizeof(T));
delete[] (uchar *)buf;
}
buf = (T *)newbuf;
}
T *reserve(int sz)
{
if(ulen+sz > alen) growbuf(ulen+sz);
return &buf[ulen];
}
void advance(int sz)
{
ulen += sz;
}
void put(const T *v, int n)
{
memcpy(reserve(n), v, n*sizeof(T));
advance(n);
}
};
static inline uint htcombine(uint h1, uint h2)
{
return h1 ^ (h2 + 0x9E3779B9 + (h1 << 6) + (h1 >> 2));
}
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);
}
// https://burtleburtle.net/bob/hash/integer.html
static inline uint hthash(uint k)
{
k = (k ^ 61) ^ (k >> 16);
k = k + (k << 3);
k = k ^ (k >> 4);
k = k * 0x27d4eb2d;
k = k ^ (k >> 15);
return k;
}
static inline uint hthash(ullong k)
{
k = (~k) + (k << 21);
k = k ^ (k >> 24);
k = (k + (k << 3)) + (k << 8);
k = k ^ (k >> 14);
k = (k + (k << 2)) + (k << 4);
k = k ^ (k << 28);
k = k + (k << 31);
return k;
}
static inline uint hthash(int k)
{
return hthash(uint(k));
}
static inline bool htcmp(int x, int y)
{
return x==y;
}
static inline bool htcmp(double x, double y)
{
return x == y;
}
static inline uint hthash(double k)
{
union { double d; ullong u; } conv;
conv.d = k;
return hthash(conv.u);
}
template <class K, class T> struct hashtable
{
typedef K key;
typedef const K const_key;
typedef T value;
typedef const T const_value;
enum { CHUNKSIZE = 64, MAXLOADFACTOR = 75, RESIZEFACTOR = 4, MAXSIZE = 64<<20 };
struct chain { T data; K key; chain *next; };
struct chainchunk { chain chains[CHUNKSIZE]; chainchunk *next; };
int size;
int numelems;
chain **table;
chainchunk *chunks;
chain *unused;
hashtable(int size = 1<<10)
: size(size)
{
numelems = 0;
chunks = NULL;
unused = NULL;
table = new chain*[size];
loopi(size) table[i] = NULL;
}
~hashtable()
{
if(table) delete[] table;
deletechunks();
}
chain *insert(const K &key, uint h)
{
if(size <= MAXSIZE / RESIZEFACTOR && numelems * 100 > size * MAXLOADFACTOR) { rehash(); h = hthash(key)&(size-1); }
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->key = key;
c->next = table[h];
table[h] = c;
numelems++;
return c;
}
#define HTFIND(success, fail) \
uint h = hthash(key)&(size-1); \
for(chain *c = table[h]; c; c = c->next) \
{ \
if(htcmp(key, c->key)) return (success); \
} \
return (fail);
template<class L>
T *access(const L &key)
{
HTFIND(&c->data, NULL);
}
template<class L>
T &access(const L &key, const T &data)
{
HTFIND(c->data, insert(key, h)->data = data);
}
template<class L>
const T &find(const L &key, const T ¬found)
{
HTFIND(c->data, notfound);
}
template<class L>
T &operator[](const L &key)
{
HTFIND(c->data, insert(key, h)->data);
}
#undef HTFIND
template<class L>
bool remove(const L &key)
{
uint h = hthash(key)&(size-1);
for(chain **p = &table[h], *c = table[h]; c; p = &c->next, c = c->next)
{
if(htcmp(key, c->key))
{
*p = c->next;
c->data.~T();
c->key.~K();
new (&c->data) T;
new (&c->key) K;
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) table[i] = NULL;
numelems = 0;
unused = NULL;
deletechunks();
}
void rehash()
{
int oldsize = size;
chain **oldtable = table;
size *= RESIZEFACTOR;
table = new chain*[size];
loopi(size) table[i] = NULL;
loopi(oldsize) for (chain *c = oldtable[i]; c;)
{
chain *p = c;
c = c->next;
uint h = hthash(p->key)&(size-1);
p->next = table[h];
table[h] = p;
}
delete[] oldtable;
}
};
#define enumerate(ht,k,e,t,f,b) loopi((ht).size) for(hashtable<k,t>::chain *enumc = (ht).table[i]; enumc;) { hashtable<k,t>::const_key &e = enumc->key; t &f = enumc->data; enumc = enumc->next; b; }
template<class T>
struct unionfind
{
struct ufval
{
int rank, next;
T val;
ufval(const T &val) : rank(0), next(-1), val(val) {}
};
vector<ufval> ufvals;
void clear()
{
ufvals.setsize(0);
}
const T &find(int k, const T &noval, const T &initval)
{
if(k>=ufvals.length()) return initval;
while(ufvals[k].next>=0) k = ufvals[k].next;
if(ufvals[k].val == noval) ufvals[k].val = initval;
return ufvals[k].val;
}
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, const T &noval)
{
while(ufvals.length() <= max(x, y)) ufvals.add(ufval(noval));
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>
struct listnode
{
T *prev, *next;
};
template<class T>
struct list
{
typedef listnode<T> node;
int size;
listnode<T> nodes;
list() { clear(); }
bool empty() const { return nodes.prev == nodes.next; }
bool notempty() const { return nodes.prev != nodes.next; }
T *first() const { return nodes.next; }
T *last() const { return nodes.prev; }
T *end() const { return (T *)&nodes; }
void clear()
{
size = 0;
nodes.prev = nodes.next = (T *)&nodes;
}
T *remove(T *node)
{
size--;
node->prev->next = node->next;
node->next->prev = node->prev;
return node;
}
T *insertafter(T *node, T *pos)
{
size++;
node->next = pos->next;
node->next->prev = node;
node->prev = pos;
pos->next = node;
return node;
}
T *insertbefore(T *node, T *pos)
{
size++;
node->prev = pos->prev;
node->prev->next = node;
node->next = pos;
pos->prev = node;
return node;
}
T *insertfirst(T *node) { return insertafter(node, end()); }
T *insertlast(T *node) { return insertbefore(node, end()); }
T *removefirst() { return remove(first()); }
T *removelast() { return remove(last()); }
};
static inline bool islittleendian() { union { int i; uchar b[sizeof(int)]; } conv; conv.i = 1; return conv.b[0] != 0; }
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); }
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 uchar endianswap<uchar>(uchar n) { return n; }
template<> inline char endianswap<char>(char n) { return n; }
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) {}
template<class T> inline T lilswap(T n) { return islittleendian() ? n : endianswap(n); }
template<class T> inline void lilswap(T *buf, int len) { if(!islittleendian()) endianswap(buf, len); }
template<class T> inline T bigswap(T n) { return islittleendian() ? endianswap(n) : n; }
template<class T> inline void bigswap(T *buf, int len) { if(islittleendian()) endianswap(buf, len); }
/* 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
{
virtual ~stream() {}
virtual void close() = 0;
virtual bool end() = 0;
virtual long tell() { return -1; }
virtual bool seek(long offset, int whence = SEEK_SET) { return false; }
virtual long 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 = strlen(str); return write(str, len) == len; }
virtual bool putline(const char *str) { return putstring(str) && putchar('\n'); }
virtual int printf(const char *fmt, ...) { return -1; }
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> 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>()); }
};
long stream::size()
{
long pos = tell(), endpos;
if(pos < 0 || !seek(0, SEEK_END)) return -1;
endpos = tell();
return pos == endpos || seek(pos, SEEK_SET) ? endpos : -1;
}
bool stream::getline(char *str, int len)
{
loopi(len-1)
{
if(read(&str[i], 1) != 1) { str[i] = '\0'; return i > 0; }
else if(str[i] == '\n') { str[i+1] = '\0'; return true; }
}
if(len > 0) str[len-1] = '\0';
return true;
}
struct filestream : stream
{
FILE *file;
filestream() : file(NULL) {}
~filestream() { close(); }
bool open(const char *name, const char *mode)
{
if(file) return false;
file = fopen(name, mode);
return file!=NULL;
}
void close()
{
if(file) { fclose(file); file = NULL; }
}
bool end() { return feof(file)!=0; }
long tell() { return ftell(file); }
bool seek(long offset, int whence) { return fseek(file, offset, whence) >= 0; }
int read(void *buf, int len) { return fread(buf, 1, len, file); }
int write(const void *buf, int len) { return fwrite(buf, 1, len, file); }
int getchar() { return fgetc(file); }
bool putchar(int c) { return fputc(c, file)!=EOF; }
bool getline(char *str, int len) { return fgets(str, len, file)!=NULL; }
bool putstring(const char *str) { return fputs(str, file)!=EOF; }
int printf(const char *fmt, ...)
{
va_list v;
va_start(v, fmt);
int result = vfprintf(file, fmt, v);
va_end(v);
return result;
}
};
char *path(char *s)
{
for(char *curpart = s;;)
{
char *endpart = strchr(curpart, '&');
if(endpart) *endpart = '\0';
if(curpart[0]=='<')
{
char *file = strrchr(curpart, '>');
if(!file) return s;
curpart = file+1;
}
for(char *t = curpart; (t = strpbrk(t, "/\\")); *t++ = PATHDIV);
for(char *prevdir = NULL, *curdir = s;;)
{
prevdir = curdir[0]==PATHDIV ? curdir+1 : curdir;
curdir = strchr(prevdir, PATHDIV);
if(!curdir) break;
if(prevdir+1==curdir && prevdir[0]=='.')
{
memmove(prevdir, curdir+1, strlen(curdir+1)+1);
curdir = prevdir;
}
else if(curdir[1]=='.' && curdir[2]=='.' && curdir[3]==PATHDIV)
{
if(prevdir+2==curdir && prevdir[0]=='.' && prevdir[1]=='.') continue;
memmove(prevdir, curdir+4, strlen(curdir+4)+1);
curdir = prevdir;
}
}
if(endpart)
{
*endpart = '&';
curpart = endpart+1;
}
else break;
}
return s;
}
char *path(const char *s, bool copy)
{
static string tmp;
copystring(tmp, s);
path(tmp);
return tmp;
}
const char *parentdir(const char *directory)
{
const char *p = directory + strlen(directory);
while(p > directory && *p != '/' && *p != '\\') p--;
static string parent;
size_t len = p-directory+1;
copystring(parent, directory, len);
return parent;
}
stream *openfile(const char *filename, const char *mode)
{
filestream *file = new filestream;
if(!file->open(path(filename, true), mode)) { delete file; return NULL; }
return file;
}
struct Vec4;
struct Vec3
{
union
{
struct { double x, y, z; };
double v[3];
};
Vec3() {}
Vec3(double x, double y, double z) : x(x), y(y), z(z) {}
explicit Vec3(const double *v) : x(v[0]), y(v[1]), z(v[2]) {}
explicit Vec3(const Vec4 &v);
double &operator[](int i) { return v[i]; }
double operator[](int i) const { return v[i]; }
bool operator==(const Vec3 &o) const { return x == o.x && y == o.y && z == o.z; }
bool operator!=(const Vec3 &o) const { return x != o.x || y != o.y || z != o.z; }
bool operator<(const Vec3 &o) const { return x < o.x || y < o.y || z < o.z; }
bool operator>(const Vec3 &o) const { return x > o.x || y > o.y || z > o.z; }
Vec3 operator+(const Vec3 &o) const { return Vec3(x+o.x, y+o.y, z+o.z); }
Vec3 operator-(const Vec3 &o) const { return Vec3(x-o.x, y-o.y, z-o.z); }
Vec3 operator+(double k) const { return Vec3(x+k, y+k, z+k); }
Vec3 operator-(double k) const { return Vec3(x-k, y-k, z-k); }
Vec3 operator-() const { return Vec3(-x, -y, -z); }
Vec3 operator*(const Vec3 &o) const { return Vec3(x*o.x, y*o.y, z*o.z); }
Vec3 operator/(const Vec3 &o) const { return Vec3(x/o.x, y/o.y, z/o.z); }
Vec3 operator*(double k) const { return Vec3(x*k, y*k, z*k); }
Vec3 operator/(double k) const { return Vec3(x/k, y/k, z/k); }
Vec3 &operator+=(const Vec3 &o) { x += o.x; y += o.y; z += o.z; return *this; }
Vec3 &operator-=(const Vec3 &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
Vec3 &operator+=(double k) { x += k; y += k; z += k; return *this; }
Vec3 &operator-=(double k) { x -= k; y -= k; z -= k; return *this; }
Vec3 &operator*=(const Vec3 &o) { x *= o.x; y *= o.y; z *= o.z; return *this; }
Vec3 &operator/=(const Vec3 &o) { x /= o.x; y /= o.y; z /= o.z; return *this; }
Vec3 &operator*=(double k) { x *= k; y *= k; z *= k; return *this; }
Vec3 &operator/=(double k) { x /= k; y /= k; z /= k; return *this; }
double dot(const Vec3 &o) const { return x*o.x + y*o.y + z*o.z; }
double magnitude() const { return sqrt(dot(*this)); }
double squaredlen() const { return dot(*this); }
double dist(const Vec3 &o) const { return (*this - o).magnitude(); }
Vec3 normalize() const { return *this * (1.0 / magnitude()); }
Vec3 cross(const Vec3 &o) const { return Vec3(y*o.z-z*o.y, z*o.x-x*o.z, x*o.y-y*o.x); }
Vec3 reflect(const Vec3 &n) const { return *this - n*2.0*dot(n); }
Vec3 project(const Vec3 &n) const { return *this - n*dot(n); }
Vec3 zxy() const { return Vec3(z, x, y); }
Vec3 zyx() const { return Vec3(z, y, x); }
Vec3 yxz() const { return Vec3(y, x, z); }
Vec3 yzx() const { return Vec3(y, z, x); }
Vec3 xzy() const { return Vec3(x, z, y); }
};
static inline bool htcmp(const Vec3 &x, const Vec3 &y)
{
return x == y;
}
static inline uint hthash(const Vec3 &k)
{
uint h = hthash(k.x);
h = htcombine(h, k.y);
h = htcombine(h, k.z);
return h;
}
struct Vec4
{
union
{
struct { double x, y, z, w; };
double v[4];
};
Vec4() {}
Vec4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {}
explicit Vec4(const Vec3 &p, double w = 0) : x(p.x), y(p.y), z(p.z), w(w) {}
explicit Vec4(const double *v) : x(v[0]), y(v[1]), z(v[2]), w(v[3]) {}
double &operator[](int i) { return v[i]; }
double operator[](int i) const { return v[i]; }
bool operator==(const Vec4 &o) const { return x == o.x && y == o.y && z == o.z && w == o.w; }
bool operator!=(const Vec4 &o) const { return x != o.x || y != o.y || z != o.z || w != o.w; }
bool operator<(const Vec4 &o) const { return x < o.x || y < o.y || z < o.z || w < o.w; }
bool operator>(const Vec4 &o) const { return x > o.x || y > o.y || z > o.z || w > o.w; }
Vec4 operator+(const Vec4 &o) const { return Vec4(x+o.x, y+o.y, z+o.z, w+o.w); }
Vec4 operator-(const Vec4 &o) const { return Vec4(x-o.x, y-o.y, z-o.z, w-o.w); }
Vec4 operator+(double k) const { return Vec4(x+k, y+k, z+k, w+k); }
Vec4 operator-(double k) const { return Vec4(x-k, y-k, z-k, w-k); }
Vec4 operator-() const { return Vec4(-x, -y, -z, -w); }
Vec4 operator*(double k) const { return Vec4(x*k, y*k, z*k, w*k); }
Vec4 operator/(double k) const { return Vec4(x/k, y/k, z/k, w/k); }
Vec4 addw(double f) const { return Vec4(x, y, z, w + f); }
Vec4 &operator+=(const Vec4 &o) { x += o.x; y += o.y; z += o.z; w += o.w; return *this; }
Vec4 &operator+=(const Vec3 &o) { x += o.x; y += o.y; z += o.z; return * this; }
Vec4 &operator-=(const Vec4 &o) { x -= o.x; y -= o.y; z -= o.z; w -= o.w; return *this; }
Vec4 &operator-=(const Vec3 &o) { x -= o.x; y -= o.y; z -= o.z; return * this; }
Vec4 &operator+=(double k) { x += k; y += k; z += k; w += k; return *this; }
Vec4 &operator-=(double k) { x -= k; y -= k; z -= k; w -= k; return *this; }
Vec4 &operator*=(double k) { x *= k; y *= k; z *= k; w *= k; return *this; }
Vec4 &operator/=(double k) { x /= k; y /= k; z /= k; w /= k; return *this; }
double dot3(const Vec4 &o) const { return x*o.x + y*o.y + z*o.z; }
double dot3(const Vec3 &o) const { return x*o.x + y*o.y + z*o.z; }
double dot(const Vec4 &o) const { return dot3(o) + w*o.w; }
double dot(const Vec3 &o) const { return x*o.x + y*o.y + z*o.z + w; }
double magnitude() const { return sqrt(dot(*this)); }
double magnitude3() const { return sqrt(dot3(*this)); }
Vec4 normalize() const { return *this * (1.0 / magnitude()); }
Vec3 cross3(const Vec4 &o) const { return Vec3(y*o.z-z*o.y, z*o.x-x*o.z, x*o.y-y*o.x); }
Vec3 cross3(const Vec3 &o) const { return Vec3(y*o.z-z*o.y, z*o.x-x*o.z, x*o.y-y*o.x); }
void setxyz(const Vec3 &o) { x = o.x; y = o.y; z = o.z; }
};
inline Vec3::Vec3(const Vec4 &v) : x(v.x), y(v.y), z(v.z) {}
static inline bool htcmp(const Vec4 &x, const Vec4 &y)
{
return x == y;
}
static inline uint hthash(const Vec4 &k)
{
uint h = hthash(k.x);
h = htcombine(h, k.y);
h = htcombine(h, k.z);
h = htcombine(h, k.w);
return h;
}
struct Matrix3x3;
struct Matrix3x4;
struct Quat : Vec4
{
Quat() {}
Quat(double x, double y, double z, double w) : Vec4(x, y, z, w) {}
Quat(double angle, const Vec3 &axis)
{
double s = sin(0.5*angle);
x = s*axis.x;
y = s*axis.y;
z = s*axis.z;
w = cos(0.5*angle);
}
explicit Quat(const Vec3 &v) : Vec4(v.x, v.y, v.z, -sqrt(max(1.0 - v.squaredlen(), 0.0))) {}
explicit Quat(const Matrix3x3 &m) { convertmatrix(m); }
explicit Quat(const Matrix3x4 &m) { convertmatrix(m); }
void restorew()
{
w = -sqrt(max(1.0 - dot3(*this), 0.0));
}
Quat operator*(const Quat &o) const
{
return Quat(w*o.x + x*o.w + y*o.z - z*o.y,
w*o.y - x*o.z + y*o.w + z*o.x,
w*o.z + x*o.y - y*o.x + z*o.w,
w*o.w - x*o.x - y*o.y - z*o.z);
}
Quat &operator*=(const Quat &o) { return (*this = *this * o); }
Quat operator+(const Vec4 &o) const { return Quat(x+o.x, y+o.y, z+o.z, w+o.w); }
Quat &operator+=(const Vec4 &o) { return (*this = *this + o); }
Quat operator-(const Vec4 &o) const { return Quat(x-o.x, y-o.y, z-o.z, w-o.w); }
Quat &operator-=(const Vec4 &o) { return (*this = *this - o); }
Quat operator-() const { return Quat(-x, -y, -z, w); }
void flip() { x = -x; y = -y; z = -z; w = -w; }
Vec3 transform(const Vec3 &p) const
{
return p + cross3(cross3(p) + p*w)*2.0;
}
template<class M>
void convertmatrix(const M &m)
{
double trace = m.a.x + m.b.y + m.c.z;
if(trace>0)
{
double r = sqrt(1 + trace), inv = 0.5/r;
w = 0.5*r;
x = (m.c.y - m.b.z)*inv;
y = (m.a.z - m.c.x)*inv;
z = (m.b.x - m.a.y)*inv;
}
else if(m.a.x > m.b.y && m.a.x > m.c.z)
{
double r = sqrt(1 + m.a.x - m.b.y - m.c.z), inv = 0.5/r;
x = 0.5*r;
y = (m.b.x + m.a.y)*inv;
z = (m.a.z + m.c.x)*inv;
w = (m.c.y - m.b.z)*inv;
}
else if(m.b.y > m.c.z)
{
double r = sqrt(1 + m.b.y - m.a.x - m.c.z), inv = 0.5/r;
x = (m.b.x + m.a.y)*inv;
y = 0.5*r;
z = (m.c.y + m.b.z)*inv;
w = (m.a.z - m.c.x)*inv;
}
else
{
double r = sqrt(1 + m.c.z - m.a.x - m.b.y), inv = 0.5/r;
x = (m.a.z + m.c.x)*inv;
y = (m.c.y + m.b.z)*inv;
z = 0.5*r;
w = (m.b.x - m.a.y)*inv;
}
}
static Quat fromangles(const Vec3 &rot)
{
double cx = cos(rot.x/2), sx = sin(rot.x/2),
cy = cos(rot.y/2), sy = sin(rot.y/2),
cz = cos(rot.z/2), sz = sin(rot.z/2);
Quat q(sx*cy*cz - cx*sy*sz,
cx*sy*cz + sx*cy*sz,
cx*cy*sz - sx*sy*cz,
cx*cy*cz + sx*sy*sz);
if(q.w > 0) q.flip();
return q;
}
static Quat fromdegrees(const Vec3 &rot) { return fromangles(rot * (M_PI / 180)); }
};
struct Matrix3x3
{
Vec3 a, b, c;