Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Created February 5, 2018 17:02
Show Gist options
  • Save eguiraud/08a96c13e4440967b56c04cc98d81d28 to your computer and use it in GitHub Desktop.
Save eguiraud/08a96c13e4440967b56c04cc98d81d28 to your computer and use it in GitHub Desktop.
#include <ROOT/TypeTraits.hxx>
#include <cassert>
#include <functional>
namespace ROOT {
namespace Internal {
template <typename... ArgTypes, typename F>
std::function<bool(ArgTypes...)> NotHelper(ROOT::TypeTraits::TypeList<ArgTypes...>, F &&f)
{
return std::function<bool(ArgTypes...)>([&](ArgTypes... args) { return !f(args...); });
}
} // namespace Internal
namespace Experimental {
namespace TDF {
template <typename F, typename Args = typename ROOT::TypeTraits::CallableTraits<typename std::decay<F>::type>::arg_types_nodecay,
typename Ret = typename ROOT::TypeTraits::CallableTraits<typename std::decay<F>::type>::ret_type>
auto Not(F &&f) -> decltype(ROOT::Internal::NotHelper(Args(), std::forward<F>(f)))
{
static_assert(std::is_same<Ret, bool>::value, "TDF::Not requires a callable that returns a bool.");
return ROOT::Internal::NotHelper(Args(), std::move(f));
}
} // namespace TDF
} // namespace Experimental
} // namespace ROOT
using namespace ROOT::Experimental;
int main()
{
auto isPos = [](int a) { return a > 0; };
assert(TDF::Not(isPos)(-1) == true);
auto isNeg = TDF::Not(isPos);
assert(!isNeg(3));
return 0;
}
@dpiparo
Copy link

dpiparo commented Feb 5, 2018

So this is a very nice implementation of std::not_fn which works for 99% of the tdf cases. I support it: presently I would not know how to make not_fn work with TDF.
I cannot resist to ask, also to myself something: how do we support this functionality for jitted filters?

@dpiparo
Copy link

dpiparo commented Feb 5, 2018

uhm, spelling the issue was helpful. The jitted case is actually perhaps simpler.

tdf.Filter("myComplexExpressionInvolvingColNames").[...]

becomes

tdf.Filter("!(myComplexExpressionInvolvingColNames)").[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment