/* Function objects for OctaSTD. * * This file is part of OctaSTD. See COPYING.md for futher information. */ #ifndef OCTA_FUNCTIONAL_H #define OCTA_FUNCTIONAL_H #include "octa/new.h" #include "octa/memory.h" #include "octa/utility.h" #include "octa/type_traits.h" namespace octa { /* basic function objects */ #define OCTA_DEFINE_BINARY_OP(_name, _op, _rettype) \ template struct _name { \ _rettype operator()(const T &x, const T &y) const { \ return x _op y; \ } \ typedef T FirstArgument; \ typedef T SecondArgument; \ typedef _rettype Result; \ }; OCTA_DEFINE_BINARY_OP(Less, <, bool) OCTA_DEFINE_BINARY_OP(LessEqual, <=, bool) OCTA_DEFINE_BINARY_OP(Greater, >, bool) OCTA_DEFINE_BINARY_OP(GreaterEqual, >=, bool) OCTA_DEFINE_BINARY_OP(Equal, ==, bool) OCTA_DEFINE_BINARY_OP(NotEqual, !=, bool) OCTA_DEFINE_BINARY_OP(LogicalAnd, &&, bool) OCTA_DEFINE_BINARY_OP(LogicalOr, ||, bool) OCTA_DEFINE_BINARY_OP(Modulus, %, T) OCTA_DEFINE_BINARY_OP(Multiplies, *, T) OCTA_DEFINE_BINARY_OP(Divides, /, T) OCTA_DEFINE_BINARY_OP(Plus, +, T) OCTA_DEFINE_BINARY_OP(Minus, -, T) OCTA_DEFINE_BINARY_OP(BitAnd, &, T) OCTA_DEFINE_BINARY_OP(BitOr, |, T) OCTA_DEFINE_BINARY_OP(BitXor, ^, T) #undef OCTA_DEFINE_BINARY_OP template struct LogicalNot { bool operator()(const T &x) const { return !x; } typedef T Argument; typedef bool Result; }; template struct Negate { bool operator()(const T &x) const { return -x; } typedef T Argument; typedef T Result; }; template struct BinaryNegate { typedef typename T::FirstArgument FirstArgument; typedef typename T::SecondArgument SecondArgument; typedef bool Result; explicit BinaryNegate(const T &f): p_fn(f) {} bool operator()(const FirstArgument &x, const SecondArgument &y) { return !p_fn(x, y); } private: T p_fn; }; template struct UnaryNegate { typedef typename T::Argument Argument; typedef bool Result; explicit UnaryNegate(const T &f): p_fn(f) {} bool operator()(const Argument &x) { return !p_fn(x); } private: T p_fn; }; template UnaryNegate not1(const T &fn) { return UnaryNegate(fn); } template BinaryNegate not2(const T &fn) { return BinaryNegate(fn); } /* hash */ template struct Hash; namespace detail { template struct HashBase { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { return size_t(v); } }; } #define OCTA_HASH_BASIC(T) template<> struct Hash: octa::detail::HashBase {}; OCTA_HASH_BASIC(bool) OCTA_HASH_BASIC(char) OCTA_HASH_BASIC(schar) OCTA_HASH_BASIC(uchar) OCTA_HASH_BASIC(char16_t) OCTA_HASH_BASIC(char32_t) OCTA_HASH_BASIC(wchar_t) OCTA_HASH_BASIC(short) OCTA_HASH_BASIC(ushort) OCTA_HASH_BASIC(int) OCTA_HASH_BASIC(uint) OCTA_HASH_BASIC(long) OCTA_HASH_BASIC(ulong) #undef OCTA_HASH_BASIC namespace detail { static inline size_t mem_hash(const void *p, size_t l) { const uchar *d = (const uchar *)p; size_t h = 5381; for (size_t i = 0; i < l; ++i) h = ((h << 5) + h) ^ d[i]; return h; } template struct ScalarHash; template struct ScalarHash { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { union { T v; size_t h; } u; u.h = 0; u.v = v; return u.h; } }; template struct ScalarHash { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { union { T v; size_t h; } u; u.v = v; return u.h; } }; template struct ScalarHash { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { union { T v; struct { size_t h1, h2; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } }; template struct ScalarHash { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { union { T v; struct { size_t h1, h2, h3; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } }; template struct ScalarHash { typedef T Argument; typedef size_t Result; size_t operator()(T v) const { union { T v; struct { size_t h1, h2, h3, h4; }; } u; u.v = v; return mem_hash((const void *)&u, sizeof(u)); } }; } /* namespace detail */ template<> struct Hash: octa::detail::ScalarHash {}; template<> struct Hash: octa::detail::ScalarHash {}; template<> struct Hash: octa::detail::ScalarHash { size_t operator()(float v) const { if (v == 0) return 0; return octa::detail::ScalarHash::operator()(v); } }; template<> struct Hash: octa::detail::ScalarHash { size_t operator()(double v) const { if (v == 0) return 0; return octa::detail::ScalarHash::operator()(v); } }; template<> struct Hash: octa::detail::ScalarHash { size_t operator()(ldouble v) const { if (v == 0) return 0; #ifdef __i386__ union { ldouble v; struct { size_t h1, h2, h3, h4; }; } u; u.h1 = u.h2 = u.h3 = u.h4 = 0; u.v = v; return (u.h1 ^ u.h2 ^ u.h3 ^ u.h4); #else #ifdef __x86_64__ union { ldouble v; struct { size_t h1, h2; }; } u; u.h1 = u.h2 = 0; u.v = v; return (u.h1 ^ u.h2); #else return octa::detail::ScalarHash::operator()(v); #endif #endif } }; template struct Hash { typedef T *Argument; typedef size_t Result; size_t operator()(T *v) const { union { T *v; size_t h; } u; u.v = v; return octa::detail::mem_hash((const void *)&u, sizeof(u)); } }; /* reference wrapper */ template struct ReferenceWrapper { typedef T type; ReferenceWrapper(T &v): p_ptr(address_of(v)) {} ReferenceWrapper(const ReferenceWrapper &) = default; ReferenceWrapper(T &&) = delete; ReferenceWrapper &operator=(const ReferenceWrapper &) = default; operator T &() const { return *p_ptr; } T &get() const { return *p_ptr; } private: T *p_ptr; }; template ReferenceWrapper ref(T &v) { return ReferenceWrapper(v); } template ReferenceWrapper ref(ReferenceWrapper v) { return ReferenceWrapper(v); } template void ref(const T &&) = delete; template ReferenceWrapper cref(const T &v) { return ReferenceWrapper(v); } template ReferenceWrapper cref(ReferenceWrapper v) { return ReferenceWrapper(v); } template void cref(const T &&) = delete; /* mem_fn */ namespace detail { template struct MemTypes; template struct MemTypes { typedef R Result; typedef T Argument; }; template struct MemTypes { typedef R Result; typedef T FirstArgument; typedef A SecondArgument; }; template struct MemTypes { typedef R Result; typedef const T Argument; }; template struct MemTypes { typedef R Result; typedef const T FirstArgument; typedef A SecondArgument; }; template class MemFn: MemTypes { R T::*p_ptr; public: MemFn(R T::*ptr): p_ptr(ptr) {} template auto operator()(T &obj, A &&...args) -> decltype(((obj).*(p_ptr))(forward(args)...)) { return ((obj).*(p_ptr))(forward(args)...); } template auto operator()(const T &obj, A &&...args) -> decltype(((obj).*(p_ptr))(forward(args)...)) const { return ((obj).*(p_ptr))(forward(args)...); } template auto operator()(T *obj, A &&...args) -> decltype(((obj)->*(p_ptr))(forward(args)...)) { return ((obj)->*(p_ptr))(forward(args)...); } template auto operator()(const T *obj, A &&...args) -> decltype(((obj)->*(p_ptr))(forward(args)...)) const { return ((obj)->*(p_ptr))(forward(args)...); } }; } /* namespace detail */ template octa::detail::MemFn mem_fn(R T:: *ptr) { return octa::detail::MemFn(ptr); } /* function impl * reference: http://probablydance.com/2013/01/13/a-faster-implementation-of-stdfunction */ template struct Function; namespace detail { struct FunctorData { void *p1, *p2; }; template struct FunctorInPlace { static constexpr bool value = sizeof(T) <= sizeof(FunctorData) && (alignof(FunctorData) % alignof(T)) == 0 && octa::IsMoveConstructible::value; }; template struct FunctorDataManager { template static R call(const FunctorData &s, A ...args) { return ((T &)s)(octa::forward(args)...); } static void store_f(FunctorData &s, T v) { new (&get_ref(s)) T(octa::forward(v)); } static void move_f(FunctorData &lhs, FunctorData &&rhs) { new (&get_ref(lhs)) T(octa::move(get_ref(rhs))); } static void destroy_f(FunctorData &s) { get_ref(s).~T(); } static T &get_ref(const FunctorData &s) { return (T &)s; } }; template struct FunctorDataManager::value> > { template static R call(const FunctorData &s, A ...args) { return (*(T *&)s)(octa::forward(args)...); } static void store_f(FunctorData &s, T v) { new (&get_ptr_ref(s)) T *(new T(octa::forward(v))); } static void move_f(FunctorData &lhs, FunctorData &&rhs) { new (&get_ptr_ref(lhs)) T *(get_ptr_ref(rhs)); get_ptr_ref(rhs) = nullptr; } static void destroy_f(FunctorData &s) { T *&ptr = get_ptr_ref(s); if (!ptr) return; delete ptr; ptr = nullptr; } static T &get_ref(const FunctorData &s) { return *get_ptr_ref(s); } static T *&get_ptr_ref(FunctorData &s) { return (T *&)s; } static T *&get_ptr_ref(const FunctorData &s) { return (T *&)s; } }; struct FunctionManager; struct FmStorage { FunctorData data; const FunctionManager *manager; }; template static const FunctionManager &get_default_fm(); struct FunctionManager { template inline static const FunctionManager create_default_manager() { return FunctionManager { &call_move_and_destroy, &call_copy, &call_destroy }; } void (* const call_move_and_destroyf)(FmStorage &lhs, FmStorage &&rhs); void (* const call_copyf)(FmStorage &lhs, const FmStorage &rhs); void (* const call_destroyf)(FmStorage &s); template static void call_move_and_destroy(FmStorage &lhs, FmStorage &&rhs) { typedef FunctorDataManager _spec; _spec::move_f(lhs.data, octa::move(rhs.data)); _spec::destroy_f(rhs.data); lhs.manager = &get_default_fm(); } template static void call_copy(FmStorage &lhs, const FmStorage &rhs) { typedef FunctorDataManager _spec; lhs.manager = &get_default_fm(); _spec::store_f(lhs.data, _spec::get_ref(rhs.data)); } template static void call_destroy(FmStorage &s) { typedef FunctorDataManager _spec; _spec::destroy_f(s.data); } }; template inline static const FunctionManager &get_default_fm() { static const FunctionManager def_manager = FunctionManager::create_default_manager(); return def_manager; } template struct FunctionBase { typedef R Result; }; template struct FunctionBase { typedef R Result; typedef T Argument; }; template struct FunctionBase { typedef R Result; typedef T FirstArgument; typedef U SecondArgument; }; template struct IsValidFunctor { static constexpr bool value = false; }; template struct IsValidFunctor, R(A...)> { static constexpr bool value = false; }; struct Empty { }; template T func_to_functor(T &&f) { return octa::forward(f); } template auto func_to_functor(RR (T::*f)(AA...)) -> decltype(mem_fn(f)) { return mem_fn(f); } template auto func_to_functor(RR (T::*f)(AA...) const) -> decltype(mem_fn(f)) { return mem_fn(f); } template struct IsValidFunctor { template static decltype(func_to_functor(octa::declval()) (octa::declval()...)) test(U *); template static Empty test(...); static constexpr bool value = octa::IsConvertible< decltype(test(nullptr)), R >::value; }; } /* namespace detail */ template struct Function: octa::detail::FunctionBase { Function( ) { init_empty(); } Function(nullptr_t) { init_empty(); } Function(Function &&f) { init_empty(); swap(f); } Function(const Function &f): p_call(f.p_call) { f.p_stor.manager->call_copyf(p_stor, f.p_stor); } template Function(T f, EnableIf< octa::detail::IsValidFunctor::value, octa::detail::Empty > = octa::detail::Empty()) { if (func_is_null(f)) { init_empty(); return; } initialize(octa::detail::func_to_functor(octa::forward(f))); } ~Function() { p_stor.manager->call_destroyf(p_stor); } Function &operator=(Function &&f) { p_stor.manager->call_destroyf(p_stor); swap(f); return *this; } Function &operator=(const Function &f) { p_stor.manager->call_destroyf(p_stor); f.p_stor.manager->call_copyf(p_stor, f.p_stor); return *this; }; R operator()(A ...args) const { return p_call(p_stor.data, octa::forward(args)...); } template void assign(_F &&f) { Function(octa::forward<_F>(f)).swap(*this); } void swap(Function &f) { octa::detail::FmStorage tmp; f.p_stor.manager->call_move_and_destroyf(tmp, octa::move(f.p_stor)); p_stor.manager->call_move_and_destroyf(f.p_stor, octa::move(p_stor)); tmp.manager->call_move_and_destroyf(p_stor, octa::move(tmp)); octa::swap(p_call, f.p_call); } operator bool() const { return p_call != nullptr; } private: octa::detail::FmStorage p_stor; R (*p_call)(const octa::detail::FunctorData &, A...); template void initialize(T f) { p_call = &octa::detail::FunctorDataManager::template call; p_stor.manager = &octa::detail::get_default_fm(); octa::detail::FunctorDataManager::store_f(p_stor.data, octa::forward(f)); } void init_empty() { typedef R(*emptyf)(A...); p_call = nullptr; p_stor.manager = &octa::detail::get_default_fm(); octa::detail::FunctorDataManager::store_f(p_stor.data, nullptr); } template static bool func_is_null(const T &) { return false; } static bool func_is_null(R (* const &fptr)(A...)) { return fptr == nullptr; } template static bool func_is_null(RR (T::* const &fptr)(AA...)) { return fptr == nullptr; } template static bool func_is_null(RR (T::* const &fptr)(AA...) const) { return fptr == nullptr; } }; template bool operator==(nullptr_t, const Function &rhs) { return !rhs; } template bool operator==(const Function &lhs, nullptr_t) { return !lhs; } template bool operator!=(nullptr_t, const Function &rhs) { return rhs; } template bool operator!=(const Function &lhs, nullptr_t) { return lhs; } } /* namespace octa */ #endif