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