1// Debugging bitset implementation -*- C++ -*-
3// Copyright (C) 2003-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 file is a GNU debug extension to the Standard C++ Library.
29#ifndef _GLIBCXX_DEBUG_BITSET
30#define _GLIBCXX_DEBUG_BITSET
33#pragma GCC system_header
37#include <debug/safe_sequence.h>
38#include <debug/safe_iterator.h>
40namespace std _GLIBCXX_VISIBILITY(default)
44 /// Class std::bitset with additional safety/checking/debug instrumentation.
47 : public _GLIBCXX_STD_C::bitset<_Nb>
48#if __cplusplus < 201103L
49 , public __gnu_debug::_Safe_sequence_base
52 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
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;
63 : private _Base::reference
64 , public __gnu_debug::_Safe_iterator_base
66 typedef typename _Base::reference _Base_ref;
71 reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
73 , _Safe_iterator_base(__seq, false)
77 reference(const reference& __x) _GLIBCXX_NOEXCEPT
79 , _Safe_iterator_base(__x, false)
83 operator=(bool __x) _GLIBCXX_NOEXCEPT
85 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
86 _M_message(__gnu_debug::__msg_bad_bitset_write)
88 *static_cast<_Base_ref*>(this) = __x;
93 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
95 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
96 _M_message(__gnu_debug::__msg_bad_bitset_read)
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;
106 operator~() const _GLIBCXX_NOEXCEPT
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));
114 operator bool() const _GLIBCXX_NOEXCEPT
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);
123 flip() _GLIBCXX_NOEXCEPT
125 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
126 _M_message(__gnu_debug::__msg_bad_bitset_flip)
127 ._M_iterator(*this));
134 // 23.3.5.1 constructors:
135 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
138#if __cplusplus >= 201103L
139 constexpr bitset(unsigned long long __val) noexcept
141 bitset(unsigned long __val)
145 template<typename _CharT, typename _Traits, typename _Alloc>
148 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
149 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
151 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
152 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
153 : _Base(__str, __pos, __n) { }
155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
156 // 396. what are characters zero and one.
157 template<class _CharT, class _Traits, class _Alloc>
159 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
160 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
162 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
164 _CharT __zero, _CharT __one = _CharT('1'))
165 : _Base(__str, __pos, __n, __zero, __one) { }
167#ifdef __cpp_lib_bitset // ... from string_view
168 template<class _CharT, class _Traits>
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) { }
179 bitset(const _Base& __x) : _Base(__x) { }
181#if __cplusplus >= 201103L
182 template<typename _CharT>
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) { }
192 // 23.3.5.2 bitset operations:
195 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
203 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
211 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
219 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
227 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
235 set() _GLIBCXX_NOEXCEPT
241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
242 // 186. bitset::set() second parameter should be bool
245 set(size_t __pos, bool __val = true)
247 _Base::set(__pos, __val);
253 reset() _GLIBCXX_NOEXCEPT
269 operator~() const _GLIBCXX_NOEXCEPT
270 { return bitset(~_M_base()); }
274 flip() _GLIBCXX_NOEXCEPT
289 // _GLIBCXX_RESOLVE_LIB_DEFECTS
290 // 11. Bitset minor problems
293 operator[](size_t __pos)
295 __glibcxx_check_subscript(__pos);
296#if __cplusplus >= 201103L
297 return _M_base()[__pos];
299 return reference(_M_base()[__pos], this);
303 // _GLIBCXX_RESOLVE_LIB_DEFECTS
304 // 11. Bitset minor problems
305 _GLIBCXX_CONSTEXPR bool
306 operator[](size_t __pos) const
308#if __cplusplus < 201103L
309 // TODO: Check in debug-mode too.
310 __glibcxx_check_subscript(__pos);
312 return _Base::operator[](__pos);
315 using _Base::to_ulong;
316#if __cplusplus >= 201103L
317 using _Base::to_ullong;
320 template <typename _CharT, typename _Traits, typename _Alloc>
322 std::basic_string<_CharT, _Traits, _Alloc>
324 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 396. what are characters zero and one.
328 template<class _CharT, class _Traits, class _Alloc>
330 std::basic_string<_CharT, _Traits, _Alloc>
331 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
333 return _M_base().template
334 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
338 // 434. bitset::to_string() hard to use.
339 template<typename _CharT, typename _Traits>
341 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
343 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
345 // _GLIBCXX_RESOLVE_LIB_DEFECTS
346 // 853. to_string needs updating with zero and one.
347 template<class _CharT, class _Traits>
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); }
354 template<typename _CharT>
356 std::basic_string<_CharT, std::char_traits<_CharT>,
357 std::allocator<_CharT> >
360 return to_string<_CharT, std::char_traits<_CharT>,
361 std::allocator<_CharT> >();
364 template<class _CharT>
366 std::basic_string<_CharT, std::char_traits<_CharT>,
367 std::allocator<_CharT> >
368 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
370 return to_string<_CharT, std::char_traits<_CharT>,
371 std::allocator<_CharT> >(__zero, __one);
375 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
378 return to_string<char,std::char_traits<char>,std::allocator<char> >();
382 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
383 to_string(char __zero, char __one = '1') const
385 return to_string<char, std::char_traits<char>,
386 std::allocator<char> >(__zero, __one);
394 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
395 { return _M_base() == __rhs._M_base(); }
397#if __cpp_impl_three_way_comparison < 201907L
399 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
400 { return _M_base() != __rhs._M_base(); }
410 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
411 { return bitset<_Nb>(_M_base() << __pos); }
415 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
416 { return bitset<_Nb>(_M_base() >> __pos); }
420 _M_base() _GLIBCXX_NOEXCEPT
425 _M_base() const _GLIBCXX_NOEXCEPT
432 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
433 { return bitset<_Nb>(__x) &= __y; }
438 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
439 { return bitset<_Nb>(__x) |= __y; }
444 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
445 { return bitset<_Nb>(__x) ^= __y; }
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(); }
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(); }
458} // namespace __debug
460#if __cplusplus >= 201103L
462 /// std::hash specialization for bitset.
464 struct hash<__debug::bitset<_Nb>>
465 : public __hash_base<size_t, __debug::bitset<_Nb>>
468 operator()(const __debug::bitset<_Nb>& __b) const noexcept
469 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }