LCOV - code coverage report
Current view: top level - src - prevector.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.6 % 284 283
Test Date: 2024-09-01 05:20:30 Functions: 98.7 % 234 231
Branches: 57.5 % 266 153

             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 <algorithm>
       9                 :             : #include <cassert>
      10                 :             : #include <cstddef>
      11                 :             : #include <cstdint>
      12                 :             : #include <cstdlib>
      13                 :             : #include <cstring>
      14                 :             : #include <iterator>
      15                 :             : #include <type_traits>
      16                 :             : #include <utility>
      17                 :             : 
      18                 :             : /** Implements a drop-in replacement for std::vector<T> which stores up to N
      19                 :             :  *  elements directly (without heap allocation). The types Size and Diff are
      20                 :             :  *  used to store element counts, and can be any unsigned + signed type.
      21                 :             :  *
      22                 :             :  *  Storage layout is either:
      23                 :             :  *  - Direct allocation:
      24                 :             :  *    - Size _size: the number of used elements (between 0 and N)
      25                 :             :  *    - T direct[N]: an array of N elements of type T
      26                 :             :  *      (only the first _size are initialized).
      27                 :             :  *  - Indirect allocation:
      28                 :             :  *    - Size _size: the number of used elements plus N + 1
      29                 :             :  *    - Size capacity: the number of allocated elements
      30                 :             :  *    - T* indirect: a pointer to an array of capacity elements of type T
      31                 :             :  *      (only the first _size are initialized).
      32                 :             :  *
      33                 :             :  *  The data type T must be movable by memmove/realloc(). Once we switch to C++,
      34                 :             :  *  move constructors can be used instead.
      35                 :             :  */
      36                 :             : template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
      37                 :             : class prevector {
      38                 :             :     static_assert(std::is_trivially_copyable_v<T>);
      39                 :             : 
      40                 :             : public:
      41                 :             :     typedef Size size_type;
      42                 :             :     typedef Diff difference_type;
      43                 :             :     typedef T value_type;
      44                 :             :     typedef value_type& reference;
      45                 :             :     typedef const value_type& const_reference;
      46                 :             :     typedef value_type* pointer;
      47                 :             :     typedef const value_type* const_pointer;
      48                 :             : 
      49                 :             :     class iterator {
      50                 :             :         T* ptr{};
      51                 :             :     public:
      52                 :             :         typedef Diff difference_type;
      53                 :             :         typedef T* pointer;
      54                 :             :         typedef T& reference;
      55                 :             :         using element_type = T;
      56                 :             :         using iterator_category = std::contiguous_iterator_tag;
      57                 :             :         iterator() = default;
      58                 :   525308052 :         iterator(T* ptr_) : ptr(ptr_) {}
      59                 :   909562593 :         T& operator*() const { return *ptr; }
      60                 :             :         T* operator->() const { return ptr; }
      61                 :             :         T& operator[](size_type pos) const { return ptr[pos]; }
      62                 :   385800818 :         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                 :   216354669 :         difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
      67                 :      154485 :         iterator operator+(size_type n) const { return iterator(ptr + n); }
      68                 :             :         iterator friend operator+(size_type n, iterator x) { return x + n; }
      69                 :             :         iterator& operator+=(size_type n) { ptr += n; return *this; }
      70                 :       15549 :         iterator operator-(size_type n) const { return iterator(ptr - n); }
      71                 :             :         iterator& operator-=(size_type n) { ptr -= n; return *this; }
      72                 :             :         bool operator==(iterator x) const { return ptr == x.ptr; }
      73                 :   387047505 :         bool operator!=(iterator x) const { return ptr != x.ptr; }
      74                 :             :         bool operator>=(iterator x) const { return ptr >= x.ptr; }
      75                 :             :         bool operator<=(iterator x) const { return ptr <= x.ptr; }
      76                 :             :         bool operator>(iterator x) const { return ptr > x.ptr; }
      77                 :             :         bool operator<(iterator x) const { return ptr < x.ptr; }
      78                 :             :     };
      79                 :             : 
      80                 :             :     class reverse_iterator {
      81                 :             :         T* ptr{};
      82                 :             :     public:
      83                 :             :         typedef Diff difference_type;
      84                 :             :         typedef T value_type;
      85                 :             :         typedef T* pointer;
      86                 :             :         typedef T& reference;
      87                 :             :         typedef std::bidirectional_iterator_tag iterator_category;
      88                 :             :         reverse_iterator() = default;
      89                 :             :         reverse_iterator(T* ptr_) : ptr(ptr_) {}
      90                 :             :         T& operator*() const { return *ptr; }
      91                 :             :         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* pointer;
     105                 :             :         typedef const T& reference;
     106                 :             :         using element_type = const T;
     107                 :             :         using iterator_category = std::contiguous_iterator_tag;
     108                 :             :         const_iterator() = default;
     109                 :  5236865709 :         const_iterator(const T* ptr_) : ptr(ptr_) {}
     110                 :      942633 :         const_iterator(iterator x) : ptr(&(*x)) {}
     111                 : 39481380712 :         const T& operator*() const { return *ptr; }
     112                 :             :         const T* operator->() const { return ptr; }
     113                 :     2960549 :         const T& operator[](size_type pos) const { return ptr[pos]; }
     114                 : 31004793828 :         const_iterator& operator++() { ptr++; return *this; }
     115                 :     6702656 :         const_iterator& operator--() { ptr--; return *this; }
     116                 :   478342474 :         const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
     117                 :             :         const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
     118                 :  1222608419 :         difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
     119                 :   113076987 :         const_iterator operator+(size_type n) const { return const_iterator(ptr + n); }
     120                 :             :         const_iterator friend operator+(size_type n, const_iterator x) { return x + n; }
     121                 :   143001436 :         const_iterator& operator+=(size_type n) { ptr += n; return *this; }
     122                 :     2254968 :         const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
     123                 :             :         const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
     124                 :     3420300 :         bool operator==(const_iterator x) const { return ptr == x.ptr; }
     125                 : 28546757234 :         bool operator!=(const_iterator x) const { return ptr != x.ptr; }
     126                 :   465740065 :         bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
     127                 :             :         bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
     128                 :             :         bool operator>(const_iterator x) const { return ptr > x.ptr; }
     129                 :   295637474 :         bool operator<(const_iterator x) const { return ptr < x.ptr; }
     130                 :             :     };
     131                 :             : 
     132                 :             :     class const_reverse_iterator {
     133                 :             :         const T* ptr{};
     134                 :             :     public:
     135                 :             :         typedef Diff difference_type;
     136                 :             :         typedef const T value_type;
     137                 :             :         typedef const T* pointer;
     138                 :             :         typedef const T& reference;
     139                 :             :         typedef std::bidirectional_iterator_tag iterator_category;
     140                 :             :         const_reverse_iterator() = default;
     141                 :             :         const_reverse_iterator(const T* ptr_) : ptr(ptr_) {}
     142                 :             :         const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
     143                 :             :         const T& operator*() const { return *ptr; }
     144                 :             :         const T* operator->() const { return ptr; }
     145                 :             :         const_reverse_iterator& operator--() { ptr++; return *this; }
     146                 :             :         const_reverse_iterator& operator++() { ptr--; return *this; }
     147                 :             :         const_reverse_iterator operator++(int) { const_reverse_iterator copy(*this); ++(*this); return copy; }
     148                 :             :         const_reverse_iterator operator--(int) { const_reverse_iterator copy(*this); --(*this); return copy; }
     149                 :             :         bool operator==(const_reverse_iterator x) const { return ptr == x.ptr; }
     150                 :             :         bool operator!=(const_reverse_iterator x) const { return ptr != x.ptr; }
     151                 :             :     };
     152                 :             : 
     153                 :             : private:
     154                 :             : #pragma pack(push, 1)
     155                 :             :     union direct_or_indirect {
     156                 :             :         char direct[sizeof(T) * N];
     157                 :             :         struct {
     158                 :             :             char* indirect;
     159                 :             :             size_type capacity;
     160                 :             :         } indirect_contents;
     161                 :             :     };
     162                 :             : #pragma pack(pop)
     163                 :   867335982 :     alignas(char*) direct_or_indirect _union = {};
     164                 :   867335982 :     size_type _size = 0;
     165                 :             : 
     166                 :             :     static_assert(alignof(char*) % alignof(size_type) == 0 && sizeof(char*) % alignof(size_type) == 0, "size_type cannot have more restrictive alignment requirement than pointer");
     167                 :             :     static_assert(alignof(char*) % alignof(T) == 0, "value_type T cannot have more restrictive alignment requirement than pointer");
     168                 :             : 
     169                 :   983716760 :     T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
     170                 :  5457142616 :     const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
     171                 :   769316437 :     T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
     172                 :  1879718286 :     const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
     173                 : 19457690170 :     bool is_direct() const { return _size <= N; }
     174                 :             : 
     175                 :   793205028 :     void change_capacity(size_type new_capacity) {
     176         [ +  + ]:   793205028 :         if (new_capacity <= N) {
           [ #  #  +  + ]
           [ #  #  #  #  
                   #  # ]
     177         [ +  + ]:   527716828 :             if (!is_direct()) {
           [ #  #  +  - ]
           [ #  #  #  #  
                   #  # ]
     178                 :      168846 :                 T* indirect = indirect_ptr(0);
     179                 :      168846 :                 T* src = indirect;
     180                 :      168846 :                 T* dst = direct_ptr(0);
     181                 :      168846 :                 memcpy(dst, src, size() * sizeof(T));
     182                 :      168846 :                 free(indirect);
     183                 :      168846 :                 _size -= N + 1;
     184                 :      168846 :             }
     185                 :   527716828 :         } else {
     186         [ +  + ]:   265488200 :             if (!is_direct()) {
           [ #  #  +  - ]
           [ #  #  #  #  
                   #  # ]
     187                 :             :                 /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
     188                 :             :                     success. These should instead use an allocator or new/delete so that handlers
     189                 :             :                     are called as necessary, but performance would be slightly degraded by doing so. */
     190                 :     1111248 :                 _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
     191         [ +  - ]:     1111248 :                 assert(_union.indirect_contents.indirect);
           [ #  #  #  # ]
           [ #  #  #  #  
                   #  # ]
     192                 :     1111248 :                 _union.indirect_contents.capacity = new_capacity;
     193                 :     1111248 :             } else {
     194                 :   264376952 :                 char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
     195         [ +  - ]:   264376952 :                 assert(new_indirect);
           [ #  #  +  - ]
           [ #  #  #  #  
                   #  # ]
     196                 :   264376952 :                 T* src = direct_ptr(0);
     197                 :   264376952 :                 T* dst = reinterpret_cast<T*>(new_indirect);
     198                 :   264376952 :                 memcpy(dst, src, size() * sizeof(T));
     199                 :   264376952 :                 _union.indirect_contents.indirect = new_indirect;
     200                 :   264376952 :                 _union.indirect_contents.capacity = new_capacity;
     201                 :   264376952 :                 _size += N + 1;
     202                 :   264376952 :             }
     203                 :             :         }
     204                 :   793205028 :     }
     205                 :             : 
     206         [ +  + ]:  1488318553 :     T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
           [ +  +  +  + ]
           [ #  #  +  +  
                   +  - ]
     207         [ +  + ]:  7336860903 :     const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
           [ +  +  +  + ]
     208                 :             : 
     209                 :   136191723 :     void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
     210                 :   136191723 :         std::fill_n(dst, count, value);
     211                 :   136191723 :     }
     212                 :             : 
     213                 :             :     template <std::input_iterator InputIterator>
     214                 :   750290396 :     void fill(T* dst, InputIterator first, InputIterator last) {
     215 [ +  + ][ +  +  : 24259259592 :         while (first != last) {
             +  +  +  + ]
           [ +  +  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  +  +  +  +  
          +  +  +  +  +  
           + ][ #  #  #  
             #  +  +  +  
           + ][ +  +  #  
          #  #  #  #  #  
                   #  # ]
     216                 : 23508969196 :             new(static_cast<void*>(dst)) T(*first);
     217                 : 23508969196 :             ++dst;
     218                 : 23508969196 :             ++first;
     219                 :             :         }
     220                 :   750290396 :     }
     221                 :             : 
     222                 :             : public:
     223                 :     1181633 :     void assign(size_type n, const T& val) {
     224                 :     1181633 :         clear();
     225         [ +  + ]:     1181633 :         if (capacity() < n) {
     226                 :       10071 :             change_capacity(n);
     227                 :       10071 :         }
     228                 :     1181633 :         _size += n;
     229                 :     1181633 :         fill(item_ptr(0), n, val);
     230                 :     1181633 :     }
     231                 :             : 
     232                 :             :     template <std::input_iterator InputIterator>
     233                 :    94002248 :     void assign(InputIterator first, InputIterator last) {
     234                 :    94002248 :         size_type n = last - first;
     235                 :    94002248 :         clear();
     236 [ +  + ][ +  +  :    94002248 :         if (capacity() < n) {
          +  -  -  +  #  
                      # ]
           [ #  #  #  # ]
     237                 :    47182589 :             change_capacity(n);
     238                 :    47182589 :         }
     239                 :    94002248 :         _size += n;
     240                 :    94002248 :         fill(item_ptr(0), first, last);
     241                 :    94002248 :     }
     242                 :             : 
     243                 :   455919424 :     prevector() = default;
     244                 :             : 
     245                 :             :     explicit prevector(size_type n) {
     246                 :             :         resize(n);
     247                 :             :     }
     248                 :             : 
     249                 :   107601414 :     explicit prevector(size_type n, const T& val) {
     250                 :   107601414 :         change_capacity(n);
     251                 :   107601414 :         _size += n;
     252                 :   107601414 :         fill(item_ptr(0), n, val);
     253                 :   107601414 :     }
     254                 :             : 
     255                 :             :     template <std::input_iterator InputIterator>
     256                 :     3244539 :     prevector(InputIterator first, InputIterator last) {
     257                 :     3244539 :         size_type n = last - first;
     258                 :     3244539 :         change_capacity(n);
     259                 :     3244539 :         _size += n;
     260                 :     3244539 :         fill(item_ptr(0), first, last);
     261                 :     3244539 :     }
     262                 :             : 
     263                 :   528530317 :     prevector(const prevector<N, T, Size, Diff>& other) {
     264                 :   528530317 :         size_type n = other.size();
     265                 :   528530317 :         change_capacity(n);
     266                 :   528530317 :         _size += n;
     267                 :   528530317 :         fill(item_ptr(0), other.begin(),  other.end());
     268                 :   528530317 :     }
     269                 :             : 
     270                 :   111587750 :     prevector(prevector<N, T, Size, Diff>&& other) noexcept
     271                 :   111587750 :         : _union(std::move(other._union)), _size(other._size)
     272                 :             :     {
     273                 :   111587750 :         other._size = 0;
     274                 :   111587750 :     }
     275                 :             : 
     276                 :    90624250 :     prevector& operator=(const prevector<N, T, Size, Diff>& other) {
     277         [ +  - ]:    90624250 :         if (&other == this) {
           [ #  #  #  # ]
     278                 :           0 :             return *this;
     279                 :             :         }
     280                 :    90624250 :         assign(other.begin(), other.end());
     281                 :    90624250 :         return *this;
     282                 :    90624250 :     }
     283                 :             : 
     284                 :    78877972 :     prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
     285         [ +  + ]:    78877972 :         if (!is_direct()) {
           [ +  -  +  + ]
     286                 :      244257 :             free(_union.indirect_contents.indirect);
     287                 :      244257 :         }
     288                 :    78877972 :         _union = std::move(other._union);
     289                 :    78877972 :         _size = other._size;
     290                 :    78877972 :         other._size = 0;
     291                 :    78877972 :         return *this;
     292                 :             :     }
     293                 :             : 
     294                 :  8428996071 :     size_type size() const {
     295         [ +  + ]:  8428996071 :         return is_direct() ? _size : _size - N - 1;
           [ +  +  +  + ]
           [ +  +  +  +  
                   +  - ]
     296                 :             :     }
     297                 :             : 
     298                 :   435421288 :     bool empty() const {
     299                 :   435421288 :         return size() == 0;
     300                 :             :     }
     301                 :             : 
     302                 :   194739932 :     iterator begin() { return iterator(item_ptr(0)); }
     303                 :  3161593402 :     const_iterator begin() const { return const_iterator(item_ptr(0)); }
     304                 :   239572164 :     iterator end() { return iterator(item_ptr(size())); }
     305                 :  1959940370 :     const_iterator end() const { return const_iterator(item_ptr(size())); }
     306                 :             : 
     307                 :             :     reverse_iterator rbegin() { return reverse_iterator(item_ptr(size() - 1)); }
     308                 :             :     const_reverse_iterator rbegin() const { return const_reverse_iterator(item_ptr(size() - 1)); }
     309                 :             :     reverse_iterator rend() { return reverse_iterator(item_ptr(-1)); }
     310                 :             :     const_reverse_iterator rend() const { return const_reverse_iterator(item_ptr(-1)); }
     311                 :             : 
     312                 :   324042152 :     size_t capacity() const {
     313         [ +  + ]:   324042152 :         if (is_direct()) {
           [ +  -  +  - ]
     314                 :   162486753 :             return N;
     315                 :             :         } else {
     316                 :   161555399 :             return _union.indirect_contents.capacity;
     317                 :             :         }
     318                 :   324042152 :     }
     319                 :             : 
     320                 :    16216208 :     T& operator[](size_type pos) {
     321                 :    16216208 :         return *item_ptr(pos);
     322                 :             :     }
     323                 :             : 
     324                 :  1176240307 :     const T& operator[](size_type pos) const {
     325                 :  1176240307 :         return *item_ptr(pos);
     326                 :             :     }
     327                 :             : 
     328                 :   255602249 :     void resize(size_type new_size) {
     329                 :   255602249 :         size_type cur_size = size();
     330         [ +  + ]:   255602249 :         if (cur_size == new_size) {
           [ +  +  #  # ]
     331                 :   205744854 :             return;
     332                 :             :         }
     333         [ +  + ]:    49857395 :         if (cur_size > new_size) {
           [ -  +  #  # ]
     334                 :    22470517 :             erase(item_ptr(new_size), end());
     335                 :    22470517 :             return;
     336                 :             :         }
     337         [ +  + ]:    27386878 :         if (new_size > capacity()) {
           [ +  -  #  # ]
     338                 :    16221637 :             change_capacity(new_size);
     339                 :    16221637 :         }
     340                 :    27386878 :         ptrdiff_t increase = new_size - cur_size;
     341                 :    27386878 :         fill(item_ptr(cur_size), increase);
     342                 :    27386878 :         _size += increase;
     343         [ -  + ]:   255602249 :     }
           [ -  +  #  # ]
     344                 :             : 
     345                 :       14677 :     void reserve(size_type new_capacity) {
     346         [ +  + ]:       14677 :         if (new_capacity > capacity()) {
     347                 :        7623 :             change_capacity(new_capacity);
     348                 :        7623 :         }
     349                 :       14677 :     }
     350                 :             : 
     351                 :    85105814 :     void shrink_to_fit() {
     352                 :    85105814 :         change_capacity(size());
     353                 :    85105814 :     }
     354                 :             : 
     355                 :   196439324 :     void clear() {
     356                 :   196439324 :         resize(0);
     357                 :   196439324 :     }
     358                 :             : 
     359                 :    68348651 :     iterator insert(iterator pos, const T& value) {
     360                 :    68348651 :         size_type p = pos - begin();
     361                 :    68348651 :         size_type new_size = size() + 1;
     362         [ +  + ]:    68348651 :         if (capacity() < new_size) {
     363                 :       77579 :             change_capacity(new_size + (new_size >> 1));
     364                 :       77579 :         }
     365                 :    68348651 :         T* ptr = item_ptr(p);
     366                 :    68348651 :         memmove(ptr + 1, ptr, (size() - p) * sizeof(T));
     367                 :    68348651 :         _size++;
     368                 :    68348651 :         new(static_cast<void*>(ptr)) T(value);
     369                 :    68348651 :         return iterator(ptr);
     370                 :    68348651 :     }
     371                 :             : 
     372                 :       21798 :     void insert(iterator pos, size_type count, const T& value) {
     373                 :       21798 :         size_type p = pos - begin();
     374                 :       21798 :         size_type new_size = size() + count;
     375         [ +  + ]:       21798 :         if (capacity() < new_size) {
     376                 :        1749 :             change_capacity(new_size + (new_size >> 1));
     377                 :        1749 :         }
     378                 :       21798 :         T* ptr = item_ptr(p);
     379                 :       21798 :         memmove(ptr + count, ptr, (size() - p) * sizeof(T));
     380                 :       21798 :         _size += count;
     381                 :       21798 :         fill(item_ptr(p), count, value);
     382                 :       21798 :     }
     383                 :             : 
     384                 :             :     template <std::input_iterator InputIterator>
     385                 :   124513292 :     void insert(iterator pos, InputIterator first, InputIterator last) {
     386                 :   124513292 :         size_type p = pos - begin();
     387                 :   124513292 :         difference_type count = last - first;
     388                 :   124513292 :         size_type new_size = size() + count;
     389   [ +  +  #  #  :   124513292 :         if (capacity() < new_size) {
           +  + ][ +  +  
             +  +  #  # ]
                 [ +  + ]
     390                 :     2685572 :             change_capacity(new_size + (new_size >> 1));
     391                 :     2685572 :         }
     392                 :   124513292 :         T* ptr = item_ptr(p);
     393                 :   124513292 :         memmove(ptr + count, ptr, (size() - p) * sizeof(T));
     394                 :   124513292 :         _size += count;
     395                 :   124513292 :         fill(ptr, first, last);
     396                 :   124513292 :     }
     397                 :             : 
     398                 :     7334031 :     inline void resize_uninitialized(size_type new_size) {
     399                 :             :         // resize_uninitialized changes the size of the prevector but does not initialize it.
     400                 :             :         // If size < new_size, the added elements must be initialized explicitly.
     401         [ +  + ]:     7334031 :         if (capacity() < new_size) {
     402                 :     2526405 :             change_capacity(new_size);
     403                 :     2526405 :             _size += new_size - size();
     404                 :     2526405 :             return;
     405                 :             :         }
     406         [ +  + ]:     4807626 :         if (new_size < size()) {
     407                 :        6754 :             erase(item_ptr(new_size), end());
     408                 :        6754 :         } else {
     409                 :     4800872 :             _size += new_size - size();
     410                 :             :         }
     411                 :     7334031 :     }
     412                 :             : 
     413                 :        9419 :     iterator erase(iterator pos) {
     414                 :        9419 :         return erase(pos, pos + 1);
     415                 :             :     }
     416                 :             : 
     417                 :    22527451 :     iterator erase(iterator first, iterator last) {
     418                 :             :         // Erase is not allowed to the change the object's capacity. That means
     419                 :             :         // that when starting with an indirectly allocated prevector with
     420                 :             :         // size and capacity > N, the result may be a still indirectly allocated
     421                 :             :         // prevector with size <= N and capacity > N. A shrink_to_fit() call is
     422                 :             :         // necessary to switch to the (more efficient) directly allocated
     423                 :             :         // representation (with capacity N and size <= N).
     424                 :    22527451 :         iterator p = first;
     425                 :    22527451 :         char* endp = (char*)&(*end());
     426                 :    22527451 :         _size -= last - p;
     427                 :    22527451 :         memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
     428                 :    22527451 :         return first;
     429                 :    22527451 :     }
     430                 :             : 
     431                 :             :     template<typename... Args>
     432                 :     1224523 :     void emplace_back(Args&&... args) {
     433                 :     1224523 :         size_type new_size = size() + 1;
     434         [ +  + ]:     1224523 :         if (capacity() < new_size) {
     435                 :        9719 :             change_capacity(new_size + (new_size >> 1));
     436                 :        9719 :         }
     437                 :     1224523 :         new(item_ptr(size())) T(std::forward<Args>(args)...);
     438                 :     1224523 :         _size++;
     439                 :     1224523 :     }
     440                 :             : 
     441                 :     1224523 :     void push_back(const T& value) {
     442                 :     1224523 :         emplace_back(value);
     443                 :     1224523 :     }
     444                 :             : 
     445                 :       15549 :     void pop_back() {
     446                 :       15549 :         erase(end() - 1, end());
     447                 :       15549 :     }
     448                 :             : 
     449                 :             :     T& front() {
     450                 :             :         return *item_ptr(0);
     451                 :             :     }
     452                 :             : 
     453                 :             :     const T& front() const {
     454                 :             :         return *item_ptr(0);
     455                 :             :     }
     456                 :             : 
     457                 :       19460 :     T& back() {
     458                 :       19460 :         return *item_ptr(size() - 1);
     459                 :             :     }
     460                 :             : 
     461                 :     3501384 :     const T& back() const {
     462                 :     3501384 :         return *item_ptr(size() - 1);
     463                 :             :     }
     464                 :             : 
     465                 :       20700 :     void swap(prevector<N, T, Size, Diff>& other) noexcept
     466                 :             :     {
     467                 :       20700 :         std::swap(_union, other._union);
     468                 :       20700 :         std::swap(_size, other._size);
     469                 :       20700 :     }
     470                 :             : 
     471                 :   978931130 :     ~prevector() {
     472   [ +  +  +  + ]:   978931130 :         if (!is_direct()) {
           [ +  +  #  # ]
           [ +  -  +  +  
           +  + ][ +  -  
          +  +  +  -  +  
                +  +  - ]
     473                 :   263968253 :             free(_union.indirect_contents.indirect);
     474                 :   263968253 :             _union.indirect_contents.indirect = nullptr;
     475                 :   263968253 :         }
     476                 :   978931130 :     }
     477                 :             : 
     478                 :    85222865 :     bool operator==(const prevector<N, T, Size, Diff>& other) const {
     479         [ +  + ]:    85222865 :         if (other.size() != size()) {
     480                 :         412 :             return false;
     481                 :             :         }
     482                 :    85222453 :         const_iterator b1 = begin();
     483                 :    85222453 :         const_iterator b2 = other.begin();
     484                 :    85222453 :         const_iterator e1 = end();
     485         [ +  + ]:  1535542070 :         while (b1 != e1) {
     486         [ +  + ]:  1468553543 :             if ((*b1) != (*b2)) {
     487                 :    18233926 :                 return false;
     488                 :             :             }
     489                 :  1450319617 :             ++b1;
     490                 :  1450319617 :             ++b2;
     491                 :             :         }
     492                 :    66988527 :         return true;
     493                 :    85222865 :     }
     494                 :             : 
     495                 :       34934 :     bool operator!=(const prevector<N, T, Size, Diff>& other) const {
     496                 :       34934 :         return !(*this == other);
     497                 :             :     }
     498                 :             : 
     499                 :   296584232 :     bool operator<(const prevector<N, T, Size, Diff>& other) const {
     500         [ +  + ]:   296584232 :         if (size() < other.size()) {
     501                 :     2255109 :             return true;
     502                 :             :         }
     503         [ +  + ]:   294329123 :         if (size() > other.size()) {
     504                 :     1379176 :             return false;
     505                 :             :         }
     506                 :   292949947 :         const_iterator b1 = begin();
     507                 :   292949947 :         const_iterator b2 = other.begin();
     508                 :   292949947 :         const_iterator e1 = end();
     509         [ +  + ]:  2118077950 :         while (b1 != e1) {
     510         [ +  + ]:  2064116891 :             if ((*b1) < (*b2)) {
     511                 :   133847893 :                 return true;
     512                 :             :             }
     513         [ +  + ]:  1930268998 :             if ((*b2) < (*b1)) {
     514                 :   105140995 :                 return false;
     515                 :             :             }
     516                 :  1825128003 :             ++b1;
     517                 :  1825128003 :             ++b2;
     518                 :             :         }
     519                 :    53961059 :         return false;
     520                 :   296584232 :     }
     521                 :             : 
     522                 :    28460371 :     size_t allocated_memory() const {
     523         [ +  + ]:    28460371 :         if (is_direct()) {
     524                 :    12245437 :             return 0;
     525                 :             :         } else {
     526                 :    16214934 :             return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
     527                 :             :         }
     528                 :    28460371 :     }
     529                 :             : 
     530                 :    59216427 :     value_type* data() {
     531                 :    59216427 :         return item_ptr(0);
     532                 :             :     }
     533                 :             : 
     534                 :  1035585445 :     const value_type* data() const {
     535                 :  1035585445 :         return item_ptr(0);
     536                 :             :     }
     537                 :             : };
     538                 :             : 
     539                 :             : #endif // BITCOIN_PREVECTOR_H
        

Generated by: LCOV version 2.0-1