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 : : typedef Size size_type;
42 : : typedef Diff difference_type;
43 : : typedef T value_type;
44 : : typedef value_type& reference;
45 : : typedef const value_type& const_reference;
46 : : typedef value_type* pointer;
47 : : typedef const value_type* const_pointer;
48 : :
49 : : class iterator {
50 : : T* ptr{};
51 : : public:
52 : : typedef Diff difference_type;
53 : : typedef T* pointer;
54 : : typedef T& reference;
55 : : using element_type = T;
56 : : using iterator_category = std::contiguous_iterator_tag;
57 : : iterator() = default;
58 : 276501952 : iterator(T* ptr_) : ptr(ptr_) {}
59 : 111796627 : T& operator*() const { return *ptr; }
60 : : T* operator->() const { return ptr; }
61 : : T& operator[](size_type pos) const { return ptr[pos]; }
62 : 385697403 : iterator& operator++() { ptr++; return *this; }
63 : : iterator& operator--() { ptr--; return *this; }
64 : : iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
65 : : iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
66 [ + - ]: 75636723 : difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
67 : 51472 : iterator operator+(size_type n) const { return iterator(ptr + n); }
68 : : iterator friend operator+(size_type n, iterator x) { return x + n; }
69 : : iterator& operator+=(size_type n) { ptr += n; return *this; }
70 : 7241 : iterator operator-(size_type n) const { return iterator(ptr - n); }
71 : : iterator& operator-=(size_type n) { ptr -= n; return *this; }
72 : : bool operator==(iterator x) const { return ptr == x.ptr; }
73 [ + + ]: 387383836 : bool operator!=(iterator x) const { return ptr != x.ptr; }
[ - - + + ]
74 : : bool operator>=(iterator x) const { return ptr >= x.ptr; }
75 : : bool operator<=(iterator x) const { return ptr <= x.ptr; }
76 : : bool operator>(iterator x) const { return ptr > x.ptr; }
77 : : bool operator<(iterator x) const { return ptr < x.ptr; }
78 : : };
79 : :
80 : : class const_iterator {
81 : : const T* ptr{};
82 : : public:
83 : : typedef Diff difference_type;
84 : : typedef const T* pointer;
85 : : typedef const T& reference;
86 : : using element_type = const T;
87 : : using iterator_category = std::contiguous_iterator_tag;
88 : : const_iterator() = default;
89 : 2780048670 : const_iterator(const T* ptr_) : ptr(ptr_) {}
90 : 212049 : const_iterator(iterator x) : ptr(&(*x)) {}
91 [ + + ][ - - : 1825557668 : const T& operator*() const { return *ptr; }
+ + - - -
- + + ]
92 : : const T* operator->() const { return ptr; }
93 [ - + ]: 909640 : const T& operator[](size_type pos) const { return ptr[pos]; }
94 [ + + + + : 13762165598 : const_iterator& operator++() { ptr++; return *this; }
+ + ][ + -
- + # # ]
95 [ - + - + ]: 1648192 : const_iterator& operator--() { ptr--; return *this; }
96 [ + + ]: 337600337 : const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
97 : : const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
98 [ + - ][ + + : 604988911 : difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
- - + - +
+ + + + +
+ - + + ]
[ - - - +
+ - ][ - -
+ + # # ]
99 : 63191763 : const_iterator operator+(size_type n) const { return const_iterator(ptr + n); }
100 : : const_iterator friend operator+(size_type n, const_iterator x) { return x + n; }
101 [ + + ]: 90211873 : const_iterator& operator+=(size_type n) { ptr += n; return *this; }
102 [ - + ]: 1165322 : const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
[ + - + - ]
103 : : const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
104 [ - + ]: 1733992 : bool operator==(const_iterator x) const { return ptr == x.ptr; }
105 [ + + + + : 14297416212 : bool operator!=(const_iterator x) const { return ptr != x.ptr; }
+ + ][ + +
# # # # ]
[ + + + + ]
106 [ + + ]: 321939284 : bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
107 : : bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
108 : : bool operator>(const_iterator x) const { return ptr > x.ptr; }
109 [ + + + + : 158508267 : bool operator<(const_iterator x) const { return ptr < x.ptr; }
+ + + + ]
[ + + + +
+ + ]
[ + + + + ]
[ + + # # ]
110 : : };
111 : :
112 : : private:
113 : : #pragma pack(push, 1)
114 : : union direct_or_indirect {
115 : : char direct[sizeof(T) * N];
116 : : struct {
117 : : char* indirect;
118 : : size_type capacity;
119 : : } indirect_contents;
120 : : };
121 : : #pragma pack(pop)
122 : : alignas(char*) direct_or_indirect _union = {};
123 : : size_type _size = 0;
124 : :
125 : : static_assert(alignof(char*) % alignof(size_type) == 0 && sizeof(char*) % alignof(size_type) == 0, "size_type cannot have more restrictive alignment requirement than pointer");
126 : : static_assert(alignof(char*) % alignof(T) == 0, "value_type T cannot have more restrictive alignment requirement than pointer");
127 : :
128 : 596823747 : T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
129 : 2667109218 : const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
130 : 456348330 : T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
131 : 1345386223 : const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
132 : 13850431479 : bool is_direct() const { return _size <= N; }
133 : :
134 : 492832011 : void change_capacity(size_type new_capacity) {
135 [ + + ]: 492832011 : if (new_capacity <= N) {
136 [ + + ]: 303242496 : if (!is_direct()) {
137 : 103372 : T* indirect = indirect_ptr(0);
138 : 103372 : T* src = indirect;
139 : 103372 : T* dst = direct_ptr(0);
140 : 103372 : memcpy(dst, src, size() * sizeof(T));
141 : 103372 : free(indirect);
142 : 103372 : _size -= N + 1;
143 : : }
144 : : } else {
145 [ + + ]: 189589515 : if (!is_direct()) {
146 : : /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
147 : : success. These should instead use an allocator or new/delete so that handlers
148 : : are called as necessary, but performance would be slightly degraded by doing so. */
149 : 748207 : _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
150 [ - + ]: 748207 : assert(_union.indirect_contents.indirect);
151 : 748207 : _union.indirect_contents.capacity = new_capacity;
152 : : } else {
153 : 188841308 : char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
154 [ - + ]: 188841308 : assert(new_indirect);
155 : 188841308 : T* src = direct_ptr(0);
156 : 188841308 : T* dst = reinterpret_cast<T*>(new_indirect);
157 : 188841308 : memcpy(dst, src, size() * sizeof(T));
158 : 188841308 : _union.indirect_contents.indirect = new_indirect;
159 : 188841308 : _union.indirect_contents.capacity = new_capacity;
160 : 188841308 : _size += N + 1;
161 : : }
162 : : }
163 : 492832011 : }
164 : :
165 [ + + ]: 863710890 : T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
166 [ + + ]: 4002657712 : const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
167 : :
168 : 81822936 : void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
169 : 81822936 : std::fill_n(dst, count, value);
170 : : }
171 : :
172 : : template <std::input_iterator InputIterator>
173 : 458900927 : void fill(T* dst, InputIterator first, InputIterator last) {
174 [ + + + + : 13934113246 : while (first != last) {
+ + + + ]
[ + + # #
# # # # ]
[ # # # #
# # ][ - -
+ + + + +
+ - - -
- ][ - - +
+ - - + +
+ + ]
[ + + + + ]
[ - - - -
+ + + + +
+ + + +
+ ]
175 : 13492365511 : new(static_cast<void*>(dst)) T(*first);
176 : 12565727312 : ++dst;
177 : 13492365511 : ++first;
178 : : }
179 : : }
180 : :
181 : : public:
182 : 1075232 : void assign(size_type n, const T& val) {
183 : 1075232 : clear();
184 [ + + ]: 1075232 : if (capacity() < n) {
185 : 4300 : change_capacity(n);
186 : : }
187 : 1075232 : _size += n;
188 [ + + + + ]: 2150464 : fill(item_ptr(0), n, val);
189 : 1075232 : }
190 : :
191 : : template <std::input_iterator InputIterator>
192 : 52894057 : void assign(InputIterator first, InputIterator last) {
193 : 52894057 : size_type n = last - first;
194 : 52894057 : clear();
195 [ + + ]: 52894057 : if (capacity() < n) {
196 : 26121861 : change_capacity(n);
197 : : }
198 [ + + ]: 52894057 : _size += n;
199 [ + + ]: 70047249 : fill(item_ptr(0), first, last);
200 : 52894057 : }
201 : :
202 [ + - + - ]: 98083982 : prevector() = default;
[ + - # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- ][ - - +
- + - +
- ][ + - +
- + + ]
203 : :
204 : : explicit prevector(size_type n) {
205 : : resize(n);
206 : : }
207 : :
208 : 65122690 : explicit prevector(size_type n, const T& val) {
209 : 65122690 : change_capacity(n);
210 : 65122690 : _size += n;
211 [ + - + - ]: 130245380 : fill(item_ptr(0), n, val);
212 : 65122690 : }
213 : :
214 : : template <std::input_iterator InputIterator>
215 : 2866051 : prevector(InputIterator first, InputIterator last) {
216 : 2866051 : size_type n = last - first;
217 : 2866051 : change_capacity(n);
218 [ + + ]: 2866051 : _size += n;
219 : 2866051 : fill(item_ptr(0), first, last);
220 : 2866051 : }
221 : :
222 : 345501991 : prevector(const prevector<N, T, Size, Diff>& other) {
223 : 345501991 : size_type n = other.size();
224 : 345501991 : change_capacity(n);
225 : 345501991 : _size += n;
226 : 345501991 : fill(item_ptr(0), other.begin(), other.end());
227 : 345501991 : }
228 : :
229 : 74232291 : prevector(prevector<N, T, Size, Diff>&& other) noexcept
230 : 74232291 : : _union(std::move(other._union)), _size(other._size)
231 : : {
232 [ # # # # : 73209869 : other._size = 0;
# # ][ + - ]
[ + - + - ]
233 : : }
234 : :
235 : 50586363 : prevector& operator=(const prevector<N, T, Size, Diff>& other) {
236 [ + - ]: 50586363 : if (&other == this) {
237 : : return *this;
238 : : }
239 : 101172726 : assign(other.begin(), other.end());
240 : 50586363 : return *this;
241 : : }
242 : :
243 : 45317365 : prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
244 [ + + ]: 45317365 : if (!is_direct()) {
245 : 122021 : free(_union.indirect_contents.indirect);
246 : : }
247 : 45317365 : _union = std::move(other._union);
248 : 45317365 : _size = other._size;
249 : 45317365 : other._size = 0;
250 : 45317365 : return *this;
251 : : }
252 : :
253 : 11276639292 : size_type size() const {
254 [ + + + + : 7206917820 : return is_direct() ? _size : _size - N - 1;
+ + ][ + +
+ + # # #
# # # # #
# # ][ - +
- - + + +
+ + + + +
+ + - - +
+ + + + +
+ + + + ]
[ - - - -
+ + + + +
+ + + + +
+ + + + +
+ # # # #
# # # # #
# ][ - - -
- + + - -
- - - - +
+ - - - -
- - - - +
+ - - - -
- - - - -
- - - - -
- - - - -
- ][ - - +
+ - - - +
- + - - -
+ - + - -
- + - + -
- - - - -
- - ][ - -
+ + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ][ - -
- - - - -
+ + + - -
- - - + -
+ - - - -
- - - - -
- ][ - - -
- - - - -
- - + + +
+ + + - -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - +
+ - - + +
+ + + + -
+ - - - +
- + - + +
+ - + - -
- + - + -
- - + - +
- + + + +
+ + + - +
- + - + -
+ + - + -
- + - + -
+ - + - +
+ - - + +
- - + - +
- + - - -
- - - - -
- - ][ + +
# # # # ]
[ - - + +
- - + + +
+ + + +
+ ][ - + +
+ + + + +
+ + ][ - -
+ + + + +
+ # # ][ +
+ + + + +
+ + + + +
+ # # #
# ][ + - -
+ + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + - -
+ + + + +
+ + + + +
- + + + +
+ - - - -
+ + + + +
+ - - - -
+ + - + +
+ - - + +
+ + + + -
+ - + ][ -
- + + + +
+ + + + +
+ + + + +
+ + ]
255 : : }
256 : :
257 [ + + ][ + + : 3814536812 : bool empty() const {
+ + # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - - + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
- + + + +
+ ][ + + -
- - - ][ -
- - - - -
- - - - -
- - - - -
- - - - +
+ + + + +
+ + + + +
+ ][ - - +
+ + + + +
+ + + + +
+ + + + +
- + + + -
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- + + + +
+ + + + +
+ + ]
258 [ + + ][ + + : 3814536812 : return size() == 0;
+ + # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - - + -
+ + + - +
+ + + + +
+ + + + +
+ + + +
+ ][ # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
- + + + -
+ ][ + - -
- - - ][ -
- - - - -
- - - - -
- - - - -
- - - - +
+ + + + +
+ + + + +
+ ][ - - +
+ + + + +
+ + + + +
+ + + + +
- + + + -
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ]
259 : : }
260 : :
261 [ + + + + : 86575789 : iterator begin() { return iterator(item_ptr(0)); }
+ + + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
- - - - -
- - - + +
+ + - - +
+ + + + +
- - + + -
- + + - -
+ + - - +
+ + + - -
+ + + + ]
[ + + + -
+ + + + ]
[ + + # #
# # ][ - +
+ - + - ]
[ + + + -
+ + + - +
+ + - + +
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
262 [ + + + + : 3256076331 : const_iterator begin() const { return const_iterator(item_ptr(0)); }
+ + ][ + +
+ + # # ]
[ + + - -
+ + + - +
+ + + ][ +
+ + + + +
+ + + + ]
[ + + + +
+ + + + -
+ + + +
+ ][ + + +
+ + + + +
# # # # #
# ][ + + ]
[ + + + +
- - + + +
+ + + + +
+ + + + ]
[ # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- + + + +
+ + + + -
- - - + +
+ + + + +
+ + + + +
- - + - +
+ + - ]
263 [ + + + + ]: 212674521 : iterator end() { return iterator(item_ptr(size())); }
264 [ + + + + ]: 1759348355 : const_iterator end() const { return const_iterator(item_ptr(size())); }
265 : :
266 : 173506875 : size_t capacity() const {
267 [ + + ]: 102143813 : if (is_direct()) {
[ # # # # ]
[ + + + +
+ + ][ - -
- + - + -
+ - + -
- ]
268 : : return N;
269 : : } else {
270 : 85023233 : return _union.indirect_contents.capacity;
271 : : }
272 : : }
273 : :
274 [ + - + - : 9996483 : T& operator[](size_type pos) {
+ - + - +
- - + -
+ ][ + - +
- + - + -
+ - + - ]
[ + - + -
+ - + - +
- + - - +
- + - + -
+ + - +
- ]
275 [ + + ]: 19523314 : return *item_ptr(pos);
[ + + + + ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - + -
+ - + - +
- + - + ]
[ + + + -
+ - + - +
- + - + -
+ - + - ]
[ + - + -
+ - + + +
- + - + -
+ - + - +
+ + - + -
- + - + +
+ - + - +
- + - + -
+ + - + -
+ - + - +
- + + + -
+ - ]
276 : : }
277 : :
278 [ + - ]: 80488686 : const T& operator[](size_type pos) const {
279 [ + + - + : 1037121209 : return *item_ptr(pos);
+ + - + ]
[ + + + +
# # # # #
# # # ][ +
+ + + + +
+ + + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ ][ - + +
+ - - - -
- - - - -
- - - ][ +
- + - + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + - + +
+ - + + +
- + + + -
+ - + + +
- + + + -
+ + + - +
+ + - + +
+ - ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
280 : : }
281 : :
282 [ + + ]: 141677282 : void resize(size_type new_size) {
283 : 141677282 : size_type cur_size = size();
284 [ + + ]: 141677282 : if (cur_size == new_size) {
285 : : return;
286 : : }
287 [ + + ]: 30030703 : if (cur_size > new_size) {
288 [ + + ]: 28838814 : erase(item_ptr(new_size), end());
289 : 14419407 : return;
290 : : }
291 [ + + + + ]: 15613532 : if (new_size > capacity()) {
292 : 9746897 : change_capacity(new_size);
293 : : }
294 : 15611296 : ptrdiff_t increase = new_size - cur_size;
295 [ + + + - ]: 31222592 : fill(item_ptr(cur_size), increase);
296 : 15611296 : _size += increase;
297 : : }
298 : :
299 : 6530 : void reserve(size_type new_capacity) {
300 [ + + + + ]: 10172 : if (new_capacity > capacity()) {
301 : 3353 : change_capacity(new_capacity);
302 : : }
303 : 6530 : }
304 : :
305 [ + + ]: 39454850 : void shrink_to_fit() {
306 : 39454850 : change_capacity(size());
307 : 39454850 : }
308 : :
309 : 107486487 : void clear() {
310 : 107486487 : resize(0);
311 : : }
312 : :
313 [ + + ]: 36943435 : iterator insert(iterator pos, const T& value) {
314 [ + + ]: 36943435 : size_type p = pos - begin();
315 [ + + ]: 36943435 : size_type new_size = size() + 1;
316 [ + + ]: 36943435 : if (capacity() < new_size) {
317 : 50690 : change_capacity(new_size + (new_size >> 1));
318 : : }
319 [ + + ]: 36943435 : T* ptr = item_ptr(p);
320 [ + + ]: 36943435 : T* dst = ptr + 1;
321 : 36943435 : memmove(dst, ptr, (size() - p) * sizeof(T));
322 : 36943435 : _size++;
323 : 36943435 : new(static_cast<void*>(ptr)) T(value);
324 : 36943435 : return iterator(ptr);
325 : : }
326 : :
327 [ + + ]: 13718 : void insert(iterator pos, size_type count, const T& value) {
328 [ + + ]: 13718 : size_type p = pos - begin();
329 [ + + ]: 13718 : size_type new_size = size() + count;
330 [ + + ]: 13718 : if (capacity() < new_size) {
331 : 1259 : change_capacity(new_size + (new_size >> 1));
332 : : }
333 [ + + ]: 13718 : T* ptr = item_ptr(p);
334 [ + + ]: 13718 : T* dst = ptr + count;
335 [ + + ]: 13718 : memmove(dst, ptr, (size() - p) * sizeof(T));
336 : 13718 : _size += count;
337 [ + + + - ]: 27436 : fill(item_ptr(p), count, value);
338 : 13718 : }
339 : :
340 : : template <std::input_iterator InputIterator>
341 [ + + ]: 59377573 : void insert(iterator pos, InputIterator first, InputIterator last) {
342 [ + + ]: 59377573 : size_type p = pos - begin();
343 [ + + ]: 59377573 : difference_type count = last - first;
344 [ + + ]: 59377573 : size_type new_size = size() + count;
345 [ + + ]: 59377573 : if (capacity() < new_size) {
346 : 1927932 : change_capacity(new_size + (new_size >> 1));
347 : : }
348 [ + + ]: 59377573 : T* ptr = item_ptr(p);
349 [ + + ]: 59377573 : T* dst = ptr + count;
350 : 59377573 : memmove(dst, ptr, (size() - p) * sizeof(T));
351 : 59377573 : _size += count;
352 : 59377573 : fill(ptr, first, last);
353 : 59377573 : }
354 : :
355 [ + + ]: 6668185 : inline void resize_uninitialized(size_type new_size) {
356 : : // resize_uninitialized changes the size of the prevector but does not initialize it.
357 : : // If size < new_size, the added elements must be initialized explicitly.
358 [ + + ]: 6668185 : if (capacity() < new_size) {
359 [ + - ]: 2020196 : change_capacity(new_size);
360 : 2020196 : _size += new_size - size();
361 : 2020196 : return;
362 : : }
363 [ + + ]: 4647989 : if (new_size < size()) {
364 [ + + ]: 6482 : erase(item_ptr(new_size), end());
365 : : } else {
366 : 4644748 : _size += new_size - size();
367 : : }
368 : : }
369 : :
370 : 5156 : iterator erase(iterator pos) {
371 : 5156 : return erase(pos, pos + 1);
372 : : }
373 : :
374 : 14445180 : iterator erase(iterator first, iterator last) {
375 : : // Erase is not allowed to the change the object's capacity. That means
376 : : // that when starting with an indirectly allocated prevector with
377 : : // size and capacity > N, the result may be a still indirectly allocated
378 : : // prevector with size <= N and capacity > N. A shrink_to_fit() call is
379 : : // necessary to switch to the (more efficient) directly allocated
380 : : // representation (with capacity N and size <= N).
381 : 14445180 : iterator p = first;
382 : 14445180 : char* endp = (char*)&(*end());
383 : 14445180 : _size -= last - p;
384 : 14445180 : memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
385 : 14445180 : return first;
386 : : }
387 : :
388 : : template<typename... Args>
389 [ + + ]: 910551 : void emplace_back(Args&&... args) {
390 [ + + ]: 910551 : size_type new_size = size() + 1;
391 [ + + ]: 910551 : if (capacity() < new_size) {
392 : 9941 : change_capacity(new_size + (new_size >> 1));
393 : : }
394 [ + + ]: 910551 : new(item_ptr(size())) T(std::forward<Args>(args)...);
395 : 910551 : _size++;
396 : 910551 : }
397 : :
398 : 910551 : void push_back(const T& value) {
399 : 910551 : emplace_back(value);
400 : 656433 : }
401 : :
402 : 7241 : void pop_back() {
403 : 7241 : erase(end() - 1, end());
404 : 7241 : }
405 : :
406 : : T& front() {
407 : : return *item_ptr(0);
408 : : }
409 : :
410 : : const T& front() const {
411 : : return *item_ptr(0);
412 : : }
413 : :
414 [ + + + + : 12748 : T& back() {
+ + + + ]
415 [ + + + + : 12748 : return *item_ptr(size() - 1);
+ + + + ]
416 : : }
417 : :
418 [ - + + + ]: 2846062 : const T& back() const {
419 [ + + + + : 2966597 : return *item_ptr(size() - 1);
+ + + + ]
420 : : }
421 : :
422 : 7416 : void swap(prevector<N, T, Size, Diff>& other) noexcept
423 : : {
424 : 7416 : std::swap(_union, other._union);
425 : 7416 : std::swap(_size, other._size);
426 : : }
427 : :
428 : 647545835 : ~prevector() {
429 [ + + ]: 647545835 : if (!is_direct()) {
430 : 188615915 : free(_union.indirect_contents.indirect);
431 : 188615915 : _union.indirect_contents.indirect = nullptr;
432 : : }
433 : 647545835 : }
434 : :
435 [ + + ]: 40863571 : bool operator==(const prevector<N, T, Size, Diff>& other) const {
436 [ + + + + ]: 56008477 : if (other.size() != size()) {
437 : : return false;
438 : : }
439 [ + + ]: 40863434 : const_iterator b1 = begin();
440 : 40863434 : const_iterator b2 = other.begin();
441 : 40863434 : const_iterator e1 = end();
442 [ + + ]: 675540143 : while (b1 != e1) {
443 [ + + ]: 645020373 : if ((*b1) != (*b2)) {
444 : : return false;
445 : : }
446 : 634676709 : ++b1;
447 : 634676709 : ++b2;
448 : : }
449 : : return true;
450 : : }
451 : :
452 : 78039 : bool operator!=(const prevector<N, T, Size, Diff>& other) const {
453 : 78039 : return !(*this == other);
454 : : }
455 : :
456 [ + + ]: 12046994 : bool operator<(const prevector<N, T, Size, Diff>& other) const {
457 [ + + + + ]: 17488510 : if (size() < other.size()) {
458 : : return true;
459 : : }
460 [ + + ]: 11660688 : if (size() > other.size()) {
461 : : return false;
462 : : }
463 [ + + ]: 11507219 : const_iterator b1 = begin();
464 : 11507219 : const_iterator b2 = other.begin();
465 : 11507219 : const_iterator e1 = end();
466 [ + + ]: 90933132 : while (b1 != e1) {
467 [ + + ]: 88253358 : if ((*b1) < (*b2)) {
468 : : return true;
469 : : }
470 [ + + ]: 82503341 : if ((*b2) < (*b1)) {
471 : : return false;
472 : : }
473 : 79425913 : ++b1;
474 : 79425913 : ++b2;
475 : : }
476 : : return false;
477 : : }
478 : :
479 : 30652508 : size_t allocated_memory() const {
480 [ + + + + ]: 30652508 : if (is_direct()) {
[ + + ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- - ]
481 : : return 0;
482 : : } else {
483 [ + - + - ]: 15273790 : return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
[ + - ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - ]
484 : : }
485 : : }
486 : :
487 [ - - - - : 34420463 : value_type* data() {
+ + ][ + +
+ + # # ]
[ + + # #
# # # # #
# # # # #
# # # # ]
[ + + + -
+ + + - -
+ - - - -
+ + + - ]
[ + + + -
- - - - -
- - - - -
- - + + +
- - - - -
- - - - -
- - - ][ #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
488 [ + + + + ]: 34420463 : return item_ptr(0);
[ + + # # ]
[ + + + +
- + - - -
- + + ][ #
# # # #
# ][ + + -
- - - - -
+ + - - -
- - - ][ #
# # # # #
# # # # #
# # # #
# ]
489 : : }
490 : :
491 [ + + # # : 718589912 : const value_type* data() const {
# # # # ]
[ + + + +
+ + + + ]
[ + + + +
- - + + +
+ + - ]
[ + + + + ]
[ # # # #
# # # # #
# ][ # # #
# # # # #
# # ][ + +
+ + + + +
+ + - + -
+ - + - -
+ - + + -
+ - + + +
+ + + - -
- - - - ]
492 [ + + ][ + + : 718592653 : return item_ptr(0);
+ + - + ]
[ + + + + ]
[ - + + +
+ + + + ]
[ + + + +
+ + - + -
+ + - + -
- + - + +
+ + + +
+ ]
493 : : }
494 : : };
495 : :
496 : : #endif // BITCOIN_PREVECTOR_H
|