// Interval arithmetic proposal for C++0x. // // Copyright 2005, 2006 INRIA, France. // All rights reserved. // // Author : Sylvain Pion, Herve Bronnimann, Guillaume Melquiond. #ifndef STD_BOOL_SET_H_INCLUDED #define STD_BOOL_SET_H_INCLUDED #include namespace std2 { // 20.2.3, bool\_set: class bool_set; struct bool_set { bool_set() : _v(FALSE) {} bool_set(bool t) : _v(t ? TRUE : FALSE) {} bool contains(bool_set b) const { return (_v == INDETERMINATE) || (b._v == EMPTY) || (_v == b._v); } bool equals(bool_set b) const { return _v == b._v; } bool is_emptyset() const { return _v == EMPTY; } bool is_indeterminate() const { return _v == INDETERMINATE; } bool is_singleton() const { return _v == FALSE || _v == TRUE; } operator bool() const; static bool_set emptyset() { bool_set b; b._v = EMPTY; return b; } static bool_set indeterminate() { bool_set b; b._v = INDETERMINATE; return b; } private: enum { EMPTY=0, FALSE, TRUE, INDETERMINATE } _v; }; // 20.2.3.2 \textit{bool\_set values:} bool contains(bool_set, bool_set); bool equals(bool_set, bool_set); bool is_emptyset(bool_set); bool is_indeterminate(bool_set); bool is_singleton(bool_set); bool certainly(bool_set); bool possibly(bool_set); // 20.2.3.3 \textit{bool\_set set operations:} bool_set set_union(bool, bool_set); bool_set set_union(bool_set, bool); bool_set set_union(bool_set, bool_set); bool_set set_intersection(bool, bool_set); bool_set set_intersection(bool_set, bool); bool_set set_intersection(bool_set, bool_set); bool_set set_complement(bool_set); // 20.2.3.4 \textit{bool\_set logical operators:} bool_set operator!(bool_set); bool_set operator^(bool, bool_set); bool_set operator^(bool_set, bool); bool_set operator^(bool_set, bool_set); bool_set operator|(bool, bool_set); bool_set operator|(bool_set, bool); bool_set operator|(bool_set, bool_set); bool_set operator&(bool, bool_set); bool_set operator&(bool_set, bool); bool_set operator&(bool_set, bool_set); bool_set operator||(bool, bool_set); bool_set operator||(bool_set, bool); bool_set operator||(bool_set, bool_set); bool_set operator&&(bool, bool_set); bool_set operator&&(bool_set, bool); bool_set operator&&(bool_set, bool_set); // 20.2.3.5 \textit{bool\_set relational operators:} bool_set operator==(bool, bool_set); bool_set operator==(bool_set, bool); bool_set operator==(bool_set, bool_set); bool_set operator!=(bool, bool_set); bool_set operator!=(bool_set, bool); bool_set operator!=(bool_set, bool_set); // Value functions // --------------- inline bool contains(bool_set a, bool_set b) { return a.contains(b); } inline bool equals(bool_set a, bool_set b) { return a.equals(b); } inline bool is_emptyset(bool_set a) { return a.is_emptyset(); } inline bool is_indeterminate(bool_set a) { return a.is_indeterminate(); } inline bool is_singleton(bool_set a) { return a.is_singleton(); } inline bool certainly(bool_set a) { return a.equals(true); } inline bool possibly(bool_set a) { return a.contains(true); } // Set functions // ------------- inline bool_set set_union(bool_set a, bool_set b) { if (a.is_indeterminate() || b.is_indeterminate()) return bool_set::indeterminate(); if (a.is_emptyset()) return b; if (b.is_emptyset()) return a; if (a.equals(true) && b.equals(true)) return true; if (a.equals(false) && b.equals(false)) return false; return bool_set::indeterminate(); } inline bool_set set_union(bool a, bool_set b) { return set_union(bool_set(a), b); } inline bool_set set_union(bool_set a, bool b) { return set_union(b, a); } inline bool_set set_union(bool a, bool b) { return set_union(bool_set(a), b); } inline bool_set set_intersection(bool_set a, bool_set b) { if (a.is_emptyset() || b.is_emptyset()) return bool_set::emptyset(); if (a.is_indeterminate()) return b; if (b.is_indeterminate()) return a; if (a.equals(true) && b.equals(true)) return true; if (a.equals(false) && b.equals(false)) return false; return bool_set::emptyset(); } inline bool_set set_intersection(bool a, bool_set b) { return set_intersection(bool_set(a), b); } inline bool_set set_intersection(bool_set a, bool b) { return set_intersection(b, a); } inline bool_set set_intersection(bool a, bool b) { return set_intersection(bool_set(a), b); } inline bool_set set_complement(bool_set a) { if (a.equals(true)) return false; if (a.equals(false)) return true; if (a.is_emptyset()) return bool_set::indeterminate(); return bool_set::emptyset(); } // Conversion to bool // ------------------ inline bool_set::operator bool() const { if (this->equals(true)) return true; if (this->equals(false)) return false; throw std::bad_cast(); // std::range_error("Undecidable conversion of bool_set to bool"); } // Boolean operators // ----------------- inline bool_set operator!(bool_set a) { if (a.equals(true)) return false; if (a.equals(false)) return true; return a; } inline bool_set operator^(bool_set a, bool_set b) { if (a.equals(true) ) return !b; if (a.equals(false)) return b; if (a.is_emptyset() || b.is_emptyset()) return bool_set::emptyset(); return bool_set::indeterminate(); } inline bool_set operator^(bool a, bool_set b) { return bool_set(a) ^ b; } inline bool_set operator^(bool_set a, bool b) { return a ^ bool_set(b); } inline bool_set operator|(bool_set a, bool_set b) { if (a.equals(false)) return b; if (b.equals(false)) return a; if (a.is_emptyset() || b.is_emptyset()) return bool_set::emptyset(); return a.equals(true) ? bool_set(true) : b; } inline bool_set operator|(bool a, bool_set b) { return bool_set(a) | b; } inline bool_set operator|(bool_set a, bool b) { return a | bool_set(b); } inline bool_set operator&(bool_set a, bool_set b) { if (a.equals(true)) return b; if (b.equals(true)) return a; if (a.is_emptyset() || b.is_emptyset()) return bool_set::emptyset(); return a.equals(false) ? bool_set(false) : b; } inline bool_set operator&(bool a, bool_set b) { return bool_set(a) & b; } inline bool_set operator&(bool_set a, bool b) { return a & bool_set(b); } inline bool_set operator||(bool_set a, bool_set b) { return a | b; } inline bool_set operator||(bool a, bool_set b) { return bool_set(a) | b; } inline bool_set operator||(bool_set a, bool b) { return a | bool_set(b); } inline bool_set operator&&(bool_set a, bool_set b) { return a & b; } inline bool_set operator&&(bool a, bool_set b) { return bool_set(a) & b; } inline bool_set operator&&(bool_set a, bool b) { return a & bool_set(b); } // Equality operators // ------------------ inline bool_set operator==(bool_set a, bool_set b) { if (a.is_emptyset() || b.is_emptyset()) return bool_set::emptyset(); if (a.is_indeterminate() || b.is_indeterminate()) return bool_set::indeterminate(); if (a.equals(true) && b.equals(true)) return true; if (a.equals(false) && b.equals(false)) return true; return false; } inline bool_set operator==(bool_set a, bool b) { return a == bool_set(b); } inline bool_set operator==(bool a, bool_set b) { return bool_set(a) == b; } inline bool_set operator!=(bool_set a, bool_set b) { return ! (a == b); } inline bool_set operator!=(bool_set a, bool b) { return ! (a == b); } inline bool_set operator!=(bool a, bool_set b) { return ! (a == b); } } // namespace std2 #endif // STD_BOOL_SET_H_INCLUDED