mirror of
https://github.com/monero-project/monero.git
synced 2024-12-22 19:49:30 +00:00
revisions
This commit is contained in:
parent
e5aa058a8d
commit
f50b9e3803
1 changed files with 24 additions and 16 deletions
|
@ -45,8 +45,15 @@
|
||||||
namespace tools
|
namespace tools
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// convert an arbitrary function to functor
|
||||||
|
template <typename F>
|
||||||
|
inline auto as_functor(F f)
|
||||||
|
{
|
||||||
|
return [f = std::move(f)](auto&&... args) { return f(std::forward<decltype(args)>(args)...); };
|
||||||
|
}
|
||||||
/// convert a binary comparison function to a functor
|
/// convert a binary comparison function to a functor
|
||||||
template <typename T, typename ComparisonOpT = bool(const T &a, const T &b)>
|
/// note: for most use-cases 'const T&' will work because only non-trivial types need a user-defined comparison operation
|
||||||
|
template <typename T, typename ComparisonOpT = bool(const T&, const T&)>
|
||||||
inline auto compare_func(ComparisonOpT comparison_op_func)
|
inline auto compare_func(ComparisonOpT comparison_op_func)
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
|
@ -62,7 +69,7 @@ inline auto compare_func(ComparisonOpT comparison_op_func)
|
||||||
"invalid callable - expected callable in form bool(T, T)"
|
"invalid callable - expected callable in form bool(T, T)"
|
||||||
);
|
);
|
||||||
|
|
||||||
return [func = std::move(comparison_op_func)] (const T &a, const T &b) -> bool { return func(a, b); };
|
return as_functor(std::move(comparison_op_func));
|
||||||
}
|
}
|
||||||
/// test if a container is sorted and unique according to a comparison criteria (defaults to operator<)
|
/// test if a container is sorted and unique according to a comparison criteria (defaults to operator<)
|
||||||
/// NOTE: ComparisonOpT must establish 'strict weak ordering' https://en.cppreference.com/w/cpp/named_req/Compare
|
/// NOTE: ComparisonOpT must establish 'strict weak ordering' https://en.cppreference.com/w/cpp/named_req/Compare
|
||||||
|
@ -88,7 +95,7 @@ bool is_sorted_and_unique(const T &container, ComparisonOpT comparison_op = Comp
|
||||||
|
|
||||||
if (std::adjacent_find(container.begin(),
|
if (std::adjacent_find(container.begin(),
|
||||||
container.end(),
|
container.end(),
|
||||||
[comparison_op](const ValueT &a, const ValueT &b) -> bool
|
[comparison_op = std::move(comparison_op)](const ValueT &a, const ValueT &b) -> bool
|
||||||
{
|
{
|
||||||
return !comparison_op(a, b) && !comparison_op(b, a);
|
return !comparison_op(a, b) && !comparison_op(b, a);
|
||||||
})
|
})
|
||||||
|
@ -100,31 +107,32 @@ bool is_sorted_and_unique(const T &container, ComparisonOpT comparison_op = Comp
|
||||||
/// specialization for raw function pointers
|
/// specialization for raw function pointers
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool is_sorted_and_unique(const T &container,
|
bool is_sorted_and_unique(const T &container,
|
||||||
bool (*const comparison_op_func)(const typename T::value_type &a, const typename T::value_type &b))
|
bool (*const comparison_op_func)(const typename T::value_type&, const typename T::value_type&))
|
||||||
{
|
{
|
||||||
return is_sorted_and_unique(container, compare_func<typename T::value_type>(comparison_op_func));
|
return is_sorted_and_unique(container, compare_func<typename T::value_type>(comparison_op_func));
|
||||||
}
|
}
|
||||||
/// convenience wrapper for checking if a mapped object is mapped to a key embedded in that object
|
/// convenience wrapper for checking if the key to a mapped object is correct for that object
|
||||||
/// example: std::unorderd_map<rct::key, std::pair<rct::key, rct::xmr_amount>> where the map key is supposed to
|
/// example: std::unorderd_map<rct::key, std::pair<rct::key, rct::xmr_amount>> where the map key is supposed to
|
||||||
/// reproduce the pair's rct::key; use the predicate to get the pair's rct::key element
|
/// reproduce the pair's rct::key; use the predicate to check that relationship
|
||||||
template <typename KeyT, typename ValueT, typename PredT>
|
template <typename KeyT, typename ValueT, typename PredT>
|
||||||
bool keys_match_internal_values(const std::unordered_map<KeyT, ValueT> &map, PredT get_internal_key_func)
|
bool keys_match_internal_values(const std::unordered_map<KeyT, ValueT> &map, PredT check_key_func)
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same<
|
std::is_same<
|
||||||
std::remove_cv_t<std::remove_reference_t<
|
bool,
|
||||||
KeyT
|
decltype(
|
||||||
>>,
|
check_key_func(
|
||||||
std::remove_cv_t<std::remove_reference_t<
|
std::declval<std::remove_cv_t<KeyT>>(),
|
||||||
decltype(get_internal_key_func(std::declval<std::remove_cv_t<std::remove_reference_t<ValueT>>>()))
|
std::declval<std::remove_cv_t<ValueT>>()
|
||||||
>>
|
)
|
||||||
|
)
|
||||||
>::value,
|
>::value,
|
||||||
"invalid callable - expected callable in form Key(Value)"
|
"invalid callable - expected callable in form bool(KeyT, ValueT)"
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const auto &map_element : map)
|
for (const auto &map_element : map)
|
||||||
{
|
{
|
||||||
if (!(map_element.first == get_internal_key_func(map_element.second)))
|
if (!check_key_func(map_element.first, map_element.second))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +153,7 @@ void for_all_in_map_erase_if(std::unordered_map<KeyT, ValueT> &map_inout, PredT
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same<
|
std::is_same<
|
||||||
bool,
|
bool,
|
||||||
decltype(predicate(std::declval<std::remove_cv_t<std::remove_reference_t<MapValueT>>>()))
|
decltype(predicate(std::declval<std::remove_cv_t<MapValueT>>()))
|
||||||
>::value,
|
>::value,
|
||||||
"invalid callable - expected callable in form bool(Value)"
|
"invalid callable - expected callable in form bool(Value)"
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue