libstdc++
profile/bitset
Go to the documentation of this file.
00001 // Profiling bitset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file profile/bitset
00026  *  This file is a GNU profile extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_PROFILE_BITSET
00030 #define _GLIBCXX_PROFILE_BITSET
00031 
00032 #include <bitset>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default)
00035 {
00036 namespace __profile
00037 {
00038   /// Class std::bitset wrapper with performance instrumentation.
00039   template<size_t _Nb>
00040     class bitset
00041     : public _GLIBCXX_STD_C::bitset<_Nb>
00042     {
00043       typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
00044 
00045     public:
00046       // bit reference:
00047       class reference
00048       : private _Base::reference
00049       {
00050     typedef typename _Base::reference _Base_ref;
00051 
00052     friend class bitset;
00053     reference();
00054 
00055     reference(const _Base_ref& __base, bitset* __seq)
00056     : _Base_ref(__base)
00057     { }
00058 
00059       public:
00060     reference(const reference& __x)
00061     : _Base_ref(__x)
00062     { }
00063 
00064     reference&
00065     operator=(bool __x)
00066     {
00067       *static_cast<_Base_ref*>(this) = __x;
00068       return *this;
00069     }
00070 
00071     reference&
00072     operator=(const reference& __x)
00073     {
00074       *static_cast<_Base_ref*>(this) = __x;
00075       return *this;
00076     }
00077 
00078     bool
00079     operator~() const
00080     {
00081       return ~(*static_cast<const _Base_ref*>(this));
00082     }
00083 
00084     operator bool() const
00085     {
00086       return *static_cast<const _Base_ref*>(this);
00087     }
00088 
00089     reference&
00090     flip()
00091     {
00092       _Base_ref::flip();
00093       return *this;
00094     }
00095       };
00096 
00097       // 23.3.5.1 constructors:
00098       _GLIBCXX_CONSTEXPR bitset() : _Base() { }
00099 
00100 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00101       constexpr bitset(unsigned long long __val)
00102 #else
00103       bitset(unsigned long __val)
00104 #endif
00105       : _Base(__val) { }
00106 
00107       template<typename _CharT, typename _Traits, typename _Alloc>
00108         explicit
00109         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00110            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00111            __pos = 0,
00112            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00113            __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
00114     : _Base(__str, __pos, __n) { }
00115 
00116       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00117       // 396. what are characters zero and one.
00118       template<class _CharT, class _Traits, class _Alloc>
00119     bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00120            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00121            __pos,
00122            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00123            __n,
00124            _CharT __zero, _CharT __one = _CharT('1'))
00125     : _Base(__str, __pos, __n, __zero, __one) { }
00126 
00127       bitset(const _Base& __x) : _Base(__x) { }
00128 
00129 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00130       template<typename _CharT>
00131         explicit
00132         bitset(const _CharT* __str,
00133            typename std::basic_string<_CharT>::size_type __n
00134            = std::basic_string<_CharT>::npos,
00135            _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
00136     : _Base(__str, __n, __zero, __one) { }
00137 #endif
00138 
00139       // 23.3.5.2 bitset operations:
00140       bitset<_Nb>&
00141       operator&=(const bitset<_Nb>& __rhs)
00142       {
00143     _M_base() &= __rhs;
00144     return *this;
00145       }
00146 
00147       bitset<_Nb>&
00148       operator|=(const bitset<_Nb>& __rhs)
00149       {
00150     _M_base() |= __rhs;
00151     return *this;
00152       }
00153 
00154       bitset<_Nb>&
00155       operator^=(const bitset<_Nb>& __rhs)
00156       {
00157     _M_base() ^= __rhs;
00158     return *this;
00159       }
00160 
00161       bitset<_Nb>&
00162       operator<<=(size_t __pos)
00163       {
00164     _M_base() <<= __pos;
00165     return *this;
00166       }
00167 
00168       bitset<_Nb>&
00169       operator>>=(size_t __pos)
00170       {
00171     _M_base() >>= __pos;
00172     return *this;
00173       }
00174 
00175       bitset<_Nb>&
00176       set()
00177       {
00178     _Base::set();
00179     return *this;
00180       }
00181 
00182       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00183       // 186. bitset::set() second parameter should be bool
00184       bitset<_Nb>&
00185       set(size_t __pos, bool __val = true)
00186       {
00187     _Base::set(__pos, __val);
00188     return *this;
00189       }
00190 
00191       bitset<_Nb>&
00192       reset()
00193       {
00194     _Base::reset();
00195     return *this;
00196       }
00197 
00198       bitset<_Nb>&
00199       reset(size_t __pos)
00200       {
00201     _Base::reset(__pos);
00202     return *this;
00203       }
00204 
00205       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
00206 
00207       bitset<_Nb>&
00208       flip()
00209       {
00210     _Base::flip();
00211     return *this;
00212       }
00213 
00214       bitset<_Nb>&
00215       flip(size_t __pos)
00216       {
00217     _Base::flip(__pos);
00218     return *this;
00219       }
00220 
00221       // element access:
00222       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00223       // 11. Bitset minor problems
00224       reference
00225       operator[](size_t __pos)
00226       {
00227     return reference(_M_base()[__pos], this);
00228       }
00229 
00230       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00231       // 11. Bitset minor problems
00232       bool
00233       operator[](size_t __pos) const
00234       {
00235     return _M_base()[__pos];
00236       }
00237 
00238       using _Base::to_ulong;
00239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00240       using _Base::to_ullong;
00241 #endif
00242 
00243       template <typename _CharT, typename _Traits, typename _Alloc>
00244         std::basic_string<_CharT, _Traits, _Alloc>
00245         to_string() const
00246         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
00247 
00248       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00249       // 396. what are characters zero and one.
00250       template<class _CharT, class _Traits, class _Alloc>
00251     std::basic_string<_CharT, _Traits, _Alloc>
00252     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00253     {
00254       return _M_base().template
00255         to_string<_CharT, _Traits, _Alloc>(__zero, __one);
00256     }
00257 
00258       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00259       // 434. bitset::to_string() hard to use.
00260       template<typename _CharT, typename _Traits>
00261         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00262         to_string() const
00263         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
00264 
00265       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00266       // 853. to_string needs updating with zero and one.
00267       template<class _CharT, class _Traits>
00268     std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00269     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00270     { return to_string<_CharT, _Traits,
00271                        std::allocator<_CharT> >(__zero, __one); }
00272 
00273       template<typename _CharT>
00274         std::basic_string<_CharT, std::char_traits<_CharT>,
00275                           std::allocator<_CharT> >
00276         to_string() const
00277         {
00278           return to_string<_CharT, std::char_traits<_CharT>,
00279                            std::allocator<_CharT> >();
00280         }
00281 
00282       template<class _CharT>
00283     std::basic_string<_CharT, std::char_traits<_CharT>,
00284                       std::allocator<_CharT> >
00285     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00286     {
00287       return to_string<_CharT, std::char_traits<_CharT>,
00288                        std::allocator<_CharT> >(__zero, __one);
00289     }
00290 
00291       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00292       to_string() const
00293       {
00294     return to_string<char,std::char_traits<char>,std::allocator<char> >();
00295       }
00296 
00297       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00298       to_string(char __zero, char __one = '1') const
00299       {
00300     return to_string<char, std::char_traits<char>,
00301                      std::allocator<char> >(__zero, __one);
00302       }
00303 
00304       using _Base::count;
00305       using _Base::size;
00306 
00307       bool
00308       operator==(const bitset<_Nb>& __rhs) const
00309       { return _M_base() == __rhs; }
00310 
00311       bool
00312       operator!=(const bitset<_Nb>& __rhs) const
00313       { return _M_base() != __rhs; }
00314 
00315       using _Base::test;
00316       using _Base::all;
00317       using _Base::any;
00318       using _Base::none;
00319 
00320       bitset<_Nb>
00321       operator<<(size_t __pos) const
00322       { return bitset<_Nb>(_M_base() << __pos); }
00323 
00324       bitset<_Nb>
00325       operator>>(size_t __pos) const
00326       { return bitset<_Nb>(_M_base() >> __pos); }
00327 
00328       _Base&
00329       _M_base() { return *this; }
00330 
00331       const _Base&
00332       _M_base() const { return *this; }
00333     };
00334 
00335   template<size_t _Nb>
00336     bitset<_Nb>
00337     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
00338     { return bitset<_Nb>(__x) &= __y; }
00339 
00340   template<size_t _Nb>
00341     bitset<_Nb>
00342     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
00343     { return bitset<_Nb>(__x) |= __y; }
00344 
00345   template<size_t _Nb>
00346     bitset<_Nb>
00347     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
00348     { return bitset<_Nb>(__x) ^= __y; }
00349 
00350   template<typename _CharT, typename _Traits, size_t _Nb>
00351     std::basic_istream<_CharT, _Traits>&
00352     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
00353     { return __is >> __x._M_base(); }
00354 
00355   template<typename _CharT, typename _Traits, size_t _Nb>
00356     std::basic_ostream<_CharT, _Traits>&
00357     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00358            const bitset<_Nb>& __x)
00359     { return __os << __x._M_base(); }
00360 } // namespace __profile
00361 
00362 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00363   // DR 1182.
00364   /// std::hash specialization for bitset.
00365   template<size_t _Nb>
00366     struct hash<__profile::bitset<_Nb>>
00367     : public __hash_base<size_t, __profile::bitset<_Nb>>
00368     {
00369       size_t
00370       operator()(const __profile::bitset<_Nb>& __b) const
00371       { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
00372     };
00373 #endif
00374 
00375 } // namespace std
00376 
00377 #endif