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 : 18171671 : iterator(T* ptr_) : ptr(ptr_) {}
59 : 4837231 : T& operator*() const { return *ptr; }
60 : : T* operator->() const { return ptr; }
61 : 2124599 : T& operator[](size_type pos) const { return ptr[pos]; }
62 : 39937825 : iterator& operator++() { ptr++; return *this; }
63 : 2124599 : 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 [ + - ]: 4142175 : difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
67 : 81507 : 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 : 2124599 : iterator operator-(size_type n) const { return iterator(ptr - n); }
71 : : iterator& operator-=(size_type n) { ptr -= n; return *this; }
72 : 2393502 : bool operator==(iterator x) const { return ptr == x.ptr; }
73 [ + + ]: 40282546 : 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 : 45904583 : const_iterator(const T* ptr_) : ptr(ptr_) {}
90 : 97291 : const_iterator(iterator x) : ptr(&(*x)) {}
91 [ + + ][ - - : 49195615 : const T& operator*() const { return *ptr; }
+ + - - -
- + + ]
92 : : const T* operator->() const { return ptr; }
93 : 25597 : const T& operator[](size_type pos) const { return ptr[pos]; }
94 [ - + - + : 150103783 : const_iterator& operator++() { ptr++; return *this; }
- + ][ + -
- + # # ]
95 : 2124599 : const_iterator& operator--() { ptr--; return *this; }
96 [ + + ]: 2536788 : const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
97 : : const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
98 [ + - ][ + + : 4600799 : difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
- - + - +
- + - + -
+ - + + ]
[ - - - +
+ - ][ - -
+ + # # ]
99 : 600995 : 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 [ + + ]: 1125471 : const_iterator& operator+=(size_type n) { ptr += n; return *this; }
102 [ + - + - ]: 590 : const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
103 : : const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
104 [ - + ]: 2393972 : bool operator==(const_iterator x) const { return ptr == x.ptr; }
105 [ + + ]: 157771318 : bool operator!=(const_iterator x) const { return ptr != x.ptr; }
[ + + + + ]
[ + + + +
+ + ]
106 [ + + ]: 2792518 : 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 [ + + ][ + + : 2223062 : 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 : 20491394 : T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
129 : 48771181 : const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
130 : 9854379 : T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
131 : 13706000 : const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
132 : 282560714 : bool is_direct() const { return _size <= N; }
133 : :
134 : 42122203 : void change_capacity(size_type new_capacity) {
135 [ + + ]: 42122203 : if (new_capacity <= N) {
136 [ + + ]: 40364930 : if (!is_direct()) {
137 : 58199 : T* indirect = indirect_ptr(0);
138 : 58199 : T* src = indirect;
139 : 58199 : T* dst = direct_ptr(0);
140 : 58199 : memcpy(dst, src, size() * sizeof(T));
141 : 58199 : free(indirect);
142 : 58199 : _size -= N + 1;
143 : : }
144 : : } else {
145 [ + + ]: 1757273 : 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 : 15397 : _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
150 [ - + ]: 15397 : assert(_union.indirect_contents.indirect);
151 : 15397 : _union.indirect_contents.capacity = new_capacity;
152 : : } else {
153 : 1741876 : char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
154 [ - + ]: 1741876 : assert(new_indirect);
155 : 1741876 : T* src = direct_ptr(0);
156 : 1741876 : T* dst = reinterpret_cast<T*>(new_indirect);
157 : 1741876 : memcpy(dst, src, size() * sizeof(T));
158 : 1741876 : _union.indirect_contents.indirect = new_indirect;
159 : 1741876 : _union.indirect_contents.capacity = new_capacity;
160 : 1741876 : _size += N + 1;
161 : : }
162 : : }
163 : 42122203 : }
164 : :
165 [ + + ]: 28390148 : T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
166 [ + + ]: 61775374 : const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
167 : :
168 : 270483 : void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
169 : 270483 : std::fill_n(dst, count, value);
170 : : }
171 : :
172 : : template <std::input_iterator InputIterator>
173 : 7512048 : void fill(T* dst, InputIterator first, InputIterator last) {
174 [ + + + + : 183786097 : while (first != last) {
+ + + + ]
[ + + # #
# # # # ]
[ - - + +
- - ][ - -
- - + + +
+ - - -
- ][ + + +
+ + + + +
- - # # #
# # # # #
# # # # #
# # # #
# ]
[ + + - - ]
[ + + + +
+ + + + +
+ + + + +
+ + - - +
+ - - - -
- - - - ]
[ - - - -
+ + + + +
+ + + +
+ ]
175 : 176329551 : new(static_cast<void*>(dst)) T(*first);
176 : 21385220 : ++dst;
177 : 176329551 : ++first;
178 : : }
179 : : }
180 : :
181 : : public:
182 : 87129 : void assign(size_type n, const T& val) {
183 : 87129 : clear();
184 [ + + ]: 87129 : if (capacity() < n) {
185 : 48184 : change_capacity(n);
186 : : }
187 : 87129 : _size += n;
188 [ + + + + ]: 174258 : fill(item_ptr(0), n, val);
189 : 87129 : }
190 : :
191 : : template <std::input_iterator InputIterator>
192 : 349967 : void assign(InputIterator first, InputIterator last) {
193 : 349967 : size_type n = last - first;
194 : 349967 : clear();
195 [ + + ]: 349967 : if (capacity() < n) {
196 : 126310 : change_capacity(n);
197 : : }
198 [ + + ]: 349967 : _size += n;
199 [ + + ]: 405177 : fill(item_ptr(0), first, last);
200 : 349967 : }
201 : :
202 [ + - + - ]: 36570217 : prevector() = default;
[ + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- - - #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + - ]
[ + - + -
+ + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - ][ + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - + + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
203 : :
204 : : explicit prevector(size_type n) {
205 : : resize(n);
206 : : }
207 : :
208 : 60271 : explicit prevector(size_type n, const T& val) {
209 : 60271 : change_capacity(n);
210 : 60271 : _size += n;
211 [ + - + - ]: 120542 : fill(item_ptr(0), n, val);
212 : 60271 : }
213 : :
214 : : template <std::input_iterator InputIterator>
215 : 651262 : prevector(InputIterator first, InputIterator last) {
216 : 651262 : size_type n = last - first;
217 : 651262 : change_capacity(n);
218 [ + + ]: 651262 : _size += n;
219 [ + + ]: 651554 : fill(item_ptr(0), first, last);
220 : 651262 : }
221 : :
222 : 5403296 : prevector(const prevector<N, T, Size, Diff>& other) {
223 : 5403296 : size_type n = other.size();
224 : 5403296 : change_capacity(n);
225 : 5403296 : _size += n;
226 : 5403296 : fill(item_ptr(0), other.begin(), other.end());
227 : 5403296 : }
228 : :
229 : 826043 : prevector(prevector<N, T, Size, Diff>&& other) noexcept
230 [ + - ]: 826043 : : _union(std::move(other._union)), _size(other._size)
231 : : {
232 [ + - - - : 733112 : other._size = 0;
+ - ][ + + ]
[ + - + - ]
233 : : }
234 : :
235 : 348687 : prevector& operator=(const prevector<N, T, Size, Diff>& other) {
236 [ + + ]: 348687 : if (&other == this) {
237 : : return *this;
238 : : }
239 : 671156 : assign(other.begin(), other.end());
240 : 335578 : return *this;
241 : : }
242 : :
243 : 1573101 : prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
244 [ + + ]: 1573101 : if (!is_direct()) {
245 : 28632 : free(_union.indirect_contents.indirect);
246 : : }
247 : 1573101 : _union = std::move(other._union);
248 : 1573101 : _size = other._size;
249 : 1573101 : other._size = 0;
250 : 1573101 : return *this;
251 : : }
252 : :
253 : 181931564 : size_type size() const {
254 [ + + + + : 57914365 : return is_direct() ? _size : _size - N - 1;
+ + ][ + +
+ + # # #
# # # # #
# # ][ - -
+ - + + +
+ + + + +
+ + + + +
+ + + ][ -
+ + + + +
+ + + + ]
[ - - - -
- + - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - + +
- - - - -
- - - -
- ][ - - +
+ - + - -
- - - + -
- - - - +
- - - - -
- - - - -
- - # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - -
- - + - +
- - - - -
- - - - -
# # # # #
# # # # #
# # # # #
# ][ - - -
+ - - - +
- + - + -
+ - - - +
- + - + +
+ - + - -
- + - + -
- - + - +
- + - + -
+ - + - +
- + - + -
+ + - + -
+ + + + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - ][ - -
# # # # ]
[ - + + +
+ + + - +
+ + + +
+ ][ - + -
- + + - +
+ + + + +
+ + + ][ +
+ + + + +
+ + + + +
+ + + + +
+ + ][ - +
- - + + +
+ + + + +
+ + - - +
+ + + + +
+ + + + ]
[ - - - -
+ + + + #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
# # # # ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
255 : : }
256 : :
257 [ + + ]: 16944258 : bool empty() const {
[ + + + + ]
[ - - - -
+ + + + -
+ - + - +
+ - + + -
+ - + ][ #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + - +
+ + + + ]
[ # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - - +
+ + + + +
+ + - + +
+ + + - -
- - ]
258 [ + + ]: 16944258 : return size() == 0;
[ + + + + ]
[ - - - -
+ + + - -
+ - + + -
+ - + + -
+ + - ][ #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + - +
+ + - + ]
[ # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - +
- - - - -
- - - - -
- - - + -
- + + - -
+ - + - +
- - - - -
- - - - -
- - - - +
+ + + + +
+ + - + +
+ + + - -
- - ]
259 : : }
260 : :
261 [ - - - - : 13519248 : iterator begin() { return iterator(item_ptr(0)); }
- - - - +
+ + + - -
+ + + + +
+ - - + +
- - + + -
- + + - -
+ + + - -
- + + +
+ ][ + + +
- + + + -
+ + + - +
+ + - #
# ][ + + +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + + +
- + + + +
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ ][ - + +
- - + + -
- + + - -
+ + - - +
+ - ][ - +
# # # # ]
[ - + + -
+ - ]
262 [ + + + + : 25631909 : const_iterator begin() const { return const_iterator(item_ptr(0)); }
+ + ][ + +
+ + # # ]
[ + + + +
+ - + + +
+ + + ][ +
+ + + + +
+ + + + ]
[ + + # #
# # # # ]
[ + + + +
+ + + + #
# ][ - - -
- - - + -
- + - - -
- ][ + + +
+ - - + +
+ + + + +
+ + + +
+ ][ - - -
- - - + +
- - - - -
- - - - -
- - ][ - -
- - + + +
+ + - + +
- - - - +
- + + + -
+ + + - +
+ - - + -
- + + - ]
263 [ + + + + ]: 10425317 : iterator end() { return iterator(item_ptr(size())); }
264 [ + + + + ]: 25272714 : const_iterator end() const { return const_iterator(item_ptr(size())); }
265 : :
266 : 4719182 : size_t capacity() const {
267 [ + + ]: 1056797 : if (is_direct()) {
[ - - - + ]
[ + + + +
+ + ][ - -
- + + + +
+ - + -
- ]
268 : : return N;
269 : : } else {
270 : 195167 : return _union.indirect_contents.capacity;
271 : : }
272 : : }
273 : :
274 [ + + + + : 2394584 : T& operator[](size_type pos) {
+ + + + +
+ ][ - + ]
[ + - + -
+ - + - +
- + - ][ +
- + - + -
+ - - - -
- - + - +
- - - - +
- + - ]
275 [ + + ][ - + : 6898150 : return *item_ptr(pos);
- + + - -
+ + - ][ +
+ + + # #
# # ][ + +
+ + + + +
+ # # # #
# # # # ]
[ - - - -
- + + - -
+ + - ][ +
- + - + -
+ - + - +
- + - +
- ][ + - -
+ + - - +
+ - - + +
- # # #
# ][ - + +
- + + + +
+ + + + +
+ + + +
- ][ + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - + - +
+ - - + -
+ - - - -
- - + - +
- + - + -
+ - + + +
- + - ]
276 : : }
277 : :
278 [ + - ]: 496837 : const T& operator[](size_type pos) const {
279 [ + + + - : 1973385 : return *item_ptr(pos);
+ - + - +
- + - ][ +
+ + + + +
+ + + + +
+ - - - -
+ + - + +
- + + + -
+ - + - +
+ + + + +
+ + + - +
+ + + + +
- + ]
[ + + + + ]
[ - + + +
- - - - -
- - - - -
- - ][ + -
+ - + + +
+ + + + -
+ + + + +
- + + + -
+ + + - +
+ + - + +
+ - - + -
- + - + +
+ - + + +
+ + - + +
+ + + - +
+ + + + -
+ + + - ]
[ + + + -
+ + + - +
- + - + -
+ + + + +
- + - + -
+ - + - +
+ + + + -
+ - + - +
- + - + -
+ - + - +
- ][ + + +
+ + + + +
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
280 : : }
281 : :
282 [ + + ]: 36139420 : void resize(size_type new_size) {
283 : 36139420 : size_type cur_size = size();
284 [ + + ]: 36139420 : if (cur_size == new_size) {
285 : : return;
286 : : }
287 [ + + ]: 294282 : if (cur_size > new_size) {
288 [ + + ]: 375620 : erase(item_ptr(new_size), end());
289 : 187810 : return;
290 : : }
291 [ + + + + ]: 110527 : if (new_size > capacity()) {
292 : 62337 : change_capacity(new_size);
293 : : }
294 : 106472 : ptrdiff_t increase = new_size - cur_size;
295 [ + + + - ]: 212944 : fill(item_ptr(cur_size), increase);
296 : 106472 : _size += increase;
297 : : }
298 : :
299 : 4046 : void reserve(size_type new_capacity) {
300 [ + + + + ]: 6674 : if (new_capacity > capacity()) {
301 : 1760 : change_capacity(new_capacity);
302 : : }
303 : 4046 : }
304 : :
305 [ + + ]: 35558506 : void shrink_to_fit() {
306 : 35558506 : change_capacity(size());
307 : 35558506 : }
308 : :
309 : 36018105 : void clear() {
310 : 36018105 : resize(0);
311 : : }
312 : :
313 [ + + ]: 2973771 : iterator insert(iterator pos, const T& value) {
314 [ + + ]: 2973771 : size_type p = pos - begin();
315 [ + + ]: 2973771 : size_type new_size = size() + 1;
316 [ + + ]: 2973771 : if (capacity() < new_size) {
317 : 1676 : change_capacity(new_size + (new_size >> 1));
318 : : }
319 [ + + ]: 2973771 : T* ptr = item_ptr(p);
320 [ + + ]: 2973771 : T* dst = ptr + 1;
321 : 2973771 : memmove(dst, ptr, (size() - p) * sizeof(T));
322 : 2973771 : _size++;
323 : 2973771 : new(static_cast<void*>(ptr)) T(value);
324 : 2973771 : return iterator(ptr);
325 : : }
326 : :
327 [ + + ]: 16611 : void insert(iterator pos, size_type count, const T& value) {
328 [ + + ]: 16611 : size_type p = pos - begin();
329 [ + + ]: 16611 : size_type new_size = size() + count;
330 [ + + ]: 16611 : if (capacity() < new_size) {
331 : 831 : change_capacity(new_size + (new_size >> 1));
332 : : }
333 [ + + ]: 16611 : T* ptr = item_ptr(p);
334 [ + + ]: 16611 : T* dst = ptr + count;
335 [ + + ]: 16611 : memmove(dst, ptr, (size() - p) * sizeof(T));
336 : 16611 : _size += count;
337 [ + + + - ]: 33222 : fill(item_ptr(p), count, value);
338 : 16611 : }
339 : :
340 : : template <std::input_iterator InputIterator>
341 [ + + ]: 1121326 : void insert(iterator pos, InputIterator first, InputIterator last) {
342 [ + + ]: 1121326 : size_type p = pos - begin();
343 [ + + ]: 1121326 : difference_type count = last - first;
344 [ + + ]: 1121326 : size_type new_size = size() + count;
345 [ + + ]: 1121326 : if (capacity() < new_size) {
346 : 201204 : change_capacity(new_size + (new_size >> 1));
347 : : }
348 [ + + ]: 1121326 : T* ptr = item_ptr(p);
349 [ + + ]: 1121326 : T* dst = ptr + count;
350 : 1121326 : memmove(dst, ptr, (size() - p) * sizeof(T));
351 : 1121326 : _size += count;
352 : 1121326 : fill(ptr, first, last);
353 : 1121326 : }
354 : :
355 [ + + ]: 23409 : 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 [ + + ]: 23409 : if (capacity() < new_size) {
359 [ + - ]: 6159 : change_capacity(new_size);
360 : 6159 : _size += new_size - size();
361 : 6159 : return;
362 : : }
363 [ + + ]: 17250 : if (new_size < size()) {
364 [ + + ]: 6494 : erase(item_ptr(new_size), end());
365 : : } else {
366 : 14003 : _size += new_size - size();
367 : : }
368 : : }
369 : :
370 : 27952 : iterator erase(iterator pos) {
371 : 27952 : return erase(pos, pos + 1);
372 : : }
373 : :
374 : 246100 : 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 : 246100 : iterator p = first;
382 : 246100 : char* endp = (char*)&(*end());
383 : 246100 : _size -= last - p;
384 : 246100 : memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
385 : 246100 : return first;
386 : : }
387 : :
388 : : template<typename... Args>
389 [ + + ]: 32405 : void emplace_back(Args&&... args) {
390 [ + + ]: 32405 : size_type new_size = size() + 1;
391 [ + + ]: 32405 : if (capacity() < new_size) {
392 : 407 : change_capacity(new_size + (new_size >> 1));
393 : : }
394 [ + + ]: 32405 : new(item_ptr(size())) T(std::forward<Args>(args)...);
395 : 32405 : _size++;
396 : 32405 : }
397 : :
398 : 32405 : void push_back(const T& value) {
399 : 32405 : emplace_back(value);
400 : 24040 : }
401 : :
402 : 6842 : void pop_back() {
403 : 6842 : erase(end() - 1, end());
404 : 6842 : }
405 : :
406 : : T& front() {
407 : : return *item_ptr(0);
408 : : }
409 : :
410 : : const T& front() const {
411 : : return *item_ptr(0);
412 : : }
413 : :
414 : : T& back() {
415 : : return *item_ptr(size() - 1);
416 : : }
417 : :
418 [ - + + + ]: 876 : const T& back() const {
419 [ + + + + : 1691 : return *item_ptr(size() - 1);
- + + - ]
420 : : }
421 : :
422 : 16452 : void swap(prevector<N, T, Size, Diff>& other) noexcept
423 : : {
424 : 16452 : std::swap(_union, other._union);
425 : 16452 : std::swap(_size, other._size);
426 : : }
427 : :
428 : 44698578 : ~prevector() {
429 [ + + ]: 44698578 : if (!is_direct()) {
430 : 1655045 : free(_union.indirect_contents.indirect);
431 : 1655045 : _union.indirect_contents.indirect = nullptr;
432 : : }
433 : 44698578 : }
434 : :
435 [ + + ]: 992995 : bool operator==(const prevector<N, T, Size, Diff>& other) const {
436 [ + + + + ]: 1401071 : if (other.size() != size()) {
437 : : return false;
438 : : }
439 [ + + ]: 992961 : const_iterator b1 = begin();
440 : 992961 : const_iterator b2 = other.begin();
441 : 992961 : const_iterator e1 = end();
442 [ + + ]: 17243733 : while (b1 != e1) {
443 [ + + ]: 16272806 : if ((*b1) != (*b2)) {
444 : : return false;
445 : : }
446 : 16250772 : ++b1;
447 : 16250772 : ++b2;
448 : : }
449 : : return true;
450 : : }
451 : :
452 : 921 : bool operator!=(const prevector<N, T, Size, Diff>& other) const {
453 : 921 : return !(*this == other);
454 : : }
455 : :
456 [ + + ]: 7398679 : bool operator<(const prevector<N, T, Size, Diff>& other) const {
457 [ + + + + ]: 9172319 : if (size() < other.size()) {
458 : : return true;
459 : : }
460 [ + + ]: 7395451 : if (size() > other.size()) {
461 : : return false;
462 : : }
463 [ + + ]: 7395245 : const_iterator b1 = begin();
464 : 7395245 : const_iterator b2 = other.begin();
465 : 7395245 : const_iterator e1 = end();
466 [ + + ]: 26554527 : while (b1 != e1) {
467 [ + + ]: 26512281 : if ((*b1) < (*b2)) {
468 : : return true;
469 : : }
470 [ + + ]: 22444994 : if ((*b2) < (*b1)) {
471 : : return false;
472 : : }
473 : 19159282 : ++b1;
474 : 19159282 : ++b2;
475 : : }
476 : : return false;
477 : : }
478 : :
479 : 2471964 : size_t allocated_memory() const {
480 [ + + + + ]: 2471964 : if (is_direct()) {
[ - + + +
- + ][ # # ]
[ + + + +
+ + + + +
+ + + + +
+ - + + +
+ + + -
- ]
481 : : return 0;
482 : : } else {
483 [ + - + - ]: 1119247 : return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
[ - - + -
- - ][ # # ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - -
- ]
484 : : }
485 : : }
486 : :
487 [ + + # # : 93988 : value_type* data() {
# # ]
[ + + + + ]
[ + - - -
+ + + - ]
[ + + + +
+ + + + -
+ - + ][ +
+ - + - +
# # # # #
# ][ + - -
+ - + + -
+ - - + -
+ - + - +
- + ][ + -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - -
- ][ - + -
+ - + - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # ]
488 [ + + # # : 93987 : return item_ptr(0);
# # ]
[ + + + + ]
[ + + + +
+ + + + +
+ + + ][ +
+ + - + -
# # # # #
# ][ - + +
- + - + -
- + + - -
+ + - - +
+ - ][ - +
- - - - -
- - + - -
- - - - ]
[ - + - +
- + - - -
- # # # #
# # ]
489 : : }
490 : :
491 [ + + # # : 16144673 : const value_type* data() const {
# # # # ]
[ + + + +
+ + + + ]
[ + + + + ]
[ - - + +
- - - - -
- # # ][ #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # ][ + -
+ - + - +
- + - + -
+ - + - -
+ - + + +
+ + + + +
- + - - -
- - - - ]
492 [ + + ]: 16144932 : return item_ptr(0);
[ + + + + ]
[ + - + +
+ + + + ]
[ + - - -
- + ][ - +
- + - + -
+ - + + -
+ - + + +
+ + + + +
+ + ]
493 : : }
494 : : };
495 : :
496 : : #endif // BITCOIN_PREVECTOR_H
|