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 <cstdint>
11 : : #include <cstdlib>
12 : : #include <cstring>
13 : : #include <iterator>
14 : : #include <new>
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 : 72504410 : iterator(T* ptr_) : ptr(ptr_) {}
61 : 24463908 : T& operator*() const { return *ptr; }
62 : 28633 : T* operator->() const { return ptr; }
63 : 2094592 : T& operator[](size_type pos) const { return ptr[pos]; }
64 : 39908110 : iterator& operator++() { ptr++; return *this; }
65 : 2094592 : 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 [ + - + - ]: 21306982 : difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
69 : 80929 : iterator operator+(size_type n) const { return iterator(ptr + n); }
70 : : iterator friend operator+(size_type n, iterator x) { return x + n; }
71 : : iterator& operator+=(size_type n) { ptr += n; return *this; }
72 : 2094592 : iterator operator-(size_type n) const { return iterator(ptr - n); }
73 : : iterator& operator-=(size_type n) { ptr -= n; return *this; }
74 [ + + ]: 40379488 : 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 : 30881727707 : const_iterator(const T* ptr_) : ptr(ptr_) {}
88 : 232538 : const_iterator(iterator x) : ptr(&(*x)) {}
89 [ + + + - ]: 195068445 : const T& operator*() const { return *ptr; }
[ + + + + ]
[ - - + +
+ + ]
90 : 3305608 : const T* operator->() const { return ptr; }
91 : 26596 : const T& operator[](size_type pos) const { return ptr[pos]; }
92 [ + - - + ]: 21829120636 : const_iterator& operator++() { ptr++; return *this; }
93 : 2094592 : const_iterator& operator--() { ptr--; return *this; }
94 [ + + ]: 15342579520 : const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
95 : : const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
96 [ + - ][ - + : 15366899889 : difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
+ - - + +
- + - + +
+ - + - +
- + + ]
[ + + + - ]
[ + - + -
+ + ]
97 : 4343130 : 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 [ - + ]: 6925897 : const_iterator& operator+=(size_type n) { ptr += n; return *this; }
100 [ + - + - ]: 590 : const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
101 : : const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
102 [ + + + + : 21914984301 : bool operator==(const_iterator x) const { return ptr == x.ptr; }
+ + + + ]
[ + + + -
+ - + + ]
[ + + + +
+ + ]
[ + + + + ]
103 [ + + + - : 30679973979 : 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 : 104161934 : T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
123 : 260637585 : const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
124 : 27739204 : T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
125 : 30662786028 : const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
126 : 31555632958 : bool is_direct() const { return _size <= N; }
127 : :
128 : 113173830 : void change_capacity(size_type new_capacity) {
129 [ + + ]: 113173830 : if (new_capacity <= N) {
130 [ + + ]: 110681229 : if (!is_direct()) {
131 : 45225 : T* indirect = indirect_ptr(0);
132 : 45225 : T* src = indirect;
133 : 45225 : T* dst = direct_ptr(0);
134 : 45225 : memcpy(dst, src, size() * sizeof(T));
135 : 45225 : free(indirect);
136 : 45225 : _size -= N + 1;
137 : : }
138 : : } else {
139 [ + + ]: 2492601 : 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 : 80996 : _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
144 [ - + ]: 80996 : assert(_union.indirect_contents.indirect);
145 : 80996 : _union.indirect_contents.capacity = new_capacity;
146 : : } else {
147 : 2411605 : char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
148 [ - + ]: 2411605 : assert(new_indirect);
149 : 2411605 : T* src = direct_ptr(0);
150 : 2411605 : T* dst = reinterpret_cast<T*>(new_indirect);
151 : 2411605 : memcpy(dst, src, size() * sizeof(T));
152 : 2411605 : _union.indirect_contents.indirect = new_indirect;
153 : 2411605 : _union.indirect_contents.capacity = new_capacity;
154 : 2411605 : _size += N + 1;
155 : : }
156 : : }
157 : 113173830 : }
158 : :
159 [ + + ]: 129166592 : T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
160 [ + + + + ]: 30913684517 : const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
[ + + + + ]
161 : :
162 : 1125552 : void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
163 : 1125552 : std::fill_n(dst, count, value);
164 : : }
165 : :
166 : : template <std::input_iterator InputIterator>
167 : 50964575 : void fill(T* dst, InputIterator first, InputIterator last) {
168 [ + + + + : 21970844852 : while (first != last) {
+ + + + -
- ][ + + +
+ + + + +
+ + + + +
+ ][ + + +
+ + + + +
+ + + + +
+ ][ + + +
+ - - # #
# # # # #
# ][ + + +
+ - - - -
- - ][ + +
+ + + + +
+ ][ - - +
+ - - - -
- - - - ]
[ + + + +
+ + + + +
+ + + + +
- - + + -
- - - - -
- - ]
169 : 21920116488 : new(static_cast<void*>(dst)) T(*first);
170 : 21632130760 : ++dst;
171 : 21920116488 : ++first;
172 : : }
173 : : }
174 : :
175 : : public:
176 : 144244 : void assign(size_type n, const T& val) {
177 : 144244 : clear();
178 [ + + ]: 144244 : if (capacity() < n) {
179 : 94743 : change_capacity(n);
180 : : }
181 : 144244 : _size += n;
182 [ + + + + ]: 288488 : fill(item_ptr(0), n, val);
183 : 144244 : }
184 : :
185 : : template <std::input_iterator InputIterator>
186 : 2138992 : void assign(InputIterator first, InputIterator last) {
187 : 2138992 : size_type n = last - first;
188 : 2138992 : clear();
189 [ + + ]: 2138992 : if (capacity() < n) {
190 : 121192 : change_capacity(n);
191 : : }
192 [ + + ]: 2138992 : _size += n;
193 [ + + ]: 2374908 : fill(item_ptr(0), first, last);
194 : 2138992 : }
195 : :
196 [ + + ][ - + : 59826825 : prevector() = default;
- + - + ]
197 : :
198 : : explicit prevector(size_type n) {
199 : : resize(n);
200 : : }
201 : :
202 : 415889 : explicit prevector(size_type n, const T& val) {
203 : 415889 : change_capacity(n);
204 : 415889 : _size += n;
205 [ + - + - ]: 831778 : fill(item_ptr(0), n, val);
206 : 415889 : }
207 : :
208 : : template <std::input_iterator InputIterator>
209 : 1092873 : prevector(InputIterator first, InputIterator last) {
210 : 1092873 : size_type n = last - first;
211 : 1092873 : change_capacity(n);
212 [ + + ]: 1092873 : _size += n;
213 [ + + ]: 1093168 : fill(item_ptr(0), first, last);
214 : 1092873 : }
215 : :
216 : 42347368 : prevector(const prevector<N, T, Size, Diff>& other) {
217 : 42347368 : size_type n = other.size();
218 : 42347368 : change_capacity(n);
219 : 42347368 : _size += n;
220 : 42347368 : fill(item_ptr(0), other.begin(), other.end());
221 : 42347368 : }
222 : :
223 : 6845050 : prevector(prevector<N, T, Size, Diff>&& other) noexcept
224 [ + - ]: 6845050 : : _union(std::move(other._union)), _size(other._size)
225 : : {
226 [ + - + - ]: 6534000 : other._size = 0;
[ + + ]
227 : : }
228 : :
229 : 2082289 : prevector& operator=(const prevector<N, T, Size, Diff>& other) {
230 [ + + ]: 2082289 : if (&other == this) {
231 : : return *this;
232 : : }
233 : 4138614 : assign(other.begin(), other.end());
234 : 2069307 : return *this;
235 : : }
236 : :
237 : 26736936 : prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
238 [ + + ]: 26736936 : if (!is_direct()) {
239 : 24152 : free(_union.indirect_contents.indirect);
240 : : }
241 : 26736936 : _union = std::move(other._union);
242 : 26736936 : _size = other._size;
243 : 26736936 : other._size = 0;
244 : 26736936 : return *this;
245 : : }
246 : :
247 : 31197638382 : size_type size() const {
248 [ + + + + : 30841587843 : return is_direct() ? _size : _size - N - 1;
+ + + + +
+ + + + +
- + - - -
- + + - +
- - ][ + +
+ + + + +
+ + + + +
+ + ][ - -
- + - + -
+ - + + +
+ + - - -
- + + - +
- - - - -
- - - - -
# # # # #
# # # # #
# # ][ - +
+ + + + -
+ + + + +
+ + - + +
+ + + - +
- + - + -
- - - ][ -
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + - - -
- + + - -
- - - - -
- - - ][ -
+ + - + -
- + + + -
+ + + + +
+ + + + -
- - - - -
+ + - - -
- - - - -
- - - - -
- - - ][ +
+ - - - -
- - + + -
+ - + - +
- + - + -
+ - + + -
+ - + - +
+ + + ][ +
+ + + + +
+ + - - +
+ + + - +
- + - - -
+ + + - +
+ + - + -
- - + - +
- - - + -
+ - + - +
- + - + -
+ - + - +
- + + - +
- + + + +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - ][ +
+ + + + +
+ + - + -
+ + + - +
- + - + +
+ + + - +
+ + - + -
- - + - -
- - - - -
- - + - -
- - - - -
+ - + - +
- + + - +
- - + - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - ][ +
+ + + + +
+ + + + +
+ + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + + + +
- + - + +
+ + + +
+ ][ - + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# # # #
# ][ + + +
+ + + +
+ ][ + + +
+ + + +
+ ][ + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + - -
- - - - -
- + + + +
+ + + - +
- + - ][ -
- - - - -
- - - - +
+ + + + +
- - - + -
+ - - - -
- - - - -
- ]
249 : : }
250 : :
251 [ + + + + ]: 30962094 : bool empty() const {
[ + + ][ - -
- - - - -
- - + + +
+ + + + +
+ + + - +
- + + + +
+ - + +
+ ][ + + +
+ + + +
+ ][ + + +
+ - - ][ +
+ + + - +
- + + + -
+ - - - -
- + - + +
+ + + - +
+ + - + +
+ - + + +
- + - + -
+ + + + +
+ + + + +
+ - + + +
+ + - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - + +
- - + + -
+ - + - +
+ + + + -
+ - + ]
252 [ + + + + ]: 30962094 : return size() == 0;
[ + + ][ - -
- - - - -
- + + + +
+ + + + +
+ + + + -
+ + + + +
+ + + +
+ ][ + + +
+ + + +
+ ][ + - +
+ - - ][ +
+ + + + -
- + + + -
+ - - - -
+ + + + +
+ + + + +
+ + + + +
+ - + + +
- + + - -
+ + + + +
+ + + + +
+ - + + +
+ + - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - + +
- - + - -
+ - + + -
+ - + + -
+ + - ]
253 : : }
254 : :
255 [ + + + - : 30152849 : iterator begin() { return iterator(item_ptr(0)); }
+ + + + ]
[ + + ][ + +
+ - + + +
- + + + -
+ + + - ]
[ - - - -
- - - - +
+ + + - -
+ + + + +
+ - - + +
- - + + -
- + + - -
+ + + - -
- + + +
+ ]
[ + + + + ]
[ - + + -
- + + - -
+ + - - +
+ - - + +
- ][ + + +
+ + + + +
+ + + + +
+ + + +
+ ]
256 [ + + + + : 185710222 : const_iterator begin() const { return const_iterator(item_ptr(0)); }
+ + + + +
+ ][ + + +
+ + + - -
+ + + + +
+ + + + +
+ + + + +
+ ][ + + +
+ - - - -
+ + + - +
+ - - - -
+ - + + +
- + + + -
+ + - - +
- + + +
- ][ + + +
+ + + + +
+ + + - +
+ + + + +
+ + + + +
+ ]
[ + + + + ]
[ - - + +
- - + + -
- - - + +
+ - + + +
+ ][ + + +
+ + - + -
+ + + + +
+ + - # #
# # ][ + +
+ + + - +
- - - + +
+ + ][ + +
+ + - - +
- ][ + - +
+ + + + +
+ + + + ]
[ + + ]
257 [ + + + + ]: 44578825 : iterator end() { return iterator(item_ptr(size())); }
258 [ + + + + ]: 61404727006 : const_iterator end() const { return const_iterator(item_ptr(size())); }
259 : :
260 : 18527025 : size_t capacity() const {
261 [ + + ]: 11271877 : if (is_direct()) {
[ - + - + ]
[ - - - +
+ + + + -
+ - - ][ -
- - + - +
- + - + -
+ - + - +
+ - + - ]
[ + + + +
+ + ]
262 : : return N;
263 : : } else {
264 : 5869371 : return _union.indirect_contents.capacity;
265 : : }
266 : : }
267 : :
268 [ + - + - : 4177841 : T& operator[](size_type pos) {
+ - + - +
- + - ][ +
+ + + + +
+ + + + +
+ + + + +
- + - + +
- + - ][ +
+ + + + +
+ + + + ]
[ - + # #
# # # # #
# ]
269 [ + + + + : 10449900 : return *item_ptr(pos);
+ + + + ]
[ + + # #
# # # # #
# # # # #
# # # # ]
[ + + + -
+ + + + +
+ + + + +
+ + + - ]
[ + + + + ]
[ + + + +
+ + + - +
+ + + + +
+ + + + +
- + + + +
+ + + + +
- + + + +
- + - + -
+ + - + -
+ - + - +
- + + + -
+ - ][ - -
+ + + + #
# # # # #
# # ][ + -
- + + - -
+ + - - +
+ - ][ + -
+ - + - +
- + - + -
+ - + - ]
[ - + - +
+ - - + +
- # # # #
# # ]
270 : : }
271 : :
272 [ + - ]: 8586109 : const T& operator[](size_type pos) const {
273 [ + + + + : 19920009 : return *item_ptr(pos);
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ - + + +
- + + + -
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + ][ + +
+ + + - +
- + - + -
# # # # ]
[ - + + +
- - - - -
- - - - -
- - ][ + +
+ + # # #
# # # #
# ][ + - +
- + + + +
+ + + - +
+ + + + -
+ + + - +
+ + - + +
+ - + + +
- - + - -
+ - + + +
- + + + +
+ - + + +
+ + - + +
+ + + - +
+ + - ][ +
+ + + + +
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
274 : : }
275 : :
276 [ + + ]: 72577485 : void resize(size_type new_size) {
277 : 72577485 : size_type cur_size = size();
278 [ + + ]: 72577485 : if (cur_size == new_size) {
279 : : return;
280 : : }
281 [ + + ]: 9662286 : if (cur_size > new_size) {
282 [ + + ]: 18226506 : erase(item_ptr(new_size), end());
283 : 9113253 : return;
284 : : }
285 [ + + + + ]: 553285 : if (new_size > capacity()) {
286 : 87490 : change_capacity(new_size);
287 : : }
288 : 549033 : ptrdiff_t increase = new_size - cur_size;
289 [ + + + - ]: 1098066 : fill(item_ptr(cur_size), increase);
290 : 549033 : _size += increase;
291 : : }
292 : :
293 : 4065 : void reserve(size_type new_capacity) {
294 [ + + + + ]: 6636 : if (new_capacity > capacity()) {
295 : 1828 : change_capacity(new_capacity);
296 : : }
297 : 4065 : }
298 : :
299 [ + + ]: 68377806 : void shrink_to_fit() {
300 : 68377806 : change_capacity(size());
301 : 68377806 : }
302 : :
303 : 71969495 : void clear() {
304 : 71969495 : resize(0);
305 : : }
306 : :
307 [ + + ]: 9041645 : iterator insert(iterator pos, const T& value) {
308 [ + + ]: 9041645 : size_type p = pos - begin();
309 [ + + ]: 9041645 : size_type new_size = size() + 1;
310 [ + + ]: 9041645 : if (capacity() < new_size) {
311 : 8103 : change_capacity(new_size + (new_size >> 1));
312 : : }
313 [ + + ]: 9041645 : T* ptr = item_ptr(p);
314 [ + + ]: 9041645 : T* dst = ptr + 1;
315 : 9041645 : memmove(dst, ptr, (size() - p) * sizeof(T));
316 : 9041645 : _size++;
317 : 9041645 : new(static_cast<void*>(ptr)) T(value);
318 : 9041645 : return iterator(ptr);
319 : : }
320 : :
321 [ + + ]: 16386 : void insert(iterator pos, size_type count, const T& value) {
322 [ + + ]: 16386 : size_type p = pos - begin();
323 [ + + ]: 16386 : size_type new_size = size() + count;
324 [ + + ]: 16386 : if (capacity() < new_size) {
325 : 797 : change_capacity(new_size + (new_size >> 1));
326 : : }
327 [ + + ]: 16386 : T* ptr = item_ptr(p);
328 [ + + ]: 16386 : T* dst = ptr + count;
329 [ + + ]: 16386 : memmove(dst, ptr, (size() - p) * sizeof(T));
330 : 16386 : _size += count;
331 [ + + + - ]: 32772 : fill(item_ptr(p), count, value);
332 : 16386 : }
333 : :
334 : : template <std::input_iterator InputIterator>
335 [ + + ]: 5444320 : void insert(iterator pos, InputIterator first, InputIterator last) {
336 [ + + ]: 5444320 : size_type p = pos - begin();
337 [ + + ]: 5444320 : difference_type count = last - first;
338 [ + + ]: 5444320 : size_type new_size = size() + count;
339 [ + + ]: 5444320 : if (capacity() < new_size) {
340 : 349214 : change_capacity(new_size + (new_size >> 1));
341 : : }
342 [ + + ]: 5444320 : T* ptr = item_ptr(p);
343 [ + + ]: 5444320 : T* dst = ptr + count;
344 : 5444320 : memmove(dst, ptr, (size() - p) * sizeof(T));
345 : 5444320 : _size += count;
346 : 5444320 : fill(ptr, first, last);
347 : 5444320 : }
348 : :
349 [ + + ]: 1102030 : 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 [ + + ]: 1102030 : if (capacity() < new_size) {
353 [ + - ]: 263213 : change_capacity(new_size);
354 : 263213 : _size += new_size - size();
355 : 263213 : return;
356 : : }
357 [ + + ]: 838817 : if (new_size < size()) {
358 [ + + ]: 6808 : erase(item_ptr(new_size), end());
359 : : } else {
360 : 835413 : _size += new_size - size();
361 : : }
362 : : }
363 : :
364 : 27698 : iterator erase(iterator pos) {
365 : 27698 : return erase(pos, pos + 1);
366 : : }
367 : :
368 : 9171672 : 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 : 9171672 : iterator p = first;
376 : 9171672 : char* endp = (char*)&(*end());
377 : 9171672 : _size -= last - p;
378 : 9171672 : memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
379 : 9171672 : return first;
380 : : }
381 : :
382 : : template<typename... Args>
383 [ + + ]: 82236 : void emplace_back(Args&&... args) {
384 [ + + ]: 82236 : size_type new_size = size() + 1;
385 [ + + ]: 82236 : if (capacity() < new_size) {
386 : 13314 : change_capacity(new_size + (new_size >> 1));
387 : : }
388 [ + + ]: 82236 : new(item_ptr(size())) T(std::forward<Args>(args)...);
389 : 82236 : _size++;
390 : 82236 : }
391 : :
392 : 82236 : void push_back(const T& value) {
393 : 82236 : emplace_back(value);
394 : 73649 : }
395 : :
396 : 6829 : void pop_back() {
397 : 6829 : erase(end() - 1, end());
398 : 6829 : }
399 : :
400 : : T& front() {
401 : : return *item_ptr(0);
402 : : }
403 : :
404 : : const T& front() const {
405 : : return *item_ptr(0);
406 : : }
407 : :
408 : : T& back() {
409 : : return *item_ptr(size() - 1);
410 : : }
411 : :
412 [ + + + + : 27551 : const T& back() const {
+ - ]
413 [ + + + + : 27712 : return *item_ptr(size() - 1);
- + + - ]
414 : : }
415 : :
416 : 16196 : void swap(prevector<N, T, Size, Diff>& other) noexcept
417 : : {
418 : 16196 : std::swap(_union, other._union);
419 : 16196 : std::swap(_size, other._size);
420 : : }
421 : :
422 : 136480863 : ~prevector() {
423 [ + + ]: 136480863 : if (!is_direct()) {
424 : 2342228 : free(_union.indirect_contents.indirect);
425 : 2342228 : _union.indirect_contents.indirect = nullptr;
426 : : }
427 : 136480863 : }
428 : :
429 : 6523249 : constexpr bool operator==(const prevector& other) const {
430 : 6523249 : return std::ranges::equal(*this, other);
431 : : }
432 : :
433 [ + + ]: 21772855 : bool operator<(const prevector<N, T, Size, Diff>& other) const {
434 [ + + + + ]: 21774385 : if (size() < other.size()) {
435 : : return true;
436 : : }
437 [ + + ]: 20998066 : if (size() > other.size()) {
438 : : return false;
439 : : }
440 [ + + ]: 20717567 : const_iterator b1 = begin();
441 : 20717567 : const_iterator b2 = other.begin();
442 : 20717567 : const_iterator e1 = end();
443 [ + + ]: 133682747 : while (b1 != e1) {
444 [ + + ]: 130966165 : if ((*b1) < (*b2)) {
445 : : return true;
446 : : }
447 [ + + ]: 120730518 : if ((*b2) < (*b1)) {
448 : : return false;
449 : : }
450 : 112965180 : ++b1;
451 : 112965180 : ++b2;
452 : : }
453 : : return false;
454 : : }
455 : :
456 : 35159224 : size_t allocated_memory() const {
457 [ + + + + ]: 35159224 : if (is_direct()) {
[ - + ][ + +
+ + - + +
+ + + + +
+ + + + +
+ + + + +
+ + - - ]
[ - + - +
- + - + -
+ - + - +
+ - + - ]
[ - + + +
- + ]
458 : : return 0;
459 : : } else {
460 [ + - + - ]: 914875 : return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
[ # # ][ + -
- + - - +
- - + - +
+ - - + +
- - + + -
+ - - - ]
[ - - + -
- - ]
461 : : }
462 : : }
463 : :
464 [ + + ][ + + : 579426 : value_type* data() {
+ + # # #
# # # ][ -
+ + + - +
- + + + ]
[ + - - -
+ - - - -
- - - - -
- - + - -
- - - - -
+ + - - -
- - - ][ +
+ - - + +
+ - ][ + +
+ + + + ]
[ - + + +
- + + + -
+ - + ][ +
- - + - +
- + - + -
+ - + - +
- + - + ]
465 [ + + ][ + + : 579425 : return item_ptr(0);
+ + # # #
# # # ][ -
+ + + - +
- + + + ]
[ - + - +
- - - - -
+ - - + +
- - ][ + +
+ + + - ]
[ - + + +
- + + + +
+ + + ][ -
+ + - - +
+ - - + +
- - + + -
- + + - ]
466 : : }
467 : :
468 [ + + + - : 31434755 : const value_type* data() const {
+ + + + ]
[ + + + +
+ + + + ]
[ + + + + ]
[ - - + +
- - - - -
- ][ + + +
+ + + + -
+ - - - ]
[ + - + -
+ - + - +
- + - + -
+ - - + -
+ + + + +
+ + + - +
- - - - -
- - ]
469 [ + + - + : 31438137 : return item_ptr(0);
+ + + + ]
[ + + + +
+ + + + ]
[ + + + + ]
[ + + + +
+ + ][ - +
- + - + -
+ - + + -
+ - + + +
+ + + + +
+ + ]
470 : : }
471 : : };
472 : :
473 : : #endif // BITCOIN_PREVECTOR_H
|