diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index c157583a2..63a374c30 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -34,6 +34,47 @@ #include #include +/// Operators need to stay in the global namespace. + +/// Concatenate the contents of a container onto a vector +template std::vector& operator+=(std::vector& _a, U const& _b) +{ + for (auto const& i: _b) + _a.push_back(i); + return _a; +} +/// Concatenate the contents of a container onto a vector, move variant. +template std::vector& operator+=(std::vector& _a, U&& _b) +{ + std::move(_b.begin(), _b.end(), std::back_inserter(_a)); + return _a; +} +/// Concatenate the contents of a container onto a set +template std::set& operator+=(std::set& _a, U const& _b) +{ + _a.insert(_b.begin(), _b.end()); + return _a; +} +/// Concatenate two vectors of elements. +template +inline std::vector operator+(std::vector const& _a, std::vector const& _b) +{ + std::vector ret(_a); + ret += _b; + return ret; +} +/// Concatenate two vectors of elements, moving them. +template +inline std::vector operator+(std::vector&& _a, std::vector&& _b) +{ + std::vector ret(std::move(_a)); + if (&_a == &_b) + ret += ret; + else + ret += std::move(_b); + return ret; +} + namespace dev { @@ -190,52 +231,12 @@ inline unsigned bytesRequired(T _i) for (; _i != 0; ++i, _i >>= 8) {} return i; } -/// Concatenate the contents of a container onto a vector -template std::vector& operator+=(std::vector& _a, U const& _b) -{ - for (auto const& i: _b) - _a.push_back(i); - return _a; -} -/// Concatenate the contents of a container onto a vector, move variant. -template std::vector& operator+=(std::vector& _a, U&& _b) -{ - std::move(_b.begin(), _b.end(), std::back_inserter(_a)); - return _a; -} -/// Concatenate the contents of a container onto a set -template std::set& operator+=(std::set& _a, U const& _b) -{ - _a.insert(_b.begin(), _b.end()); - return _a; -} -/// Concatenate two vectors of elements. -template -inline std::vector operator+(std::vector const& _a, std::vector const& _b) -{ - std::vector ret(_a); - ret += _b; - return ret; -} -/// Concatenate two vectors of elements, moving them. -template -inline std::vector operator+(std::vector&& _a, std::vector&& _b) -{ - std::vector ret(std::move(_a)); - if (&_a == &_b) - ret += ret; - else - ret += std::move(_b); - return ret; -} - template bool contains(T const& _t, V const& _v) { return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v); } - /// Function that iterates over a vector, calling a function on each of its /// elements. If that function returns a vector, the element is replaced by /// the returned vector. During the iteration, the original vector is only valid