1// TR2 <bool_set> -*- C++ -*-
3// Copyright (C) 2009-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This is a TR2 C++ Library header.
29#ifndef _GLIBCXX_TR2_BOOL_SET
30#define _GLIBCXX_TR2_BOOL_SET 1
33#pragma GCC system_header
39namespace std _GLIBCXX_VISIBILITY(default)
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * See N2136, Bool_set: multi-valued logic
49 * by Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion.
51 * The implicit conversion to bool is slippery! I may use the new
52 * explicit conversion. This has been specialized in the language
53 * so that in contexts requiring a bool the conversion happens
54 * implicitly. Thus most objections should be eliminated.
60 /// Default constructor.
61 constexpr bool_set() : _M_b(_S_false) { }
63 /// Constructor from bool.
64 constexpr bool_set(bool __t) : _M_b(_Bool_set_val(__t)) { }
66 // I'm not sure about this.
67 bool contains(bool_set __b) const
68 { return this->is_singleton() && this->equals(__b); }
70 /// Return true if states are equal.
71 bool equals(bool_set __b) const
72 { return __b._M_b == _M_b; }
74 /// Return true if this is empty.
75 bool is_emptyset() const
76 { return _M_b == _S_empty; }
78 /// Return true if this is indeterminate.
79 bool is_indeterminate() const
80 { return _M_b == _S_indet; }
82 /// Return true if this is false or true (normal boolean).
83 bool is_singleton() const
84 { return _M_b == _S_false || _M_b == _S_true_; }
86 /// Conversion to bool.
91 throw std::bad_cast();
96 static bool_set indeterminate()
104 static bool_set emptyset()
112 operator!(bool_set __b)
113 { return __b._M_not(); }
116 operator^(bool_set __s, bool_set __t)
117 { return __s._M_xor(__t); }
120 operator|(bool_set __s, bool_set __t)
121 { return __s._M_or(__t); }
124 operator&(bool_set __s, bool_set __t)
125 { return __s._M_and(__t); }
128 operator==(bool_set __s, bool_set __t)
129 { return __s._M_eq(__t); }
132 // These overloads replace the facet additions in the paper!
134 template<typename CharT, typename Traits>
135 friend std::basic_ostream<CharT, Traits>&
136 operator<<(std::basic_ostream<CharT, Traits>& __out, bool_set __b)
142 template<typename CharT, typename Traits>
143 friend std::basic_istream<CharT, Traits>&
144 operator>>(std::basic_istream<CharT, Traits>& __in, bool_set& __b)
148 if (__c >= _S_false && __c < _S_empty)
149 __b._M_b = static_cast<_Bool_set_val>(__c);
155 enum _Bool_set_val: unsigned char
167 bool_set(_Bool_set_val __c) : _M_b(__c) { }
170 bool_set _M_not() const
171 { return _S_not[this->_M_b]; }
174 bool_set _M_xor(bool_set __b) const
175 { return _S_xor[this->_M_b][__b._M_b]; }
178 bool_set _M_or(bool_set __b) const
179 { return _S_or[this->_M_b][__b._M_b]; }
182 bool_set _M_and(bool_set __b) const
183 { return _S_and[this->_M_b][__b._M_b]; }
186 bool_set _M_eq(bool_set __b) const
187 { return _S_eq[this->_M_b][__b._M_b]; }
190 static _Bool_set_val _S_not[4];
193 static _Bool_set_val _S_xor[4][4];
196 static _Bool_set_val _S_or[4][4];
199 static _Bool_set_val _S_and[4][4];
202 static _Bool_set_val _S_eq[4][4];
205 // 20.2.3.2 bool_set values
208 contains(bool_set __s, bool_set __t)
209 { return __s.contains(__t); }
212 equals(bool_set __s, bool_set __t)
213 { return __s.equals(__t); }
216 is_emptyset(bool_set __b)
217 { return __b.is_emptyset(); }
220 is_indeterminate(bool_set __b)
221 { return __b.is_indeterminate(); }
224 is_singleton(bool_set __b)
225 { return __b.is_singleton(); }
228 certainly(bool_set __b)
229 { return ! __b.contains(false); }
232 possibly(bool_set __b)
233 { return __b.contains(true); }
236 // 20.2.3.3 bool_set set operations
239 set_union(bool __s, bool_set __t)
240 { return bool_set(__s) | __t; }
243 set_union(bool_set __s, bool __t)
244 { return __s | bool_set(__t); }
247 set_union(bool_set __s, bool_set __t)
248 { return __s | __t; }
251 set_intersection(bool __s, bool_set __t)
252 { return bool_set(__s) & __t; }
255 set_intersection(bool_set __s, bool __t)
256 { return __s & bool_set(__t); }
259 set_intersection(bool_set __s, bool_set __t)
260 { return __s & __t; }
263 set_complement(bool_set __b)
267 // 20.2.3.4 bool_set logical operators
270 operator^(bool __s, bool_set __t)
271 { return bool_set(__s) ^ __t; }
274 operator^(bool_set __s, bool __t)
275 { return __s ^ bool_set(__t); }
278 operator|(bool __s, bool_set __t)
279 { return bool_set(__s) | __t; }
282 operator|(bool_set __s, bool __t)
283 { return __s | bool_set(__t); }
286 operator&(bool __s, bool_set __t)
287 { return bool_set(__s) & __t; }
290 operator&(bool_set __s, bool __t)
291 { return __s & bool_set(__t); }
294 // 20.2.3.5 bool_set relational operators
297 operator==(bool __s, bool_set __t)
298 { return bool_set(__s) == __t; }
301 operator==(bool_set __s, bool __t)
302 { return __s == bool_set(__t); }
305 operator!=(bool __s, bool_set __t)
306 { return ! (__s == __t); }
309 operator!=(bool_set __s, bool __t)
310 { return ! (__s == __t); }
313 operator!=(bool_set __s, bool_set __t)
314 { return ! (__s == __t); }
317_GLIBCXX_END_NAMESPACE_VERSION
320#include <tr2/bool_set.tcc>
322#endif // _GLIBCXX_TR2_BOOL_SET