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 : 20593180 : iterator(T* ptr_) : ptr(ptr_) {}
61 : 6111138 : T& operator*() const { return *ptr; }
62 : 26643 : T* operator->() const { return ptr; }
63 : 2092846 : T& operator[](size_type pos) const { return ptr[pos]; }
64 : 39306668 : iterator& operator++() { ptr++; return *this; }
65 : 2092846 : 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 [ + - + - ]: 4298968 : difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
69 : 80838 : 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 : 2092846 : iterator operator-(size_type n) const { return iterator(ptr - n); }
73 : : iterator& operator-=(size_type n) { ptr -= n; return *this; }
74 [ + + ]: 39606166 : 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 : 52846668 : const_iterator(const T* ptr_) : ptr(ptr_) {}
88 : 162099 : const_iterator(iterator x) : ptr(&(*x)) {}
89 [ + + ]: 43709463 : const T& operator*() const { return *ptr; }
[ - - + + ]
90 : 1009544 : const T* operator->() const { return ptr; }
91 : 26027 : const T& operator[](size_type pos) const { return ptr[pos]; }
92 [ + - - + ]: 142204109 : const_iterator& operator++() { ptr++; return *this; }
93 : 2092846 : const_iterator& operator--() { ptr--; return *this; }
94 [ + + ]: 6185075 : const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
95 : : const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
96 [ - + + - : 12507273 : difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
- + + - +
- + - + -
+ - + - +
+ ]
[ + + + - ]
[ - - - -
+ + ]
97 : 1624804 : 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 [ - + ]: 2662192 : 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 [ + + + + : 157005512 : bool operator==(const_iterator x) const { return ptr == x.ptr; }
+ + ][ + + ]
[ + + + + ]
103 [ + + + - : 11839557 : 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 : 24347784 : T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
123 : 60586227 : const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
124 : 9208463 : T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
125 : 8032156 : const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
126 : 300630399 : bool is_direct() const { return _size <= N; }
127 : :
128 : 43952283 : void change_capacity(size_type new_capacity) {
129 [ + + ]: 43952283 : if (new_capacity <= N) {
130 [ + + ]: 42612777 : if (!is_direct()) {
131 : 44705 : T* indirect = indirect_ptr(0);
132 : 44705 : T* src = indirect;
133 : 44705 : T* dst = direct_ptr(0);
134 : 44705 : memcpy(dst, src, size() * sizeof(T));
135 : 44705 : free(indirect);
136 : 44705 : _size -= N + 1;
137 : : }
138 : : } else {
139 [ + + ]: 1339506 : 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 : 18025 : _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
144 [ - + ]: 18025 : assert(_union.indirect_contents.indirect);
145 : 18025 : _union.indirect_contents.capacity = new_capacity;
146 : : } else {
147 : 1321481 : char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
148 [ - + ]: 1321481 : assert(new_indirect);
149 : 1321481 : T* src = direct_ptr(0);
150 : 1321481 : T* dst = reinterpret_cast<T*>(new_indirect);
151 : 1321481 : memcpy(dst, src, size() * sizeof(T));
152 : 1321481 : _union.indirect_contents.indirect = new_indirect;
153 : 1321481 : _union.indirect_contents.capacity = new_capacity;
154 : 1321481 : _size += N + 1;
155 : : }
156 : : }
157 : 43952283 : }
158 : :
159 [ + + ]: 31983269 : T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
160 [ + + ]: 67448590 : const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
161 : :
162 : 308219 : void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
163 : 308219 : std::fill_n(dst, count, value);
164 : : }
165 : :
166 : : template <std::input_iterator InputIterator>
167 : 8755273 : void fill(T* dst, InputIterator first, InputIterator last) {
168 [ - - - - : 279745005 : while (first != last) {
+ + + + +
+ + + +
+ ][ + + ]
[ + + + +
# # # # #
# # # #
# ][ + + +
+ + + +
+ ][ - - +
+ - - #
# ][ - - +
+ - - - -
- - ][ # #
# # # # #
# # # #
# ][ + + +
+ + + + +
+ + + + +
+ - - + +
- - - - -
- - - ]
169 : 271045274 : new(static_cast<void*>(dst)) T(*first);
170 : 24242237 : ++dst;
171 : 271045274 : ++first;
172 : : }
173 : : }
174 : :
175 : : public:
176 : 144043 : void assign(size_type n, const T& val) {
177 : 144043 : clear();
178 [ + + ]: 144043 : if (capacity() < n) {
179 : 94386 : change_capacity(n);
180 : : }
181 : 144043 : _size += n;
182 [ + + + + ]: 288086 : fill(item_ptr(0), n, val);
183 : 144043 : }
184 : :
185 : : template <std::input_iterator InputIterator>
186 : 356252 : void assign(InputIterator first, InputIterator last) {
187 : 356252 : size_type n = last - first;
188 : 356252 : clear();
189 [ + + ]: 356252 : if (capacity() < n) {
190 : 98018 : change_capacity(n);
191 : : }
192 [ + + ]: 356252 : _size += n;
193 [ + + ]: 411500 : fill(item_ptr(0), first, last);
194 : 356252 : }
195 : :
196 [ + - ]: 37179610 : prevector() = default;
197 : :
198 : : explicit prevector(size_type n) {
199 : : resize(n);
200 : : }
201 : :
202 : 60455 : explicit prevector(size_type n, const T& val) {
203 : 60455 : change_capacity(n);
204 : 60455 : _size += n;
205 [ + - + - ]: 120910 : fill(item_ptr(0), n, val);
206 : 60455 : }
207 : :
208 : : template <std::input_iterator InputIterator>
209 : 849125 : prevector(InputIterator first, InputIterator last) {
210 : 849125 : size_type n = last - first;
211 : 849125 : change_capacity(n);
212 [ + + ]: 849125 : _size += n;
213 [ + + ]: 849419 : fill(item_ptr(0), first, last);
214 : 849125 : }
215 : :
216 : 5394060 : prevector(const prevector<N, T, Size, Diff>& other) {
217 : 5394060 : size_type n = other.size();
218 : 5394060 : change_capacity(n);
219 : 5394060 : _size += n;
220 : 5394060 : fill(item_ptr(0), other.begin(), other.end());
221 : 5394060 : }
222 : :
223 : 776466 : prevector(prevector<N, T, Size, Diff>&& other) noexcept
224 [ + - ]: 776466 : : _union(std::move(other._union)), _size(other._size)
225 : : {
226 [ + - + - ]: 690742 : other._size = 0;
[ + + ]
227 : : }
228 : :
229 : 354706 : prevector& operator=(const prevector<N, T, Size, Diff>& other) {
230 [ + + ]: 354706 : if (&other == this) {
231 : : return *this;
232 : : }
233 : 683606 : assign(other.begin(), other.end());
234 : 341803 : return *this;
235 : : }
236 : :
237 : 1675153 : prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
238 [ + + ]: 1675153 : if (!is_direct()) {
239 : 25296 : free(_union.indirect_contents.indirect);
240 : : }
241 : 1675153 : _union = std::move(other._union);
242 : 1675153 : _size = other._size;
243 : 1675153 : other._size = 0;
244 : 1675153 : return *this;
245 : : }
246 : :
247 : 195283175 : size_type size() const {
248 [ + + + + ]: 54706108 : return is_direct() ? _size : _size - N - 1;
[ - - - -
+ + + + +
+ + + + +
- + - + +
+ + + +
+ ][ - - -
- - + - -
- - - - -
- - - - -
- - - - -
+ - - - -
- - - - +
+ - - - -
- - - - -
- ][ - - -
- - - - -
- - - - -
- - + - +
- - - - -
- - - -
- ][ - - +
- + - - +
- + - + -
+ - + - +
- + # # #
# # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - +
+ + - - -
+ - + + +
+ + + + +
+ + + - +
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - +
- - - - -
- - + - +
+ + - - -
- - - - -
- - - - ]
[ - - - +
- - - + -
+ - + - +
- - - + -
+ - + + +
- + - - -
+ - + - -
- + - + -
+ - + - +
- + - + -
+ - + - +
+ - + - +
+ + + - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- ][ - + -
- + + - +
+ + + + +
+ + + ][ -
+ + + + +
+ + + + +
+ + + + +
+ + ][ - +
+ + - + -
+ + + + +
+ + # # #
# # # # #
# # ][ + +
+ + + + +
+ + + # #
# # ][ + +
+ + + + #
# # # ][ -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - + +
+ + ][ + +
+ + + + +
+ + + +
+ ][ - - +
+ - + - +
- + - + -
+ - + - +
- + + + +
- + + + +
+ + ]
249 : : }
250 : :
251 [ + + ]: 17170591 : bool empty() const {
[ + + + + ]
[ + + - +
+ + - + ]
[ # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - +
- - - - -
- - - - -
- - - + -
+ - + - +
- + - - -
- - - - -
- - - - -
- + + + +
+ + + + -
+ + + + +
- - - - ]
[ - - - -
+ + + + -
+ - + - +
+ + + + -
+ - + ]
252 [ + + ]: 17170593 : return size() == 0;
[ + + + + ]
[ + + - +
+ + - + ]
[ # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - + -
- - - - -
- - - - -
- - + - -
+ + - - +
- + - + -
- - - - -
- - - - -
- - - + +
+ + + + +
+ - + + +
+ + - - -
- ][ - - -
- + + + -
- + - + +
- + - + +
- + + - ]
253 : : }
254 : :
255 [ + + + - : 13625073 : iterator begin() { return iterator(item_ptr(0)); }
+ + + + ]
[ - + + -
+ - ][ + + ]
[ - - - -
- - - - +
+ + + - -
+ + + + +
+ - - + +
- - + + -
- + + - -
+ + + - -
- + + +
+ ][ + + +
- + + + -
+ + + - +
+ + - ]
[ + + + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + #
# ][ - + +
- - + + -
- + + - -
+ + - - +
+ - ]
256 [ + + + + : 28577881 : const_iterator begin() const { return const_iterator(item_ptr(0)); }
- - - - +
+ + + + +
+ + + + +
+ + + ][ +
+ + + + +
+ + + + ]
[ + + + +
+ + # # #
# # # # #
# # # # #
# # # ][ +
+ + + #
# ][ + + +
+ + + + +
# # # # #
# ][ - - -
- - - + -
- + + + +
- ][ - - -
- - - + +
- - - - -
- - - - -
- - ][ + + ]
[ + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - + + +
+ + - + +
- - - - +
- + + + -
+ + + - +
+ - - + -
- + + - ]
257 [ + + + + ]: 11501883 : iterator end() { return iterator(item_ptr(size())); }
258 [ + + + + ]: 28971362 : const_iterator end() const { return const_iterator(item_ptr(size())); }
259 : :
260 : 5882028 : size_t capacity() const {
261 [ + + ]: 2098521 : if (is_direct()) {
[ - - - + ]
[ - - - +
+ + + + -
+ - - ][ +
+ + + + +
# # # # #
# # # # #
# # # # ]
[ - - - +
- + - + -
+ - + - +
- + + - +
- ]
262 : : return N;
263 : : } else {
264 : 200916 : return _union.indirect_contents.capacity;
265 : : }
266 : : }
267 : :
268 [ + - + - : 2372753 : T& operator[](size_type pos) {
+ - + - +
- + - ][ +
- + - + -
+ - - - -
- + - + -
- - - - +
- + - ][ -
+ # # # #
# # # # ]
[ + + + +
+ + + + +
+ ]
269 [ + + + + : 6822573 : return *item_ptr(pos);
+ + + + ]
[ + + ][ + +
+ - + + +
+ + + + +
+ + + + +
- ]
[ + + + + ]
[ + - + -
+ - + - +
- + - + -
- - - - -
- - - - -
+ - + - +
- + - + -
- - - - -
- + - + -
+ - + - +
- + + + -
+ - ][ - -
- - - + +
- - + +
- ][ + - -
+ + - - +
+ - - + +
- ][ + - +
- + - + -
+ - + - +
- + - ][ -
+ - + + -
- + + - #
# # # ]
270 : : }
271 : :
272 [ + - ]: 571763 : const T& operator[](size_type pos) const {
273 [ + + + + : 2106378 : return *item_ptr(pos);
+ + + + +
+ + + - -
+ - + - +
- + + + +
+ - + + +
- + + + -
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + ]
[ + + + + ]
[ + + + -
+ - + - +
- + - ][ -
+ + + - -
- - - - -
- - - -
- ][ + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ + + + +
+ + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + +
+ + + + +
- + + + +
+ - + + +
- + + + -
+ + + - +
+ + - - +
- - + - +
+ + - + +
+ + + - +
+ + + + -
+ + + + +
- + + +
- ]
274 : : }
275 : :
276 [ + + ]: 37894119 : void resize(size_type new_size) {
277 : 37894119 : size_type cur_size = size();
278 [ + + ]: 37894119 : if (cur_size == new_size) {
279 : : return;
280 : : }
281 [ + + ]: 275757 : if (cur_size > new_size) {
282 [ + + ]: 376654 : erase(item_ptr(new_size), end());
283 : 188327 : return;
284 : : }
285 [ + + + + ]: 91420 : if (new_size > capacity()) {
286 : 36897 : change_capacity(new_size);
287 : : }
288 : 87430 : ptrdiff_t increase = new_size - cur_size;
289 [ + + + - ]: 174860 : fill(item_ptr(cur_size), increase);
290 : 87430 : _size += increase;
291 : : }
292 : :
293 : 4216 : void reserve(size_type new_capacity) {
294 [ + + + + ]: 6875 : if (new_capacity > capacity()) {
295 : 1889 : change_capacity(new_capacity);
296 : : }
297 : 4216 : }
298 : :
299 [ + + ]: 37258901 : void shrink_to_fit() {
300 : 37258901 : change_capacity(size());
301 : 37258901 : }
302 : :
303 : 37791878 : void clear() {
304 : 37791878 : resize(0);
305 : : }
306 : :
307 [ + + ]: 3034077 : iterator insert(iterator pos, const T& value) {
308 [ + + ]: 3034077 : size_type p = pos - begin();
309 [ + + ]: 3034077 : size_type new_size = size() + 1;
310 [ + + ]: 3034077 : if (capacity() < new_size) {
311 : 1747 : change_capacity(new_size + (new_size >> 1));
312 : : }
313 [ + + ]: 3034077 : T* ptr = item_ptr(p);
314 [ + + ]: 3034077 : T* dst = ptr + 1;
315 : 3034077 : memmove(dst, ptr, (size() - p) * sizeof(T));
316 : 3034077 : _size++;
317 : 3034077 : new(static_cast<void*>(ptr)) T(value);
318 : 3034077 : return iterator(ptr);
319 : : }
320 : :
321 [ + + ]: 16291 : void insert(iterator pos, size_type count, const T& value) {
322 [ + + ]: 16291 : size_type p = pos - begin();
323 [ + + ]: 16291 : size_type new_size = size() + count;
324 [ + + ]: 16291 : if (capacity() < new_size) {
325 : 797 : change_capacity(new_size + (new_size >> 1));
326 : : }
327 [ + + ]: 16291 : T* ptr = item_ptr(p);
328 [ + + ]: 16291 : T* dst = ptr + count;
329 [ + + ]: 16291 : memmove(dst, ptr, (size() - p) * sizeof(T));
330 : 16291 : _size += count;
331 [ + + + - ]: 32582 : fill(item_ptr(p), count, value);
332 : 16291 : }
333 : :
334 : : template <std::input_iterator InputIterator>
335 [ + + ]: 2169649 : void insert(iterator pos, InputIterator first, InputIterator last) {
336 [ + + ]: 2169649 : size_type p = pos - begin();
337 [ + + ]: 2169649 : difference_type count = last - first;
338 [ + + ]: 2169649 : size_type new_size = size() + count;
339 [ + + ]: 2169649 : if (capacity() < new_size) {
340 : 148355 : change_capacity(new_size + (new_size >> 1));
341 : : }
342 [ + + ]: 2169649 : T* ptr = item_ptr(p);
343 [ + + ]: 2169649 : T* dst = ptr + count;
344 : 2169649 : memmove(dst, ptr, (size() - p) * sizeof(T));
345 : 2169649 : _size += count;
346 : 2169649 : fill(ptr, first, last);
347 : 2169649 : }
348 : :
349 [ + + ]: 33397 : 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 [ + + ]: 33397 : if (capacity() < new_size) {
353 [ + - ]: 7255 : change_capacity(new_size);
354 : 7255 : _size += new_size - size();
355 : 7255 : return;
356 : : }
357 [ + + ]: 26142 : if (new_size < size()) {
358 [ + + ]: 6496 : erase(item_ptr(new_size), end());
359 : : } else {
360 : 22894 : _size += new_size - size();
361 : : }
362 : : }
363 : :
364 : 27457 : iterator erase(iterator pos) {
365 : 27457 : return erase(pos, pos + 1);
366 : : }
367 : :
368 : 246207 : 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 : 246207 : iterator p = first;
376 : 246207 : char* endp = (char*)&(*end());
377 : 246207 : _size -= last - p;
378 : 246207 : memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
379 : 246207 : return first;
380 : : }
381 : :
382 : : template<typename... Args>
383 [ + + ]: 32448 : void emplace_back(Args&&... args) {
384 [ + + ]: 32448 : size_type new_size = size() + 1;
385 [ + + ]: 32448 : if (capacity() < new_size) {
386 : 398 : change_capacity(new_size + (new_size >> 1));
387 : : }
388 [ + + ]: 32448 : new(item_ptr(size())) T(std::forward<Args>(args)...);
389 : 32448 : _size++;
390 : 32448 : }
391 : :
392 : 32448 : void push_back(const T& value) {
393 : 32448 : emplace_back(value);
394 : 23965 : }
395 : :
396 : 6826 : void pop_back() {
397 : 6826 : erase(end() - 1, end());
398 : 6826 : }
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 [ + + + + : 1617 : const T& back() const {
+ - ]
413 [ + + + + : 1667 : return *item_ptr(size() - 1);
- + + - ]
414 : : }
415 : :
416 : 16288 : void swap(prevector<N, T, Size, Diff>& other) noexcept
417 : : {
418 : 16288 : std::swap(_union, other._union);
419 : 16288 : std::swap(_size, other._size);
420 : : }
421 : :
422 : 46748171 : ~prevector() {
423 [ + + ]: 46748171 : if (!is_direct()) {
424 : 1251480 : free(_union.indirect_contents.indirect);
425 : 1251480 : _union.indirect_contents.indirect = nullptr;
426 : : }
427 : 46748171 : }
428 : :
429 [ + + ]: 947913 : bool operator==(const prevector<N, T, Size, Diff>& other) const {
430 [ + + + + ]: 1281507 : if (other.size() != size()) {
431 : : return false;
432 : : }
433 [ + + ]: 947074 : const_iterator b1 = begin();
434 : 947074 : const_iterator b2 = other.begin();
435 : 947074 : const_iterator e1 = end();
436 [ + + ]: 16308787 : while (b1 != e1) {
437 [ + + ]: 15385474 : if ((*b1) != (*b2)) {
438 : : return false;
439 : : }
440 : 15361713 : ++b1;
441 : 15361713 : ++b2;
442 : : }
443 : : return true;
444 : : }
445 : :
446 [ + + ]: 7017169 : bool operator<(const prevector<N, T, Size, Diff>& other) const {
447 [ + + + + ]: 7017201 : if (size() < other.size()) {
448 : : return true;
449 : : }
450 [ + + ]: 7013817 : if (size() > other.size()) {
451 : : return false;
452 : : }
453 [ + + ]: 7013625 : const_iterator b1 = begin();
454 : 7013625 : const_iterator b2 = other.begin();
455 : 7013625 : const_iterator e1 = end();
456 [ + + ]: 25249170 : while (b1 != e1) {
457 [ + + ]: 25206224 : if ((*b1) < (*b2)) {
458 : : return true;
459 : : }
460 [ + + ]: 21353886 : if ((*b2) < (*b1)) {
461 : : return false;
462 : : }
463 : 18235545 : ++b1;
464 : 18235545 : ++b2;
465 : : }
466 : : return false;
467 : : }
468 : :
469 : 2639655 : size_t allocated_memory() const {
470 [ + + + + ]: 2639655 : if (is_direct()) {
[ # # # # ]
[ + + + +
- + + + +
+ + + + +
+ + + + +
+ + + + +
- - ][ - +
- + - + -
+ - + - +
- + + - +
- ][ - + +
+ - + ]
471 : : return 0;
472 : : } else {
473 [ + - + - ]: 891332 : return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
[ # # # # ]
[ + - + -
- - + - +
- + - + -
+ - + - +
- + - + -
- - ][ - -
+ - - - ]
474 : : }
475 : : }
476 : :
477 [ + + ][ + + : 92752 : value_type* data() {
+ + # # ]
[ - + - +
- + - - +
- ][ + - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - ]
[ + - - -
+ + + - #
# ][ + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - + + +
- + + + -
+ - + ][ +
- - + - +
- + - + -
+ - + - +
- + - + ]
478 [ + + ][ - + : 92751 : return item_ptr(0);
- + - + -
- - + ][ -
+ - - - -
- - - + -
- - - -
- ][ + + +
+ # # # #
# # ][ + +
+ + + + #
# # # # #
# # # # ]
[ - + + +
- + + + +
+ + + ][ -
+ + - - +
+ - - + +
- - + + -
- + + - ]
479 : : }
480 : :
481 [ + + ][ + + : 16273447 : const value_type* data() const {
+ - + + +
+ ][ - - +
+ - - - -
- - # # ]
[ # # # #
# # # # #
# # # ][ +
+ + + #
# ][ # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
- + - + +
+ + + + +
+ - + - -
- - - -
- ]
482 [ + + ][ - + : 16273766 : return item_ptr(0);
- + + + +
+ ][ + + +
+ # # ][ +
- - - +
- ][ - + -
+ - + - +
- + + - +
- + + + +
+ + + + +
+ ]
483 : : }
484 : : };
485 : :
486 : : #endif // BITCOIN_PREVECTOR_H
|