forked from aengelke/raspsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
superstl.h
3720 lines (3011 loc) · 88.4 KB
/
superstl.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
// -*- c++ -*-
//
// Super Standard Template Library
//
// Faster and more optimized than stock STL implementation,
// plus includes various customized features
//
// Copyright 1997-2008 Matt T. Yourst <[email protected]>
//
// This program is free software; it is licensed under the
// GNU General Public License, Version 2.
//
#ifndef _SUPERSTL_H_
#define _SUPERSTL_H_
//
// Formatting
//
#define FMT_ZEROPAD 1 /* pad with zero */
#define FMT_SIGN 2 /* unsigned/signed long */
#define FMT_PLUS 4 /* show plus */
#define FMT_SPACE 8 /* space if plus */
#define FMT_LEFT 16 /* left justified */
#define FMT_SPECIAL 32 /* 0x */
#define FMT_LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
char* format_number(char* buf, char* end, W64 num, int base, int size, int precision, int type);
int format_integer(char* buf, int bufsize, W64s v, int size = 0, int flags = 0, int base = 10, int precision = 0);
int format_float(char* buf, int bufsize, double v, int precision = 6, int pad = 0);
int current_vcpuid();
extern bool force_synchronous_streams;
namespace superstl {
//
// String buffer
//
#define stringbuf_smallbufsize 256
class stringbuf;
stringbuf& operator <<(stringbuf& os, const char* v);
stringbuf& operator <<(stringbuf& os, const char v);
class stringbuf {
public:
stringbuf() { buf = null; reset(); }
stringbuf(int length) {
buf = null;
reset(length);
}
void reset(int length = stringbuf_smallbufsize);
~stringbuf();
int remaining() const {
return (buf + length) - p;
}
operator char*() const {
return buf;
}
void resize(int newlength);
void expand() {
resize(length*2);
}
void reserve(int extra);
int size() const { return p - buf; }
bool empty() const { return (size() == 0); }
bool set() const { return !empty(); }
stringbuf& operator =(const char* str) {
if unlikely (!str) {
reset();
return *this;
}
reset(strlen(str)+1);
*this << str;
return *this;
}
stringbuf& operator =(const stringbuf& str) {
const char* s = (const char*)str;
if unlikely (!s) {
reset();
return *this;
}
reset(strlen(s)+1);
*this << s;
return *this;
}
bool operator ==(const stringbuf& s) {
return strequal((char*)(*this), (char*)s);
}
bool operator !=(const stringbuf& s) {
return !strequal((char*)(*this), (char*)s);
}
public:
char smallbuf[stringbuf_smallbufsize];
char* buf;
char* p;
int length;
};
//
// Inserters
//
#define DefineIntegerInserter(T, signedtype) \
static inline stringbuf& operator <<(stringbuf& os, const T v) { \
char buf[128]; \
format_integer(buf, sizeof(buf), ((signedtype) ? (W64s)v : (W64)v)); \
return os << buf; \
}
DefineIntegerInserter(signed short, 1);
DefineIntegerInserter(signed int, 0);
DefineIntegerInserter(signed long, 0);
DefineIntegerInserter(signed long long, 0);
DefineIntegerInserter(unsigned short, 0);
DefineIntegerInserter(unsigned int, 0);
DefineIntegerInserter(unsigned long, 0);
DefineIntegerInserter(unsigned long long, 0);
#define DefineFloatInserter(T, digits) \
static inline stringbuf& operator <<(stringbuf& os, const T v) { \
char buf[128]; \
format_float(buf, sizeof(buf), v, digits); \
return os << buf; \
}
DefineFloatInserter(float, 6);
DefineFloatInserter(double, 16);
static inline stringbuf& operator <<(stringbuf& os, const bool v) {
return os << (int)v;
}
#undef DefineInserter
#define PrintOperator(T) static inline ostream& operator <<(ostream& os, const T& obj) { return obj.print(os); }
static inline stringbuf& operator <<(stringbuf& os, const stringbuf& sb) {
os << ((char*)sb);
return os;
}
template <class T>
static inline stringbuf& operator <<(stringbuf& os, const T* v) {
char buf[128];
format_integer(buf, sizeof(buf), (W64)(Waddr)v, 0, FMT_SPECIAL, 16);
return os << buf;
}
//
// A much more intuitive syntax than STL provides:
//
template <class T>
static inline stringbuf& operator ,(stringbuf& os, const T& v) {
return os << v;
}
//
// ostream class
//
static const char endl[] = "\n";
static class iosflush { } flush;
#define OSTREAM_BUF_SIZE 256
class odstream {
protected:
int fd;
byte* buf;
int bufsize;
int tail;
odstream* chain;
W64 offset;
bool ringbuf_mode;
byte* ringbuf;
int ringbuf_tail;
public:
bool close_on_destroy;
odstream();
bool open(const char* filename, bool append = false, int bufsize = 65536);
bool open(int fd, int bufsize = 65536);
void close();
int setbuf(int bufsize);
void setchain(odstream* chain);
void set_ringbuf_mode(bool new_ringbuf_mode);
~odstream();
odstream(int fd) {
this->fd = -1;
open(fd);
}
odstream(const char* filename, bool append = false, int bufsize = 65536) {
this->fd = -1;
open(filename, append, bufsize);
}
int write(const void* buf, int count);
operator bool() const {
return ok();
}
bool ok() const {
return (fd >= 0);
}
int filehandle() const {
return fd;
}
W64 seek(W64 pos, int whence = SEEK_SET);
W64 where() const;
void flush();
};
//
// Manipulators
//
static inline odstream& operator <<(odstream& os, const iosflush& v) {
os.flush();
return os;
}
template <typename T>
static inline odstream& operator <<(odstream& os, const T& v) {
os.write(&v, sizeof(T));
return os;
}
template <typename T>
static inline odstream& operator ,(odstream& os, const T& v) {
return os << v;
}
class ostream: public odstream {
public:
ostream(): odstream() { }
ostream(int fd): odstream(fd) { }
ostream(const char* filename, bool append = false): odstream(filename, append) { }
};
//
// Inserters
//
template <typename T>
static inline ostream& operator <<(ostream& os, const T& v) {
stringbuf sb;
sb << v;
os.write((char*)sb, sb.size());
return os;
}
template <>
inline ostream& operator <<(ostream& os, const iosflush& v) {
os.flush();
return os;
}
template <>
inline ostream& operator <<(ostream& os, const char& v) {
os.write(&v, sizeof(char));
return os;
}
static inline ostream& operator <<(ostream& os, const char* v) {
if unlikely (!v) v = "<null>";
os.write(v, strlen(v));
return os;
}
template <>
inline ostream& operator <<(ostream& os, const stringbuf& v) { stringbuf sb; sb << (char*)v; os.write((char*)sb, sb.size()); return os; }
template <class T>
static inline ostream& operator ,(ostream& os, const T& v) {
return os << v;
}
#define DeclareStringBufToStream(T) inline ostream& operator <<(ostream& os, const T& arg) { stringbuf sb; sb << arg; os << sb; return os; }
// Print bits as a string:
struct bitstring {
W64 bits;
int n;
bool reverse;
bitstring() { }
bitstring(const W64 bits, const int n, bool reverse = false) {
assert(n <= 64);
this->bits = bits;
this->n = n;
this->reverse = reverse;
}
};
stringbuf& operator <<(stringbuf& os, const bitstring& bs);
DeclareStringBufToStream(bitstring);
struct bitmaskstring {
W64 bits;
W64 mask;
int n;
bool reverse;
bitmaskstring() { }
bitmaskstring(const W64 bits, W64 mask, const int n, bool reverse = false) {
assert(n <= 64);
this->bits = bits;
this->mask = mask;
this->n = n;
this->reverse = reverse;
}
};
stringbuf& operator <<(stringbuf& os, const bitmaskstring& bs);
DeclareStringBufToStream(bitmaskstring);
struct hexstring {
W64 value;
int n;
hexstring() { }
hexstring(const W64 value, const int n) {
this->value = value;
this->n = n;
}
};
stringbuf& operator <<(stringbuf& os, const hexstring& hs);
DeclareStringBufToStream(hexstring);
struct bytestring {
const byte* bytes;
int n;
int splitat;
bytestring() { }
bytestring(const byte* bytes, int n, int splitat = 16) {
this->bytes = bytes;
this->n = n;
this->splitat = splitat;
}
};
stringbuf& operator <<(stringbuf& os, const bytestring& bs);
DeclareStringBufToStream(bytestring);
struct bytemaskstring {
const byte* bytes;
W64 mask;
int n;
int splitat;
bytemaskstring() { }
bytemaskstring(const byte* bytes, W64 mask, int n, int splitat = 16) {
assert(n <= 64);
this->bytes = bytes;
this->mask = mask;
this->n = n;
this->splitat = splitat;
}
};
stringbuf& operator <<(stringbuf& os, const bytemaskstring& bs);
DeclareStringBufToStream(bytemaskstring);
struct intstring {
W64s value;
int width;
intstring() { }
intstring(W64s value, int width) {
this->value = value;
this->width = width;
}
};
stringbuf& operator <<(stringbuf& os, const intstring& is);
DeclareStringBufToStream(intstring);
struct floatstring {
double value;
int width;
int precision;
floatstring() { }
floatstring(double value, int width = 0, int precision = 6) {
this->value = value;
this->width = width;
this->precision = precision;
}
};
stringbuf& operator <<(stringbuf& os, const floatstring& fs);
DeclareStringBufToStream(floatstring);
struct padstring {
const char* value;
int width;
char pad;
padstring() { }
padstring(const char* value, int width, char pad = ' ') {
this->value = value;
this->width = width;
this->pad = pad;
}
};
stringbuf& operator <<(stringbuf& os, const padstring& s);
DeclareStringBufToStream(padstring);
struct percentstring {
double fraction;
int width;
percentstring() { }
percentstring(W64s value, W64s total, int width = 7) {
fraction = (total) ? (double(value) / double(total)) : 0;
this->width = width;
}
};
static inline stringbuf& operator <<(stringbuf& os, const percentstring& ps) {
double f = ps.fraction * 100.;
W64s intpart = W64s(f);
W64s fracpart = clipto(W64s(((f - double(intpart)) * 100) + 0.5), W64s(0), W64s(99));
stringbuf sbfrac;
sbfrac << fracpart;
stringbuf sb;
sb << intpart, '.', padstring(sbfrac, 2, '0'), '%';
os << padstring(sb, ps.width);
return os;
}
DeclareStringBufToStream(percentstring);
struct substring {
const char* str;
int length;
substring() { }
substring(const char* str, int start, int length) {
int r = strlen(str);
this->length = min(length, r - start);
this->str = str + min(start, r);
}
};
stringbuf& operator <<(stringbuf& os, const substring& s);
DeclareStringBufToStream(substring);
//
// String tools
//
int stringsubst(stringbuf& sb, const char* pattern, const char* find, const char* replace);
int stringsubst(stringbuf& sb, const char* pattern, const char* find[], const char* replace[], int substcount);
class readline;
//
// istream class
//
class idstream {
protected:
int fd;
int error;
int eos;
int head;
int tail;
int bufsize;
int bufused;
W32 bufmask;
W64 offset;
byte* buf;
int fillbuf();
int readbuf(byte* dest, int bytes);
int unread(int bytes);
inline int addmod(int a, int b) { return ((a + b) & bufmask); }
inline void reset() { fd = -1; error = 0; eos = 0; head = 0; tail = 0; buf = null; bufused = 0; bufsize = 0; bufmask = 0; offset = 0; close_on_destroy = 1; }
public:
bool close_on_destroy;
idstream() { reset(); }
bool open(const char* filename, int bufsize = 65536);
bool open(int fd, int bufsize = 65536);
int setbuf(int bufsize);
idstream(const char* filename) {
reset();
open(filename);
}
idstream(int fd) {
reset();
open(fd);
}
void close();
~idstream() {
if likely (close_on_destroy) close();
}
bool ok() const { return (!error); }
operator bool() { return ok(); }
int read(void* data, int count);
int filehandle() const { return fd; }
int readline(char* v, int len);
int readline(stringbuf& sb);
bool getc(char& c);
W64 seek(W64 pos, int whence = SEEK_SET);
W64 where() const;
W64 size() const;
void* mmap(long long size);
};
template <typename T>
inline idstream& operator >>(idstream& is, T& v) {
is.read(&v, sizeof(T));
return is;
}
template <typename T>
inline idstream& operator ,(idstream& is, T& v) {
return is >> v;
}
class istream: public idstream {
public:
istream(): idstream() { }
istream(const char* filename): idstream(filename) { }
istream(int fd): idstream(fd) { }
};
class readline {
public:
readline(char* p, size_t l): buf(p), len(l) { }
char* buf;
size_t len;
};
//inline istream& operator ,(istream& is, const readline& v) { return is >> v; }
static inline istream& operator >>(istream& is, const readline& v) {
is.readline(v.buf, v.len);
return is;
}
static inline istream& operator >>(istream& is, stringbuf& sb) {
is.readline(sb);
return is;
}
//
// Global streams:
//
extern istream cin;
extern ostream cout;
extern ostream cerr;
//
// Turn a raw-memory into an array of <T> filled with <value>
//
template <typename T, bool> struct ArrayConstructor {
inline static void init(T* noalias p, size_t length) {
foreach (i, length) { new(p + i) T(); }
}
};
// Specialization for primitive initialization
template <typename T> struct ArrayConstructor<T, true> {
inline static void init(T* noalias p, size_t length) { }
};
template <typename T, bool> struct ArrayInitializer {
inline static void init(T* noalias p, size_t length, const T value) {
foreach (i, length) { new(p + i) T(value); }
}
};
// Specialization for primitive initialization
template <typename T> struct ArrayInitializer<T, true> {
inline static void init(T* noalias p, size_t length, const T value) {
foreach (i, length) { p[i] = value; }
}
};
template <typename T>
static inline T* renew(T* p, size_t oldcount, size_t newcount) {
if unlikely (newcount <= oldcount) return p;
T* pp = (T*)malloc(sizeof(T) * newcount);
if unlikely (!p) assert(oldcount == 0);
if likely (p) {
arraycopy(pp, p, oldcount);
free(p);
}
ArrayConstructor<T, isprimitive(T) | ispointer(T)>::init(pp + oldcount, newcount - oldcount);
return pp;
}
template <class T>
struct range {
T lo, hi;
range() { }
range(T v) { this->lo = v; this->hi = v; }
range(T lo, T hi) { this->lo = lo; this->hi = hi; }
range<T>& operator ()(T lo, T hi) { this->lo = lo; this->hi = hi; return *this; }
range<T>& operator ()(T v) { this->lo = v; this->hi = v; return *this; }
bool contains(T p) const { return ((p >= lo) && (p <= hi)); }
T size() const { return abs(hi - lo); }
bool operator &(T p) const { return contains(p); }
bool operator ~() const { return size(); }
ostream& print(ostream& os) const {
return os << '[', lo, ' ', hi, ']';
}
};
template <typename T>
static inline ostream& operator <<(ostream& os, const range<T>& r) {
return r.print(os);
}
/*
* Simple array class with optional bounds checking
*/
template <typename T, int size>
struct array {
public:
array() { }
static const int length = size;
T data[size];
const T& operator [](int i) const {
#ifdef CHECK_BOUNDS
assert((i >= 0) && (i < size));
#endif
return data[i];
}
T& operator [](int i) {
#ifdef CHECK_BOUNDS
assert((i >= 0) && (i < size));
#endif
return data[i];
}
void clear() {
foreach(i, size) data[i] = T();
}
void fill(const T& v) {
foreach(i, size) data[i] = v;
}
};
template <typename T, int size>
struct stack {
public:
T data[size];
int count;
static const int length = size;
void reset() { count = 0; }
stack() { reset(); }
const T& operator [](int i) const {
return data[i];
}
T& operator [](int i) {
return data[i];
}
T& push() {
if unlikely (count >= size) abort();
T& v = data[count++];
return v;
}
T& push(const T& v) {
T& r = push();
r = v;
return r;
}
T& pop() {
if unlikely (!count) abort();
T& v = data[--count];
return v;
}
bool empty() const { return (count == 0); }
bool full() const { return (count == size); }
stack<T, size>& operator =(const stack<T, size>& stack) {
count = stack.count;
foreach (i, count) data[i] = stack.data[i];
return *this;
}
ostream& print(ostream& os) const {
foreach (i, count) { os << ((i) ? " " : ""), data[i]; }
return os;
}
};
template <typename T, int size>
static inline ostream& operator <<(ostream& os, const stack<T, size>& st) {
return st.print(os);
}
template <typename T, int size>
static inline ostream& operator <<(ostream& os, const array<T, size>& v) {
os << "Array of ", size, " elements:", endl;
for (int i = 0; i < size; i++) {
os << " [", i, "]: ", v[i], endl;
}
return os;
}
/*
* Simple STL-like dynamic array class.
*/
template <class T>
class dynarray {
protected:
public:
T* data;
size_t length;
size_t reserved;
size_t granularity;
public:
inline T& operator [](int i) { return data[i]; }
inline T operator [](int i) const { return data[i]; }
operator T*() const { return data; }
// NOTE: g *must* be a power of two!
dynarray() {
length = reserved = 0;
granularity = 16;
data = null;
}
dynarray(int initcap, int g = 16) {
length = 0;
reserved = 0;
granularity = g;
data = null;
reserve(initcap);
}
~dynarray() {
if (!(isprimitive(T) | ispointer(T))) {
foreach (i, reserved) data[i].~T();
}
free(data);
data = null;
length = 0;
reserved = 0;
}
inline int capacity() const { return reserved; }
inline bool empty() const { return (length == 0); }
inline void clear() { resize(0); }
inline int size() const { return length; }
inline int count() const { return length; }
void push(const T& obj) {
T& pushed = push();
pushed = obj;
}
T& push() {
reserve(length + 1);
length++;
return data[length-1];
}
T& pop() {
length--;
return data[length];
}
void resize(int newsize) {
if likely (newsize > length) reserve(newsize);
length = newsize;
}
void resize(int newsize, const T& emptyvalue) {
int oldlength = length;
resize(newsize);
if unlikely (newsize <= oldlength) return;
for (int i = oldlength; i < reserved; i++) { data[i] = emptyvalue; }
}
void reserve(int newsize) {
if unlikely (newsize <= reserved) return;
newsize = (newsize + (granularity-1)) & ~(granularity-1);
int oldsize = length;
data = renew(data, length, newsize);
reserved = newsize;
}
void fill(const T value) {
foreach (i, length) {
data[i] = value;
}
}
// Only works with specialization for character arrays:
char* tokenize(char* string, const char* seplist) { abort(); }
};
template <class T> static inline const T& operator <<(dynarray<T>& buf, const T& v) { return buf.push(v); }
template <class T> static inline const T& operator >>(dynarray<T>& buf, T& v) { return (v = buf.pop()); }
template <>
char* dynarray<char*>::tokenize(char* string, const char* seplist);
template <class T>
static inline ostream& operator <<(ostream& os, const dynarray<T>& v) {
os << "Array of ", v.size(), " elements (", v.capacity(), " reserved): ", endl;
for (int i = 0; i < v.size(); i++) {
os << " [", i, "]: ", v[i], endl;
}
return os;
}
/*
* CRC32
*/
struct CRC32 {
static const W32 crctable[256];
W32 crc;
inline W32 update(byte value) {
crc = crctable[byte(crc ^ value)] ^ (crc >> 8);
return crc;
}
inline W32 update(byte* data, unsigned count) {
foreach (i, count) {
update(data[i]);
}
return crc;
}
CRC32() {
reset();
}
CRC32(W32 newcrc) {
reset(newcrc);
}
inline void reset(W32 newcrc = 0xffffffff) {
crc = newcrc;
}
operator W32() const {
return crc;
}
};
template <typename T>
static inline CRC32& operator <<(CRC32& crc, const T& t) {
crc.update((byte*)&t, sizeof(T));
return crc;
}
template <class T>
static inline CRC32& operator ,(CRC32& crc, const T& v) {
return crc << v;
}
//
// selflistlink class
// Double linked list without pointer: useful as root
// of inheritance hierarchy for another class to save
// space, since object pointed to is implied
//
class selflistlink {
public:
selflistlink* next;
selflistlink* prev;
public:
void reset() { next = null; prev = null; }
selflistlink() { reset(); }
selflistlink* unlink() {
if likely (prev) prev->next = next;
if likely (next) next->prev = prev;
prev = null;
next = null;
return this;
}
selflistlink* replacewith(selflistlink* newlink) {
if likely (prev) prev->next = newlink;
if likely (next) next->prev = newlink;
newlink->prev = prev;
newlink->next = next;
return newlink;
}
void addto(selflistlink*& root) {
// THIS <-> root <-> a <-> b <-> c
this->prev = (selflistlink*)&root;
this->next = root;
if likely (root) root->prev = this;
// Do not touch root->next since it might not even exist
root = this;
}
bool linked() const {
return (next || prev);
}
bool unlinked() const {
return !linked();
}
};
static inline ostream& operator <<(ostream& os, const selflistlink& link) {
return os << "[prev ", link.prev, ", next ", link.next, "]";
}