LCOV - code coverage report
Current view: top level - src - prevector.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 243 243
Test Date: 2025-12-13 04:17:47 Functions: 98.8 % 82 81
Branches: 53.6 % 2434 1304

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2015-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #ifndef BITCOIN_PREVECTOR_H
       6                 :             : #define BITCOIN_PREVECTOR_H
       7                 :             : 
       8                 :             : #include <algorithm>
       9                 :             : #include <cassert>
      10                 :             : #include <cstddef>
      11                 :             : #include <cstdint>
      12                 :             : #include <cstdlib>
      13                 :             : #include <cstring>
      14                 :             : #include <iterator>
      15                 :             : #include <type_traits>
      16                 :             : #include <utility>
      17                 :             : 
      18                 :             : /** Implements a drop-in replacement for std::vector<T> which stores up to N
      19                 :             :  *  elements directly (without heap allocation). The types Size and Diff are
      20                 :             :  *  used to store element counts, and can be any unsigned + signed type.
      21                 :             :  *
      22                 :             :  *  Storage layout is either:
      23                 :             :  *  - Direct allocation:
      24                 :             :  *    - Size _size: the number of used elements (between 0 and N)
      25                 :             :  *    - T direct[N]: an array of N elements of type T
      26                 :             :  *      (only the first _size are initialized).
      27                 :             :  *  - Indirect allocation:
      28                 :             :  *    - Size _size: the number of used elements plus N + 1
      29                 :             :  *    - Size capacity: the number of allocated elements
      30                 :             :  *    - T* indirect: a pointer to an array of capacity elements of type T
      31                 :             :  *      (only the first _size are initialized).
      32                 :             :  *
      33                 :             :  *  The data type T must be movable by memmove/realloc(). Once we switch to C++,
      34                 :             :  *  move constructors can be used instead.
      35                 :             :  */
      36                 :             : template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
      37                 :             : class prevector {
      38                 :             :     static_assert(std::is_trivially_copyable_v<T>);
      39                 :             : 
      40                 :             : public:
      41                 :             :     static constexpr unsigned int STATIC_SIZE{N};
      42                 :             : 
      43                 :             :     typedef Size size_type;
      44                 :             :     typedef Diff difference_type;
      45                 :             :     typedef T value_type;
      46                 :             :     typedef value_type& reference;
      47                 :             :     typedef const value_type& const_reference;
      48                 :             :     typedef value_type* pointer;
      49                 :             :     typedef const value_type* const_pointer;
      50                 :             : 
      51                 :             :     class iterator {
      52                 :             :         T* ptr{};
      53                 :             :     public:
      54                 :             :         typedef Diff difference_type;
      55                 :             :         typedef T* pointer;
      56                 :             :         typedef T& reference;
      57                 :             :         using element_type = T;
      58                 :             :         using iterator_category = std::contiguous_iterator_tag;
      59                 :             :         iterator() = default;
      60                 :   403994036 :         iterator(T* ptr_) : ptr(ptr_) {}
      61                 :   162663340 :         T& operator*() const { return *ptr; }
      62                 :      415774 :         T* operator->() const { return ptr; }
      63                 :             :         T& operator[](size_type pos) const { return ptr[pos]; }
      64                 :   695623616 :         iterator& operator++() { ptr++; return *this; }
      65                 :             :         iterator& operator--() { ptr--; return *this; }
      66                 :             :         iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
      67                 :             :         iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
      68   [ +  +  +  - ]:   111826802 :         difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
      69                 :       76000 :         iterator operator+(size_type n) const { return iterator(ptr + n); }
      70                 :             :         iterator friend operator+(size_type n, iterator x) { return x + n; }
      71                 :         256 :         iterator& operator+=(size_type n) { ptr += n; return *this; }
      72                 :       11024 :         iterator operator-(size_type n) const { return iterator(ptr - n); }
      73                 :             :         iterator& operator-=(size_type n) { ptr -= n; return *this; }
      74         [ +  + ]:   697945409 :         bool operator==(iterator x) const { return ptr == x.ptr; }
      75                 :             :         auto operator<=>(iterator x) const { return ptr <=> x.ptr; }
      76                 :             :     };
      77                 :             : 
      78                 :             :     class const_iterator {
      79                 :             :         const T* ptr{};
      80                 :             :     public:
      81                 :             :         typedef Diff difference_type;
      82                 :             :         typedef const T* pointer;
      83                 :             :         typedef const T& reference;
      84                 :             :         using element_type = const T;
      85                 :             :         using iterator_category = std::contiguous_iterator_tag;
      86                 :             :         const_iterator() = default;
      87                 :  3817673219 :         const_iterator(const T* ptr_) : ptr(ptr_) {}
      88                 :      283133 :         const_iterator(iterator x) : ptr(&(*x)) {}
      89         [ +  + ]:  1949111821 :         const T& operator*() const { return *ptr; }
           [ -  -  +  + ]
      90                 :   167165569 :         const T* operator->() const { return ptr; }
      91         [ -  + ]:     1215917 :         const T& operator[](size_type pos) const { return ptr[pos]; }
      92   [ +  -  -  + ]: 17311648589 :         const_iterator& operator++() { ptr++; return *this; }
      93   [ -  +  -  + ]:     2374220 :         const_iterator& operator--() { ptr--; return *this; }
      94         [ +  + ]:   562790581 :         const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
      95                 :             :         const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
      96   [ -  +  -  -  :  1198624224 :         difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
          -  +  -  -  +  
          -  +  +  +  +  
          +  +  +  -  +  
                      + ]
           [ +  -  +  - ]
           [ -  -  -  -  
                   +  + ]
      97                 :   124094136 :         const_iterator operator+(size_type n) const { return const_iterator(ptr + n); }
      98                 :             :         const_iterator friend operator+(size_type n, const_iterator x) { return x + n; }
      99         [ -  + ]:   159810018 :         const_iterator& operator+=(size_type n) { ptr += n; return *this; }
     100   [ +  -  +  - ]:     1732434 :         const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
                 [ -  + ]
     101                 :             :         const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
     102   [ +  +  +  +  : 17793380593 :         bool operator==(const_iterator x) const { return ptr == x.ptr; }
           +  + ][ +  + ]
           [ +  +  +  + ]
     103   [ +  +  +  -  :   842044182 :         auto operator<=>(const_iterator x) const { return ptr <=> x.ptr; }
          +  +  +  -  +  
          +  +  -  #  #  
          #  #  #  #  #  
           # ][ +  +  +  
          -  +  +  +  -  
          +  +  +  -  +  
          +  +  -  +  +  
                   +  - ]
           [ +  +  +  - ]
           [ +  +  +  -  
             +  +  +  - ]
     104                 :             :     };
     105                 :             : 
     106                 :             : private:
     107                 :             : #pragma pack(push, 1)
     108                 :             :     union direct_or_indirect {
     109                 :             :         char direct[sizeof(T) * N];
     110                 :             :         struct {
     111                 :             :             char* indirect;
     112                 :             :             size_type capacity;
     113                 :             :         } indirect_contents;
     114                 :             :     };
     115                 :             : #pragma pack(pop)
     116                 :             :     alignas(char*) direct_or_indirect _union = {};
     117                 :             :     size_type _size = 0;
     118                 :             : 
     119                 :             :     static_assert(alignof(char*) % alignof(size_type) == 0 && sizeof(char*) % alignof(size_type) == 0, "size_type cannot have more restrictive alignment requirement than pointer");
     120                 :             :     static_assert(alignof(char*) % alignof(T) == 0, "value_type T cannot have more restrictive alignment requirement than pointer");
     121                 :             : 
     122                 :   744319708 :     T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
     123                 :  4195255641 :     const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
     124                 :   511556468 :     T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
     125                 :  1299799264 :     const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
     126                 : 32259745284 :     bool is_direct() const { return _size <= N; }
     127                 :             : 
     128                 :   572028036 :     void change_capacity(size_type new_capacity) {
     129         [ +  + ]:   572028036 :         if (new_capacity <= N) {
     130         [ +  + ]:   444757985 :             if (!is_direct()) {
     131                 :       25555 :                 T* indirect = indirect_ptr(0);
     132                 :       25555 :                 T* src = indirect;
     133                 :       25555 :                 T* dst = direct_ptr(0);
     134                 :       25555 :                 memcpy(dst, src, size() * sizeof(T));
     135                 :       25555 :                 free(indirect);
     136                 :       25555 :                 _size -= N + 1;
     137                 :             :             }
     138                 :             :         } else {
     139         [ +  + ]:   127270051 :             if (!is_direct()) {
     140                 :             :                 /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
     141                 :             :                     success. These should instead use an allocator or new/delete so that handlers
     142                 :             :                     are called as necessary, but performance would be slightly degraded by doing so. */
     143                 :      861985 :                 _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
     144         [ -  + ]:      861985 :                 assert(_union.indirect_contents.indirect);
     145                 :      861985 :                 _union.indirect_contents.capacity = new_capacity;
     146                 :             :             } else {
     147                 :   126408066 :                 char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
     148         [ -  + ]:   126408066 :                 assert(new_indirect);
     149                 :   126408066 :                 T* src = direct_ptr(0);
     150                 :   126408066 :                 T* dst = reinterpret_cast<T*>(new_indirect);
     151                 :   126408066 :                 memcpy(dst, src, size() * sizeof(T));
     152                 :   126408066 :                 _union.indirect_contents.indirect = new_indirect;
     153                 :   126408066 :                 _union.indirect_contents.capacity = new_capacity;
     154                 :   126408066 :                 _size += N + 1;
     155                 :             :             }
     156                 :             :         }
     157                 :   572028036 :     }
     158                 :             : 
     159         [ +  + ]:  1129119962 :     T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
     160         [ +  + ]:  5483074354 :     const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
     161                 :             : 
     162                 :   104413783 :     void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
     163                 :   104413783 :         std::fill_n(dst, count, value);
     164                 :             :     }
     165                 :             : 
     166                 :             :     template <std::input_iterator InputIterator>
     167                 :   556821200 :     void fill(T* dst, InputIterator first, InputIterator last) {
     168   [ -  -  -  -  : 23505808713 :         while (first != last) {
          +  +  +  +  +  
             +  +  +  +  
           + ][ +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
           [ +  +  +  + ]
           [ +  +  +  +  
             +  +  +  + ]
           [ #  #  #  #  
           #  # ][ -  -  
          +  +  -  -  +  
           +  +  + ][ -  
          -  +  +  -  -  
          -  -  -  -  -  
                      - ]
     169                 : 22970858109 :             new(static_cast<void*>(dst)) T(*first);
     170                 : 21641140698 :             ++dst;
     171                 : 22970858109 :             ++first;
     172                 :             :         }
     173                 :             :     }
     174                 :             : 
     175                 :             : public:
     176                 :      898072 :     void assign(size_type n, const T& val) {
     177                 :      898072 :         clear();
     178         [ +  + ]:      898072 :         if (capacity() < n) {
     179                 :        6744 :             change_capacity(n);
     180                 :             :         }
     181                 :      898072 :         _size += n;
     182   [ +  +  +  + ]:     1796144 :         fill(item_ptr(0), n, val);
     183                 :      898072 :     }
     184                 :             : 
     185                 :             :     template <std::input_iterator InputIterator>
     186                 :    60952622 :     void assign(InputIterator first, InputIterator last) {
     187                 :    60952622 :         size_type n = last - first;
     188                 :    60952622 :         clear();
     189         [ +  + ]:    60952622 :         if (capacity() < n) {
     190                 :     3409619 :             change_capacity(n);
     191                 :             :         }
     192         [ +  + ]:    60952622 :         _size += n;
     193         [ +  + ]:    82823218 :         fill(item_ptr(0), first, last);
     194                 :    60952622 :     }
     195                 :             : 
     196         [ +  - ]:    89896154 :     prevector() = default;
     197                 :             : 
     198                 :             :     explicit prevector(size_type n) {
     199                 :             :         resize(n);
     200                 :             :     }
     201                 :             : 
     202                 :    83298293 :     explicit prevector(size_type n, const T& val) {
     203                 :    83298293 :         change_capacity(n);
     204                 :    83298293 :         _size += n;
     205   [ +  -  +  - ]:   166596586 :         fill(item_ptr(0), n, val);
     206                 :    83298293 :     }
     207                 :             : 
     208                 :             :     template <std::input_iterator InputIterator>
     209                 :     3132716 :     prevector(InputIterator first, InputIterator last) {
     210                 :     3132716 :         size_type n = last - first;
     211                 :     3132716 :         change_capacity(n);
     212         [ +  + ]:     3132716 :         _size += n;
     213                 :     3132716 :         fill(item_ptr(0), first, last);
     214                 :     3132716 :     }
     215                 :             : 
     216                 :   409908423 :     prevector(const prevector<N, T, Size, Diff>& other) {
     217                 :   409908423 :         size_type n = other.size();
     218                 :   409908423 :         change_capacity(n);
     219                 :   409908423 :         _size += n;
     220                 :   409908423 :         fill(item_ptr(0), other.begin(),  other.end());
     221                 :   409908423 :     }
     222                 :             : 
     223                 :    73570369 :     prevector(prevector<N, T, Size, Diff>&& other) noexcept
     224                 :    73570369 :         : _union(std::move(other._union)), _size(other._size)
     225                 :             :     {
     226   [ -  -  +  - ]:    72690059 :         other._size = 0;
         [ +  - ][ -  -  
             +  -  +  - ]
     227                 :             :     }
     228                 :             : 
     229                 :    57815882 :     prevector& operator=(const prevector<N, T, Size, Diff>& other) {
     230         [ +  - ]:    57815882 :         if (&other == this) {
     231                 :             :             return *this;
     232                 :             :         }
     233                 :   115631764 :         assign(other.begin(), other.end());
     234                 :    57815882 :         return *this;
     235                 :             :     }
     236                 :             : 
     237                 :    56546632 :     prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
     238         [ +  + ]:    56546632 :         if (!is_direct()) {
     239                 :      167305 :             free(_union.indirect_contents.indirect);
     240                 :             :         }
     241                 :    56546632 :         _union = std::move(other._union);
     242                 :    56546632 :         _size = other._size;
     243                 :    56546632 :         other._size = 0;
     244                 :    56546632 :         return *this;
     245                 :             :     }
     246                 :             : 
     247                 : 29139358116 :     size_type size() const {
     248   [ -  +  -  -  : 16341318810 :         return is_direct() ? _size : _size - N - 1;
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
             +  +  +  +  
           + ][ -  -  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          #  #  #  #  #  
           # ][ -  -  -  
          -  +  +  -  -  
          -  -  -  -  +  
          +  -  -  -  -  
          -  -  -  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
           - ][ -  -  -  
          -  -  -  -  +  
          +  +  -  -  -  
          -  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  +  +  +  
          +  +  +  -  -  
          -  -  -  -  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  +  
          +  -  -  +  +  
          +  +  +  +  -  
          +  -  -  -  +  
          -  +  -  +  +  
          +  -  +  -  -  
          -  +  -  +  -  
          -  -  +  -  +  
          -  +  +  +  +  
          +  +  +  -  +  
          -  +  -  +  -  
          +  +  -  +  -  
          -  +  -  +  -  
          +  -  +  -  +  
          +  -  -  +  +  
          -  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
           -  - ][ -  -  
          -  +  +  +  +  
          +  +  +  -  -  
             +  +  +  + ]
           [ +  +  -  -  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
          +  -  -  -  -  
          +  +  +  +  +  
          +  -  -  -  -  
          +  +  -  +  +  
          +  -  -  +  +  
          +  +  +  +  -  
           +  -  + ][ -  
          +  +  +  -  +  
          +  +  +  +  +  
           +  +  + ][ +  
          +  +  +  +  +  
             +  +  +  + ]
           [ +  +  +  +  
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
              # ][ +  + ]
           [ -  -  -  +  
             +  +  +  + ]
           [ +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ -  
          -  +  +  -  -  
          -  +  -  +  -  
          -  -  +  -  +  
          -  -  -  +  -  
          +  -  -  -  -  
             -  -  -  - ]
           [ +  +  +  +  
          #  #  #  #  #  
                #  #  # ]
     249                 :             :     }
     250                 :             : 
     251   [ +  +  +  + ]: 12028043856 :     bool empty() const {
         [ +  + ][ +  +  
          -  +  +  +  -  
           + ][ #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  -  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  -  +  
          +  +  +  +  +  
             +  +  +  +  
           + ][ +  +  -  
          -  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     252   [ +  +  +  + ]: 12028045668 :         return size() == 0;
         [ +  + ][ +  +  
          -  +  +  +  -  
           + ][ #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  -  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
           +  + ][ +  +  
          -  -  +  -  +  
          +  +  -  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     253                 :             :     }
     254                 :             : 
     255   [ -  +  +  -  :   132829452 :     iterator begin() { return iterator(item_ptr(0)); }
             +  +  +  + ]
           [ -  +  +  -  
           +  - ][ +  + ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  +  +  
          -  +  +  +  -  
          +  +  +  -  +  
           +  +  - ][ +  
          +  +  +  +  +  
             +  +  +  + ]
           [ -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          +  +  -  -  +  
          +  +  +  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  +  -  -  +  
          +  +  +  -  -  
             +  +  +  + ]
     256   [ +  +  +  +  :  4274922471 :     const_iterator begin() const { return const_iterator(item_ptr(0)); }
          -  -  -  -  -  
          -  +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ +  
          +  +  +  +  +  
          +  +  +  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  +  +  
           +  +  + ][ +  
          +  +  +  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          +  +  +  +  +  
           +  + ][ +  +  
          +  +  +  +  +  
          +  -  +  +  +  
           +  + ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
         [ +  + ][ +  +  
          -  -  +  +  +  
          -  +  +  +  +  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ -  -  -  -  
          +  +  +  +  +  
          +  +  +  -  -  
          -  -  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
          -  +  -  +  +  
                   +  - ]
     257   [ +  +  +  + ]:   306880068 :     iterator end() { return iterator(item_ptr(size())); }
     258   [ +  +  +  + ]:  2458568891 :     const_iterator end() const { return const_iterator(item_ptr(size())); }
     259                 :             : 
     260                 :   234391337 :     size_t capacity() const {
     261         [ +  + ]:   128061176 :         if (is_direct()) {
           [ #  #  #  # ]
           [ -  -  -  +  
          -  +  -  +  -  
           +  -  - ][ +  
             +  +  +  +  
                      + ]
     262                 :             :             return N;
     263                 :             :         } else {
     264                 :   123007561 :             return _union.indirect_contents.capacity;
     265                 :             :         }
     266                 :             :     }
     267                 :             : 
     268   [ +  -  +  -  :    13470715 :     T& operator[](size_type pos) {
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  -  +  +  
           -  +  - ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     269   [ -  -  -  -  :    26667661 :         return *item_ptr(pos);
             +  -  +  + ]
         [ +  + ][ +  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
           [ +  +  +  + ]
           [ +  -  +  -  
          +  -  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          +  +  -  +  -  
          +  -  +  -  +  
          +  +  -  +  -  
          -  +  -  +  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     270                 :             :     }
     271                 :             : 
     272         [ +  - ]:   152885591 :     const T& operator[](size_type pos) const {
     273   [ +  +  +  +  :  1455362851 :         return *item_ptr(pos);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                   +  + ]
           [ +  +  +  + ]
           [ +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ -  
          +  +  +  -  -  
          -  -  -  -  -  
             -  -  -  -  
           - ][ +  +  +  
          +  +  +  +  +  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
           +  +  + ][ +  
          -  +  -  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  +  
                      + ]
     274                 :             :     }
     275                 :             : 
     276         [ +  + ]:   184628615 :     void resize(size_type new_size) {
     277                 :   184628615 :         size_type cur_size = size();
     278         [ +  + ]:   184628615 :         if (cur_size == new_size) {
     279                 :             :             return;
     280                 :             :         }
     281         [ +  + ]:    38754801 :         if (cur_size > new_size) {
     282         [ +  + ]:    37117222 :             erase(item_ptr(new_size), end());
     283                 :    18558611 :             return;
     284                 :             :         }
     285   [ +  +  +  + ]:    20199670 :         if (new_size > capacity()) {
     286                 :    12573436 :             change_capacity(new_size);
     287                 :             :         }
     288                 :    20196190 :         ptrdiff_t increase = new_size - cur_size;
     289   [ +  +  +  - ]:    40392380 :         fill(item_ptr(cur_size), increase);
     290                 :    20196190 :         _size += increase;
     291                 :             :     }
     292                 :             : 
     293                 :       10133 :     void reserve(size_type new_capacity) {
     294   [ +  +  +  + ]:       16096 :         if (new_capacity > capacity()) {
     295                 :        5150 :             change_capacity(new_capacity);
     296                 :             :         }
     297                 :       10133 :     }
     298                 :             : 
     299         [ +  + ]:    55031911 :     void shrink_to_fit() {
     300                 :    55031911 :         change_capacity(size());
     301                 :    55031911 :     }
     302                 :             : 
     303                 :   137727597 :     void clear() {
     304                 :   137727597 :         resize(0);
     305                 :             :     }
     306                 :             : 
     307         [ +  + ]:    57083257 :     iterator insert(iterator pos, const T& value) {
     308         [ +  + ]:    57083257 :         size_type p = pos - begin();
     309         [ +  + ]:    57083257 :         size_type new_size = size() + 1;
     310         [ +  + ]:    57083257 :         if (capacity() < new_size) {
     311                 :      123697 :             change_capacity(new_size + (new_size >> 1));
     312                 :             :         }
     313         [ +  + ]:    57083257 :         T* ptr = item_ptr(p);
     314         [ +  + ]:    57083257 :         T* dst = ptr + 1;
     315                 :    57083257 :         memmove(dst, ptr, (size() - p) * sizeof(T));
     316                 :    57083257 :         _size++;
     317                 :    57083257 :         new(static_cast<void*>(ptr)) T(value);
     318                 :    57083257 :         return iterator(ptr);
     319                 :             :     }
     320                 :             : 
     321         [ +  + ]:       21228 :     void insert(iterator pos, size_type count, const T& value) {
     322         [ +  + ]:       21228 :         size_type p = pos - begin();
     323         [ +  + ]:       21228 :         size_type new_size = size() + count;
     324         [ +  + ]:       21228 :         if (capacity() < new_size) {
     325                 :        2051 :             change_capacity(new_size + (new_size >> 1));
     326                 :             :         }
     327         [ +  + ]:       21228 :         T* ptr = item_ptr(p);
     328         [ +  + ]:       21228 :         T* dst = ptr + count;
     329         [ +  + ]:       21228 :         memmove(dst, ptr, (size() - p) * sizeof(T));
     330                 :       21228 :         _size += count;
     331   [ +  +  +  - ]:       42456 :         fill(item_ptr(p), count, value);
     332                 :       21228 :     }
     333                 :             : 
     334                 :             :     template <std::input_iterator InputIterator>
     335         [ +  + ]:    85030779 :     void insert(iterator pos, InputIterator first, InputIterator last) {
     336         [ +  + ]:    85030779 :         size_type p = pos - begin();
     337         [ +  + ]:    85030779 :         difference_type count = last - first;
     338         [ +  + ]:    85030779 :         size_type new_size = size() + count;
     339         [ +  + ]:    85030779 :         if (capacity() < new_size) {
     340                 :     2081140 :             change_capacity(new_size + (new_size >> 1));
     341                 :             :         }
     342         [ +  + ]:    85030779 :         T* ptr = item_ptr(p);
     343         [ +  + ]:    85030779 :         T* dst = ptr + count;
     344                 :    85030779 :         memmove(dst, ptr, (size() - p) * sizeof(T));
     345                 :    85030779 :         _size += count;
     346                 :    85030779 :         fill(ptr, first, last);
     347                 :    85030779 :     }
     348                 :             : 
     349         [ +  + ]:     8666333 :     inline void resize_uninitialized(size_type new_size) {
     350                 :             :         // resize_uninitialized changes the size of the prevector but does not initialize it.
     351                 :             :         // If size < new_size, the added elements must be initialized explicitly.
     352         [ +  + ]:     8666333 :         if (capacity() < new_size) {
     353         [ +  - ]:     2435255 :             change_capacity(new_size);
     354                 :     2435255 :             _size += new_size - size();
     355                 :     2435255 :             return;
     356                 :             :         }
     357         [ +  + ]:     6231078 :         if (new_size < size()) {
     358         [ +  + ]:       10122 :             erase(item_ptr(new_size), end());
     359                 :             :         } else {
     360                 :     6226017 :             _size += new_size - size();
     361                 :             :         }
     362                 :             :     }
     363                 :             : 
     364                 :        7304 :     iterator erase(iterator pos) {
     365                 :        7304 :         return erase(pos, pos + 1);
     366                 :             :     }
     367                 :             : 
     368                 :    18596489 :     iterator erase(iterator first, iterator last) {
     369                 :             :         // Erase is not allowed to the change the object's capacity. That means
     370                 :             :         // that when starting with an indirectly allocated prevector with
     371                 :             :         // size and capacity > N, the result may be a still indirectly allocated
     372                 :             :         // prevector with size <= N and capacity > N. A shrink_to_fit() call is
     373                 :             :         // necessary to switch to the (more efficient) directly allocated
     374                 :             :         // representation (with capacity N and size <= N).
     375                 :    18596489 :         iterator p = first;
     376                 :    18596489 :         char* endp = (char*)&(*end());
     377                 :    18596489 :         _size -= last - p;
     378                 :    18596489 :         memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
     379                 :    18596489 :         return first;
     380                 :             :     }
     381                 :             : 
     382                 :             :     template<typename... Args>
     383         [ +  + ]:     1522919 :     void emplace_back(Args&&... args) {
     384         [ +  + ]:     1522919 :         size_type new_size = size() + 1;
     385         [ +  + ]:     1522919 :         if (capacity() < new_size) {
     386                 :       19601 :             change_capacity(new_size + (new_size >> 1));
     387                 :             :         }
     388         [ +  + ]:     1522919 :         new(item_ptr(size())) T(std::forward<Args>(args)...);
     389                 :     1522919 :         _size++;
     390                 :     1522919 :     }
     391                 :             : 
     392                 :     1522919 :     void push_back(const T& value) {
     393                 :     1522919 :         emplace_back(value);
     394                 :     1163014 :     }
     395                 :             : 
     396                 :       11024 :     void pop_back() {
     397                 :       11024 :         erase(end() - 1, end());
     398                 :       11024 :     }
     399                 :             : 
     400                 :             :     T& front() {
     401                 :             :         return *item_ptr(0);
     402                 :             :     }
     403                 :             : 
     404                 :             :     const T& front() const {
     405                 :             :         return *item_ptr(0);
     406                 :             :     }
     407                 :             : 
     408   [ +  +  +  +  :       18218 :     T& back() {
             +  +  +  + ]
     409   [ +  +  +  +  :       18218 :         return *item_ptr(size() - 1);
             +  +  +  + ]
     410                 :             :     }
     411                 :             : 
     412   [ +  +  +  +  :     4348997 :     const T& back() const {
                   +  + ]
     413   [ +  +  +  +  :     4389312 :         return *item_ptr(size() - 1);
             +  +  +  + ]
     414                 :             :     }
     415                 :             : 
     416                 :       12526 :     void swap(prevector<N, T, Size, Diff>& other) noexcept
     417                 :             :     {
     418                 :       12526 :         std::swap(_union, other._union);
     419                 :       12526 :         std::swap(_size, other._size);
     420                 :             :     }
     421                 :             : 
     422                 :   773401896 :     ~prevector() {
     423         [ +  + ]:   773401896 :         if (!is_direct()) {
     424                 :   126215206 :             free(_union.indirect_contents.indirect);
     425                 :   126215206 :             _union.indirect_contents.indirect = nullptr;
     426                 :             :         }
     427                 :   773401896 :     }
     428                 :             : 
     429         [ +  + ]:    52928309 :     bool operator==(const prevector<N, T, Size, Diff>& other) const {
     430   [ +  +  +  + ]:    71770444 :         if (other.size() != size()) {
     431                 :             :             return false;
     432                 :             :         }
     433         [ +  + ]:    52927108 :         const_iterator b1 = begin();
     434                 :    52927108 :         const_iterator b2 = other.begin();
     435                 :    52927108 :         const_iterator e1 = end();
     436         [ +  + ]:  1072456988 :         while (b1 != e1) {
     437         [ +  + ]:  1033192316 :             if ((*b1) != (*b2)) {
     438                 :             :                 return false;
     439                 :             :             }
     440                 :  1019529880 :             ++b1;
     441                 :  1019529880 :             ++b2;
     442                 :             :         }
     443                 :             :         return true;
     444                 :             :     }
     445                 :             : 
     446         [ +  + ]:    11422358 :     bool operator<(const prevector<N, T, Size, Diff>& other) const {
     447   [ +  +  +  + ]:    12467892 :         if (size() < other.size()) {
     448                 :             :             return true;
     449                 :             :         }
     450         [ +  + ]:    11351626 :         if (size() > other.size()) {
     451                 :             :             return false;
     452                 :             :         }
     453         [ +  + ]:    11285473 :         const_iterator b1 = begin();
     454                 :    11285473 :         const_iterator b2 = other.begin();
     455                 :    11285473 :         const_iterator e1 = end();
     456         [ +  + ]:    88082800 :         while (b1 != e1) {
     457         [ +  + ]:    85114378 :             if ((*b1) < (*b2)) {
     458                 :             :                 return true;
     459                 :             :             }
     460         [ +  + ]:    79529477 :             if ((*b2) < (*b1)) {
     461                 :             :                 return false;
     462                 :             :             }
     463                 :    76797327 :             ++b1;
     464                 :    76797327 :             ++b2;
     465                 :             :         }
     466                 :             :         return false;
     467                 :             :     }
     468                 :             : 
     469                 :    24248630 :     size_t allocated_memory() const {
     470   [ +  +  +  + ]:    24248630 :         if (is_direct()) {
           [ +  +  #  # ]
           [ -  +  +  +  
          -  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                   -  - ]
     471                 :             :             return 0;
     472                 :             :         } else {
     473   [ +  -  +  - ]:     1544332 :             return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
           [ +  -  #  # ]
           [ -  -  +  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   -  - ]
     474                 :             :         }
     475                 :             :     }
     476                 :             : 
     477 [ +  + ][ +  +  :    47169003 :     value_type* data() {
             +  +  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
           # ][ +  +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          +  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
           [ #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  -  
          -  +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  +  +  -  
          +  +  +  -  -  
          +  -  -  -  -  
             +  +  +  - ]
     478 [ +  + ][ #  #  :    47169003 :         return item_ptr(0);
          #  #  #  #  #  
           #  #  # ][ +  
          +  -  -  -  -  
          -  -  +  +  -  
             -  -  -  -  
           - ][ +  +  +  
          +  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  +  +  +  
          -  +  -  -  -  
                -  +  + ]
     479                 :             :     }
     480                 :             : 
     481 [ +  + ][ +  +  :   961027194 :     const value_type* data() const {
          +  +  +  +  +  
           + ][ #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  +  +  +  
          -  -  +  +  +  
           +  +  - ][ +  
             +  +  +  #  
           # ][ #  #  #  
           #  #  # ][ +  
          +  +  +  +  +  
          +  +  +  -  +  
          -  +  -  +  -  
          -  +  -  +  +  
          -  +  -  +  +  
          +  +  +  +  -  
             -  -  -  -  
                      - ]
     482 [ +  + ][ -  +  :   961032980 :         return item_ptr(0);
          +  +  +  +  +  
           + ][ +  +  +  
           +  #  # ][ +  
             +  +  +  +  
           - ][ +  +  +  
          +  +  +  -  +  
          -  +  +  -  +  
          -  -  +  -  +  
          +  +  +  +  +  
                      + ]
     483                 :             :     }
     484                 :             : };
     485                 :             : 
     486                 :             : #endif // BITCOIN_PREVECTOR_H
        

Generated by: LCOV version 2.0-1