-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevector.hpp
More file actions
2953 lines (2578 loc) · 87.4 KB
/
Copy pathdevector.hpp
File metadata and controls
2953 lines (2578 loc) · 87.4 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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Benedek Thaler 2015-2016. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://erenon.hu/double_ended for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_DOUBLE_ENDED_DEVECTOR_HPP
#define BOOST_DOUBLE_ENDED_DEVECTOR_HPP
#include <algorithm>
#include <cstring> // memcpy
#include <stdexcept>
#include <type_traits>
#include <boost/assert.hpp>
#include <boost/aligned_storage.hpp>
#include <boost/throw_exception.hpp>
#include <boost/double_ended/detail/allocator.hpp>
#include <boost/double_ended/detail/iterators.hpp>
#include <boost/double_ended/detail/algorithm.hpp>
namespace boost {
namespace double_ended {
/**
* Instructs devector to make space for `Size` elements
* inline with its internals, allowing to store that much elements
* without allocating memory.
*
* Models the `SmallBufferPolicy` concept of the devector class.
*/
template <unsigned Size>
struct small_buffer_size
{
BOOST_STATIC_CONSTANT(unsigned, size = Size);
};
/**
* Controls a devectors reallocation policy.
*
* Models the `GrowthPolicy` concept of the devector class.
*/
struct devector_growth_policy
{
/**
* Sets the `size_type` type of the devector.
*/
typedef unsigned int size_type;
/**
* **Returns**: 4 times the old capacity or 16 if it's 0.
*
* @param capacity The current capacity of the devector, equals to `capacity()`.
*/
static size_type new_capacity(size_type capacity)
{
return (capacity) ? capacity * 4u : 16u;
}
/**
* **Returns**: true, if the contents fit in the small buffer.
*
* @param size The element count of the devector, equals to `size()`
* @param capacity The current capacity of the devector, equals to `capacity()`.
* @param small_buffer_size The size of the small buffer, specified by the `SmallBufferPolicy`.
*/
static bool should_shrink(size_type size, size_type capacity, size_type small_buffer_size)
{
(void)capacity;
return size <= small_buffer_size;
}
};
struct reserve_only_tag {};
struct unsafe_uninitialized_tag {};
/**
* A vector-like sequence container providing front and back operations
* (e.g: `push_front`/`pop_front`/`push_back`/`pop_back`) with amortized constant complexity,
* small buffer optimization and unsafe methods geared towards additional performance.
*
* Models the [SequenceContainer], [ReversibleContainer], and [AllocatorAwareContainer] concepts.
*
* **Requires**:
* - `T` shall be [MoveInsertable] into the devector.
* - `T` shall be [Erasable] from any `devector<T, Allocator, SBP, GP>`.
* - `SmallBufferPolicy`, `GrowthPolicy`, and `Allocator` must model the concepts with the same names.
*
* **Definition**: `T` is `NothrowConstructible` if it's either nothrow move constructible or
* nothrow copy constructible.
*
* **Definition**: `T` is `NothrowAssignable` if it's either nothrow move assignable or
* nothrow copy assignable.
*
* **Definition**: A devector `d` is _small_ if it has a non-zero size small buffer and
* `d.size()` is smaller or equal than the size of the small buffer of `d`.
*
* **Exceptions**: The exception specifications assume `T` is nothrow [Destructible].
*
* Most methods providing the strong exception guarantee assume `T` either has a move
* constructor marked noexcept or is [CopyInsertable] into the devector. If it isn't true,
* and the move constructor throws, the guarantee is waived and the effects are unspecified.
*
* In addition to the exceptions specified in the **Throws** clause, the following operations
* of `T` can throw when any of the specified concept is required (assuming `std::allocator` is used):
* - [DefaultInsertable][]: Default constructor
* - [MoveInsertable][]: Move constructor
* - [CopyInsertable][]: Copy constructor
* - [DefaultConstructible][]: Default constructor
* - [EmplaceConstructible][]: Constructor selected by the given arguments
* - [MoveAssignable][]: Move assignment operator
* - [CopyAssignable][]: Copy assignment operator
*
* Furthermore, not `noexcept` methods throws whatever the allocator throws
* if memory allocation fails. Such methods also throw `std::length_error` if the capacity
* exceeds `max_size()`.
*
* **Remark**: If a method invalidates some iterators, it also invalidates references
* and pointers to the elements pointed by the invalidated iterators.
*
* **Policies**:
*
* The type `SBP` models the `SmallBufferPolicy` concept if it satisfies the following requirements:
*
* Expression | Return type | Description
* -----------|-------------|------------
* `SBP::size` | `size_type` | The total number of elements that can be stored without allocation
*
* @ref small_buffer_size models the `SmallBufferPolicy` concept.
*
* The type `GP` models the `GrowthPolicy` concept if it satisfies the following requirements
* when `gp` is an instance of such a class:
*
* Expression | Return type | Description
* -----------|-------------|------------
* `GP::size_type` | Unsigned integral type | Sets the `size_type` type of the `devector`, thus the maximum number of elements it can hold. Must be a compile time constant.
* `gp.new_capacity(old_capacity)` | `size_type` | Computes the new capacity to be allocated. The returned value must be greater than `old_capacity`. This method is always used when a new buffer gets allocated. `old_capacity` is convertible to `size_type`.
* `gp.should_shrink(size, capacity, small_buffer_size)` | `bool` | Returns `true`, if superfluous memory should be released. Arguments are convertible to `size_type`.
*
* @ref devector_growth_policy models the `GrowthPolicy` concept.
*
* [SequenceContainer]: http://en.cppreference.com/w/cpp/concept/SequenceContainer
* [ReversibleContainer]: http://en.cppreference.com/w/cpp/concept/ReversibleContainer
* [AllocatorAwareContainer]: http://en.cppreference.com/w/cpp/concept/AllocatorAwareContainer
* [DefaultInsertable]: http://en.cppreference.com/w/cpp/concept/DefaultInsertable
* [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
* [Erasable]: http://en.cppreference.com/w/cpp/concept/Erasable
* [DefaultConstructible]: http://en.cppreference.com/w/cpp/concept/DefaultConstructible
* [Destructible]: http://en.cppreference.com/w/cpp/concept/Destructible
* [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
* [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
* [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
*/
template <
typename T,
typename SmallBufferPolicy = small_buffer_size<0>,
typename GrowthPolicy = devector_growth_policy,
typename Allocator = std::allocator<T>
>
class devector
#ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
: Allocator
#endif // ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
{
#ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
typedef detail::allocator_traits<Allocator> allocator_traits;
#endif // ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
public:
// Standard Interface Types:
typedef T value_type;
typedef Allocator allocator_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename allocator_traits::pointer pointer;
typedef typename allocator_traits::const_pointer const_pointer;
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef typename GrowthPolicy::size_type size_type;
typedef typename std::make_signed<size_type>::type difference_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
private:
typedef typename allocator_traits::allocation_guard allocation_guard;
typedef typename allocator_traits::construction_guard construction_guard;
typedef typename allocator_traits::move_guard move_guard;
// Random access pseudo iterator always yielding to the same result
typedef detail::constant_iterator<T, difference_type> cvalue_iterator;
static constexpr bool t_is_nothrow_constructible = allocator_traits::t_is_nothrow_constructible;
static constexpr bool t_is_trivially_copyable = allocator_traits::t_is_trivially_copyable;
static constexpr size_type sbuffer_size = SmallBufferPolicy::size;
static constexpr bool no_small_buffer = (sbuffer_size == 0);
#endif // ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
// Standard Interface
public:
// construct/copy/destroy
/**
* **Effects**: Constructs an empty devector.
*
* **Postcondition**: `empty() && front_free_capacity() == 0
* && back_free_capacity() == small buffer size`.
*
* **Complexity**: Constant.
*/
devector() noexcept
:_buffer(_storage.small_buffer_address()),
_front_index(0),
_back_index(0)
{}
/**
* **Effects**: Constructs an empty devector, using the specified allocator.
*
* **Postcondition**: `empty() && front_free_capacity() == 0
* && back_free_capacity() == small buffer size`.
*
* **Complexity**: Constant.
*/
explicit devector(const Allocator& allocator) noexcept
:Allocator(allocator),
_buffer(_storage.small_buffer_address()),
_front_index(0),
_back_index(0)
{}
/**
* **Effects**: Constructs an empty devector, using the specified allocator
* and reserves `n` slots as if `reserve(n)` was called.
*
* **Postcondition**: `empty() && front_free_capacity() == 0
* && back_free_capacity() >= n`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Constant.
*/
devector(size_type n, reserve_only_tag, const Allocator& allocator = Allocator())
:devector(0, n, reserve_only_tag{}, allocator)
{}
/**
* **Effects**: Constructs an empty devector, using the specified allocator
* and reserves `front_cap + back_cap` slots as if `reserve_front(front_cap)` and
* `reserve_back(back_cap)` was called.
*
* **Postcondition**: `empty() && front_free_capacity() == front_cap
* && back_free_capacity() >= back_cap`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Constant.
*
* **Remarks**: This constructor can be used to split the small buffer
* between expected front and back insertions.
*/
devector(size_type front_cap, size_type back_cap, reserve_only_tag, const Allocator& allocator = Allocator())
:Allocator(allocator),
_storage(front_cap + back_cap),
_buffer(allocate(_storage._capacity)),
_front_index(front_cap),
_back_index(front_cap)
{}
/**
* **Unsafe constructor**, use with care.
*
* **Effects**: Constructs a devector containing `n` uninitialized elements.
*
* **Postcondition**: `size() == n && front_free_capacity() == 0`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Constant.
*
* **Remarks**: The devector does not keep track of initialization of the elements,
* the initially provided uninitialized elements must be manually initialized,
* e.g: through the pointer returned by `data()`. This constructor is unsafe
* because the user must take care of the initialization, before the destructor
* is called at latest. Failing that, the destructor would invoke undefined
* behavior if the destructor of `T` is non-trivial, even if the devectors
* destructor is called by the stack unwinding effect of an exception.
*/
devector(size_type n, unsafe_uninitialized_tag, const Allocator& allocator = Allocator())
:Allocator(allocator),
_storage(n),
_buffer(allocate(_storage._capacity)),
_front_index(),
_back_index(n)
{}
/**
* [DefaultInsertable]: http://en.cppreference.com/w/cpp/concept/DefaultInsertable
*
* **Effects**: Constructs a devector with `n` default-inserted elements using the specified allocator.
*
* **Requires**: `T` shall be [DefaultInsertable] into `*this`.
*
* **Postcondition**: `size() == n && front_free_capacity() == 0`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Linear in `n`.
*/
explicit devector(size_type n, const Allocator& allocator = Allocator())
:Allocator(allocator),
_storage(n),
_buffer(allocate(_storage._capacity)),
_front_index(),
_back_index(n)
{
// Cannot use construct_from_range/constant_iterator and copy_range,
// because we are not allowed to default construct T
allocation_guard buffer_guard(_buffer, _storage._capacity, get_allocator_ref());
if (is_small()) { buffer_guard.release(); } // avoid disposing small buffer
construction_guard copy_guard(_buffer, get_allocator_ref());
for (size_type i = 0; i < n; ++i)
{
alloc_construct(_buffer + i);
copy_guard.extend();
}
copy_guard.release();
buffer_guard.release();
BOOST_ASSERT(invariants_ok());
}
/**
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
*
* **Effects**: Constructs a devector with `n` copies of `value`, using the specified allocator.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this`.
*
* **Postcondition**: `size() == n && front_free_capacity() == 0`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Linear in `n`.
*/
devector(size_type n, const T& value, const Allocator& allocator = Allocator())
:Allocator(allocator),
_storage(n),
_buffer(allocate(_storage._capacity)),
_front_index(),
_back_index(n)
{
construct_from_range(cvalue_iterator(value, n), cvalue_iterator());
BOOST_ASSERT(invariants_ok());
}
/**
* **Effects**: Constructs a devector equal to the range `[first,last)`, using the specified allocator.
*
* **Requires**: `T` shall be [EmplaceConstructible] into `*this` from `*first`. If the specified
* iterator does not meet the forward iterator requirements, `T` shall also be [MoveInsertable]
* into `*this`.
*
* **Postcondition**: `size() == std::distance(first, last)
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Makes only `N` calls to the copy constructor of `T` (where `N` is the distance between `first`
* and `last`), at most one allocation and no reallocations if iterators first and last are of forward,
* bidirectional, or random access categories. It makes `O(N)` calls to the copy constructor of `T`
* and `O(log(N)) reallocations if they are just input iterators.
*
* **Remarks**: Each iterator in the range `[first,last)` shall be dereferenced exactly once,
* unless an exception is thrown.
*
* [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
* [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
*/
template <BOOST_DOUBLE_ENDED_REQUIRE_INPUT_ITERATOR(InputIterator)>
devector(InputIterator first, InputIterator last, const Allocator& allocator = Allocator())
:devector(allocator) // Use the destructor to clean up on exception
{
_front_index = _back_index = 0; // use the full small buffer
while (first != last)
{
emplace_back(*first++);
}
BOOST_ASSERT(invariants_ok());
}
#ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
template <BOOST_DOUBLE_ENDED_REQUIRE_FW_ITERATOR(ForwardIterator)>
devector(ForwardIterator first, ForwardIterator last, const Allocator& allocator = Allocator())
:Allocator(allocator),
_storage(std::distance(first, last)),
_buffer(allocate(_storage._capacity)),
_front_index(),
_back_index(std::distance(first, last))
{
construct_from_range(first, last);
BOOST_ASSERT(invariants_ok());
}
#endif // ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
/**
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
*
* **Effects**: Copy constructs a devector.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this`.
*
* **Postcondition**: `this->size() == x.size() && front_free_capacity() == 0`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Linear in the size of `x`.
*/
devector(const devector& x)
:devector(
x.begin(), x.end(),
allocator_traits::select_on_container_copy_construction(x.get_allocator_ref())
)
{}
/**
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
*
* **Effects**: Copy constructs a devector, using the specified allocator.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this`.
*
* **Postcondition**: `this->size() == x.size() && front_free_capacity() == 0`.
*
* **Exceptions**: Strong exception guarantee.
*
* **Complexity**: Linear in the size of `x`.
*/
devector(const devector& x, const Allocator& allocator)
:devector(x.begin(), x.end(), allocator)
{}
/**
* **Effects**: Moves `rhs`'s resources to `*this`.
*
* **Throws**: If `rhs` _small_ and `T`'s move constructor throws.
*
* **Postcondition**: `rhs` is left in an unspecified but valid state.
*
* **Exceptions**: Strong exception guarantee if not `noexcept`.
*
* **Complexity**: Linear in the size of the small buffer.
*/
devector(devector&& rhs) noexcept(no_small_buffer || t_is_nothrow_constructible)
:devector(std::move(rhs), rhs.get_allocator_ref())
{}
/**
* **Effects**: Moves `rhs`'s resources to `*this`, using the specified allocator.
*
* **Throws**: If `rhs` _small_ and `T`'s move constructor throws.
*
* **Postcondition**: `rhs` is left in an unspecified but valid state.
*
* **Exceptions**: Strong exception guarantee if not `noexcept`.
*
* **Complexity**: Linear in the size of the small buffer.
*/
devector(devector&& rhs, const Allocator& allocator) noexcept(
no_small_buffer || t_is_nothrow_constructible
)
:Allocator(allocator),
_storage(rhs.capacity()),
_buffer(
(rhs.is_small()) ? _storage.small_buffer_address() : rhs._buffer
),
_front_index(rhs._front_index),
_back_index(rhs._back_index)
{
// TODO should move elems-by-elems if the two allocators differ
if (rhs.is_small() == false)
{
// buffer is already acquired, reset rhs
rhs._storage._capacity = sbuffer_size;
rhs._buffer = rhs._storage.small_buffer_address();
rhs._front_index = 0;
rhs._back_index = 0;
}
else
{
// elems must be moved/copied to small buffer
opt_move_or_copy(rhs.begin(), rhs.end(), begin());
}
BOOST_ASSERT( invariants_ok());
BOOST_ASSERT(rhs.invariants_ok());
}
/**
* **Equivalent to**: `devector(il.begin(), il.end())` or `devector(il.begin(), il.end(), allocator)`.
*/
devector(const std::initializer_list<T>& il, const Allocator& allocator = Allocator())
:devector(il.begin(), il.end(), allocator)
{}
/**
* **Effects**: Destroys the devector. All stored values are destroyed and
* used memory, if any, deallocated.
*
* **Complexity**: Linear in the size of `*this`.
*/
~devector() noexcept
{
destroy_elements(_buffer + _front_index, _buffer + _back_index);
deallocate_buffer();
}
/**
* **Effects**: Copies elements of `x` to `*this`. Previously
* held elements get copy assigned to or destroyed.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this`.
*
* **Postcondition**: `this->size() == x.size()`, the elements of
* `*this` are copies of elements in `x` in the same order.
*
* **Returns**: `*this`.
*
* **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
* and the allocator is allowed to be propagated
* ([propagate_on_container_copy_assignment] is true),
* Basic exception guarantee otherwise.
*
* **Complexity**: Linear in the size of `x` and `*this`.
*
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
* [propagate_on_container_copy_assignment]: http://en.cppreference.com/w/cpp/memory/allocator_traits
*/
devector& operator=(const devector& x)
{
if (this == &x) { return *this; } // skip self
if (allocator_traits::propagate_on_container_copy_assignment::value)
{
if (get_allocator_ref() != x.get_allocator_ref())
{
// new allocator cannot free existing storage
clear();
deallocate_buffer();
_storage._capacity = sbuffer_size;
_buffer = _storage.small_buffer_address();
}
get_allocator_ref() = x.get_allocator_ref();
}
size_type n = x.size();
if (capacity() >= n)
{
const_iterator first = x.begin();
const_iterator last = x.end();
overwrite_buffer(first, last);
}
else
{
allocate_and_copy_range(x.begin(), x.end());
}
BOOST_ASSERT(invariants_ok());
return *this;
}
/**
* [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
*
* **Effects**: Moves elements of `x` to `*this`. Previously
* held elements get move/copy assigned to or destroyed.
*
* **Requires**: `T` shall be [MoveInsertable] into `*this`.
*
* **Postcondition**: `x` is left in an unspecified but valid state.
*
* **Returns**: `*this`.
*
* **Exceptions**: Basic exception guarantee if not `noexcept`.
*
* Remark: If `x` is _small_ or the allocator forbids propagation,
* the contents of `x` is moved one-by-one instead of stealing the buffer.
*
* **Complexity**: Linear in the size of the small buffer plus the size of `*this`
* and the size of `x` if the allocator forbids propagation.
*/
devector& operator=(devector&& x) noexcept(
(no_small_buffer || t_is_nothrow_constructible)
&& (allocator_traits::propagate_on_move_assignment || allocator_traits::is_always_equal)
)
{
constexpr bool copy_alloc = allocator_traits::propagate_on_move_assignment;
const bool equal_alloc = (get_allocator_ref() == x.get_allocator_ref());
if ((copy_alloc || equal_alloc) && x.is_small() == false)
{
clear();
deallocate_buffer();
if (copy_alloc)
{
get_allocator_ref() = std::move(x.get_allocator_ref());
}
_storage._capacity = x._storage._capacity;
_buffer = x._buffer;
_front_index = x._front_index;
_back_index = x._back_index;
// leave x in valid state
x._storage._capacity = sbuffer_size;
x._buffer = _storage.small_buffer_address();
x._back_index = x._front_index = 0;
}
else
{
// if the allocator shouldn't be copied and they do not compare equal
// or the rvalue has a small buffer, we can't steal memory.
auto xbegin = std::make_move_iterator(x.begin());
auto xend = std::make_move_iterator(x.end());
if (copy_alloc)
{
get_allocator_ref() = std::move(x.get_allocator_ref());
}
if (capacity() >= x.size())
{
overwrite_buffer(xbegin, xend);
}
else
{
allocate_and_copy_range(xbegin, xend);
}
}
BOOST_ASSERT(invariants_ok());
return *this;
}
/**
* **Effects**: Copies elements of `il` to `*this`. Previously
* held elements get copy assigned to or destroyed.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this` and [CopyAssignable].
*
* **Postcondition**: `this->size() == il.size()`, the elements of
* `*this` are copies of elements in `il` in the same order.
*
* **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
* from `T` and `NothrowConstructible`, Basic exception guarantee otherwise.
*
* **Returns**: `*this`.
*
* **Complexity**: Linear in the size of `il` and `*this`.
*
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
* [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
*/
devector& operator=(std::initializer_list<T> il)
{
assign(il.begin(), il.end());
return *this;
}
/**
* **Effects**: Replaces elements of `*this` with a copy of `[first,last)`.
* Previously held elements get copy assigned to or destroyed.
*
* **Requires**: `T` shall be [EmplaceConstructible] from `*first`. If the specified iterator
* does not meet the forward iterator requirements, `T` shall be also [MoveInsertable] into `*this`.
*
* **Precondition**: `first` and `last` are not iterators into `*this`.
*
* **Postcondition**: `size() == N`, where `N` is the distance between `first` and `last`.
*
* **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
* from `*first` and `NothrowConstructible`, Basic exception guarantee otherwise.
*
* **Complexity**: Linear in the distance between `first` and `last`.
* Makes a single reallocation at most if the iterators `first` and `last`
* are of forward, bidirectional, or random access categories. It makes
* `O(log(N))` reallocations if they are just input iterators.
*
* **Remarks**: Each iterator in the range `[first,last)` shall be dereferenced exactly once,
* unless an exception is thrown.
*
* [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
* [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
*/
template <BOOST_DOUBLE_ENDED_REQUIRE_INPUT_ITERATOR(InputIterator)>
void assign(InputIterator first, InputIterator last)
{
overwrite_buffer_impl(first, last);
while (first != last)
{
emplace_back(*first++);
}
}
#ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
template <BOOST_DOUBLE_ENDED_REQUIRE_FW_ITERATOR(ForwardIterator)>
void assign(ForwardIterator first, ForwardIterator last)
{
const size_type n = std::distance(first, last);
if (capacity() >= n)
{
overwrite_buffer(first, last);
}
else
{
allocate_and_copy_range(first, last);
}
BOOST_ASSERT(invariants_ok());
}
#endif // ifndef BOOST_DOUBLE_ENDED_DOXYGEN_INVOKED
/**
* **Effects**: Replaces elements of `*this` with `n` copies of `u`.
* Previously held elements get copy assigned to or destroyed.
*
* **Requires**: `T` shall be [CopyInsertable] into `*this` and
* [CopyAssignable].
*
* **Precondition**: `u` is not a reference into `*this`.
*
* **Postcondition**: `size() == n` and the elements of
* `*this` are copies of `u`.
*
* **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
* from `u` and `NothrowConstructible`, Basic exception guarantee otherwise.
*
* **Complexity**: Linear in `n` and the size of `*this`.
*
* [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
* [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
*/
void assign(size_type n, const T& u)
{
cvalue_iterator first(u, n);
cvalue_iterator last;
assign(first, last);
}
/** **Equivalent to**: `assign(il.begin(), il.end())`. */
void assign(std::initializer_list<T> il)
{
assign(il.begin(), il.end());
}
/**
* **Returns**: A copy of the allocator associated with the container.
*
* **Complexity**: Constant.
*/
allocator_type get_allocator() const noexcept
{
return static_cast<const allocator_type&>(*this);
}
// iterators
/**
* **Returns**: A iterator pointing to the first element in the devector,
* or the past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
iterator begin() noexcept
{
return _buffer + _front_index;
}
/**
* **Returns**: A constant iterator pointing to the first element in the devector,
* or the past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
const_iterator begin() const noexcept
{
return _buffer + _front_index;
}
/**
* **Returns**: An iterator pointing past the last element of the container.
*
* **Complexity**: Constant.
*/
iterator end() noexcept
{
return _buffer + _back_index;
}
/**
* **Returns**: A constant iterator pointing past the last element of the container.
*
* **Complexity**: Constant.
*/
const_iterator end() const noexcept
{
return _buffer + _back_index;
}
/**
* **Returns**: A reverse iterator pointing to the first element in the reversed devector,
* or the reverse past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
reverse_iterator rbegin() noexcept
{
return reverse_iterator(_buffer + _back_index);
}
/**
* **Returns**: A constant reverse iterator
* pointing to the first element in the reversed devector,
* or the reverse past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(_buffer + _back_index);
}
/**
* **Returns**: A reverse iterator pointing past the last element in the
* reversed container, or to the beginning of the reversed container if it's empty.
*
* **Complexity**: Constant.
*/
reverse_iterator rend() noexcept
{
return reverse_iterator(_buffer + _front_index);
}
/**
* **Returns**: A constant reverse iterator pointing past the last element in the
* reversed container, or to the beginning of the reversed container if it's empty.
*
* **Complexity**: Constant.
*/
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(_buffer + _front_index);
}
/**
* **Returns**: A constant iterator pointing to the first element in the devector,
* or the past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
const_iterator cbegin() const noexcept
{
return _buffer + _front_index;
}
/**
* **Returns**: A constant iterator pointing past the last element of the container.
*
* **Complexity**: Constant.
*/
const_iterator cend() const noexcept
{
return _buffer + _back_index;
}
/**
* **Returns**: A constant reverse iterator
* pointing to the first element in the reversed devector,
* or the reverse past the end iterator if the devector is empty.
*
* **Complexity**: Constant.
*/
const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(_buffer + _back_index);
}
/**
* **Returns**: A constant reverse iterator pointing past the last element in the
* reversed container, or to the beginning of the reversed container if it's empty.
*
* **Complexity**: Constant.
*/
const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(_buffer + _front_index);
}
// capacity
/**
* **Returns**: True, if `size() == 0`, false otherwise.
*
* **Complexity**: Constant.
*/
bool empty() const noexcept
{
return _front_index == _back_index;
}
/**
* **Returns**: The number of elements the devector contains.
*
* **Complexity**: Constant.
*/
size_type size() const noexcept
{
return _back_index - _front_index;
}
/**
* **Returns**: The maximum number of elements the devector could possibly hold.
*
* **Complexity**: Constant.
*/
size_type max_size() const noexcept
{
auto alloc_max = allocator_traits::max_size(get_allocator_ref());
auto size_type_max = (std::numeric_limits<size_type>::max)();
return (alloc_max <= size_type_max) ? size_type(alloc_max) : size_type_max;
}
/**
* **Returns**: The total number of elements that the devector can hold without requiring reallocation.
*
* **Complexity**: Constant.
*/
size_type capacity() const noexcept
{
return _storage._capacity;
}
/**
* **Returns**: The total number of elements that can be pushed to the front of the
* devector without requiring reallocation.
*
* **Complexity**: Constant.
*/
size_type front_free_capacity() const noexcept
{
return _front_index;
}
/**
* **Returns**: The total number of elements that can be pushed to the back of the
* devector without requiring reallocation.
*
* **Complexity**: Constant.
*/
size_type back_free_capacity() const noexcept
{
return _storage._capacity - _back_index;
}
/** **Equivalent to**: `resize_back(sz)` */
void resize(size_type sz) { resize_back(sz); }
/** **Equivalent to**: `resize_back(sz, c)` */
void resize(size_type sz, const T& c) { resize_back(sz, c); }