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