]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/debug/bitset
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / bitset
CommitLineData
285b36d6
BK
1// Debugging bitset implementation -*- C++ -*-
2
83ffe9cd 3// Copyright (C) 2003-2023 Free Software Foundation, Inc.
285b36d6
BK
4//
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
285b36d6
BK
9// any later version.
10
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.
15
748086b7
JJ
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.
19
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/>.
285b36d6 24
78a53887
BK
25/** @file debug/bitset
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
285b36d6
BK
29#ifndef _GLIBCXX_DEBUG_BITSET
30#define _GLIBCXX_DEBUG_BITSET
31
541a9b10
JW
32#pragma GCC system_header
33
285b36d6
BK
34#include <bitset>
35#include <debug/safe_sequence.h>
36#include <debug/safe_iterator.h>
37
12ffa228 38namespace std _GLIBCXX_VISIBILITY(default)
3cbc7af0 39{
45f388bb 40namespace __debug
526da49c 41{
1ceb9e06 42 /// Class std::bitset with additional safety/checking/debug instrumentation.
526da49c 43 template<size_t _Nb>
285b36d6 44 class bitset
12ffa228 45 : public _GLIBCXX_STD_C::bitset<_Nb>
734f5023 46#if __cplusplus < 201103L
17e3f4aa
PC
47 , public __gnu_debug::_Safe_sequence_base
48#endif
285b36d6 49 {
12ffa228 50 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
285b36d6
BK
51
52 public:
4f5f9962 53 // In C++11 we rely on normal reference type to preserve the property
c5589aa7 54 // of bitset to be use as a literal.
fc09e5b6 55 // TODO: Find another solution.
734f5023 56#if __cplusplus >= 201103L
c5589aa7
FD
57 typedef typename _Base::reference reference;
58#else
285b36d6 59 // bit reference:
526da49c 60 class reference
17e3f4aa 61 : private _Base::reference
17e3f4aa 62 , public __gnu_debug::_Safe_iterator_base
285b36d6
BK
63 {
64 typedef typename _Base::reference _Base_ref;
65
66 friend class bitset;
67 reference();
526da49c 68
5d045324 69 reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
17e3f4aa 70 : _Base_ref(__base)
17e3f4aa 71 , _Safe_iterator_base(__seq, false)
285b36d6
BK
72 { }
73
74 public:
5d861bf2 75 reference(const reference& __x) _GLIBCXX_NOEXCEPT
17e3f4aa 76 : _Base_ref(__x)
17e3f4aa 77 , _Safe_iterator_base(__x, false)
285b36d6
BK
78 { }
79
526da49c 80 reference&
5d861bf2 81 operator=(bool __x) _GLIBCXX_NOEXCEPT
285b36d6 82 {
5d045324 83 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
3cbc7af0 84 _M_message(__gnu_debug::__msg_bad_bitset_write)
285b36d6
BK
85 ._M_iterator(*this));
86 *static_cast<_Base_ref*>(this) = __x;
87 return *this;
88 }
89
526da49c 90 reference&
5d861bf2 91 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
285b36d6 92 {
5d045324 93 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
3cbc7af0 94 _M_message(__gnu_debug::__msg_bad_bitset_read)
285b36d6 95 ._M_iterator(__x));
5d045324 96 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
3cbc7af0 97 _M_message(__gnu_debug::__msg_bad_bitset_write)
285b36d6
BK
98 ._M_iterator(*this));
99 *static_cast<_Base_ref*>(this) = __x;
100 return *this;
101 }
526da49c
BI
102
103 bool
5d861bf2 104 operator~() const _GLIBCXX_NOEXCEPT
285b36d6 105 {
5d045324 106 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
3cbc7af0 107 _M_message(__gnu_debug::__msg_bad_bitset_read)
285b36d6
BK
108 ._M_iterator(*this));
109 return ~(*static_cast<const _Base_ref*>(this));
110 }
526da49c 111
5d861bf2 112 operator bool() const _GLIBCXX_NOEXCEPT
285b36d6 113 {
5d045324 114 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
3cbc7af0 115 _M_message(__gnu_debug::__msg_bad_bitset_read)
285b36d6
BK
116 ._M_iterator(*this));
117 return *static_cast<const _Base_ref*>(this);
118 }
526da49c
BI
119
120 reference&
5d861bf2 121 flip() _GLIBCXX_NOEXCEPT
285b36d6 122 {
5d045324 123 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
3cbc7af0 124 _M_message(__gnu_debug::__msg_bad_bitset_flip)
285b36d6
BK
125 ._M_iterator(*this));
126 _Base_ref::flip();
127 return *this;
128 }
129 };
c5589aa7 130#endif
285b36d6
BK
131
132 // 23.3.5.1 constructors:
5d861bf2
PC
133 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
134 : _Base() { }
526da49c 135
734f5023 136#if __cplusplus >= 201103L
5d861bf2 137 constexpr bitset(unsigned long long __val) noexcept
eb07a8f5
PC
138#else
139 bitset(unsigned long __val)
140#endif
141 : _Base(__val) { }
526da49c 142
47cd1557 143 template<typename _CharT, typename _Traits, typename _Alloc>
4ceb5bc4 144 _GLIBCXX23_CONSTEXPR
526da49c 145 explicit
47cd1557
PC
146 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
147 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
285b36d6 148 __pos = 0,
47cd1557
PC
149 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
150 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
285b36d6
BK
151 : _Base(__str, __pos, __n) { }
152
47cd1557
PC
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 396. what are characters zero and one.
155 template<class _CharT, class _Traits, class _Alloc>
4ceb5bc4 156 _GLIBCXX23_CONSTEXPR
47cd1557
PC
157 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
158 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
159 __pos,
160 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
161 __n,
162 _CharT __zero, _CharT __one = _CharT('1'))
163 : _Base(__str, __pos, __n, __zero, __one) { }
164
4ceb5bc4 165 _GLIBCXX23_CONSTEXPR
17e3f4aa 166 bitset(const _Base& __x) : _Base(__x) { }
285b36d6 167
734f5023 168#if __cplusplus >= 201103L
a0a2a399 169 template<typename _CharT>
4ceb5bc4 170 _GLIBCXX23_CONSTEXPR
a0a2a399
PC
171 explicit
172 bitset(const _CharT* __str,
173 typename std::basic_string<_CharT>::size_type __n
174 = std::basic_string<_CharT>::npos,
175 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
176 : _Base(__str, __n, __zero, __one) { }
2838468c
PC
177#endif
178
285b36d6 179 // 23.3.5.2 bitset operations:
4ceb5bc4 180 _GLIBCXX23_CONSTEXPR
526da49c 181 bitset<_Nb>&
5d861bf2 182 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
285b36d6
BK
183 {
184 _M_base() &= __rhs;
185 return *this;
186 }
526da49c 187
4ceb5bc4 188 _GLIBCXX23_CONSTEXPR
526da49c 189 bitset<_Nb>&
5d861bf2 190 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
285b36d6 191 {
72afd981 192 _M_base() |= __rhs;
285b36d6
BK
193 return *this;
194 }
526da49c 195
4ceb5bc4 196 _GLIBCXX23_CONSTEXPR
526da49c 197 bitset<_Nb>&
5d861bf2 198 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
285b36d6
BK
199 {
200 _M_base() ^= __rhs;
201 return *this;
202 }
526da49c 203
4ceb5bc4 204 _GLIBCXX23_CONSTEXPR
526da49c 205 bitset<_Nb>&
5d861bf2 206 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
285b36d6
BK
207 {
208 _M_base() <<= __pos;
209 return *this;
210 }
526da49c 211
4ceb5bc4 212 _GLIBCXX23_CONSTEXPR
526da49c 213 bitset<_Nb>&
5d861bf2 214 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
285b36d6
BK
215 {
216 _M_base() >>= __pos;
217 return *this;
218 }
526da49c 219
4ceb5bc4 220 _GLIBCXX23_CONSTEXPR
526da49c 221 bitset<_Nb>&
5d861bf2 222 set() _GLIBCXX_NOEXCEPT
285b36d6
BK
223 {
224 _Base::set();
225 return *this;
226 }
526da49c 227
285b36d6 228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
526da49c 229 // 186. bitset::set() second parameter should be bool
4ceb5bc4 230 _GLIBCXX23_CONSTEXPR
526da49c 231 bitset<_Nb>&
285b36d6
BK
232 set(size_t __pos, bool __val = true)
233 {
234 _Base::set(__pos, __val);
235 return *this;
236 }
526da49c 237
4ceb5bc4 238 _GLIBCXX23_CONSTEXPR
526da49c 239 bitset<_Nb>&
5d861bf2 240 reset() _GLIBCXX_NOEXCEPT
285b36d6
BK
241 {
242 _Base::reset();
243 return *this;
244 }
526da49c 245
4ceb5bc4 246 _GLIBCXX23_CONSTEXPR
526da49c 247 bitset<_Nb>&
285b36d6
BK
248 reset(size_t __pos)
249 {
250 _Base::reset(__pos);
251 return *this;
252 }
526da49c 253
4ceb5bc4 254 _GLIBCXX23_CONSTEXPR
5d861bf2
PC
255 bitset<_Nb>
256 operator~() const _GLIBCXX_NOEXCEPT
257 { return bitset(~_M_base()); }
526da49c 258
4ceb5bc4 259 _GLIBCXX23_CONSTEXPR
526da49c 260 bitset<_Nb>&
5d861bf2 261 flip() _GLIBCXX_NOEXCEPT
285b36d6
BK
262 {
263 _Base::flip();
264 return *this;
265 }
526da49c 266
4ceb5bc4 267 _GLIBCXX23_CONSTEXPR
526da49c 268 bitset<_Nb>&
285b36d6
BK
269 flip(size_t __pos)
270 {
271 _Base::flip(__pos);
272 return *this;
273 }
526da49c 274
285b36d6
BK
275 // element access:
276 // _GLIBCXX_RESOLVE_LIB_DEFECTS
526da49c 277 // 11. Bitset minor problems
4ceb5bc4 278 _GLIBCXX23_CONSTEXPR
526da49c 279 reference
285b36d6 280 operator[](size_t __pos)
526da49c 281 {
285b36d6 282 __glibcxx_check_subscript(__pos);
734f5023 283#if __cplusplus >= 201103L
c5589aa7
FD
284 return _M_base()[__pos];
285#else
526da49c 286 return reference(_M_base()[__pos], this);
c5589aa7 287#endif
285b36d6 288 }
526da49c 289
285b36d6 290 // _GLIBCXX_RESOLVE_LIB_DEFECTS
526da49c 291 // 11. Bitset minor problems
fc09e5b6 292 _GLIBCXX_CONSTEXPR bool
526da49c
BI
293 operator[](size_t __pos) const
294 {
734f5023 295#if __cplusplus < 201103L
fc09e5b6 296 // TODO: Check in debug-mode too.
285b36d6 297 __glibcxx_check_subscript(__pos);
fc09e5b6
PC
298#endif
299 return _Base::operator[](__pos);
285b36d6 300 }
526da49c 301
285b36d6 302 using _Base::to_ulong;
734f5023 303#if __cplusplus >= 201103L
700d2899
PC
304 using _Base::to_ullong;
305#endif
526da49c 306
47cd1557 307 template <typename _CharT, typename _Traits, typename _Alloc>
4ceb5bc4 308 _GLIBCXX23_CONSTEXPR
47cd1557 309 std::basic_string<_CharT, _Traits, _Alloc>
285b36d6 310 to_string() const
47cd1557
PC
311 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
312
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // 396. what are characters zero and one.
315 template<class _CharT, class _Traits, class _Alloc>
4ceb5bc4 316 _GLIBCXX23_CONSTEXPR
47cd1557
PC
317 std::basic_string<_CharT, _Traits, _Alloc>
318 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
319 {
320 return _M_base().template
321 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
322 }
526da49c 323
48cf14eb
JW
324 // _GLIBCXX_RESOLVE_LIB_DEFECTS
325 // 434. bitset::to_string() hard to use.
326 template<typename _CharT, typename _Traits>
4ceb5bc4 327 _GLIBCXX23_CONSTEXPR
48cf14eb
JW
328 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
329 to_string() const
330 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
331
47cd1557 332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
19a6a2ea 333 // 853. to_string needs updating with zero and one.
47cd1557 334 template<class _CharT, class _Traits>
4ceb5bc4 335 _GLIBCXX23_CONSTEXPR
47cd1557
PC
336 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
337 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
338 { return to_string<_CharT, _Traits,
339 std::allocator<_CharT> >(__zero, __one); }
340
48cf14eb 341 template<typename _CharT>
4ceb5bc4 342 _GLIBCXX23_CONSTEXPR
48cf14eb
JW
343 std::basic_string<_CharT, std::char_traits<_CharT>,
344 std::allocator<_CharT> >
345 to_string() const
346 {
347 return to_string<_CharT, std::char_traits<_CharT>,
348 std::allocator<_CharT> >();
349 }
350
47cd1557 351 template<class _CharT>
4ceb5bc4 352 _GLIBCXX23_CONSTEXPR
47cd1557
PC
353 std::basic_string<_CharT, std::char_traits<_CharT>,
354 std::allocator<_CharT> >
355 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
356 {
357 return to_string<_CharT, std::char_traits<_CharT>,
358 std::allocator<_CharT> >(__zero, __one);
359 }
360
4ceb5bc4 361 _GLIBCXX23_CONSTEXPR
48cf14eb 362 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
47cd1557
PC
363 to_string() const
364 {
365 return to_string<char,std::char_traits<char>,std::allocator<char> >();
366 }
367
4ceb5bc4 368 _GLIBCXX23_CONSTEXPR
47cd1557
PC
369 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
370 to_string(char __zero, char __one = '1') const
371 {
372 return to_string<char, std::char_traits<char>,
373 std::allocator<char> >(__zero, __one);
374 }
48cf14eb 375
285b36d6
BK
376 using _Base::count;
377 using _Base::size;
526da49c 378
4ceb5bc4 379 _GLIBCXX23_CONSTEXPR
526da49c 380 bool
5d861bf2 381 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
de1e3b87 382 { return _M_base() == __rhs._M_base(); }
285b36d6 383
de1e3b87 384#if __cpp_impl_three_way_comparison < 201907L
526da49c 385 bool
5d861bf2 386 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
de1e3b87
JW
387 { return _M_base() != __rhs._M_base(); }
388#endif
526da49c 389
285b36d6 390 using _Base::test;
b96817da 391 using _Base::all;
285b36d6
BK
392 using _Base::any;
393 using _Base::none;
526da49c 394
4ceb5bc4 395 _GLIBCXX23_CONSTEXPR
526da49c 396 bitset<_Nb>
5d861bf2 397 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
285b36d6 398 { return bitset<_Nb>(_M_base() << __pos); }
526da49c 399
4ceb5bc4 400 _GLIBCXX23_CONSTEXPR
526da49c 401 bitset<_Nb>
5d861bf2 402 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
285b36d6 403 { return bitset<_Nb>(_M_base() >> __pos); }
526da49c 404
4ceb5bc4 405 _GLIBCXX23_CONSTEXPR
5d861bf2
PC
406 _Base&
407 _M_base() _GLIBCXX_NOEXCEPT
408 { return *this; }
285b36d6 409
4ceb5bc4 410 _GLIBCXX23_CONSTEXPR
526da49c 411 const _Base&
5d861bf2
PC
412 _M_base() const _GLIBCXX_NOEXCEPT
413 { return *this; }
285b36d6 414 };
526da49c 415
285b36d6 416 template<size_t _Nb>
4ceb5bc4
JW
417 _GLIBCXX23_CONSTEXPR
418 inline bitset<_Nb>
5d861bf2 419 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
285b36d6 420 { return bitset<_Nb>(__x) &= __y; }
526da49c 421
285b36d6 422 template<size_t _Nb>
4ceb5bc4
JW
423 _GLIBCXX23_CONSTEXPR
424 inline bitset<_Nb>
5d861bf2 425 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
285b36d6
BK
426 { return bitset<_Nb>(__x) |= __y; }
427
428 template<size_t _Nb>
4ceb5bc4
JW
429 _GLIBCXX23_CONSTEXPR
430 inline bitset<_Nb>
5d861bf2 431 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
285b36d6
BK
432 { return bitset<_Nb>(__x) ^= __y; }
433
434 template<typename _CharT, typename _Traits, size_t _Nb>
4ceb5bc4 435 inline std::basic_istream<_CharT, _Traits>&
285b36d6
BK
436 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
437 { return __is >> __x._M_base(); }
438
439 template<typename _CharT, typename _Traits, size_t _Nb>
4ceb5bc4 440 inline std::basic_ostream<_CharT, _Traits>&
526da49c 441 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
285b36d6
BK
442 const bitset<_Nb>& __x)
443 { return __os << __x._M_base(); }
1ceb9e06 444
45f388bb 445} // namespace __debug
ec7058d6 446
734f5023 447#if __cplusplus >= 201103L
ec7058d6
PC
448 // DR 1182.
449 /// std::hash specialization for bitset.
450 template<size_t _Nb>
95addb1b 451 struct hash<__debug::bitset<_Nb>>
5d64ee19 452 : public __hash_base<size_t, __debug::bitset<_Nb>>
ec7058d6
PC
453 {
454 size_t
72f1c34b 455 operator()(const __debug::bitset<_Nb>& __b) const noexcept
12ffa228 456 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
ec7058d6
PC
457 };
458#endif
459
3cbc7af0 460} // namespace std
285b36d6
BK
461
462#endif