-
Notifications
You must be signed in to change notification settings - Fork 159
/
ds.h
4287 lines (3853 loc) · 86.3 KB
/
ds.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
/*
* Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DS_H_
#define DS_H_
#include <algorithm>
#include <stdexcept>
#include <utility>
#include <stdint.h>
#include <string.h>
#include <limits>
#include "assert_helpers.h"
#include "threading.h"
#include "random_source.h"
#include "btypes.h"
/**
* Tally how much memory is allocated to certain
*/
class MemoryTally {
public:
MemoryTally() : tot_(0), peak_(0) {
memset(tots_, 0, 256 * sizeof(uint64_t));
memset(peaks_, 0, 256 * sizeof(uint64_t));
}
/**
* Tally a memory allocation of size amt bytes.
*/
void add(int cat, uint64_t amt);
/**
* Tally a memory free of size amt bytes.
*/
void del(int cat, uint64_t amt);
/**
* Return the total amount of memory allocated.
*/
uint64_t total() { return tot_; }
/**
* Return the total amount of memory allocated in a particular
* category.
*/
uint64_t total(int cat) { return tots_[cat]; }
/**
* Return the peak amount of memory allocated.
*/
uint64_t peak() { return peak_; }
/**
* Return the peak amount of memory allocated in a particular
* category.
*/
uint64_t peak(int cat) { return peaks_[cat]; }
#ifndef NDEBUG
/**
* Check that memory tallies are internally consistent;
*/
bool repOk() const {
uint64_t tot = 0;
for(int i = 0; i < 256; i++) {
assert_leq(tots_[i], peaks_[i]);
tot += tots_[i];
}
assert_eq(tot, tot_);
return true;
}
#endif
protected:
MUTEX_T mutex_m;
uint64_t tots_[256];
uint64_t tot_;
uint64_t peaks_[256];
uint64_t peak_;
};
#ifdef USE_MEM_TALLY
extern MemoryTally gMemTally;
#endif
/**
* A simple fixed-length array of type T, automatically freed in the
* destructor.
*/
template<typename T>
class AutoArray {
public:
AutoArray(size_t sz, int cat = 0) : cat_(cat) {
t_ = NULL;
t_ = new T[sz];
#ifdef USE_MEM_TALLY
gMemTally.add(cat_, sz);
#else
(void)cat_;
#endif
memset(t_, 0, sz * sizeof(T));
sz_ = sz;
}
~AutoArray() {
if(t_ != NULL) {
delete[] t_;
#ifdef USE_MEM_TALLY
gMemTally.del(cat_, sz_);
#endif
}
}
T& operator[](size_t sz) {
return t_[sz];
}
const T& operator[](size_t sz) const {
return t_[sz];
}
size_t size() const { return sz_; }
private:
int cat_;
T *t_;
size_t sz_;
};
/**
* A wrapper for a non-array pointer that associates it with a memory
* category for tracking purposes and calls delete on it when the
* PtrWrap is destroyed.
*/
template<typename T>
class PtrWrap {
public:
explicit PtrWrap(
T* p,
bool freeable = true,
int cat = 0) :
cat_(cat),
p_(NULL)
{
init(p, freeable);
}
explicit PtrWrap(int cat = 0) :
cat_(cat),
p_(NULL)
{
reset();
}
void reset() {
free();
init(NULL);
}
~PtrWrap() { free(); }
void init(T* p, bool freeable = true) {
assert(p_ == NULL);
p_ = p;
freeable_ = freeable;
#ifdef USE_MEM_TALLY
if(p != NULL && freeable_) {
gMemTally.add(cat_, sizeof(T));
}
#else
(void)cat_;
#endif
}
void free() {
if(p_ != NULL) {
if(freeable_) {
delete p_;
#ifdef USE_MEM_TALLY
gMemTally.del(cat_, sizeof(T));
#endif
}
p_ = NULL;
}
}
inline T* get() { return p_; }
inline const T* get() const { return p_; }
private:
int cat_;
T *p_;
bool freeable_;
};
/**
* A wrapper for an array pointer that associates it with a memory
* category for tracking purposes and calls delete[] on it when the
* PtrWrap is destroyed.
*/
template<typename T>
class APtrWrap {
public:
explicit APtrWrap(
T* p,
size_t sz,
bool freeable = true,
int cat = 0) :
cat_(cat),
p_(NULL)
{
init(p, sz, freeable);
}
explicit APtrWrap(int cat = 0) :
cat_(cat),
p_(NULL)
{
reset();
}
void reset() {
free();
init(NULL, 0);
}
~APtrWrap() { free(); }
void init(T* p, size_t sz, bool freeable = true) {
assert(p_ == NULL);
p_ = p;
sz_ = sz;
freeable_ = freeable;
#ifdef USE_MEM_TALLY
if(p != NULL && freeable_) {
gMemTally.add(cat_, sizeof(T) * sz_);
}
#else
(void)cat_;
#endif
}
void free() {
if(p_ != NULL) {
if(freeable_) {
delete[] p_;
#ifdef USE_MEM_TALLY
gMemTally.del(cat_, sizeof(T) * sz_);
#endif
}
p_ = NULL;
}
}
inline T* get() { return p_; }
inline const T* get() const { return p_; }
private:
int cat_;
T *p_;
bool freeable_;
size_t sz_;
};
/**
* An EList<T> is an expandable list with these features:
*
* - Payload type is a template parameter T.
* - Initial size can be specified at construction time, otherwise
* default of 128 is used.
* - When allocated initially or when expanding, the new[] operator is
* used, which in turn calls the default constructor for T.
* - All copies (e.g. assignment of a const T& to an EList<T> element,
* or during expansion) use operator=.
* - When the EList<T> is resized to a smaller size (or cleared, which
* is like resizing to size 0), the underlying containing is not
* reshaped. Thus, ELists<T>s never release memory before
* destruction.
*
* And these requirements:
*
* - Payload type T must have a default constructor.
*
* For efficiency reasons, ELists should not be declared on the stack
* in often-called worker functions. Best practice is to declare
* ELists at a relatively stable layer of the stack (such that it
* rarely bounces in and out of scope) and let the worker function use
* it and *expand* it only as needed. The effect is that only
* relatively few allocations and copies will be incurred, and they'll
* occur toward the beginning of the computation before stabilizing at
* a "high water mark" for the remainder of the computation.
*
* A word about multidimensional lists. One way to achieve a
* multidimensional lists is to nest ELists. This works, but it often
* involves a lot more calls to the default constructor and to
* operator=, especially when the outermost EList needs expanding, than
* some of the alternatives. One alternative is use a most specialized
* container that still uses ELists but knows to use xfer instead of
* operator= when T=EList.
*
* The 'cat_' fiends encodes a category. This makes it possible to
* distinguish between object subgroups in the global memory tally.
*
* Memory allocation is lazy. Allocation is only triggered when the
* user calls push_back, expand, resize, or another function that
* increases the size of the list. This saves memory and also makes it
* easier to deal with nested ELists, since the default constructor
* doesn't set anything in stone.
*/
template <typename T, int S = 128>
class EList {
public:
/**
* Allocate initial default of S elements.
*/
explicit EList() :
cat_(0), allocCat_(-1), list_(NULL), sz_(S), cur_(0)
{
#ifndef USE_MEM_TALLY
(void)cat_;
#endif
}
/**
* Allocate initial default of S elements.
*/
explicit EList(int cat) :
cat_(cat), allocCat_(-1), list_(NULL), sz_(S), cur_(0)
{
assert_geq(cat, 0);
}
/**
* Initially allocate given number of elements; should be > 0.
*/
explicit EList(size_t isz, int cat = 0) :
cat_(cat), allocCat_(-1), list_(NULL), sz_(isz), cur_(0)
{
assert_geq(cat, 0);
}
/**
* Copy from another EList using operator=.
*/
EList(const EList<T, S>& o) :
cat_(0), allocCat_(-1), list_(NULL), sz_(0), cur_(0)
{
*this = o;
}
/**
* Copy from another EList using operator=.
*/
explicit EList(const EList<T, S>& o, int cat) :
cat_(cat), allocCat_(-1), list_(NULL), sz_(0), cur_(0)
{
*this = o;
assert_geq(cat, 0);
}
/**
* Destructor.
*/
~EList() { free(); }
/**
* Make this object into a copy of o by allocat
*/
EList<T, S>& operator=(const EList<T, S>& o) {
assert_eq(cat_, o.cat());
if(o.cur_ == 0) {
// Nothing to copy
cur_ = 0;
return *this;
}
if(list_ == NULL) {
// cat_ should already be set
lazyInit();
}
if(sz_ < o.cur_) expandNoCopy(o.cur_ + 1);
assert_geq(sz_, o.cur_);
cur_ = o.cur_;
for(size_t i = 0; i < cur_; i++) {
list_[i] = o.list_[i];
}
return *this;
}
/**
* Transfer the guts of another EList into this one without using
* operator=, etc. We have to set EList o's list_ field to NULL to
* avoid o's destructor from deleting list_ out from under us.
*/
void xfer(EList<T, S>& o) {
// What does it mean to transfer to a different-category list?
assert_eq(cat_, o.cat());
// Can only transfer into an empty object
free();
allocCat_ = cat_;
list_ = o.list_;
sz_ = o.sz_;
cur_ = o.cur_;
o.list_ = NULL;
o.sz_ = o.cur_ = 0;
o.allocCat_ = -1;
}
/**
* Return number of elements.
*/
inline size_t size() const { return cur_; }
/**
* Return number of elements allocated.
*/
inline size_t capacity() const { return sz_; }
/**
* Return the total size in bytes occupied by this list.
*/
size_t totalSizeBytes() const {
return 2 * sizeof(int) +
2 * sizeof(size_t) +
cur_ * sizeof(T);
}
/**
* Return the total capacity in bytes occupied by this list.
*/
size_t totalCapacityBytes() const {
return 2 * sizeof(int) +
2 * sizeof(size_t) +
sz_ * sizeof(T);
}
/**
* Ensure that there is sufficient capacity to expand to include
* 'thresh' more elements without having to expand.
*/
inline void ensure(size_t thresh) {
if(list_ == NULL) lazyInit();
expandCopy(cur_ + thresh);
}
/**
* Ensure that there is sufficient capacity to include 'newsz' elements.
* If there isn't enough capacity right now, expand capacity to exactly
* equal 'newsz'.
*/
inline void reserveExact(size_t newsz) {
if(list_ == NULL) lazyInitExact(newsz);
expandCopyExact(newsz);
}
/**
* Return true iff there are no elements.
*/
inline bool empty() const { return cur_ == 0; }
/**
* Return true iff list hasn't been initialized yet.
*/
inline bool null() const { return list_ == NULL; }
/**
* Add an element to the back and immediately initialize it via
* operator=.
*/
void push_back(const T& el) {
if(list_ == NULL) lazyInit();
if(cur_ == sz_) expandCopy(sz_+1);
list_[cur_++] = el;
}
/**
* Add an element to the back. No intialization is done.
*/
void expand() {
if(list_ == NULL) lazyInit();
if(cur_ == sz_) expandCopy(sz_+1);
cur_++;
}
/**
* Add an element to the back. No intialization is done.
*/
void fill(size_t begin, size_t end, const T& v) {
assert_leq(begin, end);
assert_leq(end, cur_);
for(size_t i = begin; i < end; i++) {
list_[i] = v;
}
}
/**
* Add an element to the back. No intialization is done.
*/
void fill(const T& v) {
for(size_t i = 0; i < cur_; i++) {
list_[i] = v;
}
}
/**
* Set all bits in specified range of elements in list array to 0.
*/
void fillZero(size_t begin, size_t end) {
assert_leq(begin, end);
memset(&list_[begin], 0, sizeof(T) * (end-begin));
}
/**
* Set all bits in the list array to 0.
*/
void fillZero() {
memset(list_, 0, sizeof(T) * cur_);
}
/**
* If size is less than requested size, resize up to at least sz
* and set cur_ to requested sz.
*/
void resizeNoCopy(size_t sz) {
if(sz > 0 && list_ == NULL) lazyInit();
if(sz <= cur_) {
cur_ = sz;
return;
}
if(sz_ < sz) expandNoCopy(sz);
cur_ = sz;
}
/**
* If size is less than requested size, resize up to at least sz
* and set cur_ to requested sz.
*/
void resize(size_t sz) {
if(sz > 0 && list_ == NULL) lazyInit();
if(sz <= cur_) {
cur_ = sz;
return;
}
if(sz_ < sz) {
expandCopy(sz);
}
cur_ = sz;
}
/**
* If size is less than requested size, resize up to exactly sz and set
* cur_ to requested sz.
*/
void resizeExact(size_t sz) {
if(sz > 0 && list_ == NULL) lazyInitExact(sz);
if(sz <= cur_) {
cur_ = sz;
return;
}
if(sz_ < sz) expandCopyExact(sz);
cur_ = sz;
}
/**
* Erase element at offset idx.
*/
void erase(size_t idx) {
assert_lt(idx, cur_);
for(size_t i = idx; i < cur_-1; i++) {
list_[i] = list_[i+1];
}
cur_--;
}
/**
* Erase range of elements starting at offset idx and going for len.
*/
void erase(size_t idx, size_t len) {
assert_geq(len, 0);
if(len == 0) {
return;
}
assert_lt(idx, cur_);
for(size_t i = idx; i < cur_-len; i++) {
list_[i] = list_[i+len];
}
cur_ -= len;
}
/**
* Insert value 'el' at offset 'idx'
*/
void insert(const T& el, size_t idx) {
if(list_ == NULL) lazyInit();
assert_leq(idx, cur_);
if(cur_ == sz_) expandCopy(sz_+1);
for(size_t i = cur_; i > idx; i--) {
list_[i] = list_[i-1];
}
list_[idx] = el;
cur_++;
}
/**
* Insert contents of list 'l' at offset 'idx'
*/
void insert(const EList<T>& l, size_t idx) {
if(list_ == NULL) lazyInit();
assert_lt(idx, cur_);
if(l.cur_ == 0) return;
if(cur_ + l.cur_ > sz_) expandCopy(cur_ + l.cur_);
for(size_t i = cur_ + l.cur_ - 1; i > idx + (l.cur_ - 1); i--) {
list_[i] = list_[i - l.cur_];
}
for(size_t i = 0; i < l.cur_; i++) {
list_[i+idx] = l.list_[i];
}
cur_ += l.cur_;
}
/**
* Remove an element from the top of the stack.
*/
void pop_back() {
assert_gt(cur_, 0);
cur_--;
}
/**
* Make the stack empty.
*/
void clear() {
cur_ = 0; // re-use stack memory
// Don't clear heap; re-use it
}
/**
* Get the element on the top of the stack.
*/
inline T& back() {
assert_gt(cur_, 0);
return list_[cur_-1];
}
/**
* Reverse list elements.
*/
void reverse() {
if(cur_ > 1) {
size_t n = cur_ >> 1;
for(size_t i = 0; i < n; i++) {
T tmp = list_[i];
list_[i] = list_[cur_ - i - 1];
list_[cur_ - i - 1] = tmp;
}
}
}
/**
* Get the element on the top of the stack, const version.
*/
inline const T& back() const {
assert_gt(cur_, 0);
return list_[cur_-1];
}
/**
* Get the frontmost element (bottom of stack).
*/
inline T& front() {
assert_gt(cur_, 0);
return list_[0];
}
/**
* Get the element on the bottom of the stack, const version.
*/
inline const T& front() const { return front(); }
/**
* Return true iff this list and list o contain the same elements in the
* same order according to type T's operator==.
*/
bool operator==(const EList<T, S>& o) const {
if(size() != o.size()) {
return false;
}
for(size_t i = 0; i < size(); i++) {
if(!(get(i) == o.get(i))) {
return false;
}
}
return true;
}
/**
* Return true iff this list contains all of the elements in o according to
* type T's operator==.
*/
bool isSuperset(const EList<T, S>& o) const {
if(o.size() > size()) {
// This can't be a superset if the other set contains more elts
return false;
}
// For each element in o
for(size_t i = 0; i < o.size(); i++) {
bool inthis = false;
// Check if it's in this
for(size_t j = 0; j < size(); j++) {
if(o[i] == (*this)[j]) {
inthis = true;
break;
}
}
if(!inthis) {
return false;
}
}
return true;
}
/**
* Return a reference to the ith element.
*/
inline T& operator[](size_t i) {
assert_lt(i, cur_);
return list_[i];
}
/**
* Return a reference to the ith element.
*/
inline const T& operator[](size_t i) const {
assert_lt(i, cur_);
return list_[i];
}
/**
* Return a reference to the ith element.
*/
inline T& get(size_t i) {
return operator[](i);
}
/**
* Return a reference to the ith element.
*/
inline const T& get(size_t i) const {
return operator[](i);
}
/**
* Return a reference to the ith element. This version is not
* inlined, which guarantees we can use it from the debugger.
*/
T& getSlow(size_t i) {
return operator[](i);
}
/**
* Return a reference to the ith element. This version is not
* inlined, which guarantees we can use it from the debugger.
*/
const T& getSlow(size_t i) const {
return operator[](i);
}
/**
* Sort some of the contents.
*/
void sortPortion(size_t begin, size_t num) {
assert_leq(begin+num, cur_);
if(num < 2) return;
std::stable_sort(list_ + begin, list_ + begin + num);
}
/**
* Shuffle a portion of the list.
*/
void shufflePortion(size_t begin, size_t num, RandomSource& rnd) {
assert_leq(begin+num, cur_);
if(num < 2) return;
size_t left = num;
for(size_t i = begin; i < begin + num - 1; i++) {
size_t rndi = rnd.nextSizeT() % left;
if(rndi > 0) {
std::swap(list_[i], list_[i + rndi]);
}
left--;
}
}
/**
* Sort contents
*/
void sort() {
sortPortion(0, cur_);
}
/**
* Return true iff every element is < its successor. Only operator< is
* used.
*/
bool sorted() const {
for(size_t i = 1; i < cur_; i++) {
if(!(list_[i-1] < list_[i])) {
return false;
}
}
return true;
}
/**
* Delete element at position 'idx'; slide subsequent chars up.
*/
void remove(size_t idx) {
assert_lt(idx, cur_);
assert_gt(cur_, 0);
for(size_t i = idx; i < cur_-1; i++) {
list_[i] = list_[i+1];
}
cur_--;
}
/**
* Return a pointer to the beginning of the buffer.
*/
T *ptr() { return list_; }
/**
* Return a const pointer to the beginning of the buffer.
*/
const T *ptr() const { return list_; }
/**
* Set the memory category for this object.
*/
void setCat(int cat) {
// What does it mean to set the category after the list_ is
// already allocated?
assert(null());
assert_gt(cat, 0); cat_ = cat;
}
/**
* Return memory category.
*/
int cat() const { return cat_; }
/**
* Perform a binary search for the first element that is not less
* than 'el'. Return cur_ if all elements are less than el.
*/
size_t bsearchLoBound(const T& el) const {
size_t hi = cur_;
size_t lo = 0;
while(true) {
if(lo == hi) {
return lo;
}
size_t mid = lo + ((hi-lo)>>1);
assert_neq(mid, hi);
if(list_[mid] < el) {
if(lo == mid) {
return hi;
}
lo = mid;
} else {
hi = mid;
}
}
}
private:
/**
* Initialize memory for EList.
*/
void lazyInit() {
assert(list_ == NULL);
list_ = alloc(sz_);
}
/**
* Initialize exactly the prescribed number of elements for EList.
*/
void lazyInitExact(size_t sz) {
assert_gt(sz, 0);
assert(list_ == NULL);
sz_ = sz;
list_ = alloc(sz);
}
/**
* Allocate a T array of length sz_ and store in list_. Also,
* tally into the global memory tally.
*/
T *alloc(size_t sz) {
T* tmp = new T[sz];
assert(tmp != NULL);
#ifdef USE_MEM_TALLY
gMemTally.add(cat_, sz);
#endif
allocCat_ = cat_;
return tmp;
}
/**
* Allocate a T array of length sz_ and store in list_. Also,
* tally into the global memory tally.
*/
void free() {
if(list_ != NULL) {
assert_neq(-1, allocCat_);
assert_eq(allocCat_, cat_);
delete[] list_;
#ifdef USE_MEM_TALLY
gMemTally.del(cat_, sz_);
#endif
list_ = NULL;
sz_ = cur_ = 0;
}
}
/**
* Expand the list_ buffer until it has at least 'thresh' elements. Size
* increases quadratically with number of expansions. Copy old contents
* into new buffer using operator=.
*/
void expandCopy(size_t thresh) {
if(thresh <= sz_) return;
size_t newsz = (sz_ * 2)+1;
while(newsz < thresh) newsz *= 2;
expandCopyExact(newsz);
}
/**
* Expand the list_ buffer until it has exactly 'newsz' elements. Copy
* old contents into new buffer using operator=.
*/
void expandCopyExact(size_t newsz) {
if(newsz <= sz_) return;
T* tmp = alloc(newsz);
assert(tmp != NULL);
size_t cur = cur_;
if(list_ != NULL) {
for(size_t i = 0; i < cur_; i++) {
// Note: operator= is used
tmp[i] = list_[i];
}
free();
}
list_ = tmp;
sz_ = newsz;
cur_ = cur;
}
/**
* Expand the list_ buffer until it has at least 'thresh' elements.
* Size increases quadratically with number of expansions. Don't copy old
* contents into the new buffer.
*/
void expandNoCopy(size_t thresh) {
assert(list_ != NULL);
if(thresh <= sz_) return;
size_t newsz = (sz_ * 2)+1;
while(newsz < thresh) newsz *= 2;
expandNoCopyExact(newsz);
}
/**
* Expand the list_ buffer until it has exactly 'newsz' elements. Don't
* copy old contents into the new buffer.
*/
void expandNoCopyExact(size_t newsz) {
assert(list_ != NULL);