LCOV - code coverage report
Current view: top level - src - prevector.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 214 287 74.6 %
Date: 2023-11-10 23:46:46 Functions: 68 237 28.7 %
Branches: 57 140 40.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2015-2022 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 <assert.h>
       9                 :            : #include <cstdlib>
      10                 :            : #include <stdint.h>
      11                 :            : #include <string.h>
      12                 :            : 
      13                 :            : #include <algorithm>
      14                 :            : #include <cstddef>
      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 value_type;
      54                 :            :         typedef T* pointer;
      55                 :            :         typedef T& reference;
      56                 :            :         typedef std::random_access_iterator_tag iterator_category;
      57                 :     799943 :         iterator(T* ptr_) : ptr(ptr_) {}
      58                 :     641610 :         T& operator*() const { return *ptr; }
      59                 :            :         T* operator->() const { return ptr; }
      60                 :            :         T& operator[](size_type pos) { return ptr[pos]; }
      61                 :            :         const T& operator[](size_type pos) const { return ptr[pos]; }
      62                 :      12014 :         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                 :     296542 :         difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
      67                 :          0 :         iterator operator+(size_type n) { return iterator(ptr + n); }
      68                 :            :         iterator& operator+=(size_type n) { ptr += n; return *this; }
      69                 :          0 :         iterator operator-(size_type n) { return iterator(ptr - n); }
      70                 :            :         iterator& operator-=(size_type n) { ptr -= n; return *this; }
      71                 :            :         bool operator==(iterator x) const { return ptr == x.ptr; }
      72                 :        914 :         bool operator!=(iterator x) const { return ptr != x.ptr; }
      73                 :            :         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                 :            :     };
      78                 :            : 
      79                 :            :     class reverse_iterator {
      80                 :            :         T* ptr;
      81                 :            :     public:
      82                 :            :         typedef Diff difference_type;
      83                 :            :         typedef T value_type;
      84                 :            :         typedef T* pointer;
      85                 :            :         typedef T& reference;
      86                 :            :         typedef std::bidirectional_iterator_tag iterator_category;
      87                 :            :         reverse_iterator(T* ptr_) : ptr(ptr_) {}
      88                 :            :         T& operator*() { return *ptr; }
      89                 :            :         const T& operator*() const { return *ptr; }
      90                 :            :         T* operator->() { return ptr; }
      91                 :            :         const T* operator->() const { return ptr; }
      92                 :            :         reverse_iterator& operator--() { ptr++; return *this; }
      93                 :            :         reverse_iterator& operator++() { ptr--; return *this; }
      94                 :            :         reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; }
      95                 :            :         reverse_iterator operator--(int) { reverse_iterator copy(*this); --(*this); return copy; }
      96                 :            :         bool operator==(reverse_iterator x) const { return ptr == x.ptr; }
      97                 :            :         bool operator!=(reverse_iterator x) const { return ptr != x.ptr; }
      98                 :            :     };
      99                 :            : 
     100                 :            :     class const_iterator {
     101                 :            :         const T* ptr;
     102                 :            :     public:
     103                 :            :         typedef Diff difference_type;
     104                 :            :         typedef const T value_type;
     105                 :            :         typedef const T* pointer;
     106                 :            :         typedef const T& reference;
     107                 :            :         typedef std::random_access_iterator_tag iterator_category;
     108                 :   17223882 :         const_iterator(const T* ptr_) : ptr(ptr_) {}
     109                 :      36168 :         const_iterator(iterator x) : ptr(&(*x)) {}
     110                 :  412807789 :         const T& operator*() const { return *ptr; }
     111                 :            :         const T* operator->() const { return ptr; }
     112                 :      18794 :         const T& operator[](size_type pos) const { return ptr[pos]; }
     113                 :  249296302 :         const_iterator& operator++() { ptr++; return *this; }
     114                 :          0 :         const_iterator& operator--() { ptr--; return *this; }
     115                 :     349040 :         const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
     116                 :            :         const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
     117                 :    3019712 :         difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
     118                 :    2391300 :         const_iterator operator+(size_type n) { return const_iterator(ptr + n); }
     119                 :     181231 :         const_iterator& operator+=(size_type n) { ptr += n; return *this; }
     120                 :          0 :         const_iterator operator-(size_type n) { return const_iterator(ptr - n); }
     121                 :            :         const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
     122                 :          0 :         bool operator==(const_iterator x) const { return ptr == x.ptr; }
     123                 :  145221785 :         bool operator!=(const_iterator x) const { return ptr != x.ptr; }
     124                 :     396778 :         bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
     125                 :            :         bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
     126                 :            :         bool operator>(const_iterator x) const { return ptr > x.ptr; }
     127                 :     431674 :         bool operator<(const_iterator x) const { return ptr < x.ptr; }
     128                 :            :     };
     129                 :            : 
     130                 :            :     class const_reverse_iterator {
     131                 :            :         const T* ptr;
     132                 :            :     public:
     133                 :            :         typedef Diff difference_type;
     134                 :            :         typedef const T value_type;
     135                 :            :         typedef const T* pointer;
     136                 :            :         typedef const T& reference;
     137                 :            :         typedef std::bidirectional_iterator_tag iterator_category;
     138                 :          0 :         const_reverse_iterator(const T* ptr_) : ptr(ptr_) {}
     139                 :            :         const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
     140                 :          0 :         const T& operator*() const { return *ptr; }
     141                 :            :         const T* operator->() const { return ptr; }
     142                 :            :         const_reverse_iterator& operator--() { ptr++; return *this; }
     143                 :          0 :         const_reverse_iterator& operator++() { ptr--; return *this; }
     144                 :            :         const_reverse_iterator operator++(int) { const_reverse_iterator copy(*this); ++(*this); return copy; }
     145                 :            :         const_reverse_iterator operator--(int) { const_reverse_iterator copy(*this); --(*this); return copy; }
     146                 :            :         bool operator==(const_reverse_iterator x) const { return ptr == x.ptr; }
     147                 :          0 :         bool operator!=(const_reverse_iterator x) const { return ptr != x.ptr; }
     148                 :            :     };
     149                 :            : 
     150                 :            : private:
     151                 :            : #pragma pack(push, 1)
     152                 :            :     union direct_or_indirect {
     153                 :            :         char direct[sizeof(T) * N];
     154                 :            :         struct {
     155                 :            :             char* indirect;
     156                 :            :             size_type capacity;
     157                 :            :         } indirect_contents;
     158                 :            :     };
     159                 :            : #pragma pack(pop)
     160                 :    4529024 :     alignas(char*) direct_or_indirect _union = {};
     161                 :    4529024 :     size_type _size = 0;
     162                 :            : 
     163                 :            :     static_assert(alignof(char*) % alignof(size_type) == 0 && sizeof(char*) % alignof(size_type) == 0, "size_type cannot have more restrictive alignment requirement than pointer");
     164                 :            :     static_assert(alignof(char*) % alignof(T) == 0, "value_type T cannot have more restrictive alignment requirement than pointer");
     165                 :            : 
     166                 :    2900636 :     T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
     167                 :    3029654 :     const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
     168                 :    1588274 :     T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
     169                 :   16101689 :     const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
     170                 :  119729254 :     bool is_direct() const { return _size <= N; }
     171                 :            : 
     172                 :    2251208 :     void change_capacity(size_type new_capacity) {
     173 [ +  + ][ #  # ]:    2251208 :         if (new_capacity <= N) {
                 [ #  # ]
     174 [ +  - ][ #  # ]:     888647 :             if (!is_direct()) {
                 [ #  # ]
     175                 :          0 :                 T* indirect = indirect_ptr(0);
     176                 :          0 :                 T* src = indirect;
     177                 :          0 :                 T* dst = direct_ptr(0);
     178                 :          0 :                 memcpy(dst, src, size() * sizeof(T));
     179                 :          0 :                 free(indirect);
     180                 :          0 :                 _size -= N + 1;
     181                 :          0 :             }
     182                 :     888647 :         } else {
     183 [ +  + ][ #  # ]:    1362561 :             if (!is_direct()) {
                 [ #  # ]
     184                 :            :                 /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
     185                 :            :                     success. These should instead use an allocator or new/delete so that handlers
     186                 :            :                     are called as necessary, but performance would be slightly degraded by doing so. */
     187                 :       1148 :                 _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
     188 [ +  - ][ #  # ]:       1148 :                 assert(_union.indirect_contents.indirect);
                 [ #  # ]
     189                 :       1148 :                 _union.indirect_contents.capacity = new_capacity;
     190                 :       1148 :             } else {
     191                 :    1361413 :                 char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
     192 [ +  - ][ #  # ]:    1361413 :                 assert(new_indirect);
                 [ #  # ]
     193                 :    1361413 :                 T* src = direct_ptr(0);
     194                 :    1361413 :                 T* dst = reinterpret_cast<T*>(new_indirect);
     195                 :    1361413 :                 memcpy(dst, src, size() * sizeof(T));
     196                 :    1361413 :                 _union.indirect_contents.indirect = new_indirect;
     197                 :    1361413 :                 _union.indirect_contents.capacity = new_capacity;
     198                 :    1361413 :                 _size += N + 1;
     199                 :            :             }
     200                 :            :         }
     201                 :    2251208 :     }
     202                 :            : 
     203 [ +  + ][ #  # ]:    3127497 :     T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
                 [ #  # ]
     204 [ +  + ][ #  # ]:   19131343 :     const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
     205                 :            : 
     206                 :        316 :     void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
     207                 :        316 :         std::fill_n(dst, count, value);
     208                 :        316 :     }
     209                 :            : 
     210                 :            :     template<typename InputIterator>
     211                 :    2318791 :     void fill(T* dst, InputIterator first, InputIterator last) {
     212 [ +  + ][ #  # ]:   68380203 :         while (first != last) {
         [ #  # ][ +  + ]
         [ #  # ][ #  # ]
     213                 :   66061412 :             new(static_cast<void*>(dst)) T(*first);
     214                 :   66061412 :             ++dst;
     215                 :   66061412 :             ++first;
     216                 :            :         }
     217                 :    2318791 :     }
     218                 :            : 
     219                 :            : public:
     220                 :          0 :     void assign(size_type n, const T& val) {
     221                 :          0 :         clear();
     222         [ #  # ]:          0 :         if (capacity() < n) {
     223                 :          0 :             change_capacity(n);
     224                 :          0 :         }
     225                 :          0 :         _size += n;
     226                 :          0 :         fill(item_ptr(0), n, val);
     227                 :          0 :     }
     228                 :            : 
     229                 :            :     template<typename InputIterator>
     230                 :     376187 :     void assign(InputIterator first, InputIterator last) {
     231                 :     376187 :         size_type n = last - first;
     232                 :     376187 :         clear();
     233 [ +  + ][ #  # ]:     376187 :         if (capacity() < n) {
         [ #  # ][ #  # ]
     234                 :     208114 :             change_capacity(n);
     235                 :     208114 :         }
     236                 :     376187 :         _size += n;
     237                 :     376187 :         fill(item_ptr(0), first, last);
     238                 :     376187 :     }
     239                 :            : 
     240                 :    5423310 :     prevector() {}
     241                 :            : 
     242                 :            :     explicit prevector(size_type n) {
     243                 :            :         resize(n);
     244                 :            :     }
     245                 :            : 
     246                 :         16 :     explicit prevector(size_type n, const T& val) {
     247                 :         16 :         change_capacity(n);
     248                 :         16 :         _size += n;
     249                 :         16 :         fill(item_ptr(0), n, val);
     250                 :         16 :     }
     251                 :            : 
     252                 :            :     template<typename InputIterator>
     253                 :      12056 :     prevector(InputIterator first, InputIterator last) {
     254                 :      12056 :         size_type n = last - first;
     255                 :      12056 :         change_capacity(n);
     256                 :      12056 :         _size += n;
     257                 :      12056 :         fill(item_ptr(0), first, last);
     258                 :      12056 :     }
     259                 :            : 
     260                 :    1805297 :     prevector(const prevector<N, T, Size, Diff>& other) {
     261                 :    1805297 :         size_type n = other.size();
     262                 :    1805297 :         change_capacity(n);
     263                 :    1805297 :         _size += n;
     264                 :    1805297 :         fill(item_ptr(0), other.begin(),  other.end());
     265                 :    1805297 :     }
     266                 :            : 
     267                 :     628964 :     prevector(prevector<N, T, Size, Diff>&& other) noexcept
     268                 :     628964 :         : _union(std::move(other._union)), _size(other._size)
     269                 :            :     {
     270                 :     628964 :         other._size = 0;
     271                 :     628964 :     }
     272                 :            : 
     273                 :     376187 :     prevector& operator=(const prevector<N, T, Size, Diff>& other) {
     274 [ -  + ][ #  # ]:     376187 :         if (&other == this) {
     275                 :          0 :             return *this;
     276                 :            :         }
     277                 :     376187 :         assign(other.begin(), other.end());
     278                 :     376187 :         return *this;
     279                 :     376187 :     }
     280                 :            : 
     281                 :     114843 :     prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
     282 [ +  + ][ #  # ]:     114843 :         if (!is_direct()) {
     283                 :        580 :             free(_union.indirect_contents.indirect);
     284                 :        580 :         }
     285                 :     114843 :         _union = std::move(other._union);
     286                 :     114843 :         _size = other._size;
     287                 :     114843 :         other._size = 0;
     288                 :     114843 :         return *this;
     289                 :            :     }
     290                 :            : 
     291                 :   89214206 :     size_type size() const {
     292 [ +  + ][ #  # ]:   89214206 :         return is_direct() ? _size : _size - N - 1;
                 [ #  # ]
     293                 :            :     }
     294                 :            : 
     295                 :    1103754 :     bool empty() const {
     296                 :    1103754 :         return size() == 0;
     297                 :            :     }
     298                 :            : 
     299                 :     320568 :     iterator begin() { return iterator(item_ptr(0)); }
     300                 :    9383220 :     const_iterator begin() const { return const_iterator(item_ptr(0)); }
     301                 :     308684 :     iterator end() { return iterator(item_ptr(size())); }
     302                 :    5449362 :     const_iterator end() const { return const_iterator(item_ptr(size())); }
     303                 :            : 
     304                 :            :     reverse_iterator rbegin() { return reverse_iterator(item_ptr(size() - 1)); }
     305                 :          0 :     const_reverse_iterator rbegin() const { return const_reverse_iterator(item_ptr(size() - 1)); }
     306                 :            :     reverse_iterator rend() { return reverse_iterator(item_ptr(-1)); }
     307                 :          0 :     const_reverse_iterator rend() const { return const_reverse_iterator(item_ptr(-1)); }
     308                 :            : 
     309                 :     678690 :     size_t capacity() const {
     310 [ +  + ][ #  # ]:     678690 :         if (is_direct()) {
     311                 :     614152 :             return N;
     312                 :            :         } else {
     313                 :      64538 :             return _union.indirect_contents.capacity;
     314                 :            :         }
     315                 :     678690 :     }
     316                 :            : 
     317                 :       5519 :     T& operator[](size_type pos) {
     318                 :       5519 :         return *item_ptr(pos);
     319                 :            :     }
     320                 :            : 
     321                 :    3305166 :     const T& operator[](size_type pos) const {
     322                 :    3305166 :         return *item_ptr(pos);
     323                 :            :     }
     324                 :            : 
     325                 :     546913 :     void resize(size_type new_size) {
     326                 :     546913 :         size_type cur_size = size();
     327 [ +  + ][ #  # ]:     546913 :         if (cur_size == new_size) {
     328                 :     546527 :             return;
     329                 :            :         }
     330 [ +  + ][ #  # ]:        386 :         if (cur_size > new_size) {
     331                 :         86 :             erase(item_ptr(new_size), end());
     332                 :         86 :             return;
     333                 :            :         }
     334 [ -  + ][ #  # ]:        300 :         if (new_size > capacity()) {
     335                 :        300 :             change_capacity(new_size);
     336                 :        300 :         }
     337                 :        300 :         ptrdiff_t increase = new_size - cur_size;
     338                 :        300 :         fill(item_ptr(cur_size), increase);
     339                 :        300 :         _size += increase;
     340                 :     546913 :     }
     341                 :            : 
     342                 :          0 :     void reserve(size_type new_capacity) {
     343         [ #  # ]:          0 :         if (new_capacity > capacity()) {
     344                 :          0 :             change_capacity(new_capacity);
     345                 :          0 :         }
     346                 :          0 :     }
     347                 :            : 
     348                 :     166670 :     void shrink_to_fit() {
     349                 :     166670 :         change_capacity(size());
     350                 :     166670 :     }
     351                 :            : 
     352                 :     546613 :     void clear() {
     353                 :     546613 :         resize(0);
     354                 :     546613 :     }
     355                 :            : 
     356                 :     170605 :     iterator insert(iterator pos, const T& value) {
     357                 :     170605 :         size_type p = pos - begin();
     358                 :     170605 :         size_type new_size = size() + 1;
     359         [ +  + ]:     170605 :         if (capacity() < new_size) {
     360                 :        275 :             change_capacity(new_size + (new_size >> 1));
     361                 :        275 :         }
     362                 :     170605 :         T* ptr = item_ptr(p);
     363                 :     170605 :         memmove(ptr + 1, ptr, (size() - p) * sizeof(T));
     364                 :     170605 :         _size++;
     365                 :     170605 :         new(static_cast<void*>(ptr)) T(value);
     366                 :     170605 :         return iterator(ptr);
     367                 :            :     }
     368                 :            : 
     369                 :          0 :     void insert(iterator pos, size_type count, const T& value) {
     370                 :          0 :         size_type p = pos - begin();
     371                 :          0 :         size_type new_size = size() + count;
     372         [ #  # ]:          0 :         if (capacity() < new_size) {
     373                 :          0 :             change_capacity(new_size + (new_size >> 1));
     374                 :          0 :         }
     375                 :          0 :         T* ptr = item_ptr(p);
     376                 :          0 :         memmove(ptr + count, ptr, (size() - p) * sizeof(T));
     377                 :          0 :         _size += count;
     378                 :          0 :         fill(item_ptr(p), count, value);
     379                 :          0 :     }
     380                 :            : 
     381                 :            :     template<typename InputIterator>
     382                 :     125251 :     void insert(iterator pos, InputIterator first, InputIterator last) {
     383                 :     125251 :         size_type p = pos - begin();
     384                 :     125251 :         difference_type count = last - first;
     385                 :     125251 :         size_type new_size = size() + count;
     386 [ +  + ][ +  + ]:     125251 :         if (capacity() < new_size) {
                 [ +  + ]
     387                 :      57829 :             change_capacity(new_size + (new_size >> 1));
     388                 :      57829 :         }
     389                 :     125251 :         T* ptr = item_ptr(p);
     390                 :     125251 :         memmove(ptr + count, ptr, (size() - p) * sizeof(T));
     391                 :     125251 :         _size += count;
     392                 :     125251 :         fill(ptr, first, last);
     393                 :     125251 :     }
     394                 :            : 
     395                 :       3419 :     inline void resize_uninitialized(size_type new_size) {
     396                 :            :         // resize_uninitialized changes the size of the prevector but does not initialize it.
     397                 :            :         // If size < new_size, the added elements must be initialized explicitly.
     398         [ +  + ]:       3419 :         if (capacity() < new_size) {
     399                 :        629 :             change_capacity(new_size);
     400                 :        629 :             _size += new_size - size();
     401                 :        629 :             return;
     402                 :            :         }
     403         [ -  + ]:       2790 :         if (new_size < size()) {
     404                 :          0 :             erase(item_ptr(new_size), end());
     405                 :          0 :         } else {
     406                 :       2790 :             _size += new_size - size();
     407                 :            :         }
     408                 :       3419 :     }
     409                 :            : 
     410                 :          0 :     iterator erase(iterator pos) {
     411                 :          0 :         return erase(pos, pos + 1);
     412                 :            :     }
     413                 :            : 
     414                 :         86 :     iterator erase(iterator first, iterator last) {
     415                 :            :         // Erase is not allowed to the change the object's capacity. That means
     416                 :            :         // that when starting with an indirectly allocated prevector with
     417                 :            :         // size and capacity > N, the result may be a still indirectly allocated
     418                 :            :         // prevector with size <= N and capacity > N. A shrink_to_fit() call is
     419                 :            :         // necessary to switch to the (more efficient) directly allocated
     420                 :            :         // representation (with capacity N and size <= N).
     421                 :         86 :         iterator p = first;
     422                 :         86 :         char* endp = (char*)&(*end());
     423                 :         86 :         _size -= last - p;
     424                 :         86 :         memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
     425                 :         86 :         return first;
     426                 :            :     }
     427                 :            : 
     428                 :            :     template<typename... Args>
     429                 :       2928 :     void emplace_back(Args&&... args) {
     430                 :       2928 :         size_type new_size = size() + 1;
     431         [ +  + ]:       2928 :         if (capacity() < new_size) {
     432                 :         22 :             change_capacity(new_size + (new_size >> 1));
     433                 :         22 :         }
     434                 :       2928 :         new(item_ptr(size())) T(std::forward<Args>(args)...);
     435                 :       2928 :         _size++;
     436                 :       2928 :     }
     437                 :            : 
     438                 :       2928 :     void push_back(const T& value) {
     439                 :       2928 :         emplace_back(value);
     440                 :       2928 :     }
     441                 :            : 
     442                 :          0 :     void pop_back() {
     443                 :          0 :         erase(end() - 1, end());
     444                 :          0 :     }
     445                 :            : 
     446                 :            :     T& front() {
     447                 :            :         return *item_ptr(0);
     448                 :            :     }
     449                 :            : 
     450                 :            :     const T& front() const {
     451                 :            :         return *item_ptr(0);
     452                 :            :     }
     453                 :            : 
     454                 :          0 :     T& back() {
     455                 :          0 :         return *item_ptr(size() - 1);
     456                 :            :     }
     457                 :            : 
     458                 :     755044 :     const T& back() const {
     459                 :     755044 :         return *item_ptr(size() - 1);
     460                 :            :     }
     461                 :            : 
     462                 :          0 :     void swap(prevector<N, T, Size, Diff>& other) noexcept
     463                 :            :     {
     464                 :          0 :         std::swap(_union, other._union);
     465                 :          0 :         std::swap(_size, other._size);
     466                 :          0 :     }
     467                 :            : 
     468                 :    5157988 :     ~prevector() {
     469 [ +  - ][ +  + ]:    5157988 :         if (!is_direct()) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     470                 :    1360833 :             free(_union.indirect_contents.indirect);
     471                 :    1360833 :             _union.indirect_contents.indirect = nullptr;
     472                 :    1360833 :         }
     473                 :    5157988 :     }
     474                 :            : 
     475                 :          0 :     bool operator==(const prevector<N, T, Size, Diff>& other) const {
     476         [ #  # ]:          0 :         if (other.size() != size()) {
     477                 :          0 :             return false;
     478                 :            :         }
     479                 :          0 :         const_iterator b1 = begin();
     480                 :          0 :         const_iterator b2 = other.begin();
     481                 :          0 :         const_iterator e1 = end();
     482         [ #  # ]:          0 :         while (b1 != e1) {
     483         [ #  # ]:          0 :             if ((*b1) != (*b2)) {
     484                 :          0 :                 return false;
     485                 :            :             }
     486                 :          0 :             ++b1;
     487                 :          0 :             ++b2;
     488                 :            :         }
     489                 :          0 :         return true;
     490                 :          0 :     }
     491                 :            : 
     492                 :          0 :     bool operator!=(const prevector<N, T, Size, Diff>& other) const {
     493                 :          0 :         return !(*this == other);
     494                 :            :     }
     495                 :            : 
     496                 :   32634969 :     bool operator<(const prevector<N, T, Size, Diff>& other) const {
     497         [ +  + ]:   32634969 :         if (size() < other.size()) {
     498                 :   29994927 :             return true;
     499                 :            :         }
     500         [ +  + ]:    2640042 :         if (size() > other.size()) {
     501                 :     124854 :             return false;
     502                 :            :         }
     503                 :    2515188 :         const_iterator b1 = begin();
     504                 :    2515188 :         const_iterator b2 = other.begin();
     505                 :    2515188 :         const_iterator e1 = end();
     506         [ +  + ]:   80800052 :         while (b1 != e1) {
     507         [ +  + ]:   78580718 :             if ((*b1) < (*b2)) {
     508                 :     158398 :                 return true;
     509                 :            :             }
     510         [ +  + ]:   78422320 :             if ((*b2) < (*b1)) {
     511                 :     137456 :                 return false;
     512                 :            :             }
     513                 :   78284864 :             ++b1;
     514                 :   78284864 :             ++b2;
     515                 :            :         }
     516                 :    2219334 :         return false;
     517                 :   32634969 :     }
     518                 :            : 
     519                 :      53479 :     size_t allocated_memory() const {
     520         [ +  + ]:      53479 :         if (is_direct()) {
     521                 :      43352 :             return 0;
     522                 :            :         } else {
     523                 :      10127 :             return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
     524                 :            :         }
     525                 :      53479 :     }
     526                 :            : 
     527                 :          0 :     value_type* data() {
     528                 :          0 :         return item_ptr(0);
     529                 :            :     }
     530                 :            : 
     531                 :     238551 :     const value_type* data() const {
     532                 :     238551 :         return item_ptr(0);
     533                 :            :     }
     534                 :            : };
     535                 :            : 
     536                 :            : #endif // BITCOIN_PREVECTOR_H

Generated by: LCOV version 1.14