1// Debugging bitset implementation -*- C++ -*-
3// Copyright (C) 2003-2023 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
32#pragma GCC system_header
35#include <debug/safe_sequence.h>
36#include <debug/safe_iterator.h>
38namespace std _GLIBCXX_VISIBILITY(default)
42 /// Class std::bitset with additional safety/checking/debug instrumentation.
45 : public _GLIBCXX_STD_C::bitset<_Nb>
46#if __cplusplus < 201103L
47 , public __gnu_debug::_Safe_sequence_base
50 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
53 // In C++11 we rely on normal reference type to preserve the property
54 // of bitset to be use as a literal.
55 // TODO: Find another solution.
56#if __cplusplus >= 201103L
57 typedef typename _Base::reference reference;
61 : private _Base::reference
62 , public __gnu_debug::_Safe_iterator_base
64 typedef typename _Base::reference _Base_ref;
69 reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
71 , _Safe_iterator_base(__seq, false)
75 reference(const reference& __x) _GLIBCXX_NOEXCEPT
77 , _Safe_iterator_base(__x, false)
81 operator=(bool __x) _GLIBCXX_NOEXCEPT
83 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
84 _M_message(__gnu_debug::__msg_bad_bitset_write)
86 *static_cast<_Base_ref*>(this) = __x;
91 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
93 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
94 _M_message(__gnu_debug::__msg_bad_bitset_read)
96 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
97 _M_message(__gnu_debug::__msg_bad_bitset_write)
99 *static_cast<_Base_ref*>(this) = __x;
104 operator~() const _GLIBCXX_NOEXCEPT
106 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
107 _M_message(__gnu_debug::__msg_bad_bitset_read)
108 ._M_iterator(*this));
109 return ~(*static_cast<const _Base_ref*>(this));
112 operator bool() const _GLIBCXX_NOEXCEPT
114 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
115 _M_message(__gnu_debug::__msg_bad_bitset_read)
116 ._M_iterator(*this));
117 return *static_cast<const _Base_ref*>(this);
121 flip() _GLIBCXX_NOEXCEPT
123 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
124 _M_message(__gnu_debug::__msg_bad_bitset_flip)
125 ._M_iterator(*this));
132 // 23.3.5.1 constructors:
133 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
136#if __cplusplus >= 201103L
137 constexpr bitset(unsigned long long __val) noexcept
139 bitset(unsigned long __val)
143 template<typename _CharT, typename _Traits, typename _Alloc>
146 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
147 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
149 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
150 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
151 : _Base(__str, __pos, __n) { }
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 396. what are characters zero and one.
155 template<class _CharT, class _Traits, class _Alloc>
157 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
158 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
160 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
162 _CharT __zero, _CharT __one = _CharT('1'))
163 : _Base(__str, __pos, __n, __zero, __one) { }
166 bitset(const _Base& __x) : _Base(__x) { }
168#if __cplusplus >= 201103L
169 template<typename _CharT>
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) { }
179 // 23.3.5.2 bitset operations:
182 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
190 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
198 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
206 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
214 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
222 set() _GLIBCXX_NOEXCEPT
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // 186. bitset::set() second parameter should be bool
232 set(size_t __pos, bool __val = true)
234 _Base::set(__pos, __val);
240 reset() _GLIBCXX_NOEXCEPT
256 operator~() const _GLIBCXX_NOEXCEPT
257 { return bitset(~_M_base()); }
261 flip() _GLIBCXX_NOEXCEPT
276 // _GLIBCXX_RESOLVE_LIB_DEFECTS
277 // 11. Bitset minor problems
280 operator[](size_t __pos)
282 __glibcxx_check_subscript(__pos);
283#if __cplusplus >= 201103L
284 return _M_base()[__pos];
286 return reference(_M_base()[__pos], this);
290 // _GLIBCXX_RESOLVE_LIB_DEFECTS
291 // 11. Bitset minor problems
292 _GLIBCXX_CONSTEXPR bool
293 operator[](size_t __pos) const
295#if __cplusplus < 201103L
296 // TODO: Check in debug-mode too.
297 __glibcxx_check_subscript(__pos);
299 return _Base::operator[](__pos);
302 using _Base::to_ulong;
303#if __cplusplus >= 201103L
304 using _Base::to_ullong;
307 template <typename _CharT, typename _Traits, typename _Alloc>
309 std::basic_string<_CharT, _Traits, _Alloc>
311 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // 396. what are characters zero and one.
315 template<class _CharT, class _Traits, class _Alloc>
317 std::basic_string<_CharT, _Traits, _Alloc>
318 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
320 return _M_base().template
321 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
324 // _GLIBCXX_RESOLVE_LIB_DEFECTS
325 // 434. bitset::to_string() hard to use.
326 template<typename _CharT, typename _Traits>
328 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
330 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
333 // 853. to_string needs updating with zero and one.
334 template<class _CharT, class _Traits>
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); }
341 template<typename _CharT>
343 std::basic_string<_CharT, std::char_traits<_CharT>,
344 std::allocator<_CharT> >
347 return to_string<_CharT, std::char_traits<_CharT>,
348 std::allocator<_CharT> >();
351 template<class _CharT>
353 std::basic_string<_CharT, std::char_traits<_CharT>,
354 std::allocator<_CharT> >
355 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
357 return to_string<_CharT, std::char_traits<_CharT>,
358 std::allocator<_CharT> >(__zero, __one);
362 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
365 return to_string<char,std::char_traits<char>,std::allocator<char> >();
369 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
370 to_string(char __zero, char __one = '1') const
372 return to_string<char, std::char_traits<char>,
373 std::allocator<char> >(__zero, __one);
381 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
382 { return _M_base() == __rhs._M_base(); }
384#if __cpp_impl_three_way_comparison < 201907L
386 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
387 { return _M_base() != __rhs._M_base(); }
397 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
398 { return bitset<_Nb>(_M_base() << __pos); }
402 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
403 { return bitset<_Nb>(_M_base() >> __pos); }
407 _M_base() _GLIBCXX_NOEXCEPT
412 _M_base() const _GLIBCXX_NOEXCEPT
419 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
420 { return bitset<_Nb>(__x) &= __y; }
425 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
426 { return bitset<_Nb>(__x) |= __y; }
431 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
432 { return bitset<_Nb>(__x) ^= __y; }
434 template<typename _CharT, typename _Traits, size_t _Nb>
435 inline std::basic_istream<_CharT, _Traits>&
436 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
437 { return __is >> __x._M_base(); }
439 template<typename _CharT, typename _Traits, size_t _Nb>
440 inline std::basic_ostream<_CharT, _Traits>&
441 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
442 const bitset<_Nb>& __x)
443 { return __os << __x._M_base(); }
445} // namespace __debug
447#if __cplusplus >= 201103L
449 /// std::hash specialization for bitset.
451 struct hash<__debug::bitset<_Nb>>
452 : public __hash_base<size_t, __debug::bitset<_Nb>>
455 operator()(const __debug::bitset<_Nb>& __b) const noexcept
456 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }