-
Notifications
You must be signed in to change notification settings - Fork 22
/
vecbase.hh
688 lines (658 loc) · 23.5 KB
/
vecbase.hh
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
/* Ad-hoc programming editor for DOSBox -- (C) 2011-03-08 Joel Yliluoma */
/* A std::vector replacement for pre-standard compilers
* that do not support templates at all.
*
* #defines to add before #include: T = element type
* VecType = name of the vector class type to define
* UsePlacementNew = if #defined, call Construct() -- When T is another vector type.
*/
#if defined(__cplusplus) && __cplusplus >= 199711L
#include <vector>
#define q(T,VecType) typedef std::vector<T> VecType;
o(q)
#undef q
#else
#ifndef vecBaseIncludes
# define vecBaseIncludes
# include <stdlib.h>
# include <malloc.h>
# include <stdio.h>
#endif
#define q(T,VecType) class VecType
o(q)
#undef q
{
public:
#define q(TT,VT) typedef TT T; typedef VT VecType;
o(q)
#undef q
#define q(T,VecType) VecType
typedef T value_type;
typedef T * iterator;
typedef T * pointer;
typedef T & reference;
typedef T const * const_iterator;
typedef T const * const_pointer;
typedef T const & const_reference;
typedef size_t size_type;
#ifdef UsePlacementNew
#define Ttype T const&
#else
#define Ttype T
#endif
public:
#if !(defined(__cplusplus) && __cplusplus >= 199700L)
// Construct() and Destruct() are needed only
// because of lack of placement-new in pre-standard C++
void Construct() { data=0; len=0; cap=0; }
void Destruct() { clear(); if(cap) deallocate(data,cap); }
void Construct(const VecType& b)
{
data=0; len=b.len; cap=b.len;
if(len)
{
data = allocate(len);
if(!data) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)len);
copy_construct(&data[0], &b.data[0],len);
}
}
#endif
o(q) () : data(0),len(0),cap(0) { }
~o(q) () { clear(); if(cap) deallocate(data,cap); }
o(q) (size_type length) : data(0),len(0),cap(0)
{
resize(length);
}
o(q) (size_type length, Ttype value) : data(0),len(0),cap(0)
{
resize(length, value);
}
o(q) (T const* first, T const* last) : data(0),len(0),cap(0)
{
assign(first, last);
}
o(q) (const VecType& b) : data(0), len(b.len), cap(b.len)
{
if(len)
{
data = allocate(len);
if(!data) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)len);
copy_construct(&data[0], &b.data[0], len);
}
}
#if defined(__cplusplus) && __cplusplus >= 201100L
o(q)(VecType&& b): data(b.data), len(b.len), cap(b.cap)
{
b.data = 0;
b.len = 0;
b.cap = 0;
}
#endif
VecType& operator= (const VecType& b)
{
if(&b == this) return *this;
if(len < b.len)
{
reserve(b.len);
copy_assign(&data[0], &b.data[0], len);
copy_construct(&data[len], &b.data[len], b.len-len);
}
else if(len > b.len)
{
destroy(&data[b.len], len-b.len);
copy_assign(&data[0], &b.data[0], b.len);
}
len = b.len;
return *this;
}
#if defined(__cplusplus) && __cplusplus >= 201100L
VecType& operator= (VecType&& b)
{
if(&b != this) swap(b);
return *this;
}
#endif
void assign(T const* first,
T const* last)
{
size_type newlen = (size_type) (last-first);
if(cap < newlen)
{
destroy(&data[0], len);
deallocate(data, cap);
data = allocate(cap = newlen);
if(!data) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newlen);
copy_construct(&data[0], first, newlen);
}
else if(len < newlen)
{
copy_assign(&data[0], first, len);
copy_construct(&data[len], first+len, newlen-len);
}
else // len >= newlen
{
copy_assign(&data[0], first, newlen);
destroy(&data[newlen], len-newlen);
}
len = newlen;
}
void assign(size_type newlen, Ttype value)
{
#if 0
clear();
resize(newlen, value);
#else
if(cap < newlen)
{
destroy(&data[0], len);
deallocate(data, cap);
data = allocate(cap = newlen);
if(!data) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newlen);
construct(&data[0], newlen, value);
}
else if(len < newlen)
{
for(size_type a=0; a<len; ++a)
data[a] = value;
construct(&data[len], newlen-len, value);
}
else // len >= newlen
{
for(size_type a=0; a<newlen; ++a)
data[a] = value;
destroy(&data[newlen], len-newlen);
}
len = newlen;
#endif
}
public:
reference operator[] (size_type ind) { return data[ind]; }
const_reference operator[] (size_type ind) const { return data[ind]; }
iterator begin() { return data; }
iterator end() { return data+len; }
reference front() { return *begin(); }
reference back() { return (*this)[size()-1]; } //*rbegin(); }
const_iterator begin() const { return data; }
const_iterator end() const { return data+len; }
const_reference front() const { return *begin(); }
const_reference back() const { return (*this)[size()-1]; }//*rbegin(); }
/*
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
reverse_iterator rbegin() { return reverse_iterator( end() ); }
reverse_iterator rend() { return reverse_iterator( begin() ); }
const_reverse_iterator rbegin() const { return const_reverse_iterator( end() ); }
const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
*/
void push_back(Ttype value)
{
//insert(end(), value);
if(len >= cap) reserve(cap ? cap*2 : default_size());
#ifdef UsePlacementNew
data[len++].Construct(value);
#else
/*new(&data[len++]) T ( value );*/ data[len++] = value;
#endif
}
iterator insert(iterator pos, Ttype value)
{
size_type ins_pos = pos - begin();
if(len < cap)
{
if(ins_pos == len)
{
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&data[ins_pos]) T( value );
#elif defined(UsePlacementNew)
data[ins_pos].Construct(value);
#else
data[ins_pos] = value;
#endif
}
else
{
move_construct(&data[len], &data[len-1], 1);
move_assign_backwards(&data[ins_pos+1], &data[ins_pos], len-ins_pos);
data[ins_pos] = value;
}
++len;
return data+ins_pos;
}
size_type newcap = cap ? cap*2 : default_size();
if(ins_pos == len)
{
reserve(newcap);
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&data[ins_pos]) T( value );
#elif defined(UsePlacementNew)
data[ins_pos].Construct(value);
#else
data[ins_pos] = value;
#endif
++len;
return data+ins_pos;
}
T * newdata = allocate(newcap);
if(!newdata) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newcap);
move_construct(&newdata[0], &data[0], ins_pos);
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&newdata[ins_pos]) T( value );
#elif defined(UsePlacementNew)
newdata[ins_pos].Construct(value);
#else
newdata[ins_pos] = value;
#endif
move_construct(&newdata[ins_pos+1], &data[ins_pos], len-ins_pos);
destroy(&data[0], len);
deallocate(data, cap);
++len;
data = newdata;
cap = newcap;
return data+ins_pos;
}
void insert(iterator pos,
T const* first,
T const* last)
{
size_type ins_pos = pos - begin();
size_type count = (size_type) (last-first);
if(len+count <= cap)
{
if(ins_pos == len)
copy_construct(&data[ins_pos], first, count);
else
{
/*******
* 012345678
* ^NM
* ins_pos = 5, count = 2
* len = 9, new_length = 11
* tail_length = 4 ("5678")
* tail_source_pos = 5
* tail_target_pos = 7
* num_tail_bytes_after_current_area = min(tail_length, count)
*/
//unsigned new_length = len + count;
unsigned tail_length = len - ins_pos;
unsigned tail_source_pos = ins_pos;
unsigned tail_target_pos = ins_pos + count;
unsigned num_tail_bytes_after_current_area =
count < tail_length ? count : tail_length;
unsigned num_tail_bytes_to_movecopy =
tail_length - num_tail_bytes_after_current_area;
#ifdef UsePlacementNew
/*fprintf(stdout, "Input (%u):", count);
{for(unsigned a=0; a<count; ++a) fprintf(stdout, "\n\t%p", &first[a][0]);}
fprintf(stdout, "\n");
fprintf(stdout, "Before (%u + %u):", len, cap);
{for(unsigned a=0; a<len; ++a) fprintf(stdout, "\n\t%p", &data[a][0]);}
{for(unsigned a=len; a<cap; ++a) fprintf(stdout, "\n\t[%p]", &data[a][0]);}
fprintf(stdout, "\n");*/
#else
/*fprintf(stdout, "Input (%u): '%.*s'\n", count, count*sizeof(T), (const char*)first);
fprintf(stdout, "Before (%u + %u):", len,cap);
fprintf(stdout, "\n\t%u:\t'%.*s'\n\t%u:\t'%.*s'\n",
len, len*sizeof(T), (const char*)data,
(cap-len), (cap-len)*sizeof(T), (const char*)(data+len));*/
#endif
move_construct(&data[tail_target_pos + num_tail_bytes_to_movecopy],
&data[tail_source_pos+num_tail_bytes_to_movecopy],
num_tail_bytes_after_current_area);
#ifdef UsePlacementNew
/*fprintf(stdout, "After move_construct(%u,%u,%u):",
tail_target_pos + num_tail_bytes_to_movecopy,
tail_source_pos + num_tail_bytes_to_movecopy,
num_tail_bytes_after_current_area);
{for(unsigned a=0; a<len; ++a) fprintf(stdout, "\n\t%p", &data[a][0]);}
{for(unsigned a=len; a<cap; ++a) fprintf(stdout, "\n\t[%p]", &data[a][0]);}
fprintf(stdout, "\n");*/
#else
/*fprintf(stdout, "After move_construct(%u,%u,%u):",
tail_target_pos + num_tail_bytes_to_movecopy,
tail_source_pos + num_tail_bytes_to_movecopy,
num_tail_bytes_after_current_area);
fprintf(stdout, "\n\t%u:\t'%.*s'\n\t%u:\t'%.*s'\n",
len, len*sizeof(T), (const char*)data,
(cap-len), (cap-len)*sizeof(T), (const char*)(data+len));*/
#endif
if(num_tail_bytes_to_movecopy > 0)
{
move_assign_backwards(
&data[ins_pos+count],
&data[ins_pos],
num_tail_bytes_to_movecopy);
#ifdef UsePlacementNew
/*fprintf(stdout, "After move_assign_backwards(%u,%u,%u):", ins_pos+count,ins_pos,num_tail_bytes_to_movecopy);
{for(unsigned a=0; a<len; ++a) fprintf(stdout, "\n\t%p", &data[a][0]);}
{for(unsigned a=len; a<cap; ++a) fprintf(stdout, "\n\t[%p]", &data[a][0]);}
fprintf(stdout, "\n");*/
#else
/*fprintf(stdout, "After move_assign_backwards(%u,%u,%u):", ins_pos+count,ins_pos,num_tail_bytes_to_movecopy);
fprintf(stdout, "\n\t%u:\t'%.*s'\n\t%u:\t'%.*s'\n",
len, len*sizeof(T), (const char*)data,
(cap-len), (cap-len)*sizeof(T), (const char*)(data+len));*/
#endif
}
unsigned num_piece_bytes_after_current_area =
(ins_pos + count) > len ? (ins_pos+count)-len : 0;
unsigned num_piece_bytes_to_copy =
count - num_piece_bytes_after_current_area;
if(num_piece_bytes_to_copy > 0)
{
copy_assign(
&data[ins_pos],
first,
num_piece_bytes_to_copy);
#ifdef UsePlacementNew
/*fprintf(stdout, "After copy_assign(%u,%u):", ins_pos,num_piece_bytes_to_copy);
{for(unsigned a=0; a<len; ++a) fprintf(stdout, "\n\t%p", &data[a][0]);}
{for(unsigned a=len; a<cap; ++a) fprintf(stdout, "\n\t[%p]", &data[a][0]);}
fprintf(stdout, "\n");
fflush(stdout);*/
#else
/*fprintf(stdout, "After copy_assign(%u,%u):", ins_pos,num_piece_bytes_to_copy);
fprintf(stdout, "\n\t%u:\t'%.*s'\n\t%u:\t'%.*s'\n",
len, len*sizeof(T), (const char*)data,
(cap-len), (cap-len)*sizeof(T), (const char*)(data+len));
fflush(stdout);*/
#endif
}
if(num_piece_bytes_after_current_area > 0)
{
copy_construct(
&data[len],
first+num_piece_bytes_to_copy,
num_piece_bytes_after_current_area);
#ifdef UsePlacementNew
/*fprintf(stdout, "After copy_construct(%u,%u):", len, num_piece_bytes_after_current_area);
{for(unsigned a=0; a<len; ++a) fprintf(stdout, "\n\t%p", &data[a][0]);}
{for(unsigned a=len; a<cap; ++a) fprintf(stdout, "\n\t[%p]", &data[a][0]);}
fprintf(stdout, "\n");
fflush(stdout);*/
#else
/*fprintf(stdout, "After copy_construct(%u,%u):", len, num_piece_bytes_after_current_area);
fprintf(stdout, "\n\t%u:\t'%.*s'\n\t%u:\t'%.*s'\n",
len, len*sizeof(T), (const char*)data,
(cap-len), (cap-len)*sizeof(T), (const char*)(data+len));
fflush(stdout);*/
#endif
}
}
len += count;
goto Done;
}
{
size_type newcap = (cap+count)*2;
/*if(ins_pos == len)
{
reserve(newcap);
copy_construct(&data[ins_pos], first, count);
len += count;
goto Done;
}*/
T * newdata = allocate(newcap);
if(!newdata) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newcap);
move_construct(&newdata[0], &data[0], ins_pos);
copy_construct(&newdata[ins_pos], first, count);
move_construct(&newdata[ins_pos+count], &data[ins_pos], len-ins_pos);
#ifndef UsePlacementNew
/*fprintf(stdout, "Became '%.*s'\n", (len+count)*sizeof(T), (const char*)newdata);*/
#endif
destroy(&data[0], len);
deallocate(data, cap);
len += count;
data = newdata;
cap = newcap;
}
Done:;
#ifndef UsePlacementNew
/*fprintf(stdout, "Indeed, became '%.*s'\n", len*sizeof(T), (const char*)data);*/
#endif
}
void erase(iterator pos)
{
size_type del_pos = pos - begin();
move_assign(&data[del_pos], &data[del_pos+1], len-del_pos-1);
destroy(&data[--len], 1);
}
void erase(iterator first, iterator last)
{
size_type del_pos = first - begin();
size_type count = last - first;
if(!count) return;
move_assign(&data[del_pos], &data[del_pos+count], len-del_pos-count);
destroy(&data[len-count], count);
len -= count;
}
void shrink_to(size_type newcap)
{
if(newcap == cap) return;
T * newdata = allocate(newcap);
if(!newdata) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newcap);
move_construct(&newdata[0], &data[0], len);
destroy(&data[0], len);
deallocate(data, cap);
data = newdata;
cap = newcap;
}
void reserve(size_type newcap)
{
if(cap < newcap) shrink_to(newcap);
}
void shrink_to_fit()
{
shrink_to(size());
}
void pop_back()
{
destroy(&data[--len], 1);
}
void resize(size_type newlen)
{
if(newlen < len)
{
destroy(&data[newlen], len-newlen);
len = newlen;
}
else if(newlen == len)
return;
else if(newlen <= cap) // newlen > len, too.
{
construct(&data[len], newlen-len);
len = newlen;
}
else // newlen > cap, and newlen > len.
{
// reallocation required
size_type newcap = newlen;
T * newdata = allocate(newcap);
if(!newdata) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newcap);
move_construct(&newdata[0], &data[0], len);
destroy(&data[0], len);
deallocate(data, cap);
construct(&newdata[len], newlen-len);
data = newdata;
len = newlen;
cap = newcap;
}
}
void resize(size_type newlen, Ttype value)
{
if(newlen < len)
{
destroy(&data[newlen], len-newlen);
len = newlen;
}
else if(newlen == len)
return;
else if(newlen <= cap) // newlen > len, too.
{
construct(&data[len], newlen-len, value);
len = newlen;
}
else // newlen > cap, and newlen > len.
{
// reallocation required
size_type newcap = newlen;
T * newdata = allocate(newcap);
if(!newdata) fprintf(stdout, "VecType: Failed to allocate %u bytes\n", (unsigned)newcap);
move_construct(&newdata[0], &data[0], len);
destroy(&data[0], len);
deallocate(data, cap);
construct(&newdata[len], newlen-len, value);
data = newdata;
len = newlen;
cap = newcap;
}
}
void swap(VecType& b)
{
/* std::swap(data, b.data);
std::swap(len, b.len);
std::swap(cap, b.cap);
*/
{size_type l=b.len; b.len=len; len=l;}
{size_type l=b.cap; b.cap=cap; cap=l;}
{T * d = b.data; b.data=data; data=d;}
}
int empty() const { return len==0; }
void clear()
{
if(!cap) return;
destroy(&data[0], len);
len = 0;
deallocate(data, cap);
data = 0;
cap = 0;
}
size_type size() const { return len; }
size_type capacity() const { return cap; }
private:
static void construct(T * target, size_type count)
{
for(size_type a=0; a<count; ++a)
{
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&target[a]) T();
#elif defined(UsePlacementNew)
target[a].Construct();
#else
target[a] = (T)0;
#endif
}
}
static void construct(T * target, size_type count, Ttype param)
{
for(size_type a=0; a<count; ++a)
{
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&target[a]) T(param);
#elif defined(UsePlacementNew)
target[a].Construct(param);
#else
target[a] = param;
#endif
}
}
static void destroy(T * target, size_type count)
{
#if defined(__cplusplus) && __cplusplus >= 199700L
for(size_type a=count; a-- > 0; )
target[a].~T();
#elif defined(UsePlacementNew)
for(size_type a=count; a-- > 0; )
target[a].Destruct();
#else
target=target;
count=count;
#endif
}
static void move_assign(T * target, T * source, size_type count)
{
#if defined(__cplusplus) && __cplusplus >= 201100L
for(size_type a=0; a<count; ++a)
target[a] = std::move(source[a]);
#elif defined(UsePlacementNew)
for(size_type a=0; a<count; ++a)
target[a].swap(source[a]);
#else
copy_assign(target, source, count);
#endif
}
static void move_assign_backwards(T * target, T * source, size_type count)
{
#if defined(__cplusplus) && __cplusplus >= 201100L
for(size_type a=count; a-- > 0; )
target[a] = std::move(source[a]);
#elif defined(UsePlacementNew)
for(size_type a=count; a-- > 0; )
target[a].swap(source[a]);
#else
copy_assign_backwards(target, source, count);
#endif
}
static void move_construct(T * target, T * source, size_type count)
{
#if defined(__cplusplus) && __cplusplus >= 201100L
for(size_type a=0; a<count; ++a)
new(&target[a]) T( std::move(source[a]) );
#elif defined(UsePlacementNew)
for(size_type a=0; a<count; ++a)
{
target[a].Construct();
target[a].swap(source[a]);
}
#else
copy_construct(target, source, count);
#endif
}
static T const*
copy_assign(T * target, T const* source, size_type count)
{
for(size_type a=0; a<count; ++a)
target[a] = *source++;
return source;
}
static T const*
copy_assign_backwards(T * target, T const* source, size_type count)
{
for(size_type a=count; a-- > 0; )
target[a] = source[a];
return source;
}
static T const*
copy_construct(T * target, T const* source, size_type count)
{
for(size_type a=0; a<count; ++a)
{
#if defined(__cplusplus) && __cplusplus >= 199700L
new(&target[a]) T( *source++ );
#elif defined(UsePlacementNew)
target[a].Construct(*source++);
#else
target[a] = *source++;
#endif
}
return source;
}
static size_t default_size()
{
return 1; //sizeof(T) < 16 ? 2 : (sizeof(T) < 256 ? 2 : 1);
}
private:
static T * allocate(size_type n)
{
return (T *) malloc( n * sizeof(T) );
}
static void deallocate(T * p, size_type n)
{
free( (void*) p );
n=n;
}
private:
T * data;
size_type len, cap;
#undef Ttype
#undef q
};
#endif