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