-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuvector.h
More file actions
1244 lines (1134 loc) · 40.7 KB
/
uvector.h
File metadata and controls
1244 lines (1134 loc) · 40.7 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
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
#ifndef AO_UVECTOR_H
#define AO_UVECTOR_H
#include <algorithm>
#include <cstring>
#include <iterator>
#include <memory>
#include <utility>
#include <stdexcept>
/**
* @file uvector.h
* Header file for uvector and its relational and swap functions.
* @author André Offringa
* @copyright André Offringa, 2013, distributed under the GPL license version 3.
*/
namespace ao {
/**
* @defgroup uvector Class uvector and related functions.
* @{
*/
/**
* @brief A container similar to std::vector, but one that allows construction without initializing its elements.
* @details This container is similar to a std::vector, except that it can be constructor without
* initializing its elements. This saves the overhead of initialization, hence the
* constructor @ref uvector(size_t) is significantly faster than the corresponding std::vector
* constructor, and has no overhead compared to a manually allocated array.
*
* Probably its greatest strength lies in the construction of containers with a number of elements
* that is runtime defined, but that will be initialized later. For example:
*
* @code
* // Open a file
* ifstream file("myfile.bin");
*
* // Construct a buffer for this file
* uvector<char> buffer(buffer_size);
*
* // Read some data into the buffer
* file.read(&buffer[0], buffer_size);
* @endcode
*
* However, it has a few more use-cases with improved performance over std::vector. This is
* possible because of more strengent requirements on the element's type.
*
* The container will behave correctly with any trivial type, but will not work for almost
* all non-trivial types.
*
* The methods with different semantics compared to std::vector are:
* * @ref uvector(size_t n)
* * @ref resize(size_t n)
*
* Also the following new members are introduced:
* * @ref insert_uninitialized(const_iterator position, size_t n)
* * @ref push_back(InputIterator first, InputIterator last)
* * @ref push_back(size_t n, const Tp& val)
* * @ref push_back(std::initializer_list<Tp> initlist)
* * @ref push_back_uninitialized(size_t n)
*
* All other members work exactly like std::vector's members, although some are slightly faster because of
* the stricter requirements on the element type.
*
* @tparam Tp Container's element type
* @tparam Alloc Allocator type. Default is to use the std::allocator.
*
* @author André Offringa
* @copyright André Offringa, 2013, distributed under the GPL license version 3.
*/
template<typename Tp, typename Alloc = std::allocator<Tp> >
class uvector : private Alloc
{
static_assert(std::is_standard_layout<Tp>(), "A uvector can only hold classes with standard layout");
private:
#if __cplusplus > 201402L
typedef std::allocator_traits<allocator_type>::is_always_equal allocator_is_always_equal;
#else
typedef std::false_type allocator_is_always_equal;
#endif
public:
/// Element type
typedef Tp value_type;
/// Type of allocator used to allocate and deallocate space
typedef Alloc allocator_type;
/// Reference to element type
typedef Tp& reference;
/// Constant reference to element type
typedef const Tp& const_reference;
/// Pointer to element type
typedef Tp* pointer;
/// Pointer to constant element type
typedef const Tp* const_pointer;
/// Iterator type
typedef Tp* iterator;
/// Iterator type of constant elements
typedef const Tp* const_iterator;
/// Reverse iterator type
typedef std::reverse_iterator<iterator> reverse_iterator;
/// Reverse iterator of constant elements
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
/// Difference between to iterators
typedef std::ptrdiff_t difference_type;
/// Type used for indexing elements
typedef std::size_t size_t;
/// Type used for indexing elements
typedef std::size_t size_type;
private:
pointer _begin, _end, _endOfStorage;
public:
/** @brief Construct an empty uvector.
* @param allocator Allocator used for allocating and deallocating memory.
*/
explicit uvector(const allocator_type& allocator = Alloc()) noexcept
: Alloc(allocator), _begin(nullptr), _end(nullptr), _endOfStorage(nullptr)
{
}
/** @brief Construct a vector with given amount of elements, without initializing these.
* @details This constructor deviates from std::vector's behaviour, because it will not
* value construct its elements. It is therefore faster than the corresponding constructor
* of std::vector.
* @param n Number of elements that the uvector will be initialized with.
*/
explicit uvector(size_t n) :
_begin(allocate(n)),
_end(_begin + n),
_endOfStorage(_end)
{
}
/** @brief Construct a vector with given amount of elements and set these to a specific value.
* @details This constructor will initialize its members with the given value.
* @param n Number of elements that the uvector will be initialized with.
* @param val Value to initialize all elements with
* @param allocator Allocator used for allocating and deallocating memory.
*/
uvector(size_t n, const value_type& val, const allocator_type& allocator = Alloc()) :
Alloc(allocator),
_begin(allocate(n)),
_end(_begin + n),
_endOfStorage(_end)
{
std::uninitialized_fill_n<Tp*,size_t>(_begin, n, val);
}
/** @brief Construct a vector by copying elements from a range.
* @param first Iterator to range start
* @param last Iterator to range end
* @param allocator Allocator used for allocating and deallocating memory.
*/
template<class InputIterator>
uvector(InputIterator first, InputIterator last, const allocator_type& allocator = Alloc()) :
Alloc(allocator)
{
construct_from_range<InputIterator>(first, last, std::is_integral<InputIterator>());
}
/** @brief Copy construct a uvector.
* @details The allocator of the new uvector will be initialized from
* @c std::allocator_traits<Alloc>::select_on_container_copy_construction(other).
* @param other Source uvector to be copied from.
*/
uvector(const uvector<Tp,Alloc>& other) :
Alloc(std::allocator_traits<Alloc>::select_on_container_copy_construction(static_cast<allocator_type>(other))),
_begin(allocate(other.size())),
_end(_begin + other.size()),
_endOfStorage(_end)
{
std::copy(other._begin, other._end, _begin);
}
/** @brief Copy construct a uvector with custom allocator.
* @param other Source uvector to be copied from.
* @param allocator Allocator used for allocating and deallocating memory.
*/
uvector(const uvector<Tp,Alloc>& other, const allocator_type& allocator) :
Alloc(allocator),
_begin(allocate(other.size())),
_end(_begin + other.size()),
_endOfStorage(_end)
{
std::copy(other._begin, other._end, _begin);
}
/** @brief Move construct a uvector.
* @param other Source uvector to be moved from.
*/
uvector(uvector<Tp,Alloc>&& other) noexcept :
Alloc(std::move(other)),
_begin(other._begin),
_end(other._end),
_endOfStorage(other._endOfStorage)
{
other._begin = nullptr;
other._end = nullptr;
other._endOfStorage = nullptr;
}
/** @brief Move construct a uvector with custom allocator.
* @param other Source uvector to be moved from.
* @param allocator Allocator used for allocating and deallocating memory.
*/
uvector(uvector<Tp,Alloc>&& other, const allocator_type& allocator) noexcept :
Alloc(allocator),
_begin(other._begin),
_end(other._end),
_endOfStorage(other._endOfStorage)
{
other._begin = nullptr;
other._end = nullptr;
other._endOfStorage = nullptr;
}
/** @brief Construct a uvector from a initializer list.
* @param initlist Initializer list used for initializing the new uvector.
* @param allocator Allocator used for allocating and deallocating memory.
*/
uvector(std::initializer_list<Tp> initlist, const allocator_type& allocator = Alloc()) :
Alloc(allocator),
_begin(allocate(initlist.size())),
_end(_begin + initlist.size()),
_endOfStorage(_end)
{
iterator destIter = _begin;
for(typename std::initializer_list<Tp>::const_iterator i=initlist.begin(); i!=initlist.end(); ++i)
{
*destIter = *i;
++destIter;
}
}
/** @brief Destructor. */
~uvector() noexcept
{
deallocate();
}
/** @brief Assign another uvector to this uvector.
* @details The allocator of the uvector will be assigned to @p other when
* std::allocator_traits<Alloc>::propagate_on_container_copy_assignment() is of true_type.
*/
uvector& operator=(const uvector<Tp,Alloc>& other)
{
return assign_copy_from(other, typename std::allocator_traits<Alloc>::propagate_on_container_copy_assignment());
}
/** @brief Assign another uvector to this uvector.
* @details The allocator of the uvector will be assigned to @p other when
* std::allocator_traits<Alloc>::propagate_on_container_move_assignment() is of true_type.
*/
uvector& operator=(uvector<Tp,Alloc>&& other) noexcept(
std::allocator_traits<Alloc>::propagate_on_container_move_assignment::value||
allocator_is_always_equal::value)
{
return assign_move_from(std::move(other), typename std::allocator_traits<Alloc>::propagate_on_container_move_assignment());
}
/** @brief Get iterator to first element. */
iterator begin() noexcept { return _begin; }
/** @brief Get constant iterator to first element. */
const_iterator begin() const noexcept { return _begin; }
/** @brief Get iterator to element past last element. */
iterator end() noexcept { return _end; }
/** @brief Get constant iterator to element past last element. */
const_iterator end() const noexcept { return _end; }
/** @brief Get reverse iterator to last element. */
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
/** @brief Get constant reverse iterator to last element. */
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
/** @brief Get reverse iterator to element before first element. */
reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
/** @brief Get constant reverse iterator to element before first element. */
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
/** @brief Get constant iterator to first element. */
const_iterator cbegin() const noexcept { return _begin; }
/** @brief Get constant iterator to element past last element. */
const_iterator cend() const noexcept { return _end; }
/** @brief Get constant reverse iterator to last element. */
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
/** @brief Get constant reverse iterator to element before first element. */
const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
/** @brief Get number of elements in container. */
size_t size() const noexcept { return _end - _begin; }
/** @brief Get maximum number of elements that this container can hold. */
size_t max_size() const noexcept { return Alloc::max_size(); }
/** @brief Change the number of elements in the container.
* @details If the new size is larger than the current size, new values will be
* left uninitialized. Therefore, it is more efficient than @c resize(size_t) in
* @c std::vector, as well as @ref resize(size_t, const Tp&).
* If the new size is smaller than the current size, the container will be
* truncated and elements past the new size will be removed. No destructor of the
* removed elements will be called.
* @param n The new size of the container.
*/
void resize(size_t n)
{
if(capacity() < n)
{
size_t newSize = enlarge_size(n);
pointer newStorage = allocate(newSize);
std::move(_begin, _end, newStorage);
deallocate();
_begin = newStorage;
_endOfStorage = _begin + newSize;
}
_end = _begin + n;
}
/** @brief Change the number of elements in the container.
* @details If the new size is larger than the current size, new values will be
* initialized by the given value.
* If the new size is smaller than the current size, the container will be
* truncated and elements past the new size will be removed. No destructor of the
* removed elements will be called.
* @param n The new size of the container.
* @param val New value of elements that get added to the container.
*/
void resize(size_t n, const Tp& val)
{
size_t oldSize = size();
if(capacity() < n)
{
pointer newStorage = allocate(n);
std::move(_begin, _end, newStorage);
deallocate();
_begin = newStorage;
_endOfStorage = _begin + n;
}
_end = _begin + n;
if(oldSize < n)
std::uninitialized_fill<Tp*,size_t>(_begin + oldSize, _end, val);
}
/** @brief Get the number of elements the container can currently hold without reallocating storage. */
size_t capacity() const noexcept { return _endOfStorage - _begin; }
/** @brief Determine if the container is currently empty.
* @returns @c true if @ref size() == 0. */
bool empty() const noexcept { return _begin == _end; }
/** @brief Reserve space for a number of elements, to prevent the overhead of extra
* reallocations.
* @details This has no effect on the working of the uvector, except that it might change
* the current capacity. This can enhance performance when a large number of elements are added,
* and an approximate size is known a priori.
*
* This method might cause a reallocation, causing iterators to be invalidated.
* @param n Number of elements to reserve space for.
*/
void reserve(size_t n)
{
if(capacity() < n)
{
const size_t curSize = size();
pointer newStorage = allocate(n);
std::move(_begin, _begin + curSize, newStorage);
deallocate();
_begin = newStorage;
_end = newStorage + curSize;
_endOfStorage = _begin + n;
}
}
/** @brief Change the capacity of the container such that no extra space is hold.
* @details This has no effect on the working of the uvector, except that it might change
* the current capacity. This can reduce the current memory usage of the container.
*
* This method might cause a reallocation, causing iterators to be invalidated.
*/
void shrink_to_fit()
{
const size_t curSize = size();
if(curSize == 0)
{
deallocate();
_begin = nullptr;
_end = nullptr;
_endOfStorage = nullptr;
}
else if(curSize < capacity()) {
pointer newStorage = allocate(curSize);
std::move(_begin, _begin + curSize, newStorage);
deallocate();
_begin = newStorage;
_end = newStorage + curSize;
_endOfStorage = _begin + curSize;
}
}
/** @brief Get a reference to the element at the given index. */
Tp& operator[](size_t index) noexcept { return _begin[index]; }
/** @brief Get a constant reference to the element at the given index. */
const Tp& operator[](size_t index) const noexcept { return _begin[index]; }
/** @brief Get a reference to the element at the given index with bounds checking.
* @throws std::out_of_range when given index is past the last element.
*/
Tp& at(size_t index)
{
check_bounds(index);
return _begin[index];
}
/** @brief Get a constant reference to the element at the given index with bounds checking.
* @throws std::out_of_range when given index is past the last element.
*/
const Tp& at(size_t index) const
{
check_bounds(index);
return _begin[index];
}
/** @brief Get reference to first element in container. */
Tp& front() noexcept { return *_begin; }
/** @brief Get constant reference to first element in container. */
const Tp& front() const noexcept { return *_begin; }
/** @brief Get reference to last element in container. */
Tp& back() noexcept { return *(_end - 1); }
/** @brief Get constant reference to last element in container. */
const Tp& back() const noexcept { return *(_end - 1); }
/** @brief Get pointer to internal storage. */
Tp* data() noexcept { return _begin; }
/** @brief Get constant pointer to internal storage. */
const Tp* data() const noexcept { return _begin; }
/** @brief Assign this container to be equal to the given range.
* @details The container will be resized to fit the length of the given
* range. Iterators are invalidated.
* @param first Iterator to the beginning of the range.
* @param last Iterator past the end of the range.
*/
template<class InputIterator>
void assign(InputIterator first, InputIterator last)
{
assign_from_range<InputIterator>(first, last, std::is_integral<InputIterator>());
}
/** @brief Resize the container and assign the given value to all elements.
* @details Iterators are invalidated.
* @param n New size of container
* @param val Value to be assigned to all elements.
*/
void assign(size_t n, const Tp& val)
{
if(n > capacity())
{
iterator newStorage = allocate(n);
deallocate();
_begin = newStorage;
_endOfStorage = _begin + n;
}
_end = _begin + n;
std::uninitialized_fill_n<Tp*,size_t>(_begin, n, val);
}
/** @brief Assign this container to an initializer list.
* @details The container will be resized to fit the length of the given
* initializer list. Iterators are invalidated.
* @param initlist List of values to assign to the container.
*/
void assign(std::initializer_list<Tp> initlist)
{
if(initlist.size() > capacity())
{
iterator newStorage = allocate(initlist.size());
deallocate();
_begin = newStorage;
_endOfStorage = _begin + initlist.size();
}
_end = _begin + initlist.size();
iterator destIter = _begin;
for(typename std::initializer_list<Tp>::const_iterator i=initlist.begin(); i!=initlist.end(); ++i)
{
*destIter = *i;
++destIter;
}
}
/** @brief Add the given value to the end of the container.
* @details Iterators are invalidated.
* @param item Value of new element.
*/
void push_back(const Tp& item)
{
if(_end == _endOfStorage)
enlarge(enlarge_size(1));
*_end = item;
++_end;
}
/** @brief Add the given value to the end of the container by moving it in.
* @details Iterators are invalidated.
*
* Note that this container can only hold simple types that do not perform allocations. Therefore,
* there is probably no benefit in moving the new item in over copying it in with @ref push_back(const Tp&).
* @param item Value of new element.
*/
void push_back(Tp&& item)
{
if(_end == _endOfStorage)
enlarge(enlarge_size(1));
*_end = std::move(item);
++_end;
}
/** @brief Remove the last element from the container. */
void pop_back()
{
--_end;
}
/** @brief Insert an element at a given position.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
* @param position Position of the new element. The new element will be added before the old element
* at that position.
* @param item Value of the new item.
* @return Position of the new element.
*/
iterator insert(const_iterator position, const Tp& item)
{
if(_end == _endOfStorage)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(1), index, 1);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+1);
++_end;
}
*const_cast<iterator>(position) = item;
return const_cast<iterator>(position);
}
/** @brief Insert elements at a given position and initialize them with a value.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
* @param position Position of the new elements. The new elements will be added before the old element
* at that position.
* @param n Number of elements to add.
* @param val Value of the new item.
* @return Position of the first new element.
*/
iterator insert(const_iterator position, size_t n, const Tp& val)
{
if(capacity() < size() + n)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(n), index, n);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+n);
_end += n;
}
std::uninitialized_fill_n<Tp*,size_t>(const_cast<iterator>(position), n, val);
return const_cast<iterator>(position);
}
/** @brief Insert elements at a given position and initialize them from a range.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
* @param position Position of the new elements. The new elements will be added before the old element
* at that position.
* @param first Iterator to the beginning of the range.
* @param last Iterator past the end of the range.
* @return Position of the first new element.
*/
template <class InputIterator>
iterator insert(const_iterator position, InputIterator first, InputIterator last)
{
return insert_from_range<InputIterator>(position, first, last, std::is_integral<InputIterator>());
}
/** @brief Insert an element at a given position by moving it in.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
*
* Note that this container can only hold simple types that do not perform allocations. Therefore,
* there is probably no benefit in moving the new item in over copying it in with
* @ref insert(const_iterator, const Tp&).
* @param position Position of the new element. The new element will be added before the old element
* at that position.
* @param item Value of the new item.
* @return Position of the new element.
*/
iterator insert(const_iterator position, Tp&& item)
{
if(_end == _endOfStorage)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(1), index, 1);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+1);
++_end;
}
*const_cast<iterator>(position) = std::move(item);
return const_cast<iterator>(position);
}
/** @brief Insert elements at a given position and initialize them from a initializer list.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
* @param position Position of the new elements. The new elements will be added before the old element
* at that position.
* @param initlist List of items to insert.
* @return Position of the first new element.
*/
iterator insert(const_iterator position, std::initializer_list<Tp> initlist)
{
if(capacity() < size() + initlist.size())
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(initlist.size()), index, initlist.size());
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+initlist.size());
_end += initlist.size();
}
iterator destIter = const_cast<iterator>(position);
for(typename std::initializer_list<Tp>::const_iterator i=initlist.begin(); i!=initlist.end(); ++i)
{
*destIter = *i;
++destIter;
}
return const_cast<iterator>(position);
}
/** @brief Delete an element from the container.
* @details This operation moves all elements past the removed element, and can therefore be
* expensive.
* @param position Position of element to be removed.
* @return Iterator pointing to the first element past the delete element.
*/
iterator erase(const_iterator position)
{
std::move(const_cast<iterator>(position)+1, _end, const_cast<iterator>(position));
--_end;
return const_cast<iterator>(position);
}
/** @brief Delete a range of elements from the container.
* @details This operation moves all elements past the removed elements, and can therefore be
* expensive.
* @param first Position of first element to be removed.
* @param last Position past last element to be removed.
* @return Iterator pointing to the first element past the delete element.
*/
iterator erase(const_iterator first, const_iterator last)
{
std::move(const_cast<iterator>(last), _end, const_cast<iterator>(first));
_end -= last - first;
return const_cast<iterator>(first);
}
/** @brief Swap the contents of this uvector with the given uvector.
* @details Iterators to both vectors will remain valid and will point into
* to the swapped container afterwards. This function will never reallocate
* space.
*
* The allocator will be swapped when the @c propagate_on_container_swap
* of the respective @c allocator_trait is @c true_type.
* Its behaviour is undefined when the allocators do not compare equal and
* @c propagate_on_container_swap is false.
* @param other Other uvector whose contents it to be swapped with this.
*/
void swap(uvector<Tp, Alloc>& other) noexcept
{
swap(other, typename std::allocator_traits<Alloc>::propagate_on_container_swap());
}
/** @brief Remove all elements from the container. */
void clear()
{
_end = _begin;
}
/** @brief Insert an element at a given position by constructing it in place.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive.
* @param position Position of the new element. The new element will be added before the old element
* at that position.
* @param args List of arguments to be forwarded to construct the new element.
* @return Position of the new element.
*/
template<typename... Args>
iterator emplace(const_iterator position, Args&&... args)
{
if(_end == _endOfStorage)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(1), index, 1);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+1);
++_end;
}
*const_cast<iterator>(position) = Tp(std::forward<Args>(args)...);
return const_cast<iterator>(position);
}
/** @brief Add the given value to the end of the container by constructing it in place.
* @details Iterators are invalidated.
* @param args List of arguments to be forwarded to construct the new element.
*/
template<typename... Args>
void emplace_back(Args&&... args)
{
if(_end == _endOfStorage)
enlarge(enlarge_size(1));
*_end = Tp(std::forward<Args>(args)...);
++_end;
}
/** @brief Get a copy of the allocator. */
allocator_type get_allocator() const noexcept
{
return *this;
}
// --- NON STANDARD METHODS ---
/** @brief Insert elements at a given position without initializing them.
* @details All iterators will be invalidated. This operation needs to move all elements after
* the new element, and can therefore be expensive. It will not initialize the new elements,
* and is therefore faster than @ref insert(const_iterator, size_t, const Tp&).
*
* This method is non-standard: it is not present in std::vector.
* @param position Position of the new elements. The new elements will be added before the old element
* at that position.
* @param n Number of elements to add.
*/
iterator insert_uninitialized(const_iterator position, size_t n)
{
if(capacity() < size() + n)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(n), index, n);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+n);
_end += n;
}
return const_cast<iterator>(position);
}
/** @brief Add a range of items to the end of the container.
* @details All iterators will be invalidated.
*
* This method is non-standard: it is not present in std::vector.
* @param first Iterator to the beginning of the range.
* @param last Iterator past the end of the range.
*/
template <class InputIterator>
void push_back(InputIterator first, InputIterator last)
{
push_back_range<InputIterator>(first, last, std::is_integral<InputIterator>());
}
/** @brief Add elements at the end and initialize them with a value.
* @details All iterators will be invalidated.
*
* This method is non-standard: it is not present in std::vector.
* @param n Number of elements to add.
* @param val Value of the new items.
*/
void push_back(size_t n, const Tp& val)
{
if(capacity() - size() < n)
{
enlarge(enlarge_size(n));
}
std::uninitialized_fill_n<Tp*,size_t>(_end, n, val);
_end += n;
}
/** @brief Add elements from an initializer list to the end of the container.
* @details All iterators will be invalidated.
*
* This method is non-standard: it is not present in std::vector.
* @param initlist The list with values to add.
*/
void push_back(std::initializer_list<Tp> initlist)
{
if(capacity() - size() < initlist.size())
{
enlarge(enlarge_size(initlist.size()));
}
for(typename std::initializer_list<Tp>::iterator i = initlist.begin(); i != initlist.end(); ++i)
{
*_end = *i;
++_end;
}
}
/** @brief Add elements at the end without initializing them.
* @details All iterators will be invalidated.
*
* This method is non-standard: it is not present in std::vector.
* @param n Number of elements to add.
*/
void push_back_uninitialized(size_t n)
{
resize(size() + n);
}
private:
pointer allocate(size_t n)
{
return Alloc::allocate(n);
}
void deallocate() noexcept
{
deallocate(_begin, capacity());
}
void deallocate(pointer begin, size_t n) noexcept
{
if(begin != nullptr)
Alloc::deallocate(begin, n);
}
template<typename InputIterator>
void construct_from_range(InputIterator first, InputIterator last, std::false_type)
{
construct_from_range<InputIterator>(first, last, typename std::iterator_traits<InputIterator>::iterator_category());
}
template<typename Integral>
void construct_from_range(Integral n, Integral val, std::true_type)
{
_begin = allocate(n);
_end = _begin + n;
_endOfStorage = _end;
std::uninitialized_fill_n<Tp*,size_t>(_begin, n, val);
}
template<typename InputIterator>
void construct_from_range(InputIterator first, InputIterator last, std::forward_iterator_tag)
{
size_t n = std::distance(first, last);
_begin = allocate(n);
_end = _begin + n;
_endOfStorage = _begin + n;
Tp* destIter = _begin;
while(first != last)
{
*destIter = *first;
++destIter; ++first;
}
}
template<typename InputIterator>
void assign_from_range(InputIterator first, InputIterator last, std::false_type)
{
assign_from_range<InputIterator>(first, last, typename std::iterator_traits<InputIterator>::iterator_category());
}
// This function is called from assign(iter,iter) when Tp is an integral. In that case,
// the user tried to call assign(n, &val), but it got caught by the wrong overload.
template<typename Integral>
void assign_from_range(Integral n, Integral val, std::true_type)
{
if(size_t(n) > capacity())
{
iterator newStorage = allocate(n);
deallocate();
_begin = newStorage;
_endOfStorage = _begin + n;
}
_end = _begin + n;
std::uninitialized_fill_n<Tp*,size_t>(_begin, n, val);
}
template<typename InputIterator>
void assign_from_range(InputIterator first, InputIterator last, std::forward_iterator_tag)
{
size_t n = std::distance(first, last);
if(n > capacity())
{
iterator newStorage = allocate(n);
deallocate();
_begin = newStorage;
_endOfStorage = _begin + n;
}
_end = _begin + n;
Tp* destIter = _begin;
while(first != last)
{
*destIter = *first;
++destIter; ++first;
}
}
template<typename InputIterator>
iterator insert_from_range(const_iterator position, InputIterator first, InputIterator last, std::false_type)
{
return insert_from_range<InputIterator>(position, first, last,
typename std::iterator_traits<InputIterator>::iterator_category());
}
template<typename Integral>
iterator insert_from_range(const_iterator position, Integral n, Integral val, std::true_type)
{
if(capacity() < size() + n)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(n), index, n);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+n);
_end += n;
}
std::uninitialized_fill_n<Tp*,size_t>(const_cast<iterator>(position), n, val);
return const_cast<iterator>(position);
}
template<typename InputIterator>
iterator insert_from_range(const_iterator position, InputIterator first, InputIterator last, std::forward_iterator_tag)
{
size_t n = std::distance(first, last);
if(capacity() < size() + n)
{
size_t index = position - _begin;
enlarge_for_insert(enlarge_size(n), index, n);
position = _begin + index;
}
else {
std::move_backward(const_cast<iterator>(position), _end, _end+n);
_end += n;
}
Tp* destIter = const_cast<iterator>(position);
while(first != last)
{
*destIter = *first;
++destIter; ++first;
}
return const_cast<iterator>(position);
}
void check_bounds(size_t index) const
{
if(index >= size())
throw std::out_of_range("Access to element in uvector past end");
}
size_t enlarge_size(size_t extra_space_needed) const noexcept
{
return size() + std::max(size(), extra_space_needed);
}
void enlarge(size_t newSize)
{
pointer newStorage = allocate(newSize);
std::copy(_begin, _end, newStorage);
deallocate();
_end = newStorage + size();
_begin = newStorage;
_endOfStorage = _begin + newSize;
}
void enlarge_for_insert(size_t newSize, size_t insert_position, size_t insert_count)
{
pointer newStorage = allocate(newSize);
std::copy(_begin, _begin + insert_position, newStorage);
std::copy(_begin + insert_position, _end, newStorage + insert_position + insert_count);
deallocate();