-
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy patharray.h
More file actions
675 lines (579 loc) · 14.9 KB
/
array.h
File metadata and controls
675 lines (579 loc) · 14.9 KB
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
/*
* PROGRAM: Client/Server Common Code
* MODULE: array.h
* DESCRIPTION: dynamic array of simple elements
*
* The contents of this file are subject to the Interbase Public
* License Version 1.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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* Created by: Alex Peshkov <peshkoff@mail.ru>
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
* Adriano dos Santos Fernandes
*/
#ifndef CLASSES_ARRAY_H
#define CLASSES_ARRAY_H
#include "../common/gdsassert.h"
#include <initializer_list>
#include <string.h>
#include "../common/classes/vector.h"
#include "../common/classes/alloc.h"
namespace Firebird {
// Static part of the array
template <typename T, FB_SIZE_T Capacity, typename AlignT = T>
class InlineStorage : public AutoStorage
{
public:
explicit InlineStorage(MemoryPool& p) : AutoStorage(p) { }
InlineStorage() : AutoStorage() { }
protected:
T* getStorage() noexcept
{
return buffer;
}
FB_SIZE_T getStorageSize() const noexcept
{
return Capacity;
}
private:
alignas(alignof(AlignT)) T buffer[Capacity];
};
// Used when array doesn't have static part
template <typename T>
class EmptyStorage : public AutoStorage
{
public:
explicit EmptyStorage(MemoryPool& p) : AutoStorage(p) { }
EmptyStorage() : AutoStorage() { }
protected:
T* getStorage() noexcept { return NULL; }
FB_SIZE_T getStorageSize() const noexcept { return 0; }
};
// Dynamic array of simple types
template <typename T, typename Storage = EmptyStorage<T> >
class Array : protected Storage
{
public:
typedef FB_SIZE_T size_type;
typedef FB_SSIZE_T difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
typedef pointer iterator;
typedef const_pointer const_iterator;
explicit Array(MemoryPool& p)
: Storage(p),
count(0),
capacity(this->getStorageSize()),
data(this->getStorage())
{
// Ensure we can carry byte copy operations.
fb_assert(capacity < FB_MAX_SIZEOF / sizeof(T));
}
Array(MemoryPool& p, const size_type InitialCapacity)
: Array(p)
{
ensureCapacity(InitialCapacity);
}
Array(MemoryPool& p, const Array<T, Storage>& source)
: Array(p)
{
copyFrom(source);
}
Array()
: count(0),
capacity(this->getStorageSize()),
data(this->getStorage())
{
}
explicit Array(const size_type InitialCapacity)
: Storage(),
count(0),
capacity(this->getStorageSize()),
data(this->getStorage())
{
ensureCapacity(InitialCapacity);
}
Array(const T* items, const size_type itemsCount)
: Storage(),
count(0),
capacity(this->getStorageSize()),
data(this->getStorage())
{
add(items, itemsCount);
}
Array(const Array<T, Storage>& source)
: Storage(),
count(0),
capacity(this->getStorageSize()),
data(this->getStorage())
{
copyFrom(source);
}
Array(MemoryPool& p, std::initializer_list<T> items)
: Array(p)
{
for (auto& item : items)
add(item);
}
Array(std::initializer_list<T> items)
: Array()
{
for (auto& item : items)
add(item);
}
~Array()
{
freeData();
}
void clear() noexcept
{
count = 0;
}
protected:
const T& getElement(size_type index) const noexcept
{
fb_assert(index < count);
return data[index];
}
T& getElement(size_type index) noexcept
{
fb_assert(index < count);
return data[index];
}
void freeData() noexcept
{
// CVC: Warning, after this call, "data" is an invalid pointer, be sure to reassign it
// or make it equal to this->getStorage()
if (data != this->getStorage())
Firebird::MemoryPool::globalFree(data);
}
void copyFrom(const Array<T, Storage>& source)
{
ensureCapacity(source.count, false);
if (source.count)
memcpy(static_cast<void*>(data), source.data, sizeof(T) * source.count);
count = source.count;
}
public:
Array<T, Storage>& operator =(const Array<T, Storage>& source)
{
copyFrom(source);
return *this;
}
const T& operator[](size_type index) const noexcept
{
return getElement(index);
}
T& operator[](size_type index) noexcept
{
return getElement(index);
}
const T& front() const
{
fb_assert(count > 0);
return *data;
}
const T& back() const
{
fb_assert(count > 0);
return *(data + count - 1);
}
const T* begin() const noexcept { return data; }
const T* end() const noexcept { return data + count; }
T& front()
{
fb_assert(count > 0);
return *data;
}
T& back()
{
fb_assert(count > 0);
return *(data + count - 1);
}
T* begin() noexcept { return data; }
T* end() noexcept { return data + count; }
void insert(const size_type index, const T& item)
{
fb_assert(index <= count);
fb_assert(count < FB_MAX_SIZEOF);
ensureCapacity(count + 1);
if (count - index)
memmove(static_cast<void*>(data + index + 1), data + index, sizeof(T) * (count - index));
data[index] = item;
++count;
}
void insert(const size_type index, const Array<T, Storage>& items)
{
fb_assert(index <= count);
fb_assert(count <= FB_MAX_SIZEOF - items.count);
ensureCapacity(count + items.count);
if (count - index)
memmove(static_cast<void*>(data + index + items.count), data + index, sizeof(T) * (count - index));
if (items.count)
memcpy(static_cast<void*>(data + index), items.data, sizeof(T) * items.count);
count += items.count;
}
void insert(const size_type index, const T* items, const size_type itemsCount)
{
fb_assert(index <= count);
fb_assert(count <= FB_MAX_SIZEOF - itemsCount);
ensureCapacity(count + itemsCount);
if (count - index)
memmove(static_cast<void*>(data + index + itemsCount), data + index, sizeof(T) * (count - index));
if (itemsCount)
memcpy(static_cast<void*>(data + index), items, sizeof(T) * itemsCount);
count += itemsCount;
}
size_type add(const T& item)
{
ensureCapacity(count + 1);
data[count] = item;
return count++;
}
T& add()
{
ensureCapacity(count + 1);
return *new(&data[count++]) T(); // initialize new empty data member
}
void add(const T* items, const size_type itemsCount)
{
fb_assert(count <= FB_MAX_SIZEOF - itemsCount);
ensureCapacity(count + itemsCount);
memcpy(static_cast<void*>(data + count), items, sizeof(T) * itemsCount);
count += itemsCount;
}
T* remove(const size_type index) noexcept
{
fb_assert(index < count);
memmove(static_cast<void*>(data + index), data + index + 1, sizeof(T) * (--count - index));
return &data[index];
}
T* removeRange(const size_type from, const size_type to) noexcept
{
fb_assert(from <= to);
fb_assert(to <= count);
memmove(static_cast<void*>(data + from), data + to, sizeof(T) * (count - to));
count -= (to - from);
return &data[from];
}
T* removeCount(const size_type index, const size_type n) noexcept
{
fb_assert(index + n <= count);
memmove(static_cast<void*>(data + index), data + index + n, sizeof(T) * (count - index - n));
count -= n;
return &data[index];
}
T* remove(T* itr) noexcept
{
const size_type index = itr - begin();
fb_assert(index < count);
memmove(static_cast<void*>(data + index), data + index + 1, sizeof(T) * (--count - index));
return &data[index];
}
T* remove(T* itrFrom, T* itrTo) noexcept
{
return removeRange(itrFrom - begin(), itrTo - begin());
}
void shrink(size_type newCount) noexcept
{
fb_assert(newCount <= count);
count = newCount;
}
// Grow size of our array and zero-initialize new items
void grow(const size_type newCount)
{
fb_assert(newCount >= count);
ensureCapacity(newCount);
memset(static_cast<void*>(data + count), 0, sizeof(T) * (newCount - count));
count = newCount;
}
// Resize array according to STL's vector::resize() rules
void resize(const size_type newCount, const T& val)
{
if (newCount > count)
{
ensureCapacity(newCount);
while (count < newCount) {
data[count++] = val;
}
}
else {
count = newCount;
}
}
// Resize array according to STL's vector::resize() rules
void resize(const size_type newCount)
{
if (newCount > count) {
grow(newCount);
}
else {
count = newCount;
}
}
void join(const Array<T, Storage>& L)
{
fb_assert(count <= FB_MAX_SIZEOF - L.count);
ensureCapacity(count + L.count);
memcpy(static_cast<void*>(data + count), L.data, sizeof(T) * L.count);
count += L.count;
}
void assign(const Array<T, Storage>& source)
{
copyFrom(source);
}
void assign(const T* items, const size_type itemsCount)
{
resize(itemsCount);
memcpy(static_cast<void*>(data), items, sizeof(T) * count);
}
size_type getCount() const noexcept { return count; }
bool isEmpty() const noexcept { return count == 0; }
bool hasData() const noexcept { return count != 0; }
size_type getCapacity() const noexcept { return capacity; }
void push(const T& item)
{
add(item);
}
void push(const T* items, const size_type itemsSize)
{
fb_assert(count <= FB_MAX_SIZEOF - itemsSize);
ensureCapacity(count + itemsSize);
memcpy(static_cast<void*>(data + count), items, sizeof(T) * itemsSize);
count += itemsSize;
}
void append(const T* items, const size_type itemsSize)
{
push(items, itemsSize);
}
void append(const Array<T, Storage>& source)
{
push(source.begin(), source.getCount());
}
T pop()
{
fb_assert(count > 0);
count--;
return data[count];
}
// prepare array to be used as a buffer of capacity items
T* getBuffer(const size_type capacityL, bool preserve = true)
{
ensureCapacity(capacityL, preserve);
count = capacityL;
return data;
}
// prepare array to be used as a buffer of capacity bytes aligned on given alignment
T* getAlignedBuffer(const size_type capacityL, const size_type alignL)
{
static_assert(sizeof(T) == 1, "sizeof(T) != 1");
ensureCapacity(capacityL + alignL, false);
count = capacityL + alignL;
return FB_ALIGN(data, alignL);
}
// clear array and release dynamically allocated memory
void free()
{
clear();
freeData();
capacity = this->getStorageSize();
data = this->getStorage();
}
// This method only assigns "pos" if the element is found.
// Maybe we should modify it to iterate directy with "pos".
bool find(const T& item, size_type& pos) const
{
for (size_type i = 0; i < count; i++)
{
if (data[i] == item)
{
pos = i;
return true;
}
}
return false;
}
bool findAndRemove(const T& item)
{
size_type pos;
if (find(item, pos))
{
remove(pos);
return true;
}
return false;
}
bool exist(const T& item) const
{
size_type pos; // ignored
return find(item, pos);
}
bool operator==(const Array& op) const
{
if (count != op.count)
return false;
return memcmp(data, op.data, count) == 0;
}
// Member function only for some debugging tests. Hope nobody is bothered.
void swapElems()
{
const size_type limit = count / 2;
for (size_type i = 0; i < limit; ++i)
{
T temp = data[i];
data[i] = data[count - 1 - i];
data[count - 1 - i] = temp;
}
}
void ensureCapacity(size_type newcapacity)
{
ensureCapacity(newcapacity, true);
}
protected:
size_type count, capacity;
T* data;
void ensureCapacity(size_type newcapacity, bool preserve)
{
if (newcapacity > capacity)
{
if (capacity <= FB_MAX_SIZEOF / 2)
{
if (newcapacity < capacity * 2)
newcapacity = capacity * 2;
}
else
{
newcapacity = FB_MAX_SIZEOF;
}
// Ensure we can carry byte copy operations.
// What to do here, throw in release build?
fb_assert(newcapacity < FB_MAX_SIZEOF / sizeof(T));
T* newdata = static_cast<T*>
(this->getPool().allocate(sizeof(T) * newcapacity ALLOC_ARGS));
if (preserve)
memcpy(static_cast<void*>(newdata), data, sizeof(T) * count);
freeData();
data = newdata;
capacity = newcapacity;
}
}
};
static inline constexpr int FB_ARRAY_SORT_MANUAL = 0;
static inline constexpr int FB_ARRAY_SORT_WHEN_ADD = 1;
// Dynamic sorted array of simple objects
template <typename Value,
typename Storage = EmptyStorage<Value>,
typename Key = Value,
typename KeyOfValue = DefaultKeyValue<Value>,
typename Cmp = DefaultComparator<Key> >
class SortedArray : public Array<Value, Storage>
{
public:
typedef typename Array<Value, Storage>::size_type size_type;
SortedArray(MemoryPool& p, size_type s)
: Array<Value, Storage>(p, s), sortMode(FB_ARRAY_SORT_WHEN_ADD), sorted(true)
{ }
explicit SortedArray(MemoryPool& p)
: Array<Value, Storage>(p), sortMode(FB_ARRAY_SORT_WHEN_ADD), sorted(true)
{ }
explicit SortedArray(size_type s)
: Array<Value, Storage>(s), sortMode(FB_ARRAY_SORT_WHEN_ADD), sorted(true)
{ }
SortedArray()
: Array<Value, Storage>(), sortMode(FB_ARRAY_SORT_WHEN_ADD), sorted(true)
{ }
// When item is not found, set pos to the position where the element should be
// stored if/when added.
bool find(const Key& item, size_type& pos) const
{
fb_assert(sorted);
size_type highBound = this->count, lowBound = 0;
while (highBound > lowBound)
{
const size_type temp = (highBound + lowBound) >> 1;
if (Cmp::greaterThan(item, KeyOfValue::generate(this->data[temp])))
lowBound = temp + 1;
else
highBound = temp;
}
pos = lowBound;
return highBound != this->count &&
!Cmp::greaterThan(KeyOfValue::generate(this->data[lowBound]), item);
}
bool findAndRemove(const Key& item)
{
size_type pos;
if (find(item, pos))
{
this->remove(pos);
return true;
}
return false;
}
bool exist(const Key& item) const
{
size_type pos; // ignored
return find(item, pos);
}
size_type add(const Value& item)
{
size_type pos;
if (sortMode == FB_ARRAY_SORT_WHEN_ADD)
find(KeyOfValue::generate(item), pos);
else
{
sorted = false;
pos = this->getCount();
}
this->insert(pos, item);
return pos;
}
void setSortMode(int sm)
{
if (sortMode != FB_ARRAY_SORT_WHEN_ADD && sm == FB_ARRAY_SORT_WHEN_ADD && !sorted)
{
sort();
}
sortMode = sm;
}
void sort()
{
if (sorted)
return;
auto compare = [] (const void* a, const void* b) {
const Key& first(KeyOfValue::generate(*static_cast<const Value*>(a)));
const Key& second(KeyOfValue::generate(*static_cast<const Value*>(b)));
if (Cmp::greaterThan(first, second))
return 1;
if (Cmp::greaterThan(second, first))
return -1;
return 0;
};
qsort(this->begin(), this->getCount(), sizeof(Value), compare);
sorted = true;
}
private:
int sortMode;
bool sorted;
};
// Nice shorthand for arrays with static part
template <typename T, FB_SIZE_T InlineCapacity, typename AlignT = T>
using HalfStaticArray = Array<T, InlineStorage<T, InlineCapacity, AlignT> >;
typedef HalfStaticArray<UCHAR, BUFFER_TINY> UCharBuffer;
} // namespace Firebird
#endif // CLASSES_ARRAY_H