1 #ifndef _BITCOIN_PREVECTOR_H_
2 #define _BITCOIN_PREVECTOR_H_
14 /** Implements a drop-in replacement for std::vector<T> which stores up to N
15 * elements directly (without heap allocation). The types Size and Diff are
16 * used to store element counts, and can be any unsigned + signed type.
18 * Storage layout is either:
19 * - Direct allocation:
20 * - Size _size: the number of used elements (between 0 and N)
21 * - T direct[N]: an array of N elements of type T
22 * (only the first _size are initialized).
23 * - Indirect allocation:
24 * - Size _size: the number of used elements plus N + 1
25 * - Size capacity: the number of allocated elements
26 * - T* indirect: a pointer to an array of capacity elements of type T
27 * (only the first _size are initialized).
29 * The data type T must be movable by memmove/realloc(). Once we switch to C++,
30 * move constructors can be used instead.
32 template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
35 typedef Size size_type;
36 typedef Diff difference_type;
38 typedef value_type& reference;
39 typedef const value_type& const_reference;
40 typedef value_type* pointer;
41 typedef const value_type* const_pointer;
46 typedef Diff difference_type;
50 typedef std::random_access_iterator_tag iterator_category;
51 iterator(T* ptr_) : ptr(ptr_) {}
52 T& operator*() const { return *ptr; }
53 T* operator->() const { return ptr; }
54 T& operator[](size_type pos) { return ptr[pos]; }
55 const T& operator[](size_type pos) const { return ptr[pos]; }
56 iterator& operator++() { ptr++; return *this; }
57 iterator& operator--() { ptr--; return *this; }
58 iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
59 iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
60 difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
61 iterator operator+(size_type n) { return iterator(ptr + n); }
62 iterator& operator+=(size_type n) { ptr += n; return *this; }
63 iterator operator-(size_type n) { return iterator(ptr - n); }
64 iterator& operator-=(size_type n) { ptr -= n; return *this; }
65 bool operator==(iterator x) const { return ptr == x.ptr; }
66 bool operator!=(iterator x) const { return ptr != x.ptr; }
67 bool operator>=(iterator x) const { return ptr >= x.ptr; }
68 bool operator<=(iterator x) const { return ptr <= x.ptr; }
69 bool operator>(iterator x) const { return ptr > x.ptr; }
70 bool operator<(iterator x) const { return ptr < x.ptr; }
73 class reverse_iterator {
76 typedef Diff difference_type;
80 typedef std::bidirectional_iterator_tag iterator_category;
81 reverse_iterator(T* ptr_) : ptr(ptr_) {}
82 T& operator*() { return *ptr; }
83 const T& operator*() const { return *ptr; }
84 T* operator->() { return ptr; }
85 const T* operator->() const { return ptr; }
86 reverse_iterator& operator--() { ptr++; return *this; }
87 reverse_iterator& operator++() { ptr--; return *this; }
88 reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; }
89 reverse_iterator operator--(int) { reverse_iterator copy(*this); --(*this); return copy; }
90 bool operator==(reverse_iterator x) const { return ptr == x.ptr; }
91 bool operator!=(reverse_iterator x) const { return ptr != x.ptr; }
94 class const_iterator {
97 typedef Diff difference_type;
98 typedef const T value_type;
99 typedef const T* pointer;
100 typedef const T& reference;
101 typedef std::random_access_iterator_tag iterator_category;
102 const_iterator(const T* ptr_) : ptr(ptr_) {}
103 const_iterator(iterator x) : ptr(&(*x)) {}
104 const T& operator*() const { return *ptr; }
105 const T* operator->() const { return ptr; }
106 const T& operator[](size_type pos) const { return ptr[pos]; }
107 const_iterator& operator++() { ptr++; return *this; }
108 const_iterator& operator--() { ptr--; return *this; }
109 const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
110 const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
111 difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
112 const_iterator operator+(size_type n) { return const_iterator(ptr + n); }
113 const_iterator& operator+=(size_type n) { ptr += n; return *this; }
114 const_iterator operator-(size_type n) { return const_iterator(ptr - n); }
115 const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
116 bool operator==(const_iterator x) const { return ptr == x.ptr; }
117 bool operator!=(const_iterator x) const { return ptr != x.ptr; }
118 bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
119 bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
120 bool operator>(const_iterator x) const { return ptr > x.ptr; }
121 bool operator<(const_iterator x) const { return ptr < x.ptr; }
124 class const_reverse_iterator {
127 typedef Diff difference_type;
128 typedef const T value_type;
129 typedef const T* pointer;
130 typedef const T& reference;
131 typedef std::bidirectional_iterator_tag iterator_category;
132 const_reverse_iterator(T* ptr_) : ptr(ptr_) {}
133 const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
134 const T& operator*() const { return *ptr; }
135 const T* operator->() const { return ptr; }
136 const_reverse_iterator& operator--() { ptr++; return *this; }
137 const_reverse_iterator& operator++() { ptr--; return *this; }
138 const_reverse_iterator operator++(int) { const_reverse_iterator copy(*this); ++(*this); return copy; }
139 const_reverse_iterator operator--(int) { const_reverse_iterator copy(*this); --(*this); return copy; }
140 bool operator==(const_reverse_iterator x) const { return ptr == x.ptr; }
141 bool operator!=(const_reverse_iterator x) const { return ptr != x.ptr; }
147 char direct[sizeof(T) * N];
154 T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
155 const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
156 T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect) + pos; }
157 const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect) + pos; }
158 bool is_direct() const { return _size <= N; }
160 void change_capacity(size_type new_capacity) {
161 if (new_capacity <= N) {
163 T* indirect = indirect_ptr(0);
165 T* dst = direct_ptr(0);
166 memcpy(dst, src, size() * sizeof(T));
172 /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
173 success. These should instead use an allocator or new/delete so that handlers
174 are called as necessary, but performance would be slightly degraded by doing so. */
175 _union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
176 if (!_union.indirect) { new_handler_terminate(); }
177 _union.capacity = new_capacity;
179 char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
180 if (!new_indirect) { new_handler_terminate(); }
181 T* src = direct_ptr(0);
182 T* dst = reinterpret_cast<T*>(new_indirect);
183 memcpy(dst, src, size() * sizeof(T));
184 _union.indirect = new_indirect;
185 _union.capacity = new_capacity;
191 T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
192 const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
195 void assign(size_type n, const T& val) {
197 if (capacity() < n) {
202 new(static_cast<void*>(item_ptr(size() - 1))) T(val);
206 template<typename InputIterator>
207 void assign(InputIterator first, InputIterator last) {
208 size_type n = last - first;
210 if (capacity() < n) {
213 while (first != last) {
215 new(static_cast<void*>(item_ptr(size() - 1))) T(*first);
220 prevector() : _size(0) {}
222 explicit prevector(size_type n) : _size(0) {
226 explicit prevector(size_type n, const T& val = T()) : _size(0) {
230 new(static_cast<void*>(item_ptr(size() - 1))) T(val);
234 template<typename InputIterator>
235 prevector(InputIterator first, InputIterator last) : _size(0) {
236 size_type n = last - first;
238 while (first != last) {
240 new(static_cast<void*>(item_ptr(size() - 1))) T(*first);
245 prevector(const prevector<N, T, Size, Diff>& other) : _size(0) {
246 change_capacity(other.size());
247 const_iterator it = other.begin();
248 while (it != other.end()) {
250 new(static_cast<void*>(item_ptr(size() - 1))) T(*it);
255 prevector& operator=(const prevector<N, T, Size, Diff>& other) {
256 if (&other == this) {
260 change_capacity(other.size());
261 const_iterator it = other.begin();
262 while (it != other.end()) {
264 new(static_cast<void*>(item_ptr(size() - 1))) T(*it);
270 size_type size() const {
271 return is_direct() ? _size : _size - N - 1;
278 iterator begin() { return iterator(item_ptr(0)); }
279 const_iterator begin() const { return const_iterator(item_ptr(0)); }
280 iterator end() { return iterator(item_ptr(size())); }
281 const_iterator end() const { return const_iterator(item_ptr(size())); }
283 reverse_iterator rbegin() { return reverse_iterator(item_ptr(size() - 1)); }
284 const_reverse_iterator rbegin() const { return const_reverse_iterator(item_ptr(size() - 1)); }
285 reverse_iterator rend() { return reverse_iterator(item_ptr(-1)); }
286 const_reverse_iterator rend() const { return const_reverse_iterator(item_ptr(-1)); }
288 size_t capacity() const {
292 return _union.capacity;
296 T& operator[](size_type pos) {
297 return *item_ptr(pos);
300 const T& operator[](size_type pos) const {
301 return *item_ptr(pos);
304 void resize(size_type new_size) {
305 while (size() > new_size) {
306 item_ptr(size() - 1)->~T();
309 if (new_size > capacity()) {
310 change_capacity(new_size);
312 while (size() < new_size) {
314 new(static_cast<void*>(item_ptr(size() - 1))) T();
318 void reserve(size_type new_capacity) {
319 if (new_capacity > capacity()) {
320 change_capacity(new_capacity);
324 void shrink_to_fit() {
325 change_capacity(size());
332 iterator insert(iterator pos, const T& value) {
333 size_type p = pos - begin();
334 size_type new_size = size() + 1;
335 if (capacity() < new_size) {
336 change_capacity(new_size + (new_size >> 1));
338 memmove(item_ptr(p + 1), item_ptr(p), (size() - p) * sizeof(T));
340 new(static_cast<void*>(item_ptr(p))) T(value);
341 return iterator(item_ptr(p));
344 void insert(iterator pos, size_type count, const T& value) {
345 size_type p = pos - begin();
346 size_type new_size = size() + count;
347 if (capacity() < new_size) {
348 change_capacity(new_size + (new_size >> 1));
350 memmove(item_ptr(p + count), item_ptr(p), (size() - p) * sizeof(T));
352 for (size_type i = 0; i < count; i++) {
353 new(static_cast<void*>(item_ptr(p + i))) T(value);
357 template<typename InputIterator>
358 void insert(iterator pos, InputIterator first, InputIterator last) {
359 size_type p = pos - begin();
360 difference_type count = last - first;
361 size_type new_size = size() + count;
362 if (capacity() < new_size) {
363 change_capacity(new_size + (new_size >> 1));
365 memmove(item_ptr(p + count), item_ptr(p), (size() - p) * sizeof(T));
367 while (first != last) {
368 new(static_cast<void*>(item_ptr(p))) T(*first);
374 iterator erase(iterator pos) {
376 memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
381 iterator erase(iterator first, iterator last) {
383 char* endp = (char*)&(*end());
389 memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
393 void push_back(const T& value) {
394 size_type new_size = size() + 1;
395 if (capacity() < new_size) {
396 change_capacity(new_size + (new_size >> 1));
398 new(item_ptr(size())) T(value);
410 const T& front() const {
415 return *item_ptr(size() - 1);
418 const T& back() const {
419 return *item_ptr(size() - 1);
422 void swap(prevector<N, T, Size, Diff>& other) {
423 if (_size & other._size & 1) {
424 std::swap(_union.capacity, other._union.capacity);
425 std::swap(_union.indirect, other._union.indirect);
427 std::swap(_union, other._union);
429 std::swap(_size, other._size);
435 free(_union.indirect);
436 _union.indirect = NULL;
440 bool operator==(const prevector<N, T, Size, Diff>& other) const {
441 if (other.size() != size()) {
444 const_iterator b1 = begin();
445 const_iterator b2 = other.begin();
446 const_iterator e1 = end();
448 if ((*b1) != (*b2)) {
457 bool operator!=(const prevector<N, T, Size, Diff>& other) const {
458 return !(*this == other);
461 bool operator<(const prevector<N, T, Size, Diff>& other) const {
462 if (size() < other.size()) {
465 if (size() > other.size()) {
468 const_iterator b1 = begin();
469 const_iterator b2 = other.begin();
470 const_iterator e1 = end();
484 size_t allocated_memory() const {
488 return ((size_t)(sizeof(T))) * _union.capacity;