#pragma once
#include <iterator>
#include <libsolutil/Common.h>
#include <vector>
#include <type_traits>
#include <cstring>
#include <optional>
#include <string>
#include <set>
#include <functional>
#include <utility>
#include <type_traits>
#include <list>
#include <algorithm>
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U& _b)
{
for (auto const& i: _b)
_a.push_back(T(i));
return _a;
}
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U&& _b)
{
std::move(_b.begin(), _b.end(), std::back_inserter(_a));
return _a;
}
template <class T, class U> std::list<T>& operator+=(std::list<T>& _a, U& _b)
{
for (auto const& i: _b)
_a.push_back(T(i));
return _a;
}
template <class T, class U> std::list<T>& operator+=(std::list<T>& _a, U&& _b)
{
std::move(_b.begin(), _b.end(), std::back_inserter(_a));
return _a;
}
template <class U, class... T> std::multiset<T...>& operator+=(std::multiset<T...>& _a, U& _b)
{
_a.insert(_b.begin(), _b.end());
return _a;
}
template <class U, class... T> std::multiset<T...>& operator+=(std::multiset<T...>& _a, U&& _b)
{
for (auto&& x: _b)
_a.insert(std::move(x));
return _a;
}
template <class U, class... T> std::set<T...>& operator+=(std::set<T...>& _a, U& _b)
{
_a.insert(_b.begin(), _b.end());
return _a;
}
template <class U, class... T> std::set<T...>& operator+=(std::set<T...>& _a, U&& _b)
{
for (auto&& x: _b)
_a.insert(std::move(x));
return _a;
}
template <class T>
inline std::vector<T> operator+(std::vector<T> const& _a, std::vector<T> const& _b)
{
std::vector<T> ret(_a);
ret += _b;
return ret;
}
template <class T>
inline std::vector<T> operator+(std::vector<T>&& _a, std::vector<T>&& _b)
{
std::vector<T> ret(std::move(_a));
assert(&_a != &_b);
ret += std::move(_b);
return ret;
}
template <class U, class... T>
inline std::set<T...> operator+(std::set<T...> const& _a, U&& _b)
{
std::set<T...> ret(_a);
ret += std::forward<U>(_b);
return ret;
}
template <class U, class... T>
inline std::set<T...> operator+(std::set<T...>&& _a, U&& _b)
{
std::set<T...> ret(std::move(_a));
ret += std::forward<U>(_b);
return ret;
}
template <class C, class... T>
inline std::set<T...>& operator-=(std::set<T...>& _a, C const& _b)
{
for (auto const& x: _b)
_a.erase(x);
return _a;
}
template <class C, class... T>
inline std::set<T...> operator-(std::set<T...> const& _a, C const& _b)
{
auto result = _a;
result -= _b;
return result;
}
template <class C, class... T>
inline std::multiset<T...>& operator-=(std::multiset<T...>& _a, C const& _b)
{
for (auto const& x: _b)
_a.erase(x);
return _a;
}
namespace solidity::util
{
template<class Container, class Callable, class OutputContainer =
std::vector<std::invoke_result_t<
Callable,
decltype(*std::begin(std::declval<Container>()))
>>>
auto applyMap(Container const& _c, Callable&& _op, OutputContainer _oc = OutputContainer{})
{
std::transform(std::begin(_c), std::end(_c), std::inserter(_oc, std::end(_oc)), _op);
return _oc;
}
template<typename T>
std::vector<T> filter(std::vector<T> const& _vec, std::vector<bool> const& _mask)
{
assert(_vec.size() == _mask.size());
std::vector<T> ret;
for (size_t i = 0; i < _mask.size(); ++i)
if (_mask[i])
ret.push_back(_vec[i]);
return ret;
}
template<class C, class T, class Callable>
auto fold(C const& _c, T _acc, Callable&& _binaryOp)
{
for (auto const& e: _c)
_acc = _binaryOp(std::move(_acc), e);
return _acc;
}
template <class T, class U>
T convertContainer(U const& _from)
{
return T{_from.cbegin(), _from.cend()};
}
template <class T, class U>
T convertContainer(U&& _from)
{
return T{
std::make_move_iterator(_from.begin()),
std::make_move_iterator(_from.end())
};
}
template <typename K, typename V>
std::map<V, K> invertMap(std::map<K, V> const& originalMap)
{
std::map<V, K> inverseMap;
for (auto const& originalPair: originalMap)
{
assert(inverseMap.count(originalPair.second) == 0);
inverseMap.insert({originalPair.second, originalPair.first});
}
return inverseMap;
}
template <typename K, typename V>
std::set<K> keys(std::map<K, V> const& _map)
{
return applyMap(_map, [](auto const& _elem) { return _elem.first; }, std::set<K>{});
}
template<typename MapType, typename KeyType>
decltype(auto) valueOrNullptr(MapType&& _map, KeyType const& _key)
{
auto it = _map.find(_key);
return (it == _map.end()) ? nullptr : &it->second;
}
namespace detail
{
struct allow_copy {};
}
static constexpr auto allow_copy = detail::allow_copy{};
template<
typename MapType,
typename KeyType,
typename ValueType = std::decay_t<decltype(std::declval<MapType>().find(std::declval<KeyType>())->second)> const&,
typename AllowCopyType = std::conditional_t<std::is_pod_v<ValueType> || std::is_pointer_v<ValueType>, detail::allow_copy, void*>
>
decltype(auto) valueOrDefault(
MapType&& _map,
KeyType const& _key,
ValueType&& _defaultValue = {},
AllowCopyType = {}
)
{
auto it = _map.find(_key);
static_assert(
std::is_same_v<AllowCopyType, detail::allow_copy> ||
std::is_reference_v<decltype((it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second)>,
"valueOrDefault does not allow copies by default. Pass allow_copy as additional argument, if you want to allow copies."
);
return (it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second;
}
namespace detail
{
template<typename Callable>
struct MapTuple
{
Callable callable;
template<typename TupleType>
decltype(auto) operator()(TupleType&& _tuple) {
using PlainTupleType = std::remove_cv_t<std::remove_reference_t<TupleType>>;
return operator()(
std::forward<TupleType>(_tuple),
std::make_index_sequence<std::tuple_size_v<PlainTupleType>>{}
);
}
private:
template<typename TupleType, size_t... I>
decltype(auto) operator()(TupleType&& _tuple, std::index_sequence<I...>)
{
return callable(std::get<I>(std::forward<TupleType>(_tuple))...);
}
};
}
template<typename Callable>
decltype(auto) mapTuple(Callable&& _callable)
{
return detail::MapTuple<Callable>{std::forward<Callable>(_callable)};
}
template <class K, class V, class F>
void joinMap(std::map<K, V>& _a, std::map<K, V>&& _b, F _conflictSolver)
{
auto ita = _a.begin();
auto aend = _a.end();
auto itb = _b.begin();
auto bend = _b.end();
for (; itb != bend; ++ita)
{
if (ita == aend)
ita = _a.insert(ita, std::move(*itb++));
else if (ita->first < itb->first)
continue;
else if (itb->first < ita->first)
ita = _a.insert(ita, std::move(*itb++));
else
{
_conflictSolver(ita->second, std::move(itb->second));
++itb;
}
}
}
namespace detail
{
template<typename Container, typename Value>
auto findOffset(Container&& _container, Value&& _value, int)
-> decltype(_container.find(_value) == _container.end(), std::distance(_container.begin(), _container.find(_value)), std::optional<size_t>())
{
auto it = _container.find(std::forward<Value>(_value));
auto end = _container.end();
if (it == end)
return std::nullopt;
return std::distance(_container.begin(), it);
}
template<typename Range, typename Value>
auto findOffset(Range&& _range, Value&& _value, void*)
-> decltype(std::find(std::begin(_range), std::end(_range), std::forward<Value>(_value)) == std::end(_range), std::optional<size_t>())
{
auto begin = std::begin(_range);
auto end = std::end(_range);
auto it = std::find(begin, end, std::forward<Value>(_value));
if (it == end)
return std::nullopt;
return std::distance(begin, it);
}
}
template<typename Range>
auto findOffset(Range&& _range, std::remove_reference_t<decltype(*std::cbegin(_range))> const& _value)
-> decltype(detail::findOffset(std::forward<Range>(_range), _value, 0))
{
return detail::findOffset(std::forward<Range>(_range), _value, 0);
}
enum class WhenError
{
DontThrow = 0,
Throw = 1,
};
enum class HexPrefix
{
DontAdd = 0,
Add = 1,
};
enum class HexCase
{
Lower = 0,
Upper = 1,
Mixed = 2,
};
std::string toHex(uint8_t _data, HexCase _case = HexCase::Lower);
std::string toHex(bytes const& _data, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower);
int fromHex(char _i, WhenError _throw);
bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow);
inline std::string asString(bytes const& _b)
{
return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
}
inline std::string asString(bytesConstRef _b)
{
return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
}
inline bytes asBytes(std::string const& _b)
{
return bytes((uint8_t const*)_b.data(), (uint8_t const*)(_b.data() + _b.size()));
}
template <class T, class V>
bool contains(T const& _t, V const& _v)
{
return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
}
template <class T, class Predicate>
bool contains_if(T const& _t, Predicate const& _p)
{
return std::end(_t) != std::find_if(std::begin(_t), std::end(_t), _p);
}
template <typename T, typename F>
void iterateReplacing(std::vector<T>& _vector, F const& _f)
{
bool useModified = false;
std::vector<T> modifiedVector;
for (size_t i = 0; i < _vector.size(); ++i)
{
if (std::optional<std::vector<T>> r = _f(_vector[i]))
{
if (!useModified)
{
std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
useModified = true;
}
modifiedVector += std::move(*r);
}
else if (useModified)
modifiedVector.emplace_back(std::move(_vector[i]));
}
if (useModified)
_vector = std::move(modifiedVector);
}
namespace detail
{
template <typename T, typename F, std::size_t... I>
void iterateReplacingWindow(std::vector<T>& _vector, F const& _f, std::index_sequence<I...>)
{
bool useModified = false;
std::vector<T> modifiedVector;
size_t i = 0;
for (; i + sizeof...(I) <= _vector.size(); ++i)
{
if (std::optional<std::vector<T>> r = _f(_vector[i + I]...))
{
if (!useModified)
{
std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
useModified = true;
}
modifiedVector += std::move(*r);
i += sizeof...(I) - 1;
}
else if (useModified)
modifiedVector.emplace_back(std::move(_vector[i]));
}
if (useModified)
{
for (; i < _vector.size(); ++i)
modifiedVector.emplace_back(std::move(_vector[i]));
_vector = std::move(modifiedVector);
}
}
}
template <std::size_t N, typename T, typename F>
void iterateReplacingWindow(std::vector<T>& _vector, F const& _f)
{
detail::iterateReplacingWindow(_vector, _f, std::make_index_sequence<N>{});
}
bool passesAddressChecksum(std::string const& _str, bool _strict);
std::string getChecksummedAddress(std::string const& _addr);
bool isValidHex(std::string const& _string);
bool isValidDecimal(std::string const& _string);
std::string formatAsStringOrNumber(std::string const& _value);
std::string escapeAndQuoteString(std::string const& _input);
template<typename Container, typename Compare>
bool containerEqual(Container const& _lhs, Container const& _rhs, Compare&& _compare)
{
return std::equal(std::begin(_lhs), std::end(_lhs), std::begin(_rhs), std::end(_rhs), std::forward<Compare>(_compare));
}
inline std::string findAnyOf(std::string const& _haystack, std::vector<std::string> const& _needles)
{
for (std::string const& needle: _needles)
if (_haystack.find(needle) != std::string::npos)
return needle;
return "";
}
namespace detail
{
template<typename T>
void variadicEmplaceBack(std::vector<T>&) {}
template<typename T, typename A, typename... Args>
void variadicEmplaceBack(std::vector<T>& _vector, A&& _a, Args&&... _args)
{
_vector.emplace_back(std::forward<A>(_a));
variadicEmplaceBack(_vector, std::forward<Args>(_args)...);
}
}
template<typename T, typename... Args>
std::vector<T> make_vector(Args&&... _args)
{
std::vector<T> result;
result.reserve(sizeof...(_args));
detail::variadicEmplaceBack(result, std::forward<Args>(_args)...);
return result;
}
}