diff --git a/examples/stream1.cc b/examples/stream1.cc index 7de4388..3cabae9 100644 --- a/examples/stream1.cc +++ b/examples/stream1.cc @@ -1,4 +1,4 @@ -#include +#include #include using namespace ostd; diff --git a/ostd/algorithm.hh b/ostd/algorithm.hh index f761615..e13ba57 100644 --- a/ostd/algorithm.hh +++ b/ostd/algorithm.hh @@ -9,8 +9,8 @@ #include #include +#include -#include "ostd/functional.hh" #include "ostd/range.hh" #include "ostd/utility.hh" #include "ostd/initializer_list.hh" @@ -173,7 +173,7 @@ inline auto sort_cmp(C &&compare) { template inline R sort(R range) { - return sort_cmp(range, Less>()); + return sort_cmp(range, std::less>()); } inline auto sort() { return [](auto &&obj) { return sort(std::forward(obj)); }; diff --git a/ostd/event.hh b/ostd/event.hh index f90d93d..4aa7249 100644 --- a/ostd/event.hh +++ b/ostd/event.hh @@ -6,7 +6,8 @@ #ifndef OSTD_EVENT_HH #define OSTD_EVENT_HH -#include "ostd/functional.hh" +#include + #include "ostd/utility.hh" namespace ostd { diff --git a/ostd/functional.hh b/ostd/functional.hh deleted file mode 100644 index 2a1925f..0000000 --- a/ostd/functional.hh +++ /dev/null @@ -1,226 +0,0 @@ -/* Function objects for OctaSTD. - * - * This file is part of OctaSTD. See COPYING.md for futher information. - */ - -#ifndef OSTD_FUNCTIONAL_HH -#define OSTD_FUNCTIONAL_HH - -#include - -#include -#include -#include - -#include "ostd/platform.hh" -#include "ostd/utility.hh" -#include "ostd/type_traits.hh" - -namespace ostd { - -/* basic function objects */ - -#define OSTD_DEFINE_BINARY_OP(name, op, RT) \ -template \ -struct name { \ - RT operator()(T const &x, T const &y) const { \ - return x op y; \ - } \ - using FirstArgument = T; \ - using SecondARgument = T; \ - using Result = RT; \ -}; - -OSTD_DEFINE_BINARY_OP(Less, <, bool) -OSTD_DEFINE_BINARY_OP(LessEqual, <=, bool) -OSTD_DEFINE_BINARY_OP(Greater, >, bool) -OSTD_DEFINE_BINARY_OP(GreaterEqual, >=, bool) -OSTD_DEFINE_BINARY_OP(Equal, ==, bool) -OSTD_DEFINE_BINARY_OP(NotEqual, !=, bool) -OSTD_DEFINE_BINARY_OP(LogicalAnd, &&, bool) -OSTD_DEFINE_BINARY_OP(LogicalOr, ||, bool) -OSTD_DEFINE_BINARY_OP(Modulo, %, T) -OSTD_DEFINE_BINARY_OP(Multiply, *, T) -OSTD_DEFINE_BINARY_OP(Divide, /, T) -OSTD_DEFINE_BINARY_OP(Add, +, T) -OSTD_DEFINE_BINARY_OP(Subtract, -, T) -OSTD_DEFINE_BINARY_OP(BitAnd, &, T) -OSTD_DEFINE_BINARY_OP(BitOr, |, T) -OSTD_DEFINE_BINARY_OP(BitXor, ^, T) - -#undef OSTD_DEFINE_BINARY_OP - -namespace detail { - template, char>> - struct CharEqual { - using FirstArgument = T *; - using SecondArgument = T *; - using Result = bool; - bool operator()(T *x, T *y) const { - return !strcmp(x, y); - } - }; - - template - struct CharEqual { - using FirstArgument = T *; - using SecondArgument = T *; - using Result = bool; - bool operator()(T *x, T *y) const { - return x == y; - } - }; -} - -template -struct EqualWithCstr { - using FirstArgument = T; - using SecondArgument = T; - bool operator()(T const &x, T const &y) const { - return x == y; - } -}; - -template -struct EqualWithCstr: detail::CharEqual {}; - -template -struct LogicalNot { - bool operator()(T const &x) const { return !x; } - using Argument = T; - using Result = bool; -}; - -template -struct Negate { - bool operator()(T const &x) const { return -x; } - using Argument = T; - using Result = T; -}; - -template -struct BinaryNegate { - using FirstArgument = typename T::FirstArgument; - using SecondArgument = typename T::SecondArgument; - using Result = bool; - - explicit BinaryNegate(T const &f): p_fn(f) {} - - bool operator()(FirstArgument const &x, SecondArgument const &y) { - return !p_fn(x, y); - } -private: - T p_fn; -}; - -template -struct UnaryNegate { - using Argument = typename T::Argument; - using Result = bool; - - explicit UnaryNegate(T const &f): p_fn(f) {} - bool operator()(Argument const &x) { - return !p_fn(x); - } -private: - T p_fn; -}; - -template -UnaryNegate not1(T const &fn) { - return UnaryNegate(fn); -} - -template -BinaryNegate not2(T const &fn) { - return BinaryNegate(fn); -} - -/* endian swap */ - -template> -struct EndianSwap; - -template -struct EndianSwap { - using Argument = T; - using Result = T; - T operator()(T v) const { - union { T iv; std::uint16_t sv; } u; - u.iv = v; - u.sv = endian_swap16(u.sv); - return u.iv; - } -}; - -template -struct EndianSwap { - using Argument = T; - using Result = T; - T operator()(T v) const { - union { T iv; std::uint32_t sv; } u; - u.iv = v; - u.sv = endian_swap32(u.sv); - return u.iv; - } -}; - -template -struct EndianSwap { - using Argument = T; - using Result = T; - T operator()(T v) const { - union { T iv; std::uint64_t sv; } u; - u.iv = v; - u.sv = endian_swap64(u.sv); - return u.iv; - } -}; - -template -T endian_swap(T x) { return EndianSwap()(x); } - -namespace detail { - template> - struct EndianSame; - - template - struct EndianSame { - using Argument = T; - using Result = T; - T operator()(T v) const { return v; } - }; - template - struct EndianSame { - using Argument = T; - using Result = T; - T operator()(T v) const { return v; } - }; - template - struct EndianSame { - using Argument = T; - using Result = T; - T operator()(T v) const { return v; } - }; -} - -#if OSTD_BYTE_ORDER == OSTD_ENDIAN_LIL -template -struct FromLilEndian: detail::EndianSame {}; -template -struct FromBigEndian: EndianSwap {}; -#else -template -struct FromLilEndian: EndianSwap {}; -template -struct FromBigEndian: detail::EndianSame {}; -#endif - -template -T from_lil_endian(T x) { return FromLilEndian()(x); } -template -T from_big_endian(T x) { return FromBigEndian()(x); } - -} /* namespace ostd */ - -#endif diff --git a/ostd/platform.hh b/ostd/platform.hh index 8a89b06..6b5962d 100644 --- a/ostd/platform.hh +++ b/ostd/platform.hh @@ -9,6 +9,8 @@ #include #include +#include + #if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__)) # define OSTD_PLATFORM_WIN32 1 # if defined(WIN64) || defined(_WIN64) @@ -144,6 +146,93 @@ inline std::uint64_t endian_swap64(std::uint64_t x) noexcept { #endif +/* endian swap */ + +template> +struct EndianSwap; + +template +struct EndianSwap { + using Argument = T; + using Result = T; + T operator()(T v) const { + union { T iv; std::uint16_t sv; } u; + u.iv = v; + u.sv = endian_swap16(u.sv); + return u.iv; + } +}; + +template +struct EndianSwap { + using Argument = T; + using Result = T; + T operator()(T v) const { + union { T iv; std::uint32_t sv; } u; + u.iv = v; + u.sv = endian_swap32(u.sv); + return u.iv; + } +}; + +template +struct EndianSwap { + using Argument = T; + using Result = T; + T operator()(T v) const { + union { T iv; std::uint64_t sv; } u; + u.iv = v; + u.sv = endian_swap64(u.sv); + return u.iv; + } +}; + +template +T endian_swap(T x) { return EndianSwap()(x); } + +namespace detail { + template< + typename T, size_t N = sizeof(T), bool IsNum = std::is_arithmetic_v + > + struct EndianSame; + + template + struct EndianSame { + using Argument = T; + using Result = T; + T operator()(T v) const { return v; } + }; + template + struct EndianSame { + using Argument = T; + using Result = T; + T operator()(T v) const { return v; } + }; + template + struct EndianSame { + using Argument = T; + using Result = T; + T operator()(T v) const { return v; } + }; +} + +#if OSTD_BYTE_ORDER == OSTD_ENDIAN_LIL +template +struct FromLilEndian: detail::EndianSame {}; +template +struct FromBigEndian: EndianSwap {}; +#else +template +struct FromLilEndian: EndianSwap {}; +template +struct FromBigEndian: detail::EndianSame {}; +#endif + +template +T from_lil_endian(T x) { return FromLilEndian()(x); } +template +T from_big_endian(T x) { return FromBigEndian()(x); } + } #endif diff --git a/ostd/string.hh b/ostd/string.hh index b993770..83b8878 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -13,11 +13,11 @@ #include #include #include +#include #include "ostd/utility.hh" #include "ostd/range.hh" #include "ostd/vector.hh" -#include "ostd/functional.hh" #include "ostd/type_traits.hh" #include "ostd/algorithm.hh"