tuple/value: add all and none queries

This commit is contained in:
Danny Robson 2020-02-25 11:17:05 +11:00
parent 1427a61745
commit 61cc9b9ed2
2 changed files with 88 additions and 0 deletions

View File

@ -37,5 +37,25 @@ main (void)
tap.expect_eq (result, expected, "tuple reverse"); tap.expect_eq (result, expected, "tuple reverse");
} }
tap.expect (
cruft::tuple::value::all (std::tuple<bool,int> { true, 3 }),
"tuple::all succeeds"
);
tap.expect (
!cruft::tuple::value::all (std::tuple<bool,int> (true, 1)),
"tuple::all fails"
);
tap.expect (
cruft::tuple::value::none (std::tuple<bool,int> (false, 0)),
"tuple::none succeeds"
);
tap.expect (
cruft::tuple::value::none (std::tuple<bool,int> (false, 0)),
"tuple::none fails"
);
return tap.status (); return tap.status ();
} }

View File

@ -66,6 +66,74 @@ namespace cruft::tuple::value {
} }
///////////////////////////////////////////////////////////////////////////
namespace detail {
template <
typename TupleT,
typename FunctionT,
std::size_t... IndicesV
>
bool
all (TupleT &&val, FunctionT &&pred, std::index_sequence<IndicesV...>)
{
return (pred (std::get<IndicesV> (val)) && ...);
}
}
///------------------------------------------------------------------------
/// Returns true if all members of the tuple satisfy the predicate.
template <typename TupleT, typename FunctionT>
bool
all (TupleT &&val, FunctionT &&pred)
{
return detail::all (
std::forward<TupleT> (val),
std::forward<FunctionT> (pred),
std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<TupleT>>> {}
);
}
///------------------------------------------------------------------------
/// Returns true if all members of the tuple are convertible to true.
template <typename TupleT>
bool
all (TupleT &&val)
{
return all (
std::forward<TupleT> (val),
[] (auto const &i) { return bool (i); }
);
}
///------------------------------------------------------------------------
/// Returns true if none of the members of the tuple satisfy the predicate.
template <typename TupleT, typename FunctionT>
bool
none (TupleT &&val, FunctionT &&pred)
{
return all (
std::forward<TupleT> (val),
[&pred] (auto &&arg) { return !pred (arg); }
);
}
///------------------------------------------------------------------------
/// Returns true if none of the members of the tuple are convertible to
/// true.
template <typename TupleT>
bool
none (TupleT &&val)
{
return none (
std::forward<TupleT> (val),
[] (auto const &i) { return bool (i); }
);
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
namespace detail { namespace detail {
template <typename FuncT, typename ArgT, std::size_t ...Indices> template <typename FuncT, typename ArgT, std::size_t ...Indices>