1// Debugging bitset implementation -*- C++ -*-
3// Copyright (C) 2003-2024 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) { }
168 bitset(const _Base& __x) : _Base(__x) { }
170#if __cplusplus >= 201103L
171 template<typename _CharT>
174 bitset(const _CharT* __str,
175 typename std::basic_string<_CharT>::size_type __n
176 = std::basic_string<_CharT>::npos,
177 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
178 : _Base(__str, __n, __zero, __one) { }
181 // 23.3.5.2 bitset operations:
184 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
192 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
200 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
208 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
216 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
224 set() _GLIBCXX_NOEXCEPT
230 // _GLIBCXX_RESOLVE_LIB_DEFECTS
231 // 186. bitset::set() second parameter should be bool
234 set(size_t __pos, bool __val = true)
236 _Base::set(__pos, __val);
242 reset() _GLIBCXX_NOEXCEPT
258 operator~() const _GLIBCXX_NOEXCEPT
259 { return bitset(~_M_base()); }
263 flip() _GLIBCXX_NOEXCEPT
278 // _GLIBCXX_RESOLVE_LIB_DEFECTS
279 // 11. Bitset minor problems
282 operator[](size_t __pos)
284 __glibcxx_check_subscript(__pos);
285#if __cplusplus >= 201103L
286 return _M_base()[__pos];
288 return reference(_M_base()[__pos], this);
292 // _GLIBCXX_RESOLVE_LIB_DEFECTS
293 // 11. Bitset minor problems
294 _GLIBCXX_CONSTEXPR bool
295 operator[](size_t __pos) const
297#if __cplusplus < 201103L
298 // TODO: Check in debug-mode too.
299 __glibcxx_check_subscript(__pos);
301 return _Base::operator[](__pos);
304 using _Base::to_ulong;
305#if __cplusplus >= 201103L
306 using _Base::to_ullong;
309 template <typename _CharT, typename _Traits, typename _Alloc>
311 std::basic_string<_CharT, _Traits, _Alloc>
313 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
315 // _GLIBCXX_RESOLVE_LIB_DEFECTS
316 // 396. what are characters zero and one.
317 template<class _CharT, class _Traits, class _Alloc>
319 std::basic_string<_CharT, _Traits, _Alloc>
320 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
322 return _M_base().template
323 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 434. bitset::to_string() hard to use.
328 template<typename _CharT, typename _Traits>
330 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
332 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
334 // _GLIBCXX_RESOLVE_LIB_DEFECTS
335 // 853. to_string needs updating with zero and one.
336 template<class _CharT, class _Traits>
338 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
339 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
340 { return to_string<_CharT, _Traits,
341 std::allocator<_CharT> >(__zero, __one); }
343 template<typename _CharT>
345 std::basic_string<_CharT, std::char_traits<_CharT>,
346 std::allocator<_CharT> >
349 return to_string<_CharT, std::char_traits<_CharT>,
350 std::allocator<_CharT> >();
353 template<class _CharT>
355 std::basic_string<_CharT, std::char_traits<_CharT>,
356 std::allocator<_CharT> >
357 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
359 return to_string<_CharT, std::char_traits<_CharT>,
360 std::allocator<_CharT> >(__zero, __one);
364 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
367 return to_string<char,std::char_traits<char>,std::allocator<char> >();
371 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
372 to_string(char __zero, char __one = '1') const
374 return to_string<char, std::char_traits<char>,
375 std::allocator<char> >(__zero, __one);
383 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
384 { return _M_base() == __rhs._M_base(); }
386#if __cpp_impl_three_way_comparison < 201907L
388 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
389 { return _M_base() != __rhs._M_base(); }
399 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
400 { return bitset<_Nb>(_M_base() << __pos); }
404 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
405 { return bitset<_Nb>(_M_base() >> __pos); }
409 _M_base() _GLIBCXX_NOEXCEPT
414 _M_base() const _GLIBCXX_NOEXCEPT
421 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
422 { return bitset<_Nb>(__x) &= __y; }
427 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
428 { return bitset<_Nb>(__x) |= __y; }
433 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
434 { return bitset<_Nb>(__x) ^= __y; }
436 template<typename _CharT, typename _Traits, size_t _Nb>
437 inline std::basic_istream<_CharT, _Traits>&
438 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
439 { return __is >> __x._M_base(); }
441 template<typename _CharT, typename _Traits, size_t _Nb>
442 inline std::basic_ostream<_CharT, _Traits>&
443 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
444 const bitset<_Nb>& __x)
445 { return __os << __x._M_base(); }
447} // namespace __debug
449#if __cplusplus >= 201103L
451 /// std::hash specialization for bitset.
453 struct hash<__debug::bitset<_Nb>>
454 : public __hash_base<size_t, __debug::bitset<_Nb>>
457 operator()(const __debug::bitset<_Nb>& __b) const noexcept
458 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }