-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy patharray.h
594 lines (485 loc) · 14.9 KB
/
array.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
/****************************************************************************
* Copyright (C) from 2009 to Present EPAM Systems.
*
* This file is part of Indigo toolkit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#ifndef __array_h__
#define __array_h__
#include <cctype>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <new>
#include <utility>
#include "base_c/defs.h"
#include "base_cpp/exception.h"
namespace indigo
{
DECL_EXCEPTION(ArrayError);
template <typename T>
class Array
{
public:
DECL_TPL_ERROR(ArrayError);
explicit Array() : _reserved(0), _length(0), _array(nullptr)
{
}
Array(Array&& other) noexcept : Array()
{
swap(other);
}
~Array()
{
if (_array != nullptr)
{
std::free(static_cast<void*>(_array));
_array = nullptr;
_length = 0;
_reserved = 0;
}
}
Array& operator=(Array&& src) noexcept
{
swap(src);
return *this;
}
void clear()
{
_length = 0;
}
void reserve(int to_reserve)
{
if (to_reserve < 0)
throw Error("to_reserve = %d", to_reserve);
if (to_reserve > _reserved)
{
if (_length < 1)
{
if (_array != nullptr)
{
std::free(static_cast<void*>(_array));
_array = nullptr;
_length = 0;
_reserved = 0;
}
}
T* oldptr = _array;
_array = static_cast<T*>(std::realloc(static_cast<void*>(_array), sizeof(T) * to_reserve));
if (_array == nullptr)
{
_array = oldptr;
throw std::bad_alloc();
}
_reserved = to_reserve;
}
}
void zerofill()
{
if (_length > 0)
memset(_array, 0, _length * sizeof(T));
}
void fffill()
{
if (_length > 0)
memset(_array, 0xFF, _length * sizeof(T));
}
void fill(const T& value)
{
for (int i = 0; i < size(); i++)
_array[i] = value;
}
const T* ptr() const
{
return _array;
}
T* ptr()
{
return _array;
}
const T& operator[](int index) const
{
if (index < 0 || _length - index <= 0)
throw Error("invalid index %d (size=%d)", index, _length);
return _array[index];
}
T& operator[](int index)
{
if (index < 0 || _length - index <= 0)
throw Error("invalid index %d (size=%d)", index, _length);
return _array[index];
}
const T& at(int index) const
{
if (index < 0 || _length - index <= 0)
throw Error("invalid index %d (size=%d)", index, _length);
return (*this)[index];
}
T& at(int index)
{
if (index < 0 || _length - index <= 0)
throw Error("invalid index %d (size=%d)", index, _length);
return (*this)[index];
}
int size() const
{
return _length;
}
int sizeInBytes() const
{
return _length * sizeof(T);
}
void copy(const Array<T>& other)
{
copy(other._array, other._length);
}
void copy(const T* other, int count)
{
if (count > 0)
{
clear_resize(count);
memcpy(_array, other, count * sizeof(T));
}
else
{
_length = 0;
}
}
void concat(const Array<T>& other)
{
concat(other._array, other.size());
}
void concat(const T* other, int count)
{
if (count > 0)
{
int length = _length;
resize(length + count);
memcpy(_array + length, other, count * sizeof(T));
}
}
int memcmp(const Array<T>& other) const
{
if (_length < other._length)
return -1;
if (_length > other._length)
return -1;
if (_length == 0)
return 0;
return ::memcmp(_array, other._array, _length * sizeof(T));
}
void remove(int idx, int span = 1)
{
if (idx < 0 || idx - _length - span + 1 >= 0)
throw Error("remove(): invalid index %d with span %d (size=%d)", idx, span, _length);
memmove(_array + idx, _array + idx + span, sizeof(T) * (_length - idx - span));
_length -= span;
}
void remove_replace(int idx)
{
if (idx < 0 || idx >= _length)
throw Error("remove_replace(): invalid index %d (size=%d)", idx, _length);
if (idx < _length - 1)
_array[idx] = _array[_length - 1];
_length--;
}
int find(const T& value) const
{
return find(0, _length, value);
}
int find(int from, int to, const T& value) const
{
for (int i = from; i < to; i++)
if (_array[i] == value)
return i;
return -1;
}
int count(const T& value) const
{
return count(0, _length, value);
}
int count(int from, int to, const T& value) const
{
int cnt = 0;
for (int i = from; i < to; i++)
if (_array[i] == value)
cnt++;
return cnt;
}
void swap(int idx1, int idx2)
{
if (idx1 < 0 || idx1 >= _length)
throw Error("swap(): invalid index %d (size=%d)", idx1, _length);
if (idx2 < 0 || idx2 >= _length)
throw Error("swap(): invalid index %d (size=%d)", idx2, _length);
if (idx1 == idx2)
return;
std::swap(_array[idx1], _array[idx2]);
}
void push(T elem)
{
resize(_length + 1);
_array[_length - 1] = elem;
}
T& push()
{
resize(_length + 1);
return _array[_length - 1];
}
T& pop()
{
if (_length <= 0)
throw Error("stack underflow");
return _array[--_length];
}
T& top()
{
if (_length <= 0)
throw Error("stack underflow");
return _array[_length - 1];
}
const T& top() const
{
if (_length <= 0)
throw Error("stack underflow");
return _array[_length - 1];
}
T& top(int offset)
{
if (_length - offset <= 0)
throw Error("stack underflow");
return _array[_length - 1 - offset];
}
const T& top(int offset) const
{
if (_length - offset <= 0)
throw Error("stack underflow");
return _array[_length - 1 - offset];
}
void resize(int newsize)
{
if (newsize > _reserved)
reserve((newsize + 1) * 2);
_length = newsize;
}
void expand(int newsize)
{
if (_length < newsize)
resize(newsize);
}
void expandFill(int newsize, const T& value)
{
while (_length < newsize)
push(value);
}
void clear_resize(int newsize)
{
if (_reserved < newsize)
{
_length = 0;
reserve((newsize + 1) * 2);
}
_length = newsize;
}
void swap(Array<T>& other) noexcept
{
std::swap(_array, other._array);
std::swap(_reserved, other._reserved);
std::swap(_length, other._length);
}
T* begin()
{
return _array;
}
T* end()
{
return _array + _length;
}
// CMP_FUNCTOR has two arguments and returns sign of comparation
template <typename CmpFunctor>
void insertionSort(int start, int end, CmpFunctor cmp)
{
int i, j;
char tmp[sizeof(T)]; // can't use T directly because it may have destructor
for (i = start + 1; i <= end; i++)
{
j = i;
while (j > start && cmp(_array[j - 1], _array[j]) > 0)
{
T* a1 = _array + j - 1;
T* a2 = a1 + 1;
memcpy(&tmp, a1, sizeof(T));
memcpy(a1, a2, sizeof(T));
memcpy(a2, &tmp, sizeof(T));
j--;
}
}
}
// CMP_FUNCTOR has two arguments and returns sign of comparation
template <typename CmpFunctor>
void qsort(int start, int end, CmpFunctor cmp)
{
// Sort elements from start to end
if (start >= end)
return;
if (end - start < 10)
insertionSort(start, end, cmp);
struct
{
T *lo, *hi;
} stack[32], *sp;
char tmp[sizeof(T)]; // can't use T directly because it may have destructor
sp = stack;
// push our initial values onto the stack
sp->lo = _array + start;
sp->hi = _array + end + 1;
sp++;
while (sp > stack)
{
// pop lo and hi off the stack
sp--;
T *high = sp->hi, *low = sp->lo;
T* hi = high - 1;
T* lo = low;
T* pivot = low;
while (1)
{
while (lo < high && lo != pivot && cmp(*lo, *pivot) < 0)
lo++;
while (hi > low && (hi == pivot || cmp(*hi, *pivot) >= 0))
hi--;
if (lo < hi)
{
memcpy(&tmp, lo, sizeof(T));
memcpy(lo, hi, sizeof(T));
memcpy(hi, &tmp, sizeof(T));
if (lo == pivot)
pivot = hi;
else if (hi == pivot)
pivot = lo;
hi--;
}
else
{
hi++;
if (hi == high)
// done with this segment
break;
// push the larger segment onto the stack and continue
// sorting the smaller segment.
if ((hi - low) > (high - hi))
{
sp->lo = low;
sp->hi = hi;
sp++;
hi = high;
low = lo;
}
else
{
sp->hi = high;
sp->lo = hi;
sp++;
high = hi;
lo = low;
}
pivot = lo;
hi--;
}
}
}
}
template <typename T1, typename T2>
void qsort(int start, int end, int (*cmp)(T1, T2, void*), void* context)
{
this->qsort(start, end, _CmpFunctorCaller<T1, T2>(cmp, context));
}
template <typename T1, typename T2>
void qsort(int (*cmp)(T1, T2, void*), void* context)
{
this->qsort(0, _length - 1, cmp, context);
}
// Array<char>-specific
void appendString(const char* str, bool keep_zero)
{
int len = (int)strlen(str);
int initial_size = _length;
if (initial_size > 0 && _array[initial_size - 1] == 0)
initial_size--;
resize(initial_size + len);
memcpy(_array + initial_size, str, len);
if (keep_zero)
push(0);
}
void readString(const char* str, bool keep_zero)
{
clear();
appendString(str, keep_zero);
}
void upper(const char* source)
{
clear();
while (*source != 0)
push(::toupper(*source++));
push(0);
}
void lower(const char* source)
{
clear();
while (*source != 0)
push(::tolower(*source++));
push(0);
}
void toupper()
{
for (int i = 0; i < _length; i++)
_array[i] = ::toupper(_array[i]);
}
void tolower()
{
for (int i = 0; i < _length; i++)
_array[i] = ::tolower(_array[i]);
}
protected:
T* _array;
int _reserved;
int _length;
private:
Array(const Array&); // no implicit copy
Array<int>& operator=(const Array<int>& right); // no copy constructor
template <typename T1, typename T2>
class _CmpFunctorCaller
{
public:
_CmpFunctorCaller(int (*cmp)(T1, T2, void*), void* context) : _context(context), _cmp(cmp)
{
}
int operator()(T1 arg1, T2 arg2) const
{
return _cmp(arg1, arg2, _context);
}
private:
void* _context;
int (*_cmp)(T1, T2, void*);
};
};
} // namespace indigo
// operators defined here for use with ObjArray<> and ObjPool<>
template <typename T>
void* operator new(size_t size, T* allocated_area)
{
return allocated_area;
}
#endif