libstdc++
profile/bitset
Go to the documentation of this file.
00001 // Profiling bitset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009, 2010, 2011 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) _GLIBCXX_NOEXCEPT
00056     : _Base_ref(__base)
00057     { }
00058 
00059       public:
00060     reference(const reference& __x) _GLIBCXX_NOEXCEPT
00061     : _Base_ref(__x)
00062     { }
00063 
00064     reference&
00065     operator=(bool __x) _GLIBCXX_NOEXCEPT
00066     {
00067       *static_cast<_Base_ref*>(this) = __x;
00068       return *this;
00069     }
00070 
00071     reference&
00072     operator=(const reference& __x) _GLIBCXX_NOEXCEPT
00073     {
00074       *static_cast<_Base_ref*>(this) = __x;
00075       return *this;
00076     }
00077 
00078     bool
00079     operator~() const _GLIBCXX_NOEXCEPT
00080     {
00081       return ~(*static_cast<const _Base_ref*>(this));
00082     }
00083 
00084     operator bool() const _GLIBCXX_NOEXCEPT
00085     {
00086       return *static_cast<const _Base_ref*>(this);
00087     }
00088 
00089     reference&
00090     flip() _GLIBCXX_NOEXCEPT
00091     {
00092       _Base_ref::flip();
00093       return *this;
00094     }
00095       };
00096 
00097       // 23.3.5.1 constructors:
00098       _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
00099       : _Base() { }
00100 
00101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00102       constexpr bitset(unsigned long long __val) noexcept
00103 #else
00104       bitset(unsigned long __val)
00105 #endif
00106       : _Base(__val) { }
00107 
00108       template<typename _CharT, typename _Traits, typename _Alloc>
00109         explicit
00110         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00111            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00112            __pos = 0,
00113            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00114            __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
00115     : _Base(__str, __pos, __n) { }
00116 
00117       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00118       // 396. what are characters zero and one.
00119       template<class _CharT, class _Traits, class _Alloc>
00120     bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00121            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00122            __pos,
00123            typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00124            __n,
00125            _CharT __zero, _CharT __one = _CharT('1'))
00126     : _Base(__str, __pos, __n, __zero, __one) { }
00127 
00128       bitset(const _Base& __x) : _Base(__x) { }
00129 
00130 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00131       template<typename _CharT>
00132         explicit
00133         bitset(const _CharT* __str,
00134            typename std::basic_string<_CharT>::size_type __n
00135            = std::basic_string<_CharT>::npos,
00136            _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
00137     : _Base(__str, __n, __zero, __one) { }
00138 #endif
00139 
00140       // 23.3.5.2 bitset operations:
00141       bitset<_Nb>&
00142       operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00143       {
00144     _M_base() &= __rhs;
00145     return *this;
00146       }
00147 
00148       bitset<_Nb>&
00149       operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00150       {
00151     _M_base() |= __rhs;
00152     return *this;
00153       }
00154 
00155       bitset<_Nb>&
00156       operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00157       {
00158     _M_base() ^= __rhs;
00159     return *this;
00160       }
00161 
00162       bitset<_Nb>&
00163       operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
00164       {
00165     _M_base() <<= __pos;
00166     return *this;
00167       }
00168 
00169       bitset<_Nb>&
00170       operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
00171       {
00172     _M_base() >>= __pos;
00173     return *this;
00174       }
00175 
00176       bitset<_Nb>&
00177       set() _GLIBCXX_NOEXCEPT
00178       {
00179     _Base::set();
00180     return *this;
00181       }
00182 
00183       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00184       // 186. bitset::set() second parameter should be bool
00185       bitset<_Nb>&
00186       set(size_t __pos, bool __val = true)
00187       {
00188     _Base::set(__pos, __val);
00189     return *this;
00190       }
00191 
00192       bitset<_Nb>&
00193       reset() _GLIBCXX_NOEXCEPT
00194       {
00195     _Base::reset();
00196     return *this;
00197       }
00198 
00199       bitset<_Nb>&
00200       reset(size_t __pos)
00201       {
00202     _Base::reset(__pos);
00203     return *this;
00204       }
00205 
00206       bitset<_Nb>
00207       operator~() const _GLIBCXX_NOEXCEPT
00208       { return bitset(~_M_base()); }
00209 
00210       bitset<_Nb>&
00211       flip() _GLIBCXX_NOEXCEPT
00212       {
00213     _Base::flip();
00214     return *this;
00215       }
00216 
00217       bitset<_Nb>&
00218       flip(size_t __pos)
00219       {
00220     _Base::flip(__pos);
00221     return *this;
00222       }
00223 
00224       // element access:
00225       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00226       // 11. Bitset minor problems
00227       reference
00228       operator[](size_t __pos)
00229       {
00230     return reference(_M_base()[__pos], this);
00231       }
00232 
00233       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00234       // 11. Bitset minor problems
00235       _GLIBCXX_CONSTEXPR bool
00236       operator[](size_t __pos) const
00237       {
00238     return _Base::operator[](__pos);
00239       }
00240 
00241       using _Base::to_ulong;
00242 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00243       using _Base::to_ullong;
00244 #endif
00245 
00246       template <typename _CharT, typename _Traits, typename _Alloc>
00247         std::basic_string<_CharT, _Traits, _Alloc>
00248         to_string() const
00249         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
00250 
00251       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00252       // 396. what are characters zero and one.
00253       template<class _CharT, class _Traits, class _Alloc>
00254     std::basic_string<_CharT, _Traits, _Alloc>
00255     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00256     {
00257       return _M_base().template
00258         to_string<_CharT, _Traits, _Alloc>(__zero, __one);
00259     }
00260 
00261       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00262       // 434. bitset::to_string() hard to use.
00263       template<typename _CharT, typename _Traits>
00264         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00265         to_string() const
00266         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
00267 
00268       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00269       // 853. to_string needs updating with zero and one.
00270       template<class _CharT, class _Traits>
00271     std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00272     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00273     { return to_string<_CharT, _Traits,
00274                        std::allocator<_CharT> >(__zero, __one); }
00275 
00276       template<typename _CharT>
00277         std::basic_string<_CharT, std::char_traits<_CharT>,
00278                           std::allocator<_CharT> >
00279         to_string() const
00280         {
00281           return to_string<_CharT, std::char_traits<_CharT>,
00282                            std::allocator<_CharT> >();
00283         }
00284 
00285       template<class _CharT>
00286     std::basic_string<_CharT, std::char_traits<_CharT>,
00287                       std::allocator<_CharT> >
00288     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00289     {
00290       return to_string<_CharT, std::char_traits<_CharT>,
00291                        std::allocator<_CharT> >(__zero, __one);
00292     }
00293 
00294       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00295       to_string() const
00296       {
00297     return to_string<char,std::char_traits<char>,std::allocator<char> >();
00298       }
00299 
00300       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00301       to_string(char __zero, char __one = '1') const
00302       {
00303     return to_string<char, std::char_traits<char>,
00304                      std::allocator<char> >(__zero, __one);
00305       }
00306 
00307       using _Base::count;
00308       using _Base::size;
00309 
00310       bool
00311       operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
00312       { return _M_base() == __rhs; }
00313 
00314       bool
00315       operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
00316       { return _M_base() != __rhs; }
00317 
00318       using _Base::test;
00319       using _Base::all;
00320       using _Base::any;
00321       using _Base::none;
00322 
00323       bitset<_Nb>
00324       operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
00325       { return bitset<_Nb>(_M_base() << __pos); }
00326 
00327       bitset<_Nb>
00328       operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
00329       { return bitset<_Nb>(_M_base() >> __pos); }
00330 
00331       _Base&
00332       _M_base() _GLIBCXX_NOEXCEPT
00333       { return *this; }
00334 
00335       const _Base&
00336       _M_base() const _GLIBCXX_NOEXCEPT
00337       { return *this; }
00338     };
00339 
00340   template<size_t _Nb>
00341     bitset<_Nb>
00342     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT
00348     { return bitset<_Nb>(__x) |= __y; }
00349 
00350   template<size_t _Nb>
00351     bitset<_Nb>
00352     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
00353     { return bitset<_Nb>(__x) ^= __y; }
00354 
00355   template<typename _CharT, typename _Traits, size_t _Nb>
00356     std::basic_istream<_CharT, _Traits>&
00357     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
00358     { return __is >> __x._M_base(); }
00359 
00360   template<typename _CharT, typename _Traits, size_t _Nb>
00361     std::basic_ostream<_CharT, _Traits>&
00362     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00363            const bitset<_Nb>& __x)
00364     { return __os << __x._M_base(); }
00365 } // namespace __profile
00366 
00367 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00368   // DR 1182.
00369   /// std::hash specialization for bitset.
00370   template<size_t _Nb>
00371     struct hash<__profile::bitset<_Nb>>
00372     : public __hash_base<size_t, __profile::bitset<_Nb>>
00373     {
00374       size_t
00375       operator()(const __profile::bitset<_Nb>& __b) const noexcept
00376       { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
00377     };
00378 #endif
00379 
00380 } // namespace std
00381 
00382 #endif