Skip to content

Commit dc42563

Browse files
sipafurszy
authored andcommitted
Add some general std::vector utility functions
Added are: * Vector(arg1,arg2,arg3,...) constructs a vector with the specified arguments as elements. The vector's type is derived from the arguments. If some of the arguments are rvalue references, they will be moved into place rather than copied (which can't be achieved using list initialization). * Cat(vector1,vector2) returns a concatenation of the two vectors, efficiently moving elements when relevant. Vector generalizes (and replaces) the Singleton function in src/descriptor.cpp, and Cat replaces the Cat function in bech32.cpp
1 parent 7a6f029 commit dc42563

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ BITCOIN_CORE_H = \
290290
utilstrencodings.h \
291291
utilmoneystr.h \
292292
utiltime.h \
293+
util/vector.h \
293294
validation.h \
294295
validationinterface.h \
295296
version.h \

src/bech32.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "bech32.h"
6+
#include "util/vector.h"
7+
8+
#include <assert.h>
69

710
namespace
811
{
@@ -24,13 +27,6 @@ const int8_t CHARSET_REV[128] = {
2427
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
2528
};
2629

27-
/** Concatenate two byte arrays. */
28-
data Cat(data x, const data& y)
29-
{
30-
x.insert(x.end(), y.begin(), y.end());
31-
return x;
32-
}
33-
3430
/** This function will compute what 6 5-bit values to XOR into the last 6 input values, in order to
3531
* make the checksum 0. These 6 values are packed together in a single 30-bit integer. The higher
3632
* bits correspond to earlier values. */

src/txdb.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "uint256.h"
1212
#include "util/system.h"
1313
#include "zpiv/zerocoin.h"
14+
#include <"til/vector.h"
1415
1516
#include <stdint.h>
1617

@@ -113,7 +114,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap& mapCoins,
113114
// A vector is used for future extensibility, as we may want to support
114115
// interrupting after partial writes from multiple independent reorgs.
115116
batch.Erase(DB_BEST_BLOCK);
116-
batch.Write(DB_HEAD_BLOCKS, std::vector<uint256>{hashBlock, old_tip});
117+
batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
117118

118119
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
119120
if (it->second.flags & CCoinsCacheEntry::DIRTY) {

src/util/vector.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2019 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 <initializer_list>
9+
#include <type_traits>
10+
#include <vector>
11+
12+
/** Construct a vector with the specified elements.
13+
*
14+
* This is preferable over the list initializing constructor of std::vector:
15+
* - It automatically infers the element type from its arguments.
16+
* - If any arguments are rvalue references, they will be moved into the vector
17+
* (list initialization always copies).
18+
*/
19+
template<typename... Args>
20+
inline std::vector<typename std::common_type<Args...>::type> Vector(Args&&... args)
21+
{
22+
std::vector<typename std::common_type<Args...>::type> ret;
23+
ret.reserve(sizeof...(args));
24+
// The line below uses the trick from https://www.experts-exchange.com/articles/32502/None-recursive-variadic-templates-with-std-initializer-list.html
25+
(void)std::initializer_list<int>{(ret.emplace_back(std::forward<Args>(args)), 0)...};
26+
return ret;
27+
}
28+
29+
/** Concatenate two vectors, moving elements. */
30+
template<typename V>
31+
inline V Cat(V v1, V&& v2)
32+
{
33+
v1.reserve(v1.size() + v2.size());
34+
for (auto& arg : v2) {
35+
v1.push_back(std::move(arg));
36+
}
37+
return v1;
38+
}
39+
40+
/** Concatenate two vectors. */
41+
template<typename V>
42+
inline V Cat(V v1, const V& v2)
43+
{
44+
v1.reserve(v1.size() + v2.size());
45+
for (const auto& arg : v2) {
46+
v1.push_back(arg);
47+
}
48+
return v1;
49+
}
50+
51+
#endif // BITCOIN_UTIL_VECTOR_H

0 commit comments

Comments
 (0)