LCOV - code coverage report
Current view: top level - src/util - vector.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 28 28
Test Date: 2024-09-01 05:20:30 Functions: 92.3 % 39 36
Branches: 33.0 % 327 108

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-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_UTIL_VECTOR_H
       6                 :             : #define BITCOIN_UTIL_VECTOR_H
       7                 :             : 
       8                 :             : #include <functional>
       9                 :             : #include <initializer_list>
      10                 :             : #include <optional>
      11                 :             : #include <type_traits>
      12                 :             : #include <utility>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : /** Construct a vector with the specified elements.
      16                 :             :  *
      17                 :             :  * This is preferable over the list initializing constructor of std::vector:
      18                 :             :  * - It automatically infers the element type from its arguments.
      19                 :             :  * - If any arguments are rvalue references, they will be moved into the vector
      20                 :             :  *   (list initialization always copies).
      21                 :             :  */
      22                 :             : template<typename... Args>
      23                 :    31256097 : inline std::vector<typename std::common_type<Args...>::type> Vector(Args&&... args)
      24                 :             : {
      25                 :    31256097 :     std::vector<typename std::common_type<Args...>::type> ret;
      26   [ +  -  #  # ]:    31256097 :     ret.reserve(sizeof...(args));
         [ +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  #  #  #  
           # ][ #  #  #  
          #  +  -  +  -  
          +  -  #  #  #  
          #  #  #  +  -  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      27                 :             :     // The line below uses the trick from https://www.experts-exchange.com/articles/32502/None-recursive-variadic-templates-with-std-initializer-list.html
      28   [ +  -  +  -  :    31256097 :     (void)std::initializer_list<int>{(ret.emplace_back(std::forward<Args>(args)), 0)...};
             +  -  #  # ]
           [ +  -  +  - ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  #  #  #  
           # ][ #  #  #  
          #  +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      29                 :    31256097 :     return ret;
      30   [ +  -  #  # ]:    31256097 : }
         [ +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  #  #  #  
           # ][ #  #  #  
          #  +  -  +  -  
          +  -  #  #  #  
          #  #  #  +  -  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      31                 :             : 
      32                 :             : /** Concatenate two vectors, moving elements. */
      33                 :             : template<typename V>
      34                 :     4889072 : inline V Cat(V v1, V&& v2)
      35                 :             : {
      36                 :     4889072 :     v1.reserve(v1.size() + v2.size());
      37   [ #  #  #  # ]:     9336374 :     for (auto& arg : v2) {
                 [ +  + ]
      38                 :     4447302 :         v1.push_back(std::move(arg));
      39                 :     4447302 :     }
      40                 :     4889072 :     return v1;
      41                 :             : }
      42                 :             : 
      43                 :             : /** Concatenate two vectors. */
      44                 :             : template<typename V>
      45                 :        1826 : inline V Cat(V v1, const V& v2)
      46                 :             : {
      47                 :        1826 :     v1.reserve(v1.size() + v2.size());
      48         [ +  + ]:        3652 :     for (const auto& arg : v2) {
      49                 :        1826 :         v1.push_back(arg);
      50                 :        1826 :     }
      51                 :        1826 :     return v1;
      52                 :             : }
      53                 :             : 
      54                 :             : /** Clear a vector (or std::deque) and release its allocated memory. */
      55                 :             : template<typename V>
      56                 :      520795 : inline void ClearShrink(V& v) noexcept
      57                 :             : {
      58                 :             :     // There are various ways to clear a vector and release its memory:
      59                 :             :     //
      60                 :             :     // 1. V{}.swap(v)
      61                 :             :     // 2. v = V{}
      62                 :             :     // 3. v = {}; v.shrink_to_fit();
      63                 :             :     // 4. v.clear(); v.shrink_to_fit();
      64                 :             :     //
      65                 :             :     // (2) does not appear to release memory in glibc debug mode, even if v.shrink_to_fit()
      66                 :             :     // follows. (3) and (4) rely on std::vector::shrink_to_fit, which is only a non-binding
      67                 :             :     // request. Therefore, we use method (1).
      68                 :             : 
      69   [ +  -  +  - ]:      520795 :     V{}.swap(v);
      70                 :      520795 : }
      71                 :             : 
      72                 :             : template<typename V, typename L>
      73                 :        1711 : inline std::optional<V> FindFirst(const std::vector<V>& vec, const L fnc)
      74                 :             : {
      75   [ #  #  #  #  :        5349 :     for (const auto& el : vec) {
          #  #  #  #  #  
           # ][ +  +  -  
                   +  + ]
      76   [ #  #  #  # ]:        3638 :         if (fnc(el)) {
                 [ +  + ]
      77                 :        1495 :             return el;
      78                 :             :         }
      79   [ #  #  #  # ]:        3638 :     }
                 [ +  + ]
      80                 :         216 :     return std::nullopt;
      81                 :        1711 : }
      82                 :             : 
      83                 :             : #endif // BITCOIN_UTIL_VECTOR_H
        

Generated by: LCOV version 2.0-1