1*e4b17023SJohn Marino// Profiling bitset implementation -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino// Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. 4*e4b17023SJohn Marino// 5*e4b17023SJohn Marino// This file is part of the GNU ISO C++ Library. This library is free 6*e4b17023SJohn Marino// software; you can redistribute it and/or modify it under the 7*e4b17023SJohn Marino// terms of the GNU General Public License as published by the 8*e4b17023SJohn Marino// Free Software Foundation; either version 3, or (at your option) 9*e4b17023SJohn Marino// any later version. 10*e4b17023SJohn Marino 11*e4b17023SJohn Marino// This library is distributed in the hope that it will be useful, 12*e4b17023SJohn Marino// but WITHOUT ANY WARRANTY; without even the implied warranty of 13*e4b17023SJohn Marino// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*e4b17023SJohn Marino// GNU General Public License for more details. 15*e4b17023SJohn Marino 16*e4b17023SJohn Marino// Under Section 7 of GPL version 3, you are granted additional 17*e4b17023SJohn Marino// permissions described in the GCC Runtime Library Exception, version 18*e4b17023SJohn Marino// 3.1, as published by the Free Software Foundation. 19*e4b17023SJohn Marino 20*e4b17023SJohn Marino// You should have received a copy of the GNU General Public License and 21*e4b17023SJohn Marino// a copy of the GCC Runtime Library Exception along with this program; 22*e4b17023SJohn Marino// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*e4b17023SJohn Marino// <http://www.gnu.org/licenses/>. 24*e4b17023SJohn Marino 25*e4b17023SJohn Marino/** @file profile/bitset 26*e4b17023SJohn Marino * This file is a GNU profile extension to the Standard C++ Library. 27*e4b17023SJohn Marino */ 28*e4b17023SJohn Marino 29*e4b17023SJohn Marino#ifndef _GLIBCXX_PROFILE_BITSET 30*e4b17023SJohn Marino#define _GLIBCXX_PROFILE_BITSET 31*e4b17023SJohn Marino 32*e4b17023SJohn Marino#include <bitset> 33*e4b17023SJohn Marino 34*e4b17023SJohn Marinonamespace std _GLIBCXX_VISIBILITY(default) 35*e4b17023SJohn Marino{ 36*e4b17023SJohn Marinonamespace __profile 37*e4b17023SJohn Marino{ 38*e4b17023SJohn Marino /// Class std::bitset wrapper with performance instrumentation. 39*e4b17023SJohn Marino template<size_t _Nb> 40*e4b17023SJohn Marino class bitset 41*e4b17023SJohn Marino : public _GLIBCXX_STD_C::bitset<_Nb> 42*e4b17023SJohn Marino { 43*e4b17023SJohn Marino typedef _GLIBCXX_STD_C::bitset<_Nb> _Base; 44*e4b17023SJohn Marino 45*e4b17023SJohn Marino public: 46*e4b17023SJohn Marino // bit reference: 47*e4b17023SJohn Marino class reference 48*e4b17023SJohn Marino : private _Base::reference 49*e4b17023SJohn Marino { 50*e4b17023SJohn Marino typedef typename _Base::reference _Base_ref; 51*e4b17023SJohn Marino 52*e4b17023SJohn Marino friend class bitset; 53*e4b17023SJohn Marino reference(); 54*e4b17023SJohn Marino 55*e4b17023SJohn Marino reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT 56*e4b17023SJohn Marino : _Base_ref(__base) 57*e4b17023SJohn Marino { } 58*e4b17023SJohn Marino 59*e4b17023SJohn Marino public: 60*e4b17023SJohn Marino reference(const reference& __x) _GLIBCXX_NOEXCEPT 61*e4b17023SJohn Marino : _Base_ref(__x) 62*e4b17023SJohn Marino { } 63*e4b17023SJohn Marino 64*e4b17023SJohn Marino reference& 65*e4b17023SJohn Marino operator=(bool __x) _GLIBCXX_NOEXCEPT 66*e4b17023SJohn Marino { 67*e4b17023SJohn Marino *static_cast<_Base_ref*>(this) = __x; 68*e4b17023SJohn Marino return *this; 69*e4b17023SJohn Marino } 70*e4b17023SJohn Marino 71*e4b17023SJohn Marino reference& 72*e4b17023SJohn Marino operator=(const reference& __x) _GLIBCXX_NOEXCEPT 73*e4b17023SJohn Marino { 74*e4b17023SJohn Marino *static_cast<_Base_ref*>(this) = __x; 75*e4b17023SJohn Marino return *this; 76*e4b17023SJohn Marino } 77*e4b17023SJohn Marino 78*e4b17023SJohn Marino bool 79*e4b17023SJohn Marino operator~() const _GLIBCXX_NOEXCEPT 80*e4b17023SJohn Marino { 81*e4b17023SJohn Marino return ~(*static_cast<const _Base_ref*>(this)); 82*e4b17023SJohn Marino } 83*e4b17023SJohn Marino 84*e4b17023SJohn Marino operator bool() const _GLIBCXX_NOEXCEPT 85*e4b17023SJohn Marino { 86*e4b17023SJohn Marino return *static_cast<const _Base_ref*>(this); 87*e4b17023SJohn Marino } 88*e4b17023SJohn Marino 89*e4b17023SJohn Marino reference& 90*e4b17023SJohn Marino flip() _GLIBCXX_NOEXCEPT 91*e4b17023SJohn Marino { 92*e4b17023SJohn Marino _Base_ref::flip(); 93*e4b17023SJohn Marino return *this; 94*e4b17023SJohn Marino } 95*e4b17023SJohn Marino }; 96*e4b17023SJohn Marino 97*e4b17023SJohn Marino // 23.3.5.1 constructors: 98*e4b17023SJohn Marino _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT 99*e4b17023SJohn Marino : _Base() { } 100*e4b17023SJohn Marino 101*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 102*e4b17023SJohn Marino constexpr bitset(unsigned long long __val) noexcept 103*e4b17023SJohn Marino#else 104*e4b17023SJohn Marino bitset(unsigned long __val) 105*e4b17023SJohn Marino#endif 106*e4b17023SJohn Marino : _Base(__val) { } 107*e4b17023SJohn Marino 108*e4b17023SJohn Marino template<typename _CharT, typename _Traits, typename _Alloc> 109*e4b17023SJohn Marino explicit 110*e4b17023SJohn Marino bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str, 111*e4b17023SJohn Marino typename std::basic_string<_CharT, _Traits, _Alloc>::size_type 112*e4b17023SJohn Marino __pos = 0, 113*e4b17023SJohn Marino typename std::basic_string<_CharT, _Traits, _Alloc>::size_type 114*e4b17023SJohn Marino __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos)) 115*e4b17023SJohn Marino : _Base(__str, __pos, __n) { } 116*e4b17023SJohn Marino 117*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 118*e4b17023SJohn Marino // 396. what are characters zero and one. 119*e4b17023SJohn Marino template<class _CharT, class _Traits, class _Alloc> 120*e4b17023SJohn Marino bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str, 121*e4b17023SJohn Marino typename std::basic_string<_CharT, _Traits, _Alloc>::size_type 122*e4b17023SJohn Marino __pos, 123*e4b17023SJohn Marino typename std::basic_string<_CharT, _Traits, _Alloc>::size_type 124*e4b17023SJohn Marino __n, 125*e4b17023SJohn Marino _CharT __zero, _CharT __one = _CharT('1')) 126*e4b17023SJohn Marino : _Base(__str, __pos, __n, __zero, __one) { } 127*e4b17023SJohn Marino 128*e4b17023SJohn Marino bitset(const _Base& __x) : _Base(__x) { } 129*e4b17023SJohn Marino 130*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 131*e4b17023SJohn Marino template<typename _CharT> 132*e4b17023SJohn Marino explicit 133*e4b17023SJohn Marino bitset(const _CharT* __str, 134*e4b17023SJohn Marino typename std::basic_string<_CharT>::size_type __n 135*e4b17023SJohn Marino = std::basic_string<_CharT>::npos, 136*e4b17023SJohn Marino _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) 137*e4b17023SJohn Marino : _Base(__str, __n, __zero, __one) { } 138*e4b17023SJohn Marino#endif 139*e4b17023SJohn Marino 140*e4b17023SJohn Marino // 23.3.5.2 bitset operations: 141*e4b17023SJohn Marino bitset<_Nb>& 142*e4b17023SJohn Marino operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 143*e4b17023SJohn Marino { 144*e4b17023SJohn Marino _M_base() &= __rhs; 145*e4b17023SJohn Marino return *this; 146*e4b17023SJohn Marino } 147*e4b17023SJohn Marino 148*e4b17023SJohn Marino bitset<_Nb>& 149*e4b17023SJohn Marino operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 150*e4b17023SJohn Marino { 151*e4b17023SJohn Marino _M_base() |= __rhs; 152*e4b17023SJohn Marino return *this; 153*e4b17023SJohn Marino } 154*e4b17023SJohn Marino 155*e4b17023SJohn Marino bitset<_Nb>& 156*e4b17023SJohn Marino operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 157*e4b17023SJohn Marino { 158*e4b17023SJohn Marino _M_base() ^= __rhs; 159*e4b17023SJohn Marino return *this; 160*e4b17023SJohn Marino } 161*e4b17023SJohn Marino 162*e4b17023SJohn Marino bitset<_Nb>& 163*e4b17023SJohn Marino operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT 164*e4b17023SJohn Marino { 165*e4b17023SJohn Marino _M_base() <<= __pos; 166*e4b17023SJohn Marino return *this; 167*e4b17023SJohn Marino } 168*e4b17023SJohn Marino 169*e4b17023SJohn Marino bitset<_Nb>& 170*e4b17023SJohn Marino operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT 171*e4b17023SJohn Marino { 172*e4b17023SJohn Marino _M_base() >>= __pos; 173*e4b17023SJohn Marino return *this; 174*e4b17023SJohn Marino } 175*e4b17023SJohn Marino 176*e4b17023SJohn Marino bitset<_Nb>& 177*e4b17023SJohn Marino set() _GLIBCXX_NOEXCEPT 178*e4b17023SJohn Marino { 179*e4b17023SJohn Marino _Base::set(); 180*e4b17023SJohn Marino return *this; 181*e4b17023SJohn Marino } 182*e4b17023SJohn Marino 183*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 184*e4b17023SJohn Marino // 186. bitset::set() second parameter should be bool 185*e4b17023SJohn Marino bitset<_Nb>& 186*e4b17023SJohn Marino set(size_t __pos, bool __val = true) 187*e4b17023SJohn Marino { 188*e4b17023SJohn Marino _Base::set(__pos, __val); 189*e4b17023SJohn Marino return *this; 190*e4b17023SJohn Marino } 191*e4b17023SJohn Marino 192*e4b17023SJohn Marino bitset<_Nb>& 193*e4b17023SJohn Marino reset() _GLIBCXX_NOEXCEPT 194*e4b17023SJohn Marino { 195*e4b17023SJohn Marino _Base::reset(); 196*e4b17023SJohn Marino return *this; 197*e4b17023SJohn Marino } 198*e4b17023SJohn Marino 199*e4b17023SJohn Marino bitset<_Nb>& 200*e4b17023SJohn Marino reset(size_t __pos) 201*e4b17023SJohn Marino { 202*e4b17023SJohn Marino _Base::reset(__pos); 203*e4b17023SJohn Marino return *this; 204*e4b17023SJohn Marino } 205*e4b17023SJohn Marino 206*e4b17023SJohn Marino bitset<_Nb> 207*e4b17023SJohn Marino operator~() const _GLIBCXX_NOEXCEPT 208*e4b17023SJohn Marino { return bitset(~_M_base()); } 209*e4b17023SJohn Marino 210*e4b17023SJohn Marino bitset<_Nb>& 211*e4b17023SJohn Marino flip() _GLIBCXX_NOEXCEPT 212*e4b17023SJohn Marino { 213*e4b17023SJohn Marino _Base::flip(); 214*e4b17023SJohn Marino return *this; 215*e4b17023SJohn Marino } 216*e4b17023SJohn Marino 217*e4b17023SJohn Marino bitset<_Nb>& 218*e4b17023SJohn Marino flip(size_t __pos) 219*e4b17023SJohn Marino { 220*e4b17023SJohn Marino _Base::flip(__pos); 221*e4b17023SJohn Marino return *this; 222*e4b17023SJohn Marino } 223*e4b17023SJohn Marino 224*e4b17023SJohn Marino // element access: 225*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 226*e4b17023SJohn Marino // 11. Bitset minor problems 227*e4b17023SJohn Marino reference 228*e4b17023SJohn Marino operator[](size_t __pos) 229*e4b17023SJohn Marino { 230*e4b17023SJohn Marino return reference(_M_base()[__pos], this); 231*e4b17023SJohn Marino } 232*e4b17023SJohn Marino 233*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 234*e4b17023SJohn Marino // 11. Bitset minor problems 235*e4b17023SJohn Marino _GLIBCXX_CONSTEXPR bool 236*e4b17023SJohn Marino operator[](size_t __pos) const 237*e4b17023SJohn Marino { 238*e4b17023SJohn Marino return _Base::operator[](__pos); 239*e4b17023SJohn Marino } 240*e4b17023SJohn Marino 241*e4b17023SJohn Marino using _Base::to_ulong; 242*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 243*e4b17023SJohn Marino using _Base::to_ullong; 244*e4b17023SJohn Marino#endif 245*e4b17023SJohn Marino 246*e4b17023SJohn Marino template <typename _CharT, typename _Traits, typename _Alloc> 247*e4b17023SJohn Marino std::basic_string<_CharT, _Traits, _Alloc> 248*e4b17023SJohn Marino to_string() const 249*e4b17023SJohn Marino { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); } 250*e4b17023SJohn Marino 251*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 252*e4b17023SJohn Marino // 396. what are characters zero and one. 253*e4b17023SJohn Marino template<class _CharT, class _Traits, class _Alloc> 254*e4b17023SJohn Marino std::basic_string<_CharT, _Traits, _Alloc> 255*e4b17023SJohn Marino to_string(_CharT __zero, _CharT __one = _CharT('1')) const 256*e4b17023SJohn Marino { 257*e4b17023SJohn Marino return _M_base().template 258*e4b17023SJohn Marino to_string<_CharT, _Traits, _Alloc>(__zero, __one); 259*e4b17023SJohn Marino } 260*e4b17023SJohn Marino 261*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 262*e4b17023SJohn Marino // 434. bitset::to_string() hard to use. 263*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 264*e4b17023SJohn Marino std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 265*e4b17023SJohn Marino to_string() const 266*e4b17023SJohn Marino { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } 267*e4b17023SJohn Marino 268*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 269*e4b17023SJohn Marino // 853. to_string needs updating with zero and one. 270*e4b17023SJohn Marino template<class _CharT, class _Traits> 271*e4b17023SJohn Marino std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 272*e4b17023SJohn Marino to_string(_CharT __zero, _CharT __one = _CharT('1')) const 273*e4b17023SJohn Marino { return to_string<_CharT, _Traits, 274*e4b17023SJohn Marino std::allocator<_CharT> >(__zero, __one); } 275*e4b17023SJohn Marino 276*e4b17023SJohn Marino template<typename _CharT> 277*e4b17023SJohn Marino std::basic_string<_CharT, std::char_traits<_CharT>, 278*e4b17023SJohn Marino std::allocator<_CharT> > 279*e4b17023SJohn Marino to_string() const 280*e4b17023SJohn Marino { 281*e4b17023SJohn Marino return to_string<_CharT, std::char_traits<_CharT>, 282*e4b17023SJohn Marino std::allocator<_CharT> >(); 283*e4b17023SJohn Marino } 284*e4b17023SJohn Marino 285*e4b17023SJohn Marino template<class _CharT> 286*e4b17023SJohn Marino std::basic_string<_CharT, std::char_traits<_CharT>, 287*e4b17023SJohn Marino std::allocator<_CharT> > 288*e4b17023SJohn Marino to_string(_CharT __zero, _CharT __one = _CharT('1')) const 289*e4b17023SJohn Marino { 290*e4b17023SJohn Marino return to_string<_CharT, std::char_traits<_CharT>, 291*e4b17023SJohn Marino std::allocator<_CharT> >(__zero, __one); 292*e4b17023SJohn Marino } 293*e4b17023SJohn Marino 294*e4b17023SJohn Marino std::basic_string<char, std::char_traits<char>, std::allocator<char> > 295*e4b17023SJohn Marino to_string() const 296*e4b17023SJohn Marino { 297*e4b17023SJohn Marino return to_string<char,std::char_traits<char>,std::allocator<char> >(); 298*e4b17023SJohn Marino } 299*e4b17023SJohn Marino 300*e4b17023SJohn Marino std::basic_string<char, std::char_traits<char>, std::allocator<char> > 301*e4b17023SJohn Marino to_string(char __zero, char __one = '1') const 302*e4b17023SJohn Marino { 303*e4b17023SJohn Marino return to_string<char, std::char_traits<char>, 304*e4b17023SJohn Marino std::allocator<char> >(__zero, __one); 305*e4b17023SJohn Marino } 306*e4b17023SJohn Marino 307*e4b17023SJohn Marino using _Base::count; 308*e4b17023SJohn Marino using _Base::size; 309*e4b17023SJohn Marino 310*e4b17023SJohn Marino bool 311*e4b17023SJohn Marino operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 312*e4b17023SJohn Marino { return _M_base() == __rhs; } 313*e4b17023SJohn Marino 314*e4b17023SJohn Marino bool 315*e4b17023SJohn Marino operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 316*e4b17023SJohn Marino { return _M_base() != __rhs; } 317*e4b17023SJohn Marino 318*e4b17023SJohn Marino using _Base::test; 319*e4b17023SJohn Marino using _Base::all; 320*e4b17023SJohn Marino using _Base::any; 321*e4b17023SJohn Marino using _Base::none; 322*e4b17023SJohn Marino 323*e4b17023SJohn Marino bitset<_Nb> 324*e4b17023SJohn Marino operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT 325*e4b17023SJohn Marino { return bitset<_Nb>(_M_base() << __pos); } 326*e4b17023SJohn Marino 327*e4b17023SJohn Marino bitset<_Nb> 328*e4b17023SJohn Marino operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT 329*e4b17023SJohn Marino { return bitset<_Nb>(_M_base() >> __pos); } 330*e4b17023SJohn Marino 331*e4b17023SJohn Marino _Base& 332*e4b17023SJohn Marino _M_base() _GLIBCXX_NOEXCEPT 333*e4b17023SJohn Marino { return *this; } 334*e4b17023SJohn Marino 335*e4b17023SJohn Marino const _Base& 336*e4b17023SJohn Marino _M_base() const _GLIBCXX_NOEXCEPT 337*e4b17023SJohn Marino { return *this; } 338*e4b17023SJohn Marino }; 339*e4b17023SJohn Marino 340*e4b17023SJohn Marino template<size_t _Nb> 341*e4b17023SJohn Marino bitset<_Nb> 342*e4b17023SJohn Marino operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 343*e4b17023SJohn Marino { return bitset<_Nb>(__x) &= __y; } 344*e4b17023SJohn Marino 345*e4b17023SJohn Marino template<size_t _Nb> 346*e4b17023SJohn Marino bitset<_Nb> 347*e4b17023SJohn Marino operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 348*e4b17023SJohn Marino { return bitset<_Nb>(__x) |= __y; } 349*e4b17023SJohn Marino 350*e4b17023SJohn Marino template<size_t _Nb> 351*e4b17023SJohn Marino bitset<_Nb> 352*e4b17023SJohn Marino operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 353*e4b17023SJohn Marino { return bitset<_Nb>(__x) ^= __y; } 354*e4b17023SJohn Marino 355*e4b17023SJohn Marino template<typename _CharT, typename _Traits, size_t _Nb> 356*e4b17023SJohn Marino std::basic_istream<_CharT, _Traits>& 357*e4b17023SJohn Marino operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) 358*e4b17023SJohn Marino { return __is >> __x._M_base(); } 359*e4b17023SJohn Marino 360*e4b17023SJohn Marino template<typename _CharT, typename _Traits, size_t _Nb> 361*e4b17023SJohn Marino std::basic_ostream<_CharT, _Traits>& 362*e4b17023SJohn Marino operator<<(std::basic_ostream<_CharT, _Traits>& __os, 363*e4b17023SJohn Marino const bitset<_Nb>& __x) 364*e4b17023SJohn Marino { return __os << __x._M_base(); } 365*e4b17023SJohn Marino} // namespace __profile 366*e4b17023SJohn Marino 367*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 368*e4b17023SJohn Marino // DR 1182. 369*e4b17023SJohn Marino /// std::hash specialization for bitset. 370*e4b17023SJohn Marino template<size_t _Nb> 371*e4b17023SJohn Marino struct hash<__profile::bitset<_Nb>> 372*e4b17023SJohn Marino : public __hash_base<size_t, __profile::bitset<_Nb>> 373*e4b17023SJohn Marino { 374*e4b17023SJohn Marino size_t 375*e4b17023SJohn Marino operator()(const __profile::bitset<_Nb>& __b) const noexcept 376*e4b17023SJohn Marino { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); } 377*e4b17023SJohn Marino }; 378*e4b17023SJohn Marino#endif 379*e4b17023SJohn Marino 380*e4b17023SJohn Marino} // namespace std 381*e4b17023SJohn Marino 382*e4b17023SJohn Marino#endif 383