1*38fd1498Szrj// <bitset> -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj// Copyright (C) 2001-2018 Free Software Foundation, Inc. 4*38fd1498Szrj// 5*38fd1498Szrj// This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj// software; you can redistribute it and/or modify it under the 7*38fd1498Szrj// terms of the GNU General Public License as published by the 8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj// any later version. 10*38fd1498Szrj 11*38fd1498Szrj// This library is distributed in the hope that it will be useful, 12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj// GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj// 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj// You should have received a copy of the GNU General Public License and 21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj// <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj/* 26*38fd1498Szrj * Copyright (c) 1998 27*38fd1498Szrj * Silicon Graphics Computer Systems, Inc. 28*38fd1498Szrj * 29*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 30*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 31*38fd1498Szrj * provided that the above copyright notice appear in all copies and 32*38fd1498Szrj * that both that copyright notice and this permission notice appear 33*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no 34*38fd1498Szrj * representations about the suitability of this software for any 35*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 36*38fd1498Szrj */ 37*38fd1498Szrj 38*38fd1498Szrj/** @file include/bitset 39*38fd1498Szrj * This is a Standard C++ Library header. 40*38fd1498Szrj */ 41*38fd1498Szrj 42*38fd1498Szrj#ifndef _GLIBCXX_BITSET 43*38fd1498Szrj#define _GLIBCXX_BITSET 1 44*38fd1498Szrj 45*38fd1498Szrj#pragma GCC system_header 46*38fd1498Szrj 47*38fd1498Szrj#include <string> 48*38fd1498Szrj#include <bits/functexcept.h> // For invalid_argument, out_of_range, 49*38fd1498Szrj // overflow_error 50*38fd1498Szrj#include <iosfwd> 51*38fd1498Szrj#include <bits/cxxabi_forced.h> 52*38fd1498Szrj 53*38fd1498Szrj#if __cplusplus >= 201103L 54*38fd1498Szrj# include <bits/functional_hash.h> 55*38fd1498Szrj#endif 56*38fd1498Szrj 57*38fd1498Szrj#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__) 58*38fd1498Szrj#define _GLIBCXX_BITSET_WORDS(__n) \ 59*38fd1498Szrj ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \ 60*38fd1498Szrj ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1)) 61*38fd1498Szrj 62*38fd1498Szrj#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__) 63*38fd1498Szrj 64*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default) 65*38fd1498Szrj{ 66*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_CONTAINER 67*38fd1498Szrj 68*38fd1498Szrj /** 69*38fd1498Szrj * Base class, general case. It is a class invariant that _Nw will be 70*38fd1498Szrj * nonnegative. 71*38fd1498Szrj * 72*38fd1498Szrj * See documentation for bitset. 73*38fd1498Szrj */ 74*38fd1498Szrj template<size_t _Nw> 75*38fd1498Szrj struct _Base_bitset 76*38fd1498Szrj { 77*38fd1498Szrj typedef unsigned long _WordT; 78*38fd1498Szrj 79*38fd1498Szrj /// 0 is the least significant word. 80*38fd1498Szrj _WordT _M_w[_Nw]; 81*38fd1498Szrj 82*38fd1498Szrj _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 83*38fd1498Szrj : _M_w() { } 84*38fd1498Szrj 85*38fd1498Szrj#if __cplusplus >= 201103L 86*38fd1498Szrj constexpr _Base_bitset(unsigned long long __val) noexcept 87*38fd1498Szrj : _M_w{ _WordT(__val) 88*38fd1498Szrj#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__ 89*38fd1498Szrj , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD) 90*38fd1498Szrj#endif 91*38fd1498Szrj } { } 92*38fd1498Szrj#else 93*38fd1498Szrj _Base_bitset(unsigned long __val) 94*38fd1498Szrj : _M_w() 95*38fd1498Szrj { _M_w[0] = __val; } 96*38fd1498Szrj#endif 97*38fd1498Szrj 98*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 99*38fd1498Szrj _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 100*38fd1498Szrj { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 101*38fd1498Szrj 102*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 103*38fd1498Szrj _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 104*38fd1498Szrj { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 105*38fd1498Szrj 106*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 107*38fd1498Szrj _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 108*38fd1498Szrj { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 109*38fd1498Szrj 110*38fd1498Szrj static _GLIBCXX_CONSTEXPR _WordT 111*38fd1498Szrj _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 112*38fd1498Szrj { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 113*38fd1498Szrj 114*38fd1498Szrj _WordT& 115*38fd1498Szrj _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT 116*38fd1498Szrj { return _M_w[_S_whichword(__pos)]; } 117*38fd1498Szrj 118*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 119*38fd1498Szrj _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT 120*38fd1498Szrj { return _M_w[_S_whichword(__pos)]; } 121*38fd1498Szrj 122*38fd1498Szrj#if __cplusplus >= 201103L 123*38fd1498Szrj const _WordT* 124*38fd1498Szrj _M_getdata() const noexcept 125*38fd1498Szrj { return _M_w; } 126*38fd1498Szrj#endif 127*38fd1498Szrj 128*38fd1498Szrj _WordT& 129*38fd1498Szrj _M_hiword() _GLIBCXX_NOEXCEPT 130*38fd1498Szrj { return _M_w[_Nw - 1]; } 131*38fd1498Szrj 132*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 133*38fd1498Szrj _M_hiword() const _GLIBCXX_NOEXCEPT 134*38fd1498Szrj { return _M_w[_Nw - 1]; } 135*38fd1498Szrj 136*38fd1498Szrj void 137*38fd1498Szrj _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 138*38fd1498Szrj { 139*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 140*38fd1498Szrj _M_w[__i] &= __x._M_w[__i]; 141*38fd1498Szrj } 142*38fd1498Szrj 143*38fd1498Szrj void 144*38fd1498Szrj _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 145*38fd1498Szrj { 146*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 147*38fd1498Szrj _M_w[__i] |= __x._M_w[__i]; 148*38fd1498Szrj } 149*38fd1498Szrj 150*38fd1498Szrj void 151*38fd1498Szrj _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 152*38fd1498Szrj { 153*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 154*38fd1498Szrj _M_w[__i] ^= __x._M_w[__i]; 155*38fd1498Szrj } 156*38fd1498Szrj 157*38fd1498Szrj void 158*38fd1498Szrj _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 159*38fd1498Szrj 160*38fd1498Szrj void 161*38fd1498Szrj _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 162*38fd1498Szrj 163*38fd1498Szrj void 164*38fd1498Szrj _M_do_flip() _GLIBCXX_NOEXCEPT 165*38fd1498Szrj { 166*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 167*38fd1498Szrj _M_w[__i] = ~_M_w[__i]; 168*38fd1498Szrj } 169*38fd1498Szrj 170*38fd1498Szrj void 171*38fd1498Szrj _M_do_set() _GLIBCXX_NOEXCEPT 172*38fd1498Szrj { 173*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 174*38fd1498Szrj _M_w[__i] = ~static_cast<_WordT>(0); 175*38fd1498Szrj } 176*38fd1498Szrj 177*38fd1498Szrj void 178*38fd1498Szrj _M_do_reset() _GLIBCXX_NOEXCEPT 179*38fd1498Szrj { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); } 180*38fd1498Szrj 181*38fd1498Szrj bool 182*38fd1498Szrj _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT 183*38fd1498Szrj { 184*38fd1498Szrj for (size_t __i = 0; __i < _Nw; ++__i) 185*38fd1498Szrj if (_M_w[__i] != __x._M_w[__i]) 186*38fd1498Szrj return false; 187*38fd1498Szrj return true; 188*38fd1498Szrj } 189*38fd1498Szrj 190*38fd1498Szrj template<size_t _Nb> 191*38fd1498Szrj bool 192*38fd1498Szrj _M_are_all() const _GLIBCXX_NOEXCEPT 193*38fd1498Szrj { 194*38fd1498Szrj for (size_t __i = 0; __i < _Nw - 1; __i++) 195*38fd1498Szrj if (_M_w[__i] != ~static_cast<_WordT>(0)) 196*38fd1498Szrj return false; 197*38fd1498Szrj return _M_hiword() == (~static_cast<_WordT>(0) 198*38fd1498Szrj >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD 199*38fd1498Szrj - _Nb)); 200*38fd1498Szrj } 201*38fd1498Szrj 202*38fd1498Szrj bool 203*38fd1498Szrj _M_is_any() const _GLIBCXX_NOEXCEPT 204*38fd1498Szrj { 205*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 206*38fd1498Szrj if (_M_w[__i] != static_cast<_WordT>(0)) 207*38fd1498Szrj return true; 208*38fd1498Szrj return false; 209*38fd1498Szrj } 210*38fd1498Szrj 211*38fd1498Szrj size_t 212*38fd1498Szrj _M_do_count() const _GLIBCXX_NOEXCEPT 213*38fd1498Szrj { 214*38fd1498Szrj size_t __result = 0; 215*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 216*38fd1498Szrj __result += __builtin_popcountl(_M_w[__i]); 217*38fd1498Szrj return __result; 218*38fd1498Szrj } 219*38fd1498Szrj 220*38fd1498Szrj unsigned long 221*38fd1498Szrj _M_do_to_ulong() const; 222*38fd1498Szrj 223*38fd1498Szrj#if __cplusplus >= 201103L 224*38fd1498Szrj unsigned long long 225*38fd1498Szrj _M_do_to_ullong() const; 226*38fd1498Szrj#endif 227*38fd1498Szrj 228*38fd1498Szrj // find first "on" bit 229*38fd1498Szrj size_t 230*38fd1498Szrj _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT; 231*38fd1498Szrj 232*38fd1498Szrj // find the next "on" bit that follows "prev" 233*38fd1498Szrj size_t 234*38fd1498Szrj _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT; 235*38fd1498Szrj }; 236*38fd1498Szrj 237*38fd1498Szrj // Definitions of non-inline functions from _Base_bitset. 238*38fd1498Szrj template<size_t _Nw> 239*38fd1498Szrj void 240*38fd1498Szrj _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 241*38fd1498Szrj { 242*38fd1498Szrj if (__builtin_expect(__shift != 0, 1)) 243*38fd1498Szrj { 244*38fd1498Szrj const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 245*38fd1498Szrj const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 246*38fd1498Szrj 247*38fd1498Szrj if (__offset == 0) 248*38fd1498Szrj for (size_t __n = _Nw - 1; __n >= __wshift; --__n) 249*38fd1498Szrj _M_w[__n] = _M_w[__n - __wshift]; 250*38fd1498Szrj else 251*38fd1498Szrj { 252*38fd1498Szrj const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 253*38fd1498Szrj - __offset); 254*38fd1498Szrj for (size_t __n = _Nw - 1; __n > __wshift; --__n) 255*38fd1498Szrj _M_w[__n] = ((_M_w[__n - __wshift] << __offset) 256*38fd1498Szrj | (_M_w[__n - __wshift - 1] >> __sub_offset)); 257*38fd1498Szrj _M_w[__wshift] = _M_w[0] << __offset; 258*38fd1498Szrj } 259*38fd1498Szrj 260*38fd1498Szrj std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0)); 261*38fd1498Szrj } 262*38fd1498Szrj } 263*38fd1498Szrj 264*38fd1498Szrj template<size_t _Nw> 265*38fd1498Szrj void 266*38fd1498Szrj _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 267*38fd1498Szrj { 268*38fd1498Szrj if (__builtin_expect(__shift != 0, 1)) 269*38fd1498Szrj { 270*38fd1498Szrj const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 271*38fd1498Szrj const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 272*38fd1498Szrj const size_t __limit = _Nw - __wshift - 1; 273*38fd1498Szrj 274*38fd1498Szrj if (__offset == 0) 275*38fd1498Szrj for (size_t __n = 0; __n <= __limit; ++__n) 276*38fd1498Szrj _M_w[__n] = _M_w[__n + __wshift]; 277*38fd1498Szrj else 278*38fd1498Szrj { 279*38fd1498Szrj const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 280*38fd1498Szrj - __offset); 281*38fd1498Szrj for (size_t __n = 0; __n < __limit; ++__n) 282*38fd1498Szrj _M_w[__n] = ((_M_w[__n + __wshift] >> __offset) 283*38fd1498Szrj | (_M_w[__n + __wshift + 1] << __sub_offset)); 284*38fd1498Szrj _M_w[__limit] = _M_w[_Nw-1] >> __offset; 285*38fd1498Szrj } 286*38fd1498Szrj 287*38fd1498Szrj std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0)); 288*38fd1498Szrj } 289*38fd1498Szrj } 290*38fd1498Szrj 291*38fd1498Szrj template<size_t _Nw> 292*38fd1498Szrj unsigned long 293*38fd1498Szrj _Base_bitset<_Nw>::_M_do_to_ulong() const 294*38fd1498Szrj { 295*38fd1498Szrj for (size_t __i = 1; __i < _Nw; ++__i) 296*38fd1498Szrj if (_M_w[__i]) 297*38fd1498Szrj __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong")); 298*38fd1498Szrj return _M_w[0]; 299*38fd1498Szrj } 300*38fd1498Szrj 301*38fd1498Szrj#if __cplusplus >= 201103L 302*38fd1498Szrj template<size_t _Nw> 303*38fd1498Szrj unsigned long long 304*38fd1498Szrj _Base_bitset<_Nw>::_M_do_to_ullong() const 305*38fd1498Szrj { 306*38fd1498Szrj const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long); 307*38fd1498Szrj for (size_t __i = 1 + __dw; __i < _Nw; ++__i) 308*38fd1498Szrj if (_M_w[__i]) 309*38fd1498Szrj __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong")); 310*38fd1498Szrj 311*38fd1498Szrj if (__dw) 312*38fd1498Szrj return _M_w[0] + (static_cast<unsigned long long>(_M_w[1]) 313*38fd1498Szrj << _GLIBCXX_BITSET_BITS_PER_WORD); 314*38fd1498Szrj return _M_w[0]; 315*38fd1498Szrj } 316*38fd1498Szrj#endif 317*38fd1498Szrj 318*38fd1498Szrj template<size_t _Nw> 319*38fd1498Szrj size_t 320*38fd1498Szrj _Base_bitset<_Nw>:: 321*38fd1498Szrj _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 322*38fd1498Szrj { 323*38fd1498Szrj for (size_t __i = 0; __i < _Nw; __i++) 324*38fd1498Szrj { 325*38fd1498Szrj _WordT __thisword = _M_w[__i]; 326*38fd1498Szrj if (__thisword != static_cast<_WordT>(0)) 327*38fd1498Szrj return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 328*38fd1498Szrj + __builtin_ctzl(__thisword)); 329*38fd1498Szrj } 330*38fd1498Szrj // not found, so return an indication of failure. 331*38fd1498Szrj return __not_found; 332*38fd1498Szrj } 333*38fd1498Szrj 334*38fd1498Szrj template<size_t _Nw> 335*38fd1498Szrj size_t 336*38fd1498Szrj _Base_bitset<_Nw>:: 337*38fd1498Szrj _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT 338*38fd1498Szrj { 339*38fd1498Szrj // make bound inclusive 340*38fd1498Szrj ++__prev; 341*38fd1498Szrj 342*38fd1498Szrj // check out of bounds 343*38fd1498Szrj if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD) 344*38fd1498Szrj return __not_found; 345*38fd1498Szrj 346*38fd1498Szrj // search first word 347*38fd1498Szrj size_t __i = _S_whichword(__prev); 348*38fd1498Szrj _WordT __thisword = _M_w[__i]; 349*38fd1498Szrj 350*38fd1498Szrj // mask off bits below bound 351*38fd1498Szrj __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev); 352*38fd1498Szrj 353*38fd1498Szrj if (__thisword != static_cast<_WordT>(0)) 354*38fd1498Szrj return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 355*38fd1498Szrj + __builtin_ctzl(__thisword)); 356*38fd1498Szrj 357*38fd1498Szrj // check subsequent words 358*38fd1498Szrj __i++; 359*38fd1498Szrj for (; __i < _Nw; __i++) 360*38fd1498Szrj { 361*38fd1498Szrj __thisword = _M_w[__i]; 362*38fd1498Szrj if (__thisword != static_cast<_WordT>(0)) 363*38fd1498Szrj return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 364*38fd1498Szrj + __builtin_ctzl(__thisword)); 365*38fd1498Szrj } 366*38fd1498Szrj // not found, so return an indication of failure. 367*38fd1498Szrj return __not_found; 368*38fd1498Szrj } // end _M_do_find_next 369*38fd1498Szrj 370*38fd1498Szrj /** 371*38fd1498Szrj * Base class, specialization for a single word. 372*38fd1498Szrj * 373*38fd1498Szrj * See documentation for bitset. 374*38fd1498Szrj */ 375*38fd1498Szrj template<> 376*38fd1498Szrj struct _Base_bitset<1> 377*38fd1498Szrj { 378*38fd1498Szrj typedef unsigned long _WordT; 379*38fd1498Szrj _WordT _M_w; 380*38fd1498Szrj 381*38fd1498Szrj _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 382*38fd1498Szrj : _M_w(0) 383*38fd1498Szrj { } 384*38fd1498Szrj 385*38fd1498Szrj#if __cplusplus >= 201103L 386*38fd1498Szrj constexpr _Base_bitset(unsigned long long __val) noexcept 387*38fd1498Szrj#else 388*38fd1498Szrj _Base_bitset(unsigned long __val) 389*38fd1498Szrj#endif 390*38fd1498Szrj : _M_w(__val) 391*38fd1498Szrj { } 392*38fd1498Szrj 393*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 394*38fd1498Szrj _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 395*38fd1498Szrj { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 396*38fd1498Szrj 397*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 398*38fd1498Szrj _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 399*38fd1498Szrj { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 400*38fd1498Szrj 401*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 402*38fd1498Szrj _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 403*38fd1498Szrj { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 404*38fd1498Szrj 405*38fd1498Szrj static _GLIBCXX_CONSTEXPR _WordT 406*38fd1498Szrj _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 407*38fd1498Szrj { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 408*38fd1498Szrj 409*38fd1498Szrj _WordT& 410*38fd1498Szrj _M_getword(size_t) _GLIBCXX_NOEXCEPT 411*38fd1498Szrj { return _M_w; } 412*38fd1498Szrj 413*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 414*38fd1498Szrj _M_getword(size_t) const _GLIBCXX_NOEXCEPT 415*38fd1498Szrj { return _M_w; } 416*38fd1498Szrj 417*38fd1498Szrj#if __cplusplus >= 201103L 418*38fd1498Szrj const _WordT* 419*38fd1498Szrj _M_getdata() const noexcept 420*38fd1498Szrj { return &_M_w; } 421*38fd1498Szrj#endif 422*38fd1498Szrj 423*38fd1498Szrj _WordT& 424*38fd1498Szrj _M_hiword() _GLIBCXX_NOEXCEPT 425*38fd1498Szrj { return _M_w; } 426*38fd1498Szrj 427*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 428*38fd1498Szrj _M_hiword() const _GLIBCXX_NOEXCEPT 429*38fd1498Szrj { return _M_w; } 430*38fd1498Szrj 431*38fd1498Szrj void 432*38fd1498Szrj _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 433*38fd1498Szrj { _M_w &= __x._M_w; } 434*38fd1498Szrj 435*38fd1498Szrj void 436*38fd1498Szrj _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 437*38fd1498Szrj { _M_w |= __x._M_w; } 438*38fd1498Szrj 439*38fd1498Szrj void 440*38fd1498Szrj _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 441*38fd1498Szrj { _M_w ^= __x._M_w; } 442*38fd1498Szrj 443*38fd1498Szrj void 444*38fd1498Szrj _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 445*38fd1498Szrj { _M_w <<= __shift; } 446*38fd1498Szrj 447*38fd1498Szrj void 448*38fd1498Szrj _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 449*38fd1498Szrj { _M_w >>= __shift; } 450*38fd1498Szrj 451*38fd1498Szrj void 452*38fd1498Szrj _M_do_flip() _GLIBCXX_NOEXCEPT 453*38fd1498Szrj { _M_w = ~_M_w; } 454*38fd1498Szrj 455*38fd1498Szrj void 456*38fd1498Szrj _M_do_set() _GLIBCXX_NOEXCEPT 457*38fd1498Szrj { _M_w = ~static_cast<_WordT>(0); } 458*38fd1498Szrj 459*38fd1498Szrj void 460*38fd1498Szrj _M_do_reset() _GLIBCXX_NOEXCEPT 461*38fd1498Szrj { _M_w = 0; } 462*38fd1498Szrj 463*38fd1498Szrj bool 464*38fd1498Szrj _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT 465*38fd1498Szrj { return _M_w == __x._M_w; } 466*38fd1498Szrj 467*38fd1498Szrj template<size_t _Nb> 468*38fd1498Szrj bool 469*38fd1498Szrj _M_are_all() const _GLIBCXX_NOEXCEPT 470*38fd1498Szrj { return _M_w == (~static_cast<_WordT>(0) 471*38fd1498Szrj >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); } 472*38fd1498Szrj 473*38fd1498Szrj bool 474*38fd1498Szrj _M_is_any() const _GLIBCXX_NOEXCEPT 475*38fd1498Szrj { return _M_w != 0; } 476*38fd1498Szrj 477*38fd1498Szrj size_t 478*38fd1498Szrj _M_do_count() const _GLIBCXX_NOEXCEPT 479*38fd1498Szrj { return __builtin_popcountl(_M_w); } 480*38fd1498Szrj 481*38fd1498Szrj unsigned long 482*38fd1498Szrj _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 483*38fd1498Szrj { return _M_w; } 484*38fd1498Szrj 485*38fd1498Szrj#if __cplusplus >= 201103L 486*38fd1498Szrj unsigned long long 487*38fd1498Szrj _M_do_to_ullong() const noexcept 488*38fd1498Szrj { return _M_w; } 489*38fd1498Szrj#endif 490*38fd1498Szrj 491*38fd1498Szrj size_t 492*38fd1498Szrj _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 493*38fd1498Szrj { 494*38fd1498Szrj if (_M_w != 0) 495*38fd1498Szrj return __builtin_ctzl(_M_w); 496*38fd1498Szrj else 497*38fd1498Szrj return __not_found; 498*38fd1498Szrj } 499*38fd1498Szrj 500*38fd1498Szrj // find the next "on" bit that follows "prev" 501*38fd1498Szrj size_t 502*38fd1498Szrj _M_do_find_next(size_t __prev, size_t __not_found) const 503*38fd1498Szrj _GLIBCXX_NOEXCEPT 504*38fd1498Szrj { 505*38fd1498Szrj ++__prev; 506*38fd1498Szrj if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD)) 507*38fd1498Szrj return __not_found; 508*38fd1498Szrj 509*38fd1498Szrj _WordT __x = _M_w >> __prev; 510*38fd1498Szrj if (__x != 0) 511*38fd1498Szrj return __builtin_ctzl(__x) + __prev; 512*38fd1498Szrj else 513*38fd1498Szrj return __not_found; 514*38fd1498Szrj } 515*38fd1498Szrj }; 516*38fd1498Szrj 517*38fd1498Szrj /** 518*38fd1498Szrj * Base class, specialization for no storage (zero-length %bitset). 519*38fd1498Szrj * 520*38fd1498Szrj * See documentation for bitset. 521*38fd1498Szrj */ 522*38fd1498Szrj template<> 523*38fd1498Szrj struct _Base_bitset<0> 524*38fd1498Szrj { 525*38fd1498Szrj typedef unsigned long _WordT; 526*38fd1498Szrj 527*38fd1498Szrj _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 528*38fd1498Szrj { } 529*38fd1498Szrj 530*38fd1498Szrj#if __cplusplus >= 201103L 531*38fd1498Szrj constexpr _Base_bitset(unsigned long long) noexcept 532*38fd1498Szrj#else 533*38fd1498Szrj _Base_bitset(unsigned long) 534*38fd1498Szrj#endif 535*38fd1498Szrj { } 536*38fd1498Szrj 537*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 538*38fd1498Szrj _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 539*38fd1498Szrj { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 540*38fd1498Szrj 541*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 542*38fd1498Szrj _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 543*38fd1498Szrj { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 544*38fd1498Szrj 545*38fd1498Szrj static _GLIBCXX_CONSTEXPR size_t 546*38fd1498Szrj _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 547*38fd1498Szrj { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 548*38fd1498Szrj 549*38fd1498Szrj static _GLIBCXX_CONSTEXPR _WordT 550*38fd1498Szrj _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 551*38fd1498Szrj { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 552*38fd1498Szrj 553*38fd1498Szrj // This would normally give access to the data. The bounds-checking 554*38fd1498Szrj // in the bitset class will prevent the user from getting this far, 555*38fd1498Szrj // but (1) it must still return an lvalue to compile, and (2) the 556*38fd1498Szrj // user might call _Unchecked_set directly, in which case this /needs/ 557*38fd1498Szrj // to fail. Let's not penalize zero-length users unless they actually 558*38fd1498Szrj // make an unchecked call; all the memory ugliness is therefore 559*38fd1498Szrj // localized to this single should-never-get-this-far function. 560*38fd1498Szrj _WordT& 561*38fd1498Szrj _M_getword(size_t) _GLIBCXX_NOEXCEPT 562*38fd1498Szrj { 563*38fd1498Szrj __throw_out_of_range(__N("_Base_bitset::_M_getword")); 564*38fd1498Szrj return *new _WordT; 565*38fd1498Szrj } 566*38fd1498Szrj 567*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 568*38fd1498Szrj _M_getword(size_t) const _GLIBCXX_NOEXCEPT 569*38fd1498Szrj { return 0; } 570*38fd1498Szrj 571*38fd1498Szrj _GLIBCXX_CONSTEXPR _WordT 572*38fd1498Szrj _M_hiword() const _GLIBCXX_NOEXCEPT 573*38fd1498Szrj { return 0; } 574*38fd1498Szrj 575*38fd1498Szrj void 576*38fd1498Szrj _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 577*38fd1498Szrj { } 578*38fd1498Szrj 579*38fd1498Szrj void 580*38fd1498Szrj _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 581*38fd1498Szrj { } 582*38fd1498Szrj 583*38fd1498Szrj void 584*38fd1498Szrj _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 585*38fd1498Szrj { } 586*38fd1498Szrj 587*38fd1498Szrj void 588*38fd1498Szrj _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT 589*38fd1498Szrj { } 590*38fd1498Szrj 591*38fd1498Szrj void 592*38fd1498Szrj _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT 593*38fd1498Szrj { } 594*38fd1498Szrj 595*38fd1498Szrj void 596*38fd1498Szrj _M_do_flip() _GLIBCXX_NOEXCEPT 597*38fd1498Szrj { } 598*38fd1498Szrj 599*38fd1498Szrj void 600*38fd1498Szrj _M_do_set() _GLIBCXX_NOEXCEPT 601*38fd1498Szrj { } 602*38fd1498Szrj 603*38fd1498Szrj void 604*38fd1498Szrj _M_do_reset() _GLIBCXX_NOEXCEPT 605*38fd1498Szrj { } 606*38fd1498Szrj 607*38fd1498Szrj // Are all empty bitsets equal to each other? Are they equal to 608*38fd1498Szrj // themselves? How to compare a thing which has no state? What is 609*38fd1498Szrj // the sound of one zero-length bitset clapping? 610*38fd1498Szrj bool 611*38fd1498Szrj _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT 612*38fd1498Szrj { return true; } 613*38fd1498Szrj 614*38fd1498Szrj template<size_t _Nb> 615*38fd1498Szrj bool 616*38fd1498Szrj _M_are_all() const _GLIBCXX_NOEXCEPT 617*38fd1498Szrj { return true; } 618*38fd1498Szrj 619*38fd1498Szrj bool 620*38fd1498Szrj _M_is_any() const _GLIBCXX_NOEXCEPT 621*38fd1498Szrj { return false; } 622*38fd1498Szrj 623*38fd1498Szrj size_t 624*38fd1498Szrj _M_do_count() const _GLIBCXX_NOEXCEPT 625*38fd1498Szrj { return 0; } 626*38fd1498Szrj 627*38fd1498Szrj unsigned long 628*38fd1498Szrj _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 629*38fd1498Szrj { return 0; } 630*38fd1498Szrj 631*38fd1498Szrj#if __cplusplus >= 201103L 632*38fd1498Szrj unsigned long long 633*38fd1498Szrj _M_do_to_ullong() const noexcept 634*38fd1498Szrj { return 0; } 635*38fd1498Szrj#endif 636*38fd1498Szrj 637*38fd1498Szrj // Normally "not found" is the size, but that could also be 638*38fd1498Szrj // misinterpreted as an index in this corner case. Oh well. 639*38fd1498Szrj size_t 640*38fd1498Szrj _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT 641*38fd1498Szrj { return 0; } 642*38fd1498Szrj 643*38fd1498Szrj size_t 644*38fd1498Szrj _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT 645*38fd1498Szrj { return 0; } 646*38fd1498Szrj }; 647*38fd1498Szrj 648*38fd1498Szrj 649*38fd1498Szrj // Helper class to zero out the unused high-order bits in the highest word. 650*38fd1498Szrj template<size_t _Extrabits> 651*38fd1498Szrj struct _Sanitize 652*38fd1498Szrj { 653*38fd1498Szrj typedef unsigned long _WordT; 654*38fd1498Szrj 655*38fd1498Szrj static void 656*38fd1498Szrj _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT 657*38fd1498Szrj { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); } 658*38fd1498Szrj }; 659*38fd1498Szrj 660*38fd1498Szrj template<> 661*38fd1498Szrj struct _Sanitize<0> 662*38fd1498Szrj { 663*38fd1498Szrj typedef unsigned long _WordT; 664*38fd1498Szrj 665*38fd1498Szrj static void 666*38fd1498Szrj _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { } 667*38fd1498Szrj }; 668*38fd1498Szrj 669*38fd1498Szrj#if __cplusplus >= 201103L 670*38fd1498Szrj template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)> 671*38fd1498Szrj struct _Sanitize_val 672*38fd1498Szrj { 673*38fd1498Szrj static constexpr unsigned long long 674*38fd1498Szrj _S_do_sanitize_val(unsigned long long __val) 675*38fd1498Szrj { return __val; } 676*38fd1498Szrj }; 677*38fd1498Szrj 678*38fd1498Szrj template<size_t _Nb> 679*38fd1498Szrj struct _Sanitize_val<_Nb, true> 680*38fd1498Szrj { 681*38fd1498Szrj static constexpr unsigned long long 682*38fd1498Szrj _S_do_sanitize_val(unsigned long long __val) 683*38fd1498Szrj { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); } 684*38fd1498Szrj }; 685*38fd1498Szrj#endif 686*38fd1498Szrj 687*38fd1498Szrj /** 688*38fd1498Szrj * @brief The %bitset class represents a @e fixed-size sequence of bits. 689*38fd1498Szrj * @ingroup utilities 690*38fd1498Szrj * 691*38fd1498Szrj * (Note that %bitset does @e not meet the formal requirements of a 692*38fd1498Szrj * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.) 693*38fd1498Szrj * 694*38fd1498Szrj * The template argument, @a Nb, may be any non-negative number, 695*38fd1498Szrj * specifying the number of bits (e.g., "0", "12", "1024*1024"). 696*38fd1498Szrj * 697*38fd1498Szrj * In the general unoptimized case, storage is allocated in word-sized 698*38fd1498Szrj * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B 699*38fd1498Szrj * words will be used for storage. B - Nb%B bits are unused. (They are 700*38fd1498Szrj * the high-order bits in the highest word.) It is a class invariant 701*38fd1498Szrj * that those unused bits are always zero. 702*38fd1498Szrj * 703*38fd1498Szrj * If you think of %bitset as <em>a simple array of bits</em>, be 704*38fd1498Szrj * aware that your mental picture is reversed: a %bitset behaves 705*38fd1498Szrj * the same way as bits in integers do, with the bit at index 0 in 706*38fd1498Szrj * the <em>least significant / right-hand</em> position, and the bit at 707*38fd1498Szrj * index Nb-1 in the <em>most significant / left-hand</em> position. 708*38fd1498Szrj * Thus, unlike other containers, a %bitset's index <em>counts from 709*38fd1498Szrj * right to left</em>, to put it very loosely. 710*38fd1498Szrj * 711*38fd1498Szrj * This behavior is preserved when translating to and from strings. For 712*38fd1498Szrj * example, the first line of the following program probably prints 713*38fd1498Szrj * <em>b('a') is 0001100001</em> on a modern ASCII system. 714*38fd1498Szrj * 715*38fd1498Szrj * @code 716*38fd1498Szrj * #include <bitset> 717*38fd1498Szrj * #include <iostream> 718*38fd1498Szrj * #include <sstream> 719*38fd1498Szrj * 720*38fd1498Szrj * using namespace std; 721*38fd1498Szrj * 722*38fd1498Szrj * int main() 723*38fd1498Szrj * { 724*38fd1498Szrj * long a = 'a'; 725*38fd1498Szrj * bitset<10> b(a); 726*38fd1498Szrj * 727*38fd1498Szrj * cout << "b('a') is " << b << endl; 728*38fd1498Szrj * 729*38fd1498Szrj * ostringstream s; 730*38fd1498Szrj * s << b; 731*38fd1498Szrj * string str = s.str(); 732*38fd1498Szrj * cout << "index 3 in the string is " << str[3] << " but\n" 733*38fd1498Szrj * << "index 3 in the bitset is " << b[3] << endl; 734*38fd1498Szrj * } 735*38fd1498Szrj * @endcode 736*38fd1498Szrj * 737*38fd1498Szrj * Also see: 738*38fd1498Szrj * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html 739*38fd1498Szrj * for a description of extensions. 740*38fd1498Szrj * 741*38fd1498Szrj * Most of the actual code isn't contained in %bitset<> itself, but in the 742*38fd1498Szrj * base class _Base_bitset. The base class works with whole words, not with 743*38fd1498Szrj * individual bits. This allows us to specialize _Base_bitset for the 744*38fd1498Szrj * important special case where the %bitset is only a single word. 745*38fd1498Szrj * 746*38fd1498Szrj * Extra confusion can result due to the fact that the storage for 747*38fd1498Szrj * _Base_bitset @e is a regular array, and is indexed as such. This is 748*38fd1498Szrj * carefully encapsulated. 749*38fd1498Szrj */ 750*38fd1498Szrj template<size_t _Nb> 751*38fd1498Szrj class bitset 752*38fd1498Szrj : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> 753*38fd1498Szrj { 754*38fd1498Szrj private: 755*38fd1498Szrj typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base; 756*38fd1498Szrj typedef unsigned long _WordT; 757*38fd1498Szrj 758*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 759*38fd1498Szrj void 760*38fd1498Szrj _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 761*38fd1498Szrj size_t __position) const 762*38fd1498Szrj { 763*38fd1498Szrj if (__position > __s.size()) 764*38fd1498Szrj __throw_out_of_range_fmt(__N("bitset::bitset: __position " 765*38fd1498Szrj "(which is %zu) > __s.size() " 766*38fd1498Szrj "(which is %zu)"), 767*38fd1498Szrj __position, __s.size()); 768*38fd1498Szrj } 769*38fd1498Szrj 770*38fd1498Szrj void _M_check(size_t __position, const char *__s) const 771*38fd1498Szrj { 772*38fd1498Szrj if (__position >= _Nb) 773*38fd1498Szrj __throw_out_of_range_fmt(__N("%s: __position (which is %zu) " 774*38fd1498Szrj ">= _Nb (which is %zu)"), 775*38fd1498Szrj __s, __position, _Nb); 776*38fd1498Szrj } 777*38fd1498Szrj 778*38fd1498Szrj void 779*38fd1498Szrj _M_do_sanitize() _GLIBCXX_NOEXCEPT 780*38fd1498Szrj { 781*38fd1498Szrj typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type; 782*38fd1498Szrj __sanitize_type::_S_do_sanitize(this->_M_hiword()); 783*38fd1498Szrj } 784*38fd1498Szrj 785*38fd1498Szrj#if __cplusplus >= 201103L 786*38fd1498Szrj friend struct std::hash<bitset>; 787*38fd1498Szrj#endif 788*38fd1498Szrj 789*38fd1498Szrj public: 790*38fd1498Szrj /** 791*38fd1498Szrj * This encapsulates the concept of a single bit. An instance of this 792*38fd1498Szrj * class is a proxy for an actual bit; this way the individual bit 793*38fd1498Szrj * operations are done as faster word-size bitwise instructions. 794*38fd1498Szrj * 795*38fd1498Szrj * Most users will never need to use this class directly; conversions 796*38fd1498Szrj * to and from bool are automatic and should be transparent. Overloaded 797*38fd1498Szrj * operators help to preserve the illusion. 798*38fd1498Szrj * 799*38fd1498Szrj * (On a typical system, this <em>bit %reference</em> is 64 800*38fd1498Szrj * times the size of an actual bit. Ha.) 801*38fd1498Szrj */ 802*38fd1498Szrj class reference 803*38fd1498Szrj { 804*38fd1498Szrj friend class bitset; 805*38fd1498Szrj 806*38fd1498Szrj _WordT* _M_wp; 807*38fd1498Szrj size_t _M_bpos; 808*38fd1498Szrj 809*38fd1498Szrj // left undefined 810*38fd1498Szrj reference(); 811*38fd1498Szrj 812*38fd1498Szrj public: 813*38fd1498Szrj reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT 814*38fd1498Szrj { 815*38fd1498Szrj _M_wp = &__b._M_getword(__pos); 816*38fd1498Szrj _M_bpos = _Base::_S_whichbit(__pos); 817*38fd1498Szrj } 818*38fd1498Szrj 819*38fd1498Szrj ~reference() _GLIBCXX_NOEXCEPT 820*38fd1498Szrj { } 821*38fd1498Szrj 822*38fd1498Szrj // For b[i] = __x; 823*38fd1498Szrj reference& 824*38fd1498Szrj operator=(bool __x) _GLIBCXX_NOEXCEPT 825*38fd1498Szrj { 826*38fd1498Szrj if (__x) 827*38fd1498Szrj *_M_wp |= _Base::_S_maskbit(_M_bpos); 828*38fd1498Szrj else 829*38fd1498Szrj *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 830*38fd1498Szrj return *this; 831*38fd1498Szrj } 832*38fd1498Szrj 833*38fd1498Szrj // For b[i] = b[__j]; 834*38fd1498Szrj reference& 835*38fd1498Szrj operator=(const reference& __j) _GLIBCXX_NOEXCEPT 836*38fd1498Szrj { 837*38fd1498Szrj if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos))) 838*38fd1498Szrj *_M_wp |= _Base::_S_maskbit(_M_bpos); 839*38fd1498Szrj else 840*38fd1498Szrj *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 841*38fd1498Szrj return *this; 842*38fd1498Szrj } 843*38fd1498Szrj 844*38fd1498Szrj // Flips the bit 845*38fd1498Szrj bool 846*38fd1498Szrj operator~() const _GLIBCXX_NOEXCEPT 847*38fd1498Szrj { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; } 848*38fd1498Szrj 849*38fd1498Szrj // For __x = b[i]; 850*38fd1498Szrj operator bool() const _GLIBCXX_NOEXCEPT 851*38fd1498Szrj { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; } 852*38fd1498Szrj 853*38fd1498Szrj // For b[i].flip(); 854*38fd1498Szrj reference& 855*38fd1498Szrj flip() _GLIBCXX_NOEXCEPT 856*38fd1498Szrj { 857*38fd1498Szrj *_M_wp ^= _Base::_S_maskbit(_M_bpos); 858*38fd1498Szrj return *this; 859*38fd1498Szrj } 860*38fd1498Szrj }; 861*38fd1498Szrj friend class reference; 862*38fd1498Szrj 863*38fd1498Szrj // 23.3.5.1 constructors: 864*38fd1498Szrj /// All bits set to zero. 865*38fd1498Szrj _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT 866*38fd1498Szrj { } 867*38fd1498Szrj 868*38fd1498Szrj /// Initial bits bitwise-copied from a single word (others set to zero). 869*38fd1498Szrj#if __cplusplus >= 201103L 870*38fd1498Szrj constexpr bitset(unsigned long long __val) noexcept 871*38fd1498Szrj : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { } 872*38fd1498Szrj#else 873*38fd1498Szrj bitset(unsigned long __val) 874*38fd1498Szrj : _Base(__val) 875*38fd1498Szrj { _M_do_sanitize(); } 876*38fd1498Szrj#endif 877*38fd1498Szrj 878*38fd1498Szrj /** 879*38fd1498Szrj * Use a subset of a string. 880*38fd1498Szrj * @param __s A string of @a 0 and @a 1 characters. 881*38fd1498Szrj * @param __position Index of the first character in @a __s to use; 882*38fd1498Szrj * defaults to zero. 883*38fd1498Szrj * @throw std::out_of_range If @a pos is bigger the size of @a __s. 884*38fd1498Szrj * @throw std::invalid_argument If a character appears in the string 885*38fd1498Szrj * which is neither @a 0 nor @a 1. 886*38fd1498Szrj */ 887*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 888*38fd1498Szrj explicit 889*38fd1498Szrj bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 890*38fd1498Szrj size_t __position = 0) 891*38fd1498Szrj : _Base() 892*38fd1498Szrj { 893*38fd1498Szrj _M_check_initial_position(__s, __position); 894*38fd1498Szrj _M_copy_from_string(__s, __position, 895*38fd1498Szrj std::basic_string<_CharT, _Traits, _Alloc>::npos, 896*38fd1498Szrj _CharT('0'), _CharT('1')); 897*38fd1498Szrj } 898*38fd1498Szrj 899*38fd1498Szrj /** 900*38fd1498Szrj * Use a subset of a string. 901*38fd1498Szrj * @param __s A string of @a 0 and @a 1 characters. 902*38fd1498Szrj * @param __position Index of the first character in @a __s to use. 903*38fd1498Szrj * @param __n The number of characters to copy. 904*38fd1498Szrj * @throw std::out_of_range If @a __position is bigger the size 905*38fd1498Szrj * of @a __s. 906*38fd1498Szrj * @throw std::invalid_argument If a character appears in the string 907*38fd1498Szrj * which is neither @a 0 nor @a 1. 908*38fd1498Szrj */ 909*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 910*38fd1498Szrj bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 911*38fd1498Szrj size_t __position, size_t __n) 912*38fd1498Szrj : _Base() 913*38fd1498Szrj { 914*38fd1498Szrj _M_check_initial_position(__s, __position); 915*38fd1498Szrj _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1')); 916*38fd1498Szrj } 917*38fd1498Szrj 918*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 919*38fd1498Szrj // 396. what are characters zero and one. 920*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 921*38fd1498Szrj bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 922*38fd1498Szrj size_t __position, size_t __n, 923*38fd1498Szrj _CharT __zero, _CharT __one = _CharT('1')) 924*38fd1498Szrj : _Base() 925*38fd1498Szrj { 926*38fd1498Szrj _M_check_initial_position(__s, __position); 927*38fd1498Szrj _M_copy_from_string(__s, __position, __n, __zero, __one); 928*38fd1498Szrj } 929*38fd1498Szrj 930*38fd1498Szrj#if __cplusplus >= 201103L 931*38fd1498Szrj /** 932*38fd1498Szrj * Construct from a character %array. 933*38fd1498Szrj * @param __str An %array of characters @a zero and @a one. 934*38fd1498Szrj * @param __n The number of characters to use. 935*38fd1498Szrj * @param __zero The character corresponding to the value 0. 936*38fd1498Szrj * @param __one The character corresponding to the value 1. 937*38fd1498Szrj * @throw std::invalid_argument If a character appears in the string 938*38fd1498Szrj * which is neither @a __zero nor @a __one. 939*38fd1498Szrj */ 940*38fd1498Szrj template<typename _CharT> 941*38fd1498Szrj explicit 942*38fd1498Szrj bitset(const _CharT* __str, 943*38fd1498Szrj typename std::basic_string<_CharT>::size_type __n 944*38fd1498Szrj = std::basic_string<_CharT>::npos, 945*38fd1498Szrj _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) 946*38fd1498Szrj : _Base() 947*38fd1498Szrj { 948*38fd1498Szrj if (!__str) 949*38fd1498Szrj __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)")); 950*38fd1498Szrj 951*38fd1498Szrj if (__n == std::basic_string<_CharT>::npos) 952*38fd1498Szrj __n = std::char_traits<_CharT>::length(__str); 953*38fd1498Szrj _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0, 954*38fd1498Szrj __n, __zero, 955*38fd1498Szrj __one); 956*38fd1498Szrj } 957*38fd1498Szrj#endif 958*38fd1498Szrj 959*38fd1498Szrj // 23.3.5.2 bitset operations: 960*38fd1498Szrj //@{ 961*38fd1498Szrj /** 962*38fd1498Szrj * Operations on bitsets. 963*38fd1498Szrj * @param __rhs A same-sized bitset. 964*38fd1498Szrj * 965*38fd1498Szrj * These should be self-explanatory. 966*38fd1498Szrj */ 967*38fd1498Szrj bitset<_Nb>& 968*38fd1498Szrj operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 969*38fd1498Szrj { 970*38fd1498Szrj this->_M_do_and(__rhs); 971*38fd1498Szrj return *this; 972*38fd1498Szrj } 973*38fd1498Szrj 974*38fd1498Szrj bitset<_Nb>& 975*38fd1498Szrj operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 976*38fd1498Szrj { 977*38fd1498Szrj this->_M_do_or(__rhs); 978*38fd1498Szrj return *this; 979*38fd1498Szrj } 980*38fd1498Szrj 981*38fd1498Szrj bitset<_Nb>& 982*38fd1498Szrj operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 983*38fd1498Szrj { 984*38fd1498Szrj this->_M_do_xor(__rhs); 985*38fd1498Szrj return *this; 986*38fd1498Szrj } 987*38fd1498Szrj //@} 988*38fd1498Szrj 989*38fd1498Szrj //@{ 990*38fd1498Szrj /** 991*38fd1498Szrj * Operations on bitsets. 992*38fd1498Szrj * @param __position The number of places to shift. 993*38fd1498Szrj * 994*38fd1498Szrj * These should be self-explanatory. 995*38fd1498Szrj */ 996*38fd1498Szrj bitset<_Nb>& 997*38fd1498Szrj operator<<=(size_t __position) _GLIBCXX_NOEXCEPT 998*38fd1498Szrj { 999*38fd1498Szrj if (__builtin_expect(__position < _Nb, 1)) 1000*38fd1498Szrj { 1001*38fd1498Szrj this->_M_do_left_shift(__position); 1002*38fd1498Szrj this->_M_do_sanitize(); 1003*38fd1498Szrj } 1004*38fd1498Szrj else 1005*38fd1498Szrj this->_M_do_reset(); 1006*38fd1498Szrj return *this; 1007*38fd1498Szrj } 1008*38fd1498Szrj 1009*38fd1498Szrj bitset<_Nb>& 1010*38fd1498Szrj operator>>=(size_t __position) _GLIBCXX_NOEXCEPT 1011*38fd1498Szrj { 1012*38fd1498Szrj if (__builtin_expect(__position < _Nb, 1)) 1013*38fd1498Szrj { 1014*38fd1498Szrj this->_M_do_right_shift(__position); 1015*38fd1498Szrj this->_M_do_sanitize(); 1016*38fd1498Szrj } 1017*38fd1498Szrj else 1018*38fd1498Szrj this->_M_do_reset(); 1019*38fd1498Szrj return *this; 1020*38fd1498Szrj } 1021*38fd1498Szrj //@} 1022*38fd1498Szrj 1023*38fd1498Szrj //@{ 1024*38fd1498Szrj /** 1025*38fd1498Szrj * These versions of single-bit set, reset, flip, and test are 1026*38fd1498Szrj * extensions from the SGI version. They do no range checking. 1027*38fd1498Szrj * @ingroup SGIextensions 1028*38fd1498Szrj */ 1029*38fd1498Szrj bitset<_Nb>& 1030*38fd1498Szrj _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT 1031*38fd1498Szrj { 1032*38fd1498Szrj this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 1033*38fd1498Szrj return *this; 1034*38fd1498Szrj } 1035*38fd1498Szrj 1036*38fd1498Szrj bitset<_Nb>& 1037*38fd1498Szrj _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT 1038*38fd1498Szrj { 1039*38fd1498Szrj if (__val) 1040*38fd1498Szrj this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 1041*38fd1498Szrj else 1042*38fd1498Szrj this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 1043*38fd1498Szrj return *this; 1044*38fd1498Szrj } 1045*38fd1498Szrj 1046*38fd1498Szrj bitset<_Nb>& 1047*38fd1498Szrj _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT 1048*38fd1498Szrj { 1049*38fd1498Szrj this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 1050*38fd1498Szrj return *this; 1051*38fd1498Szrj } 1052*38fd1498Szrj 1053*38fd1498Szrj bitset<_Nb>& 1054*38fd1498Szrj _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT 1055*38fd1498Szrj { 1056*38fd1498Szrj this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos); 1057*38fd1498Szrj return *this; 1058*38fd1498Szrj } 1059*38fd1498Szrj 1060*38fd1498Szrj _GLIBCXX_CONSTEXPR bool 1061*38fd1498Szrj _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT 1062*38fd1498Szrj { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos)) 1063*38fd1498Szrj != static_cast<_WordT>(0)); } 1064*38fd1498Szrj //@} 1065*38fd1498Szrj 1066*38fd1498Szrj // Set, reset, and flip. 1067*38fd1498Szrj /** 1068*38fd1498Szrj * @brief Sets every bit to true. 1069*38fd1498Szrj */ 1070*38fd1498Szrj bitset<_Nb>& 1071*38fd1498Szrj set() _GLIBCXX_NOEXCEPT 1072*38fd1498Szrj { 1073*38fd1498Szrj this->_M_do_set(); 1074*38fd1498Szrj this->_M_do_sanitize(); 1075*38fd1498Szrj return *this; 1076*38fd1498Szrj } 1077*38fd1498Szrj 1078*38fd1498Szrj /** 1079*38fd1498Szrj * @brief Sets a given bit to a particular value. 1080*38fd1498Szrj * @param __position The index of the bit. 1081*38fd1498Szrj * @param __val Either true or false, defaults to true. 1082*38fd1498Szrj * @throw std::out_of_range If @a pos is bigger the size of the %set. 1083*38fd1498Szrj */ 1084*38fd1498Szrj bitset<_Nb>& 1085*38fd1498Szrj set(size_t __position, bool __val = true) 1086*38fd1498Szrj { 1087*38fd1498Szrj this->_M_check(__position, __N("bitset::set")); 1088*38fd1498Szrj return _Unchecked_set(__position, __val); 1089*38fd1498Szrj } 1090*38fd1498Szrj 1091*38fd1498Szrj /** 1092*38fd1498Szrj * @brief Sets every bit to false. 1093*38fd1498Szrj */ 1094*38fd1498Szrj bitset<_Nb>& 1095*38fd1498Szrj reset() _GLIBCXX_NOEXCEPT 1096*38fd1498Szrj { 1097*38fd1498Szrj this->_M_do_reset(); 1098*38fd1498Szrj return *this; 1099*38fd1498Szrj } 1100*38fd1498Szrj 1101*38fd1498Szrj /** 1102*38fd1498Szrj * @brief Sets a given bit to false. 1103*38fd1498Szrj * @param __position The index of the bit. 1104*38fd1498Szrj * @throw std::out_of_range If @a pos is bigger the size of the %set. 1105*38fd1498Szrj * 1106*38fd1498Szrj * Same as writing @c set(pos,false). 1107*38fd1498Szrj */ 1108*38fd1498Szrj bitset<_Nb>& 1109*38fd1498Szrj reset(size_t __position) 1110*38fd1498Szrj { 1111*38fd1498Szrj this->_M_check(__position, __N("bitset::reset")); 1112*38fd1498Szrj return _Unchecked_reset(__position); 1113*38fd1498Szrj } 1114*38fd1498Szrj 1115*38fd1498Szrj /** 1116*38fd1498Szrj * @brief Toggles every bit to its opposite value. 1117*38fd1498Szrj */ 1118*38fd1498Szrj bitset<_Nb>& 1119*38fd1498Szrj flip() _GLIBCXX_NOEXCEPT 1120*38fd1498Szrj { 1121*38fd1498Szrj this->_M_do_flip(); 1122*38fd1498Szrj this->_M_do_sanitize(); 1123*38fd1498Szrj return *this; 1124*38fd1498Szrj } 1125*38fd1498Szrj 1126*38fd1498Szrj /** 1127*38fd1498Szrj * @brief Toggles a given bit to its opposite value. 1128*38fd1498Szrj * @param __position The index of the bit. 1129*38fd1498Szrj * @throw std::out_of_range If @a pos is bigger the size of the %set. 1130*38fd1498Szrj */ 1131*38fd1498Szrj bitset<_Nb>& 1132*38fd1498Szrj flip(size_t __position) 1133*38fd1498Szrj { 1134*38fd1498Szrj this->_M_check(__position, __N("bitset::flip")); 1135*38fd1498Szrj return _Unchecked_flip(__position); 1136*38fd1498Szrj } 1137*38fd1498Szrj 1138*38fd1498Szrj /// See the no-argument flip(). 1139*38fd1498Szrj bitset<_Nb> 1140*38fd1498Szrj operator~() const _GLIBCXX_NOEXCEPT 1141*38fd1498Szrj { return bitset<_Nb>(*this).flip(); } 1142*38fd1498Szrj 1143*38fd1498Szrj //@{ 1144*38fd1498Szrj /** 1145*38fd1498Szrj * @brief Array-indexing support. 1146*38fd1498Szrj * @param __position Index into the %bitset. 1147*38fd1498Szrj * @return A bool for a <em>const %bitset</em>. For non-const 1148*38fd1498Szrj * bitsets, an instance of the reference proxy class. 1149*38fd1498Szrj * @note These operators do no range checking and throw no exceptions, 1150*38fd1498Szrj * as required by DR 11 to the standard. 1151*38fd1498Szrj * 1152*38fd1498Szrj * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already 1153*38fd1498Szrj * resolves DR 11 (items 1 and 2), but does not do the range-checking 1154*38fd1498Szrj * required by that DR's resolution. -pme 1155*38fd1498Szrj * The DR has since been changed: range-checking is a precondition 1156*38fd1498Szrj * (users' responsibility), and these functions must not throw. -pme 1157*38fd1498Szrj */ 1158*38fd1498Szrj reference 1159*38fd1498Szrj operator[](size_t __position) 1160*38fd1498Szrj { return reference(*this, __position); } 1161*38fd1498Szrj 1162*38fd1498Szrj _GLIBCXX_CONSTEXPR bool 1163*38fd1498Szrj operator[](size_t __position) const 1164*38fd1498Szrj { return _Unchecked_test(__position); } 1165*38fd1498Szrj //@} 1166*38fd1498Szrj 1167*38fd1498Szrj /** 1168*38fd1498Szrj * @brief Returns a numerical interpretation of the %bitset. 1169*38fd1498Szrj * @return The integral equivalent of the bits. 1170*38fd1498Szrj * @throw std::overflow_error If there are too many bits to be 1171*38fd1498Szrj * represented in an @c unsigned @c long. 1172*38fd1498Szrj */ 1173*38fd1498Szrj unsigned long 1174*38fd1498Szrj to_ulong() const 1175*38fd1498Szrj { return this->_M_do_to_ulong(); } 1176*38fd1498Szrj 1177*38fd1498Szrj#if __cplusplus >= 201103L 1178*38fd1498Szrj unsigned long long 1179*38fd1498Szrj to_ullong() const 1180*38fd1498Szrj { return this->_M_do_to_ullong(); } 1181*38fd1498Szrj#endif 1182*38fd1498Szrj 1183*38fd1498Szrj /** 1184*38fd1498Szrj * @brief Returns a character interpretation of the %bitset. 1185*38fd1498Szrj * @return The string equivalent of the bits. 1186*38fd1498Szrj * 1187*38fd1498Szrj * Note the ordering of the bits: decreasing character positions 1188*38fd1498Szrj * correspond to increasing bit positions (see the main class notes for 1189*38fd1498Szrj * an example). 1190*38fd1498Szrj */ 1191*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1192*38fd1498Szrj std::basic_string<_CharT, _Traits, _Alloc> 1193*38fd1498Szrj to_string() const 1194*38fd1498Szrj { 1195*38fd1498Szrj std::basic_string<_CharT, _Traits, _Alloc> __result; 1196*38fd1498Szrj _M_copy_to_string(__result, _CharT('0'), _CharT('1')); 1197*38fd1498Szrj return __result; 1198*38fd1498Szrj } 1199*38fd1498Szrj 1200*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1201*38fd1498Szrj // 396. what are characters zero and one. 1202*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1203*38fd1498Szrj std::basic_string<_CharT, _Traits, _Alloc> 1204*38fd1498Szrj to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1205*38fd1498Szrj { 1206*38fd1498Szrj std::basic_string<_CharT, _Traits, _Alloc> __result; 1207*38fd1498Szrj _M_copy_to_string(__result, __zero, __one); 1208*38fd1498Szrj return __result; 1209*38fd1498Szrj } 1210*38fd1498Szrj 1211*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1212*38fd1498Szrj // 434. bitset::to_string() hard to use. 1213*38fd1498Szrj template<class _CharT, class _Traits> 1214*38fd1498Szrj std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 1215*38fd1498Szrj to_string() const 1216*38fd1498Szrj { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } 1217*38fd1498Szrj 1218*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1219*38fd1498Szrj // 853. to_string needs updating with zero and one. 1220*38fd1498Szrj template<class _CharT, class _Traits> 1221*38fd1498Szrj std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 1222*38fd1498Szrj to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1223*38fd1498Szrj { return to_string<_CharT, _Traits, 1224*38fd1498Szrj std::allocator<_CharT> >(__zero, __one); } 1225*38fd1498Szrj 1226*38fd1498Szrj template<class _CharT> 1227*38fd1498Szrj std::basic_string<_CharT, std::char_traits<_CharT>, 1228*38fd1498Szrj std::allocator<_CharT> > 1229*38fd1498Szrj to_string() const 1230*38fd1498Szrj { 1231*38fd1498Szrj return to_string<_CharT, std::char_traits<_CharT>, 1232*38fd1498Szrj std::allocator<_CharT> >(); 1233*38fd1498Szrj } 1234*38fd1498Szrj 1235*38fd1498Szrj template<class _CharT> 1236*38fd1498Szrj std::basic_string<_CharT, std::char_traits<_CharT>, 1237*38fd1498Szrj std::allocator<_CharT> > 1238*38fd1498Szrj to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1239*38fd1498Szrj { 1240*38fd1498Szrj return to_string<_CharT, std::char_traits<_CharT>, 1241*38fd1498Szrj std::allocator<_CharT> >(__zero, __one); 1242*38fd1498Szrj } 1243*38fd1498Szrj 1244*38fd1498Szrj std::basic_string<char, std::char_traits<char>, std::allocator<char> > 1245*38fd1498Szrj to_string() const 1246*38fd1498Szrj { 1247*38fd1498Szrj return to_string<char, std::char_traits<char>, 1248*38fd1498Szrj std::allocator<char> >(); 1249*38fd1498Szrj } 1250*38fd1498Szrj 1251*38fd1498Szrj std::basic_string<char, std::char_traits<char>, std::allocator<char> > 1252*38fd1498Szrj to_string(char __zero, char __one = '1') const 1253*38fd1498Szrj { 1254*38fd1498Szrj return to_string<char, std::char_traits<char>, 1255*38fd1498Szrj std::allocator<char> >(__zero, __one); 1256*38fd1498Szrj } 1257*38fd1498Szrj 1258*38fd1498Szrj // Helper functions for string operations. 1259*38fd1498Szrj template<class _CharT, class _Traits> 1260*38fd1498Szrj void 1261*38fd1498Szrj _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t, 1262*38fd1498Szrj _CharT, _CharT); 1263*38fd1498Szrj 1264*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1265*38fd1498Szrj void 1266*38fd1498Szrj _M_copy_from_string(const std::basic_string<_CharT, 1267*38fd1498Szrj _Traits, _Alloc>& __s, size_t __pos, size_t __n, 1268*38fd1498Szrj _CharT __zero, _CharT __one) 1269*38fd1498Szrj { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n, 1270*38fd1498Szrj __zero, __one); } 1271*38fd1498Szrj 1272*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1273*38fd1498Szrj void 1274*38fd1498Szrj _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&, 1275*38fd1498Szrj _CharT, _CharT) const; 1276*38fd1498Szrj 1277*38fd1498Szrj // NB: Backward compat. 1278*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1279*38fd1498Szrj void 1280*38fd1498Szrj _M_copy_from_string(const std::basic_string<_CharT, 1281*38fd1498Szrj _Traits, _Alloc>& __s, size_t __pos, size_t __n) 1282*38fd1498Szrj { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); } 1283*38fd1498Szrj 1284*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1285*38fd1498Szrj void 1286*38fd1498Szrj _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const 1287*38fd1498Szrj { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); } 1288*38fd1498Szrj 1289*38fd1498Szrj /// Returns the number of bits which are set. 1290*38fd1498Szrj size_t 1291*38fd1498Szrj count() const _GLIBCXX_NOEXCEPT 1292*38fd1498Szrj { return this->_M_do_count(); } 1293*38fd1498Szrj 1294*38fd1498Szrj /// Returns the total number of bits. 1295*38fd1498Szrj _GLIBCXX_CONSTEXPR size_t 1296*38fd1498Szrj size() const _GLIBCXX_NOEXCEPT 1297*38fd1498Szrj { return _Nb; } 1298*38fd1498Szrj 1299*38fd1498Szrj //@{ 1300*38fd1498Szrj /// These comparisons for equality/inequality are, well, @e bitwise. 1301*38fd1498Szrj bool 1302*38fd1498Szrj operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 1303*38fd1498Szrj { return this->_M_is_equal(__rhs); } 1304*38fd1498Szrj 1305*38fd1498Szrj bool 1306*38fd1498Szrj operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 1307*38fd1498Szrj { return !this->_M_is_equal(__rhs); } 1308*38fd1498Szrj //@} 1309*38fd1498Szrj 1310*38fd1498Szrj /** 1311*38fd1498Szrj * @brief Tests the value of a bit. 1312*38fd1498Szrj * @param __position The index of a bit. 1313*38fd1498Szrj * @return The value at @a pos. 1314*38fd1498Szrj * @throw std::out_of_range If @a pos is bigger the size of the %set. 1315*38fd1498Szrj */ 1316*38fd1498Szrj bool 1317*38fd1498Szrj test(size_t __position) const 1318*38fd1498Szrj { 1319*38fd1498Szrj this->_M_check(__position, __N("bitset::test")); 1320*38fd1498Szrj return _Unchecked_test(__position); 1321*38fd1498Szrj } 1322*38fd1498Szrj 1323*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1324*38fd1498Szrj // DR 693. std::bitset::all() missing. 1325*38fd1498Szrj /** 1326*38fd1498Szrj * @brief Tests whether all the bits are on. 1327*38fd1498Szrj * @return True if all the bits are set. 1328*38fd1498Szrj */ 1329*38fd1498Szrj bool 1330*38fd1498Szrj all() const _GLIBCXX_NOEXCEPT 1331*38fd1498Szrj { return this->template _M_are_all<_Nb>(); } 1332*38fd1498Szrj 1333*38fd1498Szrj /** 1334*38fd1498Szrj * @brief Tests whether any of the bits are on. 1335*38fd1498Szrj * @return True if at least one bit is set. 1336*38fd1498Szrj */ 1337*38fd1498Szrj bool 1338*38fd1498Szrj any() const _GLIBCXX_NOEXCEPT 1339*38fd1498Szrj { return this->_M_is_any(); } 1340*38fd1498Szrj 1341*38fd1498Szrj /** 1342*38fd1498Szrj * @brief Tests whether any of the bits are on. 1343*38fd1498Szrj * @return True if none of the bits are set. 1344*38fd1498Szrj */ 1345*38fd1498Szrj bool 1346*38fd1498Szrj none() const _GLIBCXX_NOEXCEPT 1347*38fd1498Szrj { return !this->_M_is_any(); } 1348*38fd1498Szrj 1349*38fd1498Szrj //@{ 1350*38fd1498Szrj /// Self-explanatory. 1351*38fd1498Szrj bitset<_Nb> 1352*38fd1498Szrj operator<<(size_t __position) const _GLIBCXX_NOEXCEPT 1353*38fd1498Szrj { return bitset<_Nb>(*this) <<= __position; } 1354*38fd1498Szrj 1355*38fd1498Szrj bitset<_Nb> 1356*38fd1498Szrj operator>>(size_t __position) const _GLIBCXX_NOEXCEPT 1357*38fd1498Szrj { return bitset<_Nb>(*this) >>= __position; } 1358*38fd1498Szrj //@} 1359*38fd1498Szrj 1360*38fd1498Szrj /** 1361*38fd1498Szrj * @brief Finds the index of the first "on" bit. 1362*38fd1498Szrj * @return The index of the first bit set, or size() if not found. 1363*38fd1498Szrj * @ingroup SGIextensions 1364*38fd1498Szrj * @sa _Find_next 1365*38fd1498Szrj */ 1366*38fd1498Szrj size_t 1367*38fd1498Szrj _Find_first() const _GLIBCXX_NOEXCEPT 1368*38fd1498Szrj { return this->_M_do_find_first(_Nb); } 1369*38fd1498Szrj 1370*38fd1498Szrj /** 1371*38fd1498Szrj * @brief Finds the index of the next "on" bit after prev. 1372*38fd1498Szrj * @return The index of the next bit set, or size() if not found. 1373*38fd1498Szrj * @param __prev Where to start searching. 1374*38fd1498Szrj * @ingroup SGIextensions 1375*38fd1498Szrj * @sa _Find_first 1376*38fd1498Szrj */ 1377*38fd1498Szrj size_t 1378*38fd1498Szrj _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT 1379*38fd1498Szrj { return this->_M_do_find_next(__prev, _Nb); } 1380*38fd1498Szrj }; 1381*38fd1498Szrj 1382*38fd1498Szrj // Definitions of non-inline member functions. 1383*38fd1498Szrj template<size_t _Nb> 1384*38fd1498Szrj template<class _CharT, class _Traits> 1385*38fd1498Szrj void 1386*38fd1498Szrj bitset<_Nb>:: 1387*38fd1498Szrj _M_copy_from_ptr(const _CharT* __s, size_t __len, 1388*38fd1498Szrj size_t __pos, size_t __n, _CharT __zero, _CharT __one) 1389*38fd1498Szrj { 1390*38fd1498Szrj reset(); 1391*38fd1498Szrj const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos))); 1392*38fd1498Szrj for (size_t __i = __nbits; __i > 0; --__i) 1393*38fd1498Szrj { 1394*38fd1498Szrj const _CharT __c = __s[__pos + __nbits - __i]; 1395*38fd1498Szrj if (_Traits::eq(__c, __zero)) 1396*38fd1498Szrj ; 1397*38fd1498Szrj else if (_Traits::eq(__c, __one)) 1398*38fd1498Szrj _Unchecked_set(__i - 1); 1399*38fd1498Szrj else 1400*38fd1498Szrj __throw_invalid_argument(__N("bitset::_M_copy_from_ptr")); 1401*38fd1498Szrj } 1402*38fd1498Szrj } 1403*38fd1498Szrj 1404*38fd1498Szrj template<size_t _Nb> 1405*38fd1498Szrj template<class _CharT, class _Traits, class _Alloc> 1406*38fd1498Szrj void 1407*38fd1498Szrj bitset<_Nb>:: 1408*38fd1498Szrj _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s, 1409*38fd1498Szrj _CharT __zero, _CharT __one) const 1410*38fd1498Szrj { 1411*38fd1498Szrj __s.assign(_Nb, __zero); 1412*38fd1498Szrj for (size_t __i = _Nb; __i > 0; --__i) 1413*38fd1498Szrj if (_Unchecked_test(__i - 1)) 1414*38fd1498Szrj _Traits::assign(__s[_Nb - __i], __one); 1415*38fd1498Szrj } 1416*38fd1498Szrj 1417*38fd1498Szrj // 23.3.5.3 bitset operations: 1418*38fd1498Szrj //@{ 1419*38fd1498Szrj /** 1420*38fd1498Szrj * @brief Global bitwise operations on bitsets. 1421*38fd1498Szrj * @param __x A bitset. 1422*38fd1498Szrj * @param __y A bitset of the same size as @a __x. 1423*38fd1498Szrj * @return A new bitset. 1424*38fd1498Szrj * 1425*38fd1498Szrj * These should be self-explanatory. 1426*38fd1498Szrj */ 1427*38fd1498Szrj template<size_t _Nb> 1428*38fd1498Szrj inline bitset<_Nb> 1429*38fd1498Szrj operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1430*38fd1498Szrj { 1431*38fd1498Szrj bitset<_Nb> __result(__x); 1432*38fd1498Szrj __result &= __y; 1433*38fd1498Szrj return __result; 1434*38fd1498Szrj } 1435*38fd1498Szrj 1436*38fd1498Szrj template<size_t _Nb> 1437*38fd1498Szrj inline bitset<_Nb> 1438*38fd1498Szrj operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1439*38fd1498Szrj { 1440*38fd1498Szrj bitset<_Nb> __result(__x); 1441*38fd1498Szrj __result |= __y; 1442*38fd1498Szrj return __result; 1443*38fd1498Szrj } 1444*38fd1498Szrj 1445*38fd1498Szrj template <size_t _Nb> 1446*38fd1498Szrj inline bitset<_Nb> 1447*38fd1498Szrj operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1448*38fd1498Szrj { 1449*38fd1498Szrj bitset<_Nb> __result(__x); 1450*38fd1498Szrj __result ^= __y; 1451*38fd1498Szrj return __result; 1452*38fd1498Szrj } 1453*38fd1498Szrj //@} 1454*38fd1498Szrj 1455*38fd1498Szrj //@{ 1456*38fd1498Szrj /** 1457*38fd1498Szrj * @brief Global I/O operators for bitsets. 1458*38fd1498Szrj * 1459*38fd1498Szrj * Direct I/O between streams and bitsets is supported. Output is 1460*38fd1498Szrj * straightforward. Input will skip whitespace, only accept @a 0 and @a 1 1461*38fd1498Szrj * characters, and will only extract as many digits as the %bitset will 1462*38fd1498Szrj * hold. 1463*38fd1498Szrj */ 1464*38fd1498Szrj template<class _CharT, class _Traits, size_t _Nb> 1465*38fd1498Szrj std::basic_istream<_CharT, _Traits>& 1466*38fd1498Szrj operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) 1467*38fd1498Szrj { 1468*38fd1498Szrj typedef typename _Traits::char_type char_type; 1469*38fd1498Szrj typedef std::basic_istream<_CharT, _Traits> __istream_type; 1470*38fd1498Szrj typedef typename __istream_type::ios_base __ios_base; 1471*38fd1498Szrj 1472*38fd1498Szrj std::basic_string<_CharT, _Traits> __tmp; 1473*38fd1498Szrj __tmp.reserve(_Nb); 1474*38fd1498Szrj 1475*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1476*38fd1498Szrj // 303. Bitset input operator underspecified 1477*38fd1498Szrj const char_type __zero = __is.widen('0'); 1478*38fd1498Szrj const char_type __one = __is.widen('1'); 1479*38fd1498Szrj 1480*38fd1498Szrj typename __ios_base::iostate __state = __ios_base::goodbit; 1481*38fd1498Szrj typename __istream_type::sentry __sentry(__is); 1482*38fd1498Szrj if (__sentry) 1483*38fd1498Szrj { 1484*38fd1498Szrj __try 1485*38fd1498Szrj { 1486*38fd1498Szrj for (size_t __i = _Nb; __i > 0; --__i) 1487*38fd1498Szrj { 1488*38fd1498Szrj static typename _Traits::int_type __eof = _Traits::eof(); 1489*38fd1498Szrj 1490*38fd1498Szrj typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc(); 1491*38fd1498Szrj if (_Traits::eq_int_type(__c1, __eof)) 1492*38fd1498Szrj { 1493*38fd1498Szrj __state |= __ios_base::eofbit; 1494*38fd1498Szrj break; 1495*38fd1498Szrj } 1496*38fd1498Szrj else 1497*38fd1498Szrj { 1498*38fd1498Szrj const char_type __c2 = _Traits::to_char_type(__c1); 1499*38fd1498Szrj if (_Traits::eq(__c2, __zero)) 1500*38fd1498Szrj __tmp.push_back(__zero); 1501*38fd1498Szrj else if (_Traits::eq(__c2, __one)) 1502*38fd1498Szrj __tmp.push_back(__one); 1503*38fd1498Szrj else if (_Traits:: 1504*38fd1498Szrj eq_int_type(__is.rdbuf()->sputbackc(__c2), 1505*38fd1498Szrj __eof)) 1506*38fd1498Szrj { 1507*38fd1498Szrj __state |= __ios_base::failbit; 1508*38fd1498Szrj break; 1509*38fd1498Szrj } 1510*38fd1498Szrj } 1511*38fd1498Szrj } 1512*38fd1498Szrj } 1513*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 1514*38fd1498Szrj { 1515*38fd1498Szrj __is._M_setstate(__ios_base::badbit); 1516*38fd1498Szrj __throw_exception_again; 1517*38fd1498Szrj } 1518*38fd1498Szrj __catch(...) 1519*38fd1498Szrj { __is._M_setstate(__ios_base::badbit); } 1520*38fd1498Szrj } 1521*38fd1498Szrj 1522*38fd1498Szrj if (__tmp.empty() && _Nb) 1523*38fd1498Szrj __state |= __ios_base::failbit; 1524*38fd1498Szrj else 1525*38fd1498Szrj __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb, 1526*38fd1498Szrj __zero, __one); 1527*38fd1498Szrj if (__state) 1528*38fd1498Szrj __is.setstate(__state); 1529*38fd1498Szrj return __is; 1530*38fd1498Szrj } 1531*38fd1498Szrj 1532*38fd1498Szrj template <class _CharT, class _Traits, size_t _Nb> 1533*38fd1498Szrj std::basic_ostream<_CharT, _Traits>& 1534*38fd1498Szrj operator<<(std::basic_ostream<_CharT, _Traits>& __os, 1535*38fd1498Szrj const bitset<_Nb>& __x) 1536*38fd1498Szrj { 1537*38fd1498Szrj std::basic_string<_CharT, _Traits> __tmp; 1538*38fd1498Szrj 1539*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 1540*38fd1498Szrj // 396. what are characters zero and one. 1541*38fd1498Szrj const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc()); 1542*38fd1498Szrj __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1')); 1543*38fd1498Szrj return __os << __tmp; 1544*38fd1498Szrj } 1545*38fd1498Szrj //@} 1546*38fd1498Szrj 1547*38fd1498Szrj_GLIBCXX_END_NAMESPACE_CONTAINER 1548*38fd1498Szrj} // namespace std 1549*38fd1498Szrj 1550*38fd1498Szrj#undef _GLIBCXX_BITSET_WORDS 1551*38fd1498Szrj#undef _GLIBCXX_BITSET_BITS_PER_WORD 1552*38fd1498Szrj#undef _GLIBCXX_BITSET_BITS_PER_ULL 1553*38fd1498Szrj 1554*38fd1498Szrj#if __cplusplus >= 201103L 1555*38fd1498Szrj 1556*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default) 1557*38fd1498Szrj{ 1558*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION 1559*38fd1498Szrj 1560*38fd1498Szrj // DR 1182. 1561*38fd1498Szrj /// std::hash specialization for bitset. 1562*38fd1498Szrj template<size_t _Nb> 1563*38fd1498Szrj struct hash<_GLIBCXX_STD_C::bitset<_Nb>> 1564*38fd1498Szrj : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>> 1565*38fd1498Szrj { 1566*38fd1498Szrj size_t 1567*38fd1498Szrj operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept 1568*38fd1498Szrj { 1569*38fd1498Szrj const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__; 1570*38fd1498Szrj return std::_Hash_impl::hash(__b._M_getdata(), __clength); 1571*38fd1498Szrj } 1572*38fd1498Szrj }; 1573*38fd1498Szrj 1574*38fd1498Szrj template<> 1575*38fd1498Szrj struct hash<_GLIBCXX_STD_C::bitset<0>> 1576*38fd1498Szrj : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>> 1577*38fd1498Szrj { 1578*38fd1498Szrj size_t 1579*38fd1498Szrj operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept 1580*38fd1498Szrj { return 0; } 1581*38fd1498Szrj }; 1582*38fd1498Szrj 1583*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION 1584*38fd1498Szrj} // namespace 1585*38fd1498Szrj 1586*38fd1498Szrj#endif // C++11 1587*38fd1498Szrj 1588*38fd1498Szrj#ifdef _GLIBCXX_DEBUG 1589*38fd1498Szrj# include <debug/bitset> 1590*38fd1498Szrj#endif 1591*38fd1498Szrj 1592*38fd1498Szrj#ifdef _GLIBCXX_PROFILE 1593*38fd1498Szrj# include <profile/bitset> 1594*38fd1498Szrj#endif 1595*38fd1498Szrj 1596*38fd1498Szrj#endif /* _GLIBCXX_BITSET */ 1597