// // Copyright (C) 2011-15 DyND Developers // BSD 2-Clause License, see LICENSE.txt // #pragma once #include #include namespace dynd { // A boolean class that is just one byte class DYND_API bool1 { char m_value; public: bool1() = default; explicit bool1(bool value) : m_value(value) {} operator bool() const { return m_value != 0; } bool1 &operator=(bool rhs) { m_value = rhs; return *this; } bool1 operator+() const { return *this; } bool1 operator-() const { return *this; } bool1 operator!() const { return bool1(m_value == 0); } bool1 operator~() const { return bool1(m_value == 0); } bool1 operator&&(bool1 &rhs) { return bool1(m_value && rhs.m_value); } bool1 operator||(bool1 &rhs) { return bool1(m_value || rhs.m_value); } bool1 &operator+=(bool1 rhs) { m_value += rhs.m_value; return *this; } bool1 &operator-=(bool1 rhs) { m_value -= rhs.m_value; return *this; } bool1 &operator*=(bool1 rhs) { m_value *= rhs.m_value; return *this; } bool1 &operator/=(bool1 rhs) { m_value /= rhs.m_value; return *this; } friend int operator+(bool1 lhs, bool1 rhs); friend int operator-(bool1 lhs, bool1 rhs); friend int operator*(bool1 lhs, bool1 rhs); friend int operator/(bool1 lhs, bool1 rhs); }; template <> struct is_integral : std::true_type { }; inline int operator+(bool1 lhs, bool1 rhs) { return lhs.m_value + rhs.m_value; } inline int operator-(bool1 lhs, bool1 rhs) { return lhs.m_value - rhs.m_value; } inline int operator*(bool1 lhs, bool1 rhs) { return lhs.m_value * rhs.m_value; } inline int operator/(bool1 lhs, bool1 rhs) { return lhs.m_value / rhs.m_value; } } // namespace dynd namespace std { template <> struct common_type : common_type { }; template <> struct common_type { typedef dynd::bool1 type; }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template <> struct common_type : common_type { }; template struct common_type : common_type { }; } // namespace std namespace dynd { inline bool operator<(bool1 lhs, bool1 rhs) { return static_cast(lhs) < static_cast(rhs); } template typename std::enable_if::value, bool>::type operator<(bool1 lhs, T rhs) { return static_cast(lhs) < rhs; } template typename std::enable_if::value, bool>::type operator<(T lhs, bool1 rhs) { return lhs < static_cast(rhs); } inline bool operator<=(bool1 lhs, bool1 rhs) { return static_cast(lhs) <= static_cast(rhs); } template typename std::enable_if::value, bool>::type operator<=(bool1 lhs, T rhs) { return static_cast(lhs) <= rhs; } template typename std::enable_if::value, bool>::type operator<=(T lhs, bool1 rhs) { return lhs <= static_cast(rhs); } inline bool operator==(bool1 lhs, bool1 rhs) { return static_cast(lhs) == static_cast(rhs); } template typename std::enable_if::value, bool>::type operator==(bool1 lhs, T rhs) { return static_cast(lhs) == rhs; } template typename std::enable_if::value, bool>::type operator==(T lhs, bool1 rhs) { return lhs == static_cast(rhs); } inline bool operator!=(bool1 lhs, bool1 rhs) { return static_cast(lhs) != static_cast(rhs); } template typename std::enable_if::value, bool>::type operator!=(bool1 lhs, T rhs) { return static_cast(lhs) != rhs; } template typename std::enable_if::value, bool>::type operator!=(T lhs, bool1 rhs) { return lhs != static_cast(rhs); } inline bool operator>=(bool1 lhs, bool1 rhs) { return static_cast(lhs) >= static_cast(rhs); } template typename std::enable_if::value, bool>::type operator>=(bool1 lhs, T rhs) { return static_cast(lhs) >= rhs; } template typename std::enable_if::value, bool>::type operator>=(T lhs, bool1 rhs) { return lhs >= static_cast(rhs); } inline bool operator>(bool1 lhs, bool1 rhs) { return static_cast(lhs) > static_cast(rhs); } template typename std::enable_if::value, bool>::type operator>(bool1 lhs, T rhs) { return static_cast(lhs) > rhs; } template typename std::enable_if::value, bool>::type operator>(T lhs, bool1 rhs) { return lhs > static_cast(rhs); } inline std::ostream &operator<<(std::ostream &o, const bool1 &rhs) { return o << static_cast(rhs); } } // namespace dynd