Branch data Line data Source code
1 : : // Copyright (c) 2015-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_PREVECTOR_H
6 : : #define BITCOIN_PREVECTOR_H
7 : :
8 : : #include <algorithm>
9 : : #include <cassert>
10 : : #include <cstddef>
11 : : #include <cstdint>
12 : : #include <cstdlib>
13 : : #include <cstring>
14 : : #include <iterator>
15 : : #include <type_traits>
16 : : #include <utility>
17 : :
18 : : /** Implements a drop-in replacement for std::vector<T> which stores up to N
19 : : * elements directly (without heap allocation). The types Size and Diff are
20 : : * used to store element counts, and can be any unsigned + signed type.
21 : : *
22 : : * Storage layout is either:
23 : : * - Direct allocation:
24 : : * - Size _size: the number of used elements (between 0 and N)
25 : : * - T direct[N]: an array of N elements of type T
26 : : * (only the first _size are initialized).
27 : : * - Indirect allocation:
28 : : * - Size _size: the number of used elements plus N + 1
29 : : * - Size capacity: the number of allocated elements
30 : : * - T* indirect: a pointer to an array of capacity elements of type T
31 : : * (only the first _size are initialized).
32 : : *
33 : : * The data type T must be movable by memmove/realloc(). Once we switch to C++,
34 : : * move constructors can be used instead.
35 : : */
36 : : template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
37 : : class prevector {
38 : : static_assert(std::is_trivially_copyable_v<T>);
39 : :
40 : : public:
41 : : static constexpr unsigned int STATIC_SIZE{N};
42 : :
43 : : typedef Size size_type;
44 : : typedef Diff difference_type;
45 : : typedef T value_type;
46 : : typedef value_type& reference;
47 : : typedef const value_type& const_reference;
48 : : typedef value_type* pointer;
49 : : typedef const value_type* const_pointer;
50 : :
51 : : class iterator {
52 : : T* ptr{};
53 : : public:
54 : : typedef Diff difference_type;
55 : : typedef T* pointer;
56 : : typedef T& reference;
57 : : using element_type = T;
58 : : using iterator_category = std::contiguous_iterator_tag;
59 : : iterator() = default;
60 : 211280407 : iterator(T* ptr_) : ptr(ptr_) {}
61 : 83995097 : T& operator*() const { return *ptr; }
62 : 97095 : T* operator->() const { return ptr; }
63 : : T& operator[](size_type pos) const { return ptr[pos]; }
64 : 387048648 : iterator& operator++() { ptr++; return *this; }
65 : : iterator& operator--() { ptr--; return *this; }
66 : : iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
67 : : iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
68 [ + + + - ]: 61712294 : difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
69 : 27176 : iterator operator+(size_type n) const { return iterator(ptr + n); }
70 : : iterator friend operator+(size_type n, iterator x) { return x + n; }
71 : 256 : iterator& operator+=(size_type n) { ptr += n; return *this; }
72 : 3430 : iterator operator-(size_type n) const { return iterator(ptr - n); }
73 : : iterator& operator-=(size_type n) { ptr -= n; return *this; }
74 [ + + ]: 387869309 : bool operator==(iterator x) const { return ptr == x.ptr; }
75 : : auto operator<=>(iterator x) const { return ptr <=> x.ptr; }
76 : : };
77 : :
78 : : class const_iterator {
79 : : const T* ptr{};
80 : : public:
81 : : typedef Diff difference_type;
82 : : typedef const T* pointer;
83 : : typedef const T& reference;
84 : : using element_type = const T;
85 : : using iterator_category = std::contiguous_iterator_tag;
86 : : const_iterator() = default;
87 : 2321035728 : const_iterator(const T* ptr_) : ptr(ptr_) {}
88 : 206509 : const_iterator(iterator x) : ptr(&(*x)) {}
89 [ + + ]: 2368408934 : const T& operator*() const { return *ptr; }
[ + + - - ]
[ - - + +
+ + ]
90 : 111812794 : const T* operator->() const { return ptr; }
91 [ - + ]: 501873 : const T& operator[](size_type pos) const { return ptr[pos]; }
92 [ + - - + ]: 4963635490 : const_iterator& operator++() { ptr++; return *this; }
93 [ - + - + ]: 957354 : const_iterator& operator--() { ptr--; return *this; }
94 [ + + ]: 356690464 : const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
95 : : const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
96 [ + - ][ + - : 762822919 : difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
+ - + + ]
[ # # # # ]
[ - + - -
- + - - +
- + + + +
+ + + - +
+ ]
97 : 82799992 : const_iterator operator+(size_type n) const { return const_iterator(ptr + n); }
98 : : const_iterator friend operator+(size_type n, const_iterator x) { return x + n; }
99 [ - + ]: 104743372 : const_iterator& operator+=(size_type n) { ptr += n; return *this; }
100 [ - + ]: 681103 : const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
[ + - + - ]
101 : : const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
102 [ + + # # : 5167411315 : bool operator==(const_iterator x) const { return ptr == x.ptr; }
# # # # ]
[ + + + +
+ + + + ]
[ + + + +
+ + ]
[ + + + + ]
103 [ + + + - : 497186919 : auto operator<=>(const_iterator x) const { return ptr <=> x.ptr; }
+ + + - +
+ + - + +
+ - + + +
- ][ + + +
- + + + -
+ + + - ]
[ + + + -
+ + + - ]
[ + + + -
# # # # ]
104 : : };
105 : :
106 : : private:
107 : : #pragma pack(push, 1)
108 : : union direct_or_indirect {
109 : : char direct[sizeof(T) * N];
110 : : struct {
111 : : char* indirect;
112 : : size_type capacity;
113 : : } indirect_contents;
114 : : };
115 : : #pragma pack(pop)
116 : : alignas(char*) direct_or_indirect _union = {};
117 : : size_type _size = 0;
118 : :
119 : : static_assert(alignof(char*) % alignof(size_type) == 0 && sizeof(char*) % alignof(size_type) == 0, "size_type cannot have more restrictive alignment requirement than pointer");
120 : : static_assert(alignof(char*) % alignof(T) == 0, "value_type T cannot have more restrictive alignment requirement than pointer");
121 : :
122 : 470502070 : T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
123 : 2638680607 : const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
124 : 265894749 : T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
125 : 752758482 : const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
126 : 13833450397 : bool is_direct() const { return _size <= N; }
127 : :
128 : 366519662 : void change_capacity(size_type new_capacity) {
129 [ + + ]: 366519662 : if (new_capacity <= N) {
130 [ + + ]: 291707327 : if (!is_direct()) {
131 : 14588 : T* indirect = indirect_ptr(0);
132 : 14588 : T* src = indirect;
133 : 14588 : T* dst = direct_ptr(0);
134 : 14588 : memcpy(dst, src, size() * sizeof(T));
135 : 14588 : free(indirect);
136 : 14588 : _size -= N + 1;
137 : : }
138 : : } else {
139 [ + + ]: 74812335 : if (!is_direct()) {
140 : : /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
141 : : success. These should instead use an allocator or new/delete so that handlers
142 : : are called as necessary, but performance would be slightly degraded by doing so. */
143 : 493100 : _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
144 [ - + ]: 493100 : assert(_union.indirect_contents.indirect);
145 : 493100 : _union.indirect_contents.capacity = new_capacity;
146 : : } else {
147 : 74319235 : char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
148 [ - + ]: 74319235 : assert(new_indirect);
149 : 74319235 : T* src = direct_ptr(0);
150 : 74319235 : T* dst = reinterpret_cast<T*>(new_indirect);
151 : 74319235 : memcpy(dst, src, size() * sizeof(T));
152 : 74319235 : _union.indirect_contents.indirect = new_indirect;
153 : 74319235 : _union.indirect_contents.capacity = new_capacity;
154 : 74319235 : _size += N + 1;
155 : : }
156 : : }
157 : 366519662 : }
158 : :
159 [ + + ]: 661833171 : T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
160 [ + + ]: 3351072110 : const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
[ - - + + ]
161 : :
162 : 60644551 : void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
163 : 60644551 : std::fill_n(dst, count, value);
164 : : }
165 : :
166 : : template <std::input_iterator InputIterator>
167 : 353014046 : void fill(T* dst, InputIterator first, InputIterator last) {
168 [ + + + + : 13440178621 : while (first != last) {
+ + + + ]
[ + + # #
# # # # #
# # # ][ #
# # # #
# ][ - - +
+ - - - -
- - - - ]
[ - - + +
- - + + +
+ ]
[ + + + + ]
[ - - - -
+ + + + +
+ + + +
+ ]
169 : 13100999717 : new(static_cast<void*>(dst)) T(*first);
170 : 12445822666 : ++dst;
171 : 13100999717 : ++first;
172 : : }
173 : : }
174 : :
175 : : public:
176 : 454111 : void assign(size_type n, const T& val) {
177 : 454111 : clear();
178 [ + + ]: 454111 : if (capacity() < n) {
179 : 2462 : change_capacity(n);
180 : : }
181 : 454111 : _size += n;
182 [ + + + + ]: 908222 : fill(item_ptr(0), n, val);
183 : 454111 : }
184 : :
185 : : template <std::input_iterator InputIterator>
186 : 44459515 : void assign(InputIterator first, InputIterator last) {
187 : 44459515 : size_type n = last - first;
188 : 44459515 : clear();
189 [ + + ]: 44459515 : if (capacity() < n) {
190 : 2074031 : change_capacity(n);
191 : : }
192 [ + + ]: 44459515 : _size += n;
193 [ + + ]: 58294657 : fill(item_ptr(0), first, last);
194 : 44459515 : }
195 : :
196 [ + - ]: 61756223 : prevector() = default;
197 : :
198 : : explicit prevector(size_type n) {
199 : : resize(n);
200 : : }
201 : :
202 : 49337860 : explicit prevector(size_type n, const T& val) {
203 : 49337860 : change_capacity(n);
204 : 49337860 : _size += n;
205 [ + - + - ]: 98675720 : fill(item_ptr(0), n, val);
206 : 49337860 : }
207 : :
208 : : template <std::input_iterator InputIterator>
209 : 2846129 : prevector(InputIterator first, InputIterator last) {
210 : 2846129 : size_type n = last - first;
211 : 2846129 : change_capacity(n);
212 [ + + ]: 2846129 : _size += n;
213 : 2846129 : fill(item_ptr(0), first, last);
214 : 2846129 : }
215 : :
216 : 266237286 : prevector(const prevector<N, T, Size, Diff>& other) {
217 : 266237286 : size_type n = other.size();
218 : 266237286 : change_capacity(n);
219 : 266237286 : _size += n;
220 : 266237286 : fill(item_ptr(0), other.begin(), other.end());
221 : 266237286 : }
222 : :
223 : 58498106 : prevector(prevector<N, T, Size, Diff>&& other) noexcept
224 : 58498106 : : _union(std::move(other._union)), _size(other._size)
225 : : {
226 [ + - # # ]: 57537168 : other._size = 0;
[ - - + - ]
[ - - + -
+ - ]
227 : : }
228 : :
229 : 42645760 : prevector& operator=(const prevector<N, T, Size, Diff>& other) {
230 [ + - ]: 42645760 : if (&other == this) {
231 : : return *this;
232 : : }
233 : 85291520 : assign(other.begin(), other.end());
234 : 42645760 : return *this;
235 : : }
236 : :
237 : 34699922 : prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
238 [ + + ]: 34699922 : if (!is_direct()) {
239 : 92711 : free(_union.indirect_contents.indirect);
240 : : }
241 : 34699922 : _union = std::move(other._union);
242 : 34699922 : _size = other._size;
243 : 34699922 : other._size = 0;
244 : 34699922 : return *this;
245 : : }
246 : :
247 : 11881206523 : size_type size() const {
248 [ + + + + : 6986081206 : return is_direct() ? _size : _size - N - 1;
+ + ][ + +
+ + # # #
# # # # #
# # # # ]
[ - - - -
+ + - - -
- - - + +
- - - - -
- - - + +
- - - - -
- - - - -
- - - - -
- - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - +
+ - - - +
- + + + +
+ + + + +
+ + + + +
+ - - - -
- - # # ]
[ - - - -
- - - - -
- + + + +
+ + - - -
- - - # #
# # # # #
# ][ - + -
- + + + +
+ + - - +
+ + + + +
- + + + +
+ + + +
+ ][ - + +
+ - - - -
+ + + + +
+ # # # #
# # # # ]
[ - - - -
- - - + +
+ - - - -
- - - - -
+ - + - -
- - - - -
- - - ][ +
+ + + - -
+ + - - +
+ + + + +
- + - - -
+ - + - +
+ + - + -
- - + - +
- - - + -
+ - + + +
+ + + + -
+ - + - +
- + + - +
- - + - +
- + - + -
+ + - - +
+ - - + -
+ - + - -
- - - - -
- - - ][ +
+ + + + +
+ + + + +
+ + + +
+ ][ + + ]
[ + + - +
+ + + + ]
[ + + + +
+ + + + +
+ ][ - - -
+ - - - -
+ + + + +
+ - - + +
+ + ][ - -
+ + + + +
+ + + + +
+ + + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - + + +
+ + + + +
+ # # # #
# # ][ + +
- - + + +
+ + + + +
+ + + + -
+ + + + +
- - - - +
+ + + + +
- - - - +
+ + + - +
+ + - - -
- - - + +
+ + + + -
+ - + ]
249 : : }
250 : :
251 [ + + ][ - - : 4367422208 : bool empty() const {
- - - - +
+ + + + +
+ + + + +
+ + + -
- ][ + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - - + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ + + -
+ + + -
+ ][ # # #
# # # ][ -
- - - - -
- - - - -
- - - - -
- - - - +
+ + + + +
+ + + + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
+ + + + +
+ + + + +
+ + + + +
+ - + + +
- + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + -
+ + + + +
+ + + + +
+ ]
252 [ + + ][ - - : 4367423223 : return size() == 0;
- - - - +
+ + + + +
+ + + + +
+ + + -
- ][ + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - - + -
+ + + - +
+ + + + +
+ + + + +
+ + + +
+ ][ + + -
+ + + -
+ ][ # # #
# # # ][ -
- - - - -
- - - - -
- - - - -
- - - - +
+ + + + +
+ + + + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
+ + + + +
+ + + + +
+ + + + +
+ - + + +
- + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
253 : : }
254 : :
255 [ - - - - : 71067209 : iterator begin() { return iterator(item_ptr(0)); }
- - - - -
- - - + +
+ + - - +
+ + + + +
- - + + -
- + + - -
+ + - - +
+ + + - -
+ + + + ]
[ + + + +
+ + + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + ][ - +
+ - + + +
+ ][ + + +
- + + + -
+ + + - +
+ + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
256 [ + + + + : 2586082590 : const_iterator begin() const { return const_iterator(item_ptr(0)); }
+ + ][ + +
+ + # # #
# # # #
# ][ + + +
+ - - - -
+ + + + +
+ - - - -
+ + + + +
+ + + + +
+ + - - +
- + + +
- ][ - - -
- + + + -
+ + + + ]
[ + + + +
+ + + + #
# # # # #
# # ][ + +
+ + + + +
+ + + ][ +
+ - - - -
- - - - -
- - - ][ +
+ # # # #
# # # # #
# # # ][ +
+ + + + +
+ + + + -
+ + + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ + - - -
- - - - -
+ + + + +
+ + + + +
+ + ]
257 [ + + + + ]: 156587533 : iterator end() { return iterator(item_ptr(size())); }
258 [ + + + + ]: 1498918726 : const_iterator end() const { return const_iterator(item_ptr(size())); }
259 : :
260 : 135031210 : size_t capacity() const {
261 [ + + ]: 76522723 : if (is_direct()) {
[ # # # # ]
[ + + + +
+ + ][ - -
- + - + -
+ - + -
- ]
262 : : return N;
263 : : } else {
264 : 60897396 : return _union.indirect_contents.capacity;
265 : : }
266 : : }
267 : :
268 [ + - + - : 9188856 : T& operator[](size_type pos) {
+ - + - +
- + - +
- ][ + - +
- + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - - + -
+ + - +
- ]
269 [ + + ][ + - : 18118086 : return *item_ptr(pos);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + + +
+ # # ][ -
- + + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ - + - +
- + - + -
+ - + - +
- ][ - - -
- + - +
+ ][ + - +
- + - + +
+ - + - +
- + - + -
+ + + - +
- + - + -
+ + + - +
- - + - +
- + + - +
- + - + -
+ - + + +
- + - ]
270 : : }
271 : :
272 [ + - ]: 119156201 : const T& operator[](size_type pos) const {
273 [ + + + + : 869952358 : return *item_ptr(pos);
# # # # ]
[ + + + +
+ + + + +
+ + + ][ +
+ + + + +
+ + # # #
# ][ - + +
+ - - - -
- - - - -
- - - ][ +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ + - +
- + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ][ +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
274 : : }
275 : :
276 [ + + ]: 120303210 : void resize(size_type new_size) {
277 : 120303210 : size_type cur_size = size();
278 [ + + ]: 120303210 : if (cur_size == new_size) {
279 : : return;
280 : : }
281 [ + + ]: 21806623 : if (cur_size > new_size) {
282 [ + + ]: 21925198 : erase(item_ptr(new_size), end());
283 : 10962599 : return;
284 : : }
285 [ + + + + ]: 10845234 : if (new_size > capacity()) {
286 : 7463071 : change_capacity(new_size);
287 : : }
288 : 10844024 : ptrdiff_t increase = new_size - cur_size;
289 [ + + + - ]: 21688048 : fill(item_ptr(cur_size), increase);
290 : 10844024 : _size += increase;
291 : : }
292 : :
293 : 4131 : void reserve(size_type new_capacity) {
294 [ + + + + ]: 6626 : if (new_capacity > capacity()) {
295 : 2002 : change_capacity(new_capacity);
296 : : }
297 : 4131 : }
298 : :
299 [ + + ]: 36013563 : void shrink_to_fit() {
300 : 36013563 : change_capacity(size());
301 : 36013563 : }
302 : :
303 : 94488763 : void clear() {
304 : 94488763 : resize(0);
305 : : }
306 : :
307 [ + + ]: 31309702 : iterator insert(iterator pos, const T& value) {
308 [ + + ]: 31309702 : size_type p = pos - begin();
309 [ + + ]: 31309702 : size_type new_size = size() + 1;
310 [ + + ]: 31309702 : if (capacity() < new_size) {
311 : 53482 : change_capacity(new_size + (new_size >> 1));
312 : : }
313 [ + + ]: 31309702 : T* ptr = item_ptr(p);
314 [ + + ]: 31309702 : T* dst = ptr + 1;
315 : 31309702 : memmove(dst, ptr, (size() - p) * sizeof(T));
316 : 31309702 : _size++;
317 : 31309702 : new(static_cast<void*>(ptr)) T(value);
318 : 31309702 : return iterator(ptr);
319 : : }
320 : :
321 [ + + ]: 8556 : void insert(iterator pos, size_type count, const T& value) {
322 [ + + ]: 8556 : size_type p = pos - begin();
323 [ + + ]: 8556 : size_type new_size = size() + count;
324 [ + + ]: 8556 : if (capacity() < new_size) {
325 : 1197 : change_capacity(new_size + (new_size >> 1));
326 : : }
327 [ + + ]: 8556 : T* ptr = item_ptr(p);
328 [ + + ]: 8556 : T* dst = ptr + count;
329 [ + + ]: 8556 : memmove(dst, ptr, (size() - p) * sizeof(T));
330 : 8556 : _size += count;
331 [ + + + - ]: 17112 : fill(item_ptr(p), count, value);
332 : 8556 : }
333 : :
334 : : template <std::input_iterator InputIterator>
335 [ + + ]: 40863260 : void insert(iterator pos, InputIterator first, InputIterator last) {
336 [ + + ]: 40863260 : size_type p = pos - begin();
337 [ + + ]: 40863260 : difference_type count = last - first;
338 [ + + ]: 40863260 : size_type new_size = size() + count;
339 [ + + ]: 40863260 : if (capacity() < new_size) {
340 : 1033527 : change_capacity(new_size + (new_size >> 1));
341 : : }
342 [ + + ]: 40863260 : T* ptr = item_ptr(p);
343 [ + + ]: 40863260 : T* dst = ptr + count;
344 : 40863260 : memmove(dst, ptr, (size() - p) * sizeof(T));
345 : 40863260 : _size += count;
346 : 40863260 : fill(ptr, first, last);
347 : 40863260 : }
348 : :
349 [ + + ]: 6330680 : inline void resize_uninitialized(size_type new_size) {
350 : : // resize_uninitialized changes the size of the prevector but does not initialize it.
351 : : // If size < new_size, the added elements must be initialized explicitly.
352 [ + + ]: 6330680 : if (capacity() < new_size) {
353 [ + - ]: 1446095 : change_capacity(new_size);
354 : 1446095 : _size += new_size - size();
355 : 1446095 : return;
356 : : }
357 [ + + ]: 4884585 : if (new_size < size()) {
358 [ + + ]: 3202 : erase(item_ptr(new_size), end());
359 : : } else {
360 : 4882984 : _size += new_size - size();
361 : : }
362 : : }
363 : :
364 : 2861 : iterator erase(iterator pos) {
365 : 2861 : return erase(pos, pos + 1);
366 : : }
367 : :
368 : 10975138 : iterator erase(iterator first, iterator last) {
369 : : // Erase is not allowed to the change the object's capacity. That means
370 : : // that when starting with an indirectly allocated prevector with
371 : : // size and capacity > N, the result may be a still indirectly allocated
372 : : // prevector with size <= N and capacity > N. A shrink_to_fit() call is
373 : : // necessary to switch to the (more efficient) directly allocated
374 : : // representation (with capacity N and size <= N).
375 : 10975138 : iterator p = first;
376 : 10975138 : char* endp = (char*)&(*end());
377 : 10975138 : _size -= last - p;
378 : 10975138 : memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
379 : 10975138 : return first;
380 : : }
381 : :
382 : : template<typename... Args>
383 [ + + ]: 753327 : void emplace_back(Args&&... args) {
384 [ + + ]: 753327 : size_type new_size = size() + 1;
385 [ + + ]: 753327 : if (capacity() < new_size) {
386 : 8957 : change_capacity(new_size + (new_size >> 1));
387 : : }
388 [ + + ]: 753327 : new(item_ptr(size())) T(std::forward<Args>(args)...);
389 : 753327 : _size++;
390 : 753327 : }
391 : :
392 : 753327 : void push_back(const T& value) {
393 : 753327 : emplace_back(value);
394 : 510810 : }
395 : :
396 : 3430 : void pop_back() {
397 : 3430 : erase(end() - 1, end());
398 : 3430 : }
399 : :
400 : : T& front() {
401 : : return *item_ptr(0);
402 : : }
403 : :
404 : : const T& front() const {
405 : : return *item_ptr(0);
406 : : }
407 : :
408 [ + + + + : 11784 : T& back() {
+ + + + ]
409 [ + + + + : 11784 : return *item_ptr(size() - 1);
+ + + + ]
410 : : }
411 : :
412 [ + + + + : 2880851 : const T& back() const {
+ + ]
413 [ + + + + : 2904738 : return *item_ptr(size() - 1);
+ + + + ]
414 : : }
415 : :
416 : 4038 : void swap(prevector<N, T, Size, Diff>& other) noexcept
417 : : {
418 : 4038 : std::swap(_union, other._union);
419 : 4038 : std::swap(_size, other._size);
420 : : }
421 : :
422 : 521668146 : ~prevector() {
423 [ + + ]: 521668146 : if (!is_direct()) {
424 : 74211936 : free(_union.indirect_contents.indirect);
425 : 74211936 : _union.indirect_contents.indirect = nullptr;
426 : : }
427 : 521668146 : }
428 : :
429 : 31388113 : constexpr bool operator==(const prevector& other) const {
430 : 31388113 : return std::ranges::equal(*this, other);
431 : : }
432 : :
433 [ + + ]: 6960935 : bool operator<(const prevector<N, T, Size, Diff>& other) const {
434 [ + + + + ]: 7503967 : if (size() < other.size()) {
435 : : return true;
436 : : }
437 [ + + ]: 6922977 : if (size() > other.size()) {
438 : : return false;
439 : : }
440 [ + + ]: 6883288 : const_iterator b1 = begin();
441 : 6883288 : const_iterator b2 = other.begin();
442 : 6883288 : const_iterator e1 = end();
443 [ + + ]: 48058695 : while (b1 != e1) {
444 [ + + ]: 46394265 : if ((*b1) < (*b2)) {
445 : : return true;
446 : : }
447 [ + + ]: 42969557 : if ((*b2) < (*b1)) {
448 : : return false;
449 : : }
450 : 41175407 : ++b1;
451 : 41175407 : ++b2;
452 : : }
453 : : return false;
454 : : }
455 : :
456 : 25989803 : size_t allocated_memory() const {
457 [ + + + + ]: 25989803 : if (is_direct()) {
[ + + ][ - +
+ + - + +
+ + + + +
+ + + + +
+ + + + +
+ + - - ]
458 : : return 0;
459 : : } else {
460 [ + - + - ]: 1118203 : return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
[ + - ][ - -
- + - - +
- - + - +
+ - - + +
- - + + -
+ - - - ]
461 : : }
462 : : }
463 : :
464 [ + + + + ]: 26218833 : value_type* data() {
[ - - - -
+ + ][ + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + + -
+ + + - +
+ + - - +
- - - - +
+ + - + +
+ - ][ + +
+ - - - -
- - - - -
- - - - +
+ + - - -
- - - - -
- - - -
- ][ # # #
# # # #
# ][ - + +
+ - - - -
- - ]
465 [ + + ]: 26218833 : return item_ptr(0);
[ + + + + ]
[ + + + +
+ + - + +
+ - - + +
+ + ][ - +
+ + - - -
- - - ][ #
# # # #
# ]
466 : : }
467 : :
468 [ + + ][ + + : 630224604 : const value_type* data() const {
+ + + + +
+ ][ + + +
+ - - + +
+ + + - ]
[ # # # #
# # # # #
# ]
[ + + + + ]
[ + + + +
+ + + + +
- + - + -
+ - - + -
+ + - + -
+ + + + +
+ - - - -
- - ]
469 [ + + ][ + + : 630226967 : return item_ptr(0);
+ + + - ]
[ + + + + ]
[ - + + +
+ + + + ]
[ + + + +
+ + - + -
+ + - + -
- + - + +
+ + + +
+ ]
470 : : }
471 : : };
472 : :
473 : : #endif // BITCOIN_PREVECTOR_H
|