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