-
Notifications
You must be signed in to change notification settings - Fork 109
/
vector.h
1238 lines (844 loc) · 30.8 KB
/
vector.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
// ============================================================================
//
// = LIBRARY
// ULib - c++ library
//
// = FILENAME
// vector.h
//
// = AUTHOR
// Stefano Casazza
//
// ============================================================================
#ifndef ULIB_VECTOR_H
#define ULIB_VECTOR_H 1
#include <ulib/container/construct.h>
/**
* A vector is a sequence of elements that are stored contiguously in memory and can change in size.
* As a result, it has support for random-access and provides methods to add and delete elements.
* It is typically used when an array is required, but the exact number if elements is unknown at compile-time
*
* Simple vector template class. Supports pushing at end and random-access deletions. Dynamically sized.
*/
class UHTTP;
class UThreadPool;
class UHttpPlugIn;
class UFileConfig;
class UNoCatPlugIn;
template <class T> class UVector;
template <class T> class UOrmTypeHandler;
template <class T> class UJsonTypeHandler;
//#define U_RING_BUFFER
template <> class U_EXPORT UVector<void*> {
public:
// Check for memory error
U_MEMORY_TEST
// Allocator e Deallocator
U_MEMORY_ALLOCATOR
U_MEMORY_DEALLOCATOR
// allocate and deallocate methods
void allocate(uint32_t n)
{
U_TRACE(0, "UVector<void*>::allocate(%u)", n)
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MINOR(n, ((0xfffffff / sizeof(void*)) - sizeof(UVector<void*>)))
vec = (const void**) UMemoryPool::_malloc(&n, sizeof(void*));
_capacity = n;
}
void deallocate()
{
U_TRACE_NO_PARAM(0, "UVector<void*>::deallocate()")
U_CHECK_MEMORY
U_INTERNAL_ASSERT_RANGE(1, _capacity, ((0xfffffff / sizeof(void*)) - sizeof(UVector<void*>)))
UMemoryPool::_free(vec, _capacity, sizeof(void*));
}
// Costruttori e distruttore
UVector(uint32_t n = 64U) // create an empty vector with a size estimate
{
U_TRACE_REGISTER_OBJECT(0, UVector<void*>, "%u", n)
# if defined(U_RING_BUFFER) && !defined(U_STATIC_ONLY)
head = tail = 0;
# endif
_length = 0;
allocate(n);
}
~UVector()
{
U_TRACE_UNREGISTER_OBJECT(0, UVector<void*>)
deallocate();
}
// Size and Capacity
uint32_t size() const
{
U_TRACE_NO_PARAM(0, "UVector<void*>::size()")
U_RETURN(_length);
}
uint32_t capacity() const
{
U_TRACE_NO_PARAM(0, "UVector<void*>::capacity()")
U_RETURN(_capacity);
}
bool empty() const
{
U_TRACE_NO_PARAM(0, "UVector<void*>::empty()")
U_RETURN(_length == 0);
}
// Make room for a total of n element
void reserve(uint32_t n);
// ELEMENT ACCESS
const void* begin() { return *vec; }
const void* end() { return *(vec + _length); }
const void* rbegin() { return *(vec + _length - 1); }
const void* rend() { return *(vec + 1); }
const void* front() { return begin(); }
const void* back() { return rbegin(); }
const void*& at(uint32_t pos) __pure
{
U_TRACE(0, "UVector<void*>::at(%u)", pos)
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT_MINOR(pos, _length)
return vec[pos];
}
const void* at(uint32_t pos) const __pure
{
U_TRACE(0, "UVector<void*>::at(%u) const", pos)
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT_MINOR(pos, _length)
return vec[pos];
}
const void*& operator[](uint32_t pos) { return at(pos); }
const void* operator[](uint32_t pos) const { return at(pos); }
void replace(uint32_t pos, const void* elem)
{
U_TRACE(0, "UVector<void*>::replace(%u,%p)", pos, elem)
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT_MINOR(pos, _length)
vec[pos] = elem;
}
// STACK OPERATIONS
void push( const void* elem);
void push_back(const void* elem) { push(elem); } // add to end
const void* last() // return last element
{
U_TRACE_NO_PARAM(0, "UVector<void*>::last()")
U_CHECK_MEMORY
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
return vec[_length-1];
}
const void* pop() // remove last element
{
U_TRACE_NO_PARAM(0, "UVector<void*>::pop()")
U_CHECK_MEMORY
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
return vec[--_length];
}
const void* pop_front() { return pop(); } // remove last element
// LIST OPERATIONS
void insert(uint32_t pos, const void* elem); // add elem before pos
void insert(uint32_t pos, uint32_t n, const void* elem); // add n copy of elem before pos
void erase(uint32_t pos) // remove element at pos
{
U_TRACE(1, "UVector<void*>::erase(%u)", pos)
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MINOR(pos, _length)
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
if (--_length) (void) U_SYSCALL(apex_memmove, "%p,%p,%u", vec + pos, vec + pos + 1, (_length - pos) * sizeof(void*));
}
void erase(uint32_t first, uint32_t _last) // erase [first,last[
{
U_TRACE(1, "UVector<void*>::erase(%u,%u)", first, _last)
U_CHECK_MEMORY
U_INTERNAL_ASSERT(_last <= _length)
U_INTERNAL_ASSERT_MINOR(first, _last)
U_INTERNAL_ASSERT_MINOR(first, _length)
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
uint32_t new_length = (_length - (_last - first));
if (new_length) (void) U_SYSCALL(apex_memmove, "%p,%p,%u", vec + first, vec + _last, (_length - _last) * sizeof(void*));
_length = new_length;
}
void swap(uint32_t from, uint32_t to)
{
U_TRACE(0, "UVector<void*>::swap(%u,%u)", from, to)
U_CHECK_MEMORY
U_INTERNAL_ASSERT(to <= _length)
U_INTERNAL_ASSERT(from <= _length)
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
const void* tmp = vec[from];
vec[from] = vec[to];
vec[to] = tmp;
}
// BINARY HEAP
const void* bh_min() const __pure
{
U_TRACE_NO_PARAM(0, "UVector<void*>::bh_min()")
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT_RANGE(1,_length,_capacity)
// The item at the top of the binary heap has the minimum key value.
return vec[1];
}
// Call function for all entry
void callForAllEntry(vPFpv function)
{
U_TRACE(0, "UVector<void*>::callForAllEntry(%p)", function)
U_CHECK_MEMORY
U_INTERNAL_DUMP("_length = %u", _length)
for (uint32_t i = 0; i < _length; ++i) function((void*)vec[i]);
}
void callForAllEntry(bPFpv function)
{
U_TRACE(0, "UVector<void*>::callForAllEntry(%p)", function)
U_CHECK_MEMORY
U_INTERNAL_DUMP("_length = %u", _length)
for (uint32_t i = 0; i < _length && function((void*)vec[i]); ++i) {}
}
uint32_t find(const void* elem)
{
U_TRACE(0, "UVector<void*>::find(%p)", elem)
U_CHECK_MEMORY
for (uint32_t i = 0; i < _length; ++i)
{
if (vec[i] == elem) U_RETURN(i);
}
U_RETURN(U_NOT_FOUND);
}
// EXTENSION
void sort(qcompare compare_obj)
{
U_TRACE(0+256, "UVector<void*>::sort(%p)", compare_obj)
U_INTERNAL_DUMP("_length = %u", _length)
U_INTERNAL_ASSERT_RANGE(2,_length,_capacity)
U_SYSCALL_VOID(qsort, "%p,%u,%d,%p", (void*)vec, _length, sizeof(void*), compare_obj);
}
void move(UVector<void*>& source); // add to end and reset source
#if defined(U_RING_BUFFER) && !defined(U_STATIC_ONLY)
bool isEmptyRingBuffer()
{
U_TRACE_NO_PARAM(0, "UVector<void*>::isEmptyRingBuffer()")
U_CHECK_MEMORY
U_INTERNAL_DUMP("head = %u tail = %u", head, tail)
U_INTERNAL_ASSERT_MINOR(head, _capacity)
U_INTERNAL_ASSERT_MINOR(tail, _capacity)
if (head == tail) U_RETURN(true);
U_RETURN(false);
}
uint32_t sizeRingBuffer()
{
U_TRACE_NO_PARAM(0, "UVector<void*>::sizeRingBuffer()")
U_CHECK_MEMORY
U_INTERNAL_DUMP("head = %u tail = %u", head, tail)
U_INTERNAL_ASSERT_MINOR(head, _capacity)
U_INTERNAL_ASSERT_MINOR(tail, _capacity)
uint32_t sz = 0, i = head;
while (i != tail)
{
++sz;
i = ((i+1) % _capacity);
}
U_RETURN(sz);
}
void callForAllEntryRingBuffer(bPFpv function)
{
U_TRACE(0, "UVector<void*>::callForAllEntryRingBuffer(%p)", function)
U_CHECK_MEMORY
U_INTERNAL_DUMP("head = %u tail = %u", head, tail)
U_INTERNAL_ASSERT_MINOR(head, _capacity)
U_INTERNAL_ASSERT_MINOR(tail, _capacity)
for (uint32_t i = head; i != tail && function((void*)vec[i]); i = ((i+1) % _capacity)) {}
}
#endif
#ifdef DEBUG
bool check_memory(); // check all element
# ifdef U_STDCPP_ENABLE
const char* dump(bool reset) const;
# endif
#endif
// STREAMS
static bool istream_loading;
protected:
const void** vec;
uint32_t _length, _capacity;
#if defined(U_RING_BUFFER) && !defined(U_STATIC_ONLY)
volatile uint32_t tail; // input index
volatile uint32_t head; // output index
#endif
private:
#ifdef U_COMPILER_DELETE_MEMBERS
UVector<void*>& operator=(const UVector<void*>&) = delete;
#else
UVector<void*>& operator=(const UVector<void*>&) { return *this; }
#endif
friend class UThreadPool;
template <class T> friend class UOrmTypeHandler;
template <class T> friend class UJsonTypeHandler;
};
template <class T> class U_EXPORT UVector<T*> : public UVector<void*> {
public:
void clear() // erase all element
{
U_TRACE_NO_PARAM(0+256, "UVector<T*>::clear()")
U_CHECK_MEMORY
U_INTERNAL_DUMP("_length = %u", _length)
U_INTERNAL_ASSERT(_length <= _capacity)
if (_length)
{
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T**)vec, _length);
# endif
_length = 0;
}
}
// Costruttori e distruttore
UVector(uint32_t n = 64U) : UVector<void*>(n)
{
U_TRACE_REGISTER_OBJECT(0, UVector<T*>, "%u", n)
}
~UVector()
{
U_TRACE_UNREGISTER_OBJECT(0, UVector<T*>)
clear();
}
// ELEMENT ACCESS
T* begin() { return (T*) UVector<void*>::begin(); }
T* end() { return (T*) UVector<void*>::end(); }
T* rbegin() { return (T*) UVector<void*>::rbegin(); }
T* rend() { return (T*) UVector<void*>::rend(); }
T* front() { return (T*) UVector<void*>::front(); }
T* back() { return (T*) UVector<void*>::back(); }
T*& at(uint32_t pos) __pure { return (T*&) UVector<void*>::at(pos); }
T* at(uint32_t pos) const __pure { return (T*) UVector<void*>::at(pos); }
__pure T*& operator[](uint32_t pos) { return at(pos); }
T* operator[](uint32_t pos) const { return at(pos); }
uint32_t find(void* elem) { return UVector<void*>::find(elem); }
void replace(uint32_t pos, const T* elem)
{
U_TRACE(0, "UVector<T*>::replace(%u,%p)", pos, elem)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(&elem, false);
# endif
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T*)vec[pos]);
# endif
UVector<void*>::replace(pos, elem);
}
// STACK OPERATIONS
void push(const T* elem) // add to end
{
U_TRACE(0, "UVector<T*>::push(%p)", elem)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(&elem, istream_loading);
# endif
UVector<void*>::push(elem);
}
void push_back(const T* elem) { push(elem); }
T* last() // return last element
{
U_TRACE_NO_PARAM(0, "UVector<T*>::last()")
T* elem = (T*) UVector<void*>::last();
U_RETURN_POINTER(elem,T);
}
T* pop() // remove last element
{
U_TRACE_NO_PARAM(0, "UVector<T*>::pop()")
T* elem = (T*) UVector<void*>::pop();
U_RETURN_POINTER(elem,T);
}
T* pop_front() { return pop(); } // remove last element
// LIST OPERATIONS
void insert(uint32_t pos, const T* elem) // add elem before pos
{
U_TRACE(0, "UVector<T*>::insert(%u,%p)", pos, elem)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(&elem, false);
# endif
UVector<void*>::insert(pos, elem);
}
void insert(uint32_t pos, uint32_t n, const T* elem) // add n copy of elem before pos
{
U_TRACE(0, "UVector<T*>::insert(%u,%u,%p)", pos, n, elem)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(elem, n);
# endif
UVector<void*>::insert(pos, n, elem);
}
void erase(uint32_t pos) // remove element at pos
{
U_TRACE(0, "UVector<T*>::erase(%u)", pos)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T*)vec[pos]);
# endif
UVector<void*>::erase(pos);
}
void erase(uint32_t first, uint32_t _last) // erase [first,last[
{
U_TRACE(0, "UVector<T*>::erase(%u,%u)", first, _last)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T**)(vec+first), _last - first);
# endif
UVector<void*>::erase(first, _last);
}
// ASSIGNMENTS
void assign(uint32_t n, const T* elem)
{
U_TRACE(0, "UVector<T*>::assign(%u,%p)", n, elem)
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MAJOR(n, 0)
U_INTERNAL_ASSERT(_length <= _capacity)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(elem, n);
# endif
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T**)vec, U_min(n, _length));
# endif
if (n > _capacity)
{
UVector<void*>::deallocate();
UVector<void*>::allocate(n);
}
for (uint32_t i = 0; i < n; ++i) vec[i] = elem;
_length = n;
}
#if defined(U_RING_BUFFER) && !defined(U_STATIC_ONLY)
bool put(const T* elem) // queue an element at the end
{
U_TRACE(0, "UVector<T*>::put(%p)", elem)
U_CHECK_MEMORY
U_INTERNAL_DUMP("head = %u tail = %u", head, tail)
U_INTERNAL_ASSERT_MINOR(head, _capacity)
U_INTERNAL_ASSERT_MINOR(tail, _capacity)
// Producer only: updates tail index after writing
uint32_t nextTail = (tail + 1) % _capacity;
// changes only the tail, but verifies that queue is not full (check of head)
U_INTERNAL_DUMP("nextTail = %u head = %u", nextTail, head)
if (nextTail != head)
{
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(&elem, false);
# endif
vec[tail] = elem;
tail = nextTail;
U_RETURN(true);
}
// queue was full
U_RETURN(false);
}
bool get(const T*& elem) // dequeue the element off the front
{
U_TRACE(0, "UVector<T*>::get(%p)", &elem)
U_CHECK_MEMORY
U_INTERNAL_DUMP("head = %u tail = %u", head, tail)
U_INTERNAL_ASSERT_MINOR(head, _capacity)
U_INTERNAL_ASSERT_MINOR(tail, _capacity)
// changes only the head but verifies that the queue is not empty (check of tail)
if (head == tail) U_RETURN(false); // empty queue
// Consumer only: updates head index after reading
uint32_t nextHead = (head + 1) % _capacity;
U_INTERNAL_DUMP("nextHead = %u", nextHead)
elem = (const T*) vec[head];
head = nextHead;
U_RETURN(true);
}
#endif
/**
* BINARY HEAP
*
* A binary heap can be used to find the C (where C <= n) smallest numbers out of n input numbers without sorting the entire input.
*
* The binary heap is a heap ordered binary tree. A binary tree allows each node in the tree to have two children. Each node has a
* value associated with it, called its key. The term `heap ordered' means that no child in the tree has a key greater than the key
* of its parent. By maintaining heap order in the tree, the root node has the smallest key. Because the heap has simple access to
* the minimum node, the find_min() operation to takes O(1) time. Because of the binary heaps simplicity, it is possible to maintain
* it using one dimensional arrays. The root node is located at position 1 in the array. The first child of the root is located at
* position 2 and the second child at position 3. In general, the children of the node at position i are located at 2*i and 2*i + 1.
* So the children of the node at position 3 in the array are located at positions 6 and 7. Similarly, the parent of the node at
* position i is located at i div 2.
*
* Note that array entry 0 is unused
*/
T* bh_min() const __pure { return (T*) UVector<void*>::bh_min(); }
void bh_put(const T* elem)
{
U_TRACE(0, "UVector<T*>::bh_put(%p)", elem)
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT(_length <= _capacity)
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_construct<T>(&elem, false);
# endif
if (++_length == _capacity) reserve(_capacity * 2);
// i - insertion point
// j - parent of i
// y - parent's entry in the heap.
T* y;
uint32_t j;
// i initially indexes the new entry at the bottom of the heap.
uint32_t i = _length;
// Stop if the insertion point reaches the top of the heap.
while (i >= 2)
{
// j indexes the parent of i. y is the parent's entry.
j = i / 2;
y = (T*) vec[j];
// We have the correct insertion point when the item is >= parent
// Otherwise we move the parent down and insertion point up.
if (*((T*)elem) >= *y) break;
vec[i] = y;
i = j;
}
// Insert the new item at the insertion point found.
vec[i] = elem;
}
T* bh_get()
{
U_TRACE_NO_PARAM(0, "UVector<T*>::bh_get()")
U_CHECK_MEMORY
U_INTERNAL_ASSERT_MAJOR(_capacity, 0)
U_INTERNAL_ASSERT(_length <= _capacity)
if (_length)
{
T* elem = bh_min();
// y - the heap entry of the root
// j - the current insertion point for the root
// k - the child of the insertion point
// z - heap entry of the child of the insertion point
T* z;
// Get the value of the root and initialise the insertion point and child
T* y = (T*)vec[_length--];
uint32_t j = 1;
uint32_t k = 2 * 1;
// sift-up only if there is a child of the insertion point
while (k <= _length)
{
// Choose the minimum child unless there is only one
z = (T*) vec[k];
if (k < _length)
{
if (*z > *((T*)vec[k+1])) z = (T*) vec[++k];
}
// We stop if the insertion point for the root is in the correct place.
// Otherwise the child goes up and the root goes down. (i.e. swap)
if (*y <= *z) break;
vec[j] = z;
j = k;
k = 2 * j;
}
// Insert the root in the correct place in the heap
vec[j] = y;
U_RETURN_POINTER(elem, T);
}
U_RETURN_POINTER(0, T);
}
// EXTENSION
uint32_t find(bPFpv function)
{
U_TRACE(0, "UVector<T*>::find(%p)", function)
U_CHECK_MEMORY
U_INTERNAL_DUMP("_length = %u", _length)
T* elem;
for (uint32_t i = 0; i < _length; ++i)
{
elem = at(i);
if (function(elem)) U_RETURN(i);
}
U_RETURN(U_NOT_FOUND);
}
void move(UVector<T*>& source) { UVector<void*>::move(source); } // add to end and reset source
// STREAMS
#ifdef U_STDCPP_ENABLE
friend istream& operator>>(istream& is, UVector<T*>& v)
{
U_TRACE(0+256,"UVector<T*>::operator>>(%p,%p)", &is, &v)
U_INTERNAL_ASSERT_MAJOR(v._capacity,0)
U_INTERNAL_ASSERT(is.peek() == '[' || is.peek() == '(')
int c = EOF;
if (is.good())
{
istream_loading = true; // NB: we need this flag for distinguish this operation in type's ctor...
T* _elem;
U_NEW(T, _elem, T);
streambuf* sb = is.rdbuf();
c = sb->sbumpc(); // skip '[' or '('
while (c != EOF)
{
do { c = sb->sbumpc(); } while (c != EOF && u__isspace(c)); // skip white-space
// U_INTERNAL_DUMP("c = %C", c)
if (c == ')' ||
c == ']' ||
c == EOF)
{
break;
}
if (c == '#')
{
do { c = sb->sbumpc(); } while (c != '\n' && c != EOF); // skip line comment
continue;
}
U_INTERNAL_ASSERT_EQUALS(u__isspace(c),false)
sb->sputbackc(c);
is >> *_elem;
if (is.bad()) is.clear();
else v.push(_elem);
}
// coverity[RESOURCE_LEAK]
# ifndef U_COVERITY_FALSE_POSITIVE
u_destroy<T>((const T*)_elem);
# endif
istream_loading = false;
}
if (c == EOF) is.setstate(ios::eofbit);
// -------------------------------------------------
// NB: we can load an empty vector
// -------------------------------------------------
// if (v._length == 0) is.setstate(ios::failbit);
// -------------------------------------------------
return is;
}
friend ostream& operator<<(ostream& _os, const UVector<T*>& v)
{
U_TRACE(0+256, "UVector<T*>::operator<<(%p,%p)", &_os, &v)
_os.put('(');
_os.put(' ');
for (const void** ptr = v.vec; ptr < (v.vec + v._length); ++ptr)
{
_os << *((T*)(*ptr));
_os.put(' ');
}
_os.put(')');
return _os;
}
# ifdef DEBUG
const char* dump(bool reset) const { return UVector<void*>::dump(reset); }
# endif
#endif
private:
friend class UThreadPool;
#ifdef U_COMPILER_DELETE_MEMBERS
UVector<T*>& operator=(const UVector<T*>&) = delete;
#else
UVector<T*>& operator=(const UVector<T*>&) { return *this; }
#endif
};
// specializzazione stringa
template <> class U_EXPORT UVector<UString> : public UVector<UStringRep*> {
public:
// Costruttori e distruttore
UVector(uint32_t n = 64U) : UVector<UStringRep*>(n)
{
U_TRACE_REGISTER_OBJECT(0, UVector<UString>, "%u", n)
}
UVector(const UString& str, char delim);
UVector(const UString& str, const char* delim = 0);
UVector(UVector<UString>& source, uint32_t n) : UVector<UStringRep*>(n)
{
U_TRACE_REGISTER_OBJECT(0, UVector<UString>, "%p,%u", &source, n)
UVector<void*>::move(source); // add to end and reset source
}
~UVector()
{
U_TRACE_UNREGISTER_OBJECT(0, UVector<UString>)
U_ASSERT(check_memory())
}
// ELEMENT ACCESS
UString begin() { return UString(UVector<UStringRep*>::begin()); }
UString end() { return UString(UVector<UStringRep*>::end()); }
UString rbegin() { return UString(UVector<UStringRep*>::rbegin()); }
UString rend() { return UString(UVector<UStringRep*>::rend()); }
UString front() { return UString(UVector<UStringRep*>::front()); }
UString back() { return UString(UVector<UStringRep*>::back()); }
UString at(uint32_t pos) const __pure
{
U_TRACE(0, "UVector<UString>::at(%u)", pos)
UString result(UVector<UStringRep*>::at(pos));
U_RETURN_STRING(result);
}
UString operator[](uint32_t pos) const;
char* c_pointer(uint32_t pos)
{
U_TRACE(0, "UVector<UString>::c_pointer(%u)", pos)
U_CHECK_MEMORY
if (empty()) return 0;
UStringRep* rep = UVector<UStringRep*>::at(pos);
return rep->data();
}
void replace(uint32_t pos, const UString& str)
{
U_TRACE(0, "UVector<UString>::replace(%u,%V)", pos, str.rep)
UVector<UStringRep*>::replace(pos, str.rep);
}
// STACK OPERATIONS
void push(const UString& str) // add to end
{
U_TRACE(0, "UVector<UString>::push(%V)", str.rep)
UVector<UStringRep*>::push(str.rep);
U_INTERNAL_DUMP("str.rep = %p at(%u) = %p", str.rep, _length-1, UVector<UStringRep*>::at(_length-1))
U_ASSERT_EQUALS(str.rep, UVector<UStringRep*>::at(_length-1))