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
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_D::bitset<_Nb>
00042     {
00043       typedef _GLIBCXX_STD_D::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       bitset() : _Base() { }
00099 
00100 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00101       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       explicit
00131       bitset(const char* __str) : _Base(__str) { }
00132 #endif
00133 
00134       // 23.3.5.2 bitset operations:
00135       bitset<_Nb>&
00136       operator&=(const bitset<_Nb>& __rhs)
00137       {
00138     _M_base() &= __rhs;
00139     return *this;
00140       }
00141 
00142       bitset<_Nb>&
00143       operator|=(const bitset<_Nb>& __rhs)
00144       {
00145     _M_base() |= __rhs;
00146     return *this;
00147       }
00148 
00149       bitset<_Nb>&
00150       operator^=(const bitset<_Nb>& __rhs)
00151       {
00152     _M_base() ^= __rhs;
00153     return *this;
00154       }
00155 
00156       bitset<_Nb>&
00157       operator<<=(size_t __pos)
00158       {
00159     _M_base() <<= __pos;
00160     return *this;
00161       }
00162 
00163       bitset<_Nb>&
00164       operator>>=(size_t __pos)
00165       {
00166     _M_base() >>= __pos;
00167     return *this;
00168       }
00169 
00170       bitset<_Nb>&
00171       set()
00172       {
00173     _Base::set();
00174     return *this;
00175       }
00176 
00177       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00178       // 186. bitset::set() second parameter should be bool
00179       bitset<_Nb>&
00180       set(size_t __pos, bool __val = true)
00181       {
00182     _Base::set(__pos, __val);
00183     return *this;
00184       }
00185 
00186       bitset<_Nb>&
00187       reset()
00188       {
00189     _Base::reset();
00190     return *this;
00191       }
00192 
00193       bitset<_Nb>&
00194       reset(size_t __pos)
00195       {
00196     _Base::reset(__pos);
00197     return *this;
00198       }
00199 
00200       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
00201 
00202       bitset<_Nb>&
00203       flip()
00204       {
00205     _Base::flip();
00206     return *this;
00207       }
00208 
00209       bitset<_Nb>&
00210       flip(size_t __pos)
00211       {
00212     _Base::flip(__pos);
00213     return *this;
00214       }
00215 
00216       // element access:
00217       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00218       // 11. Bitset minor problems
00219       reference
00220       operator[](size_t __pos)
00221       {
00222     return reference(_M_base()[__pos], this);
00223       }
00224 
00225       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00226       // 11. Bitset minor problems
00227       bool
00228       operator[](size_t __pos) const
00229       {
00230     return _M_base()[__pos];
00231       }
00232 
00233       using _Base::to_ulong;
00234 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00235       using _Base::to_ullong;
00236 #endif
00237 
00238       template <typename _CharT, typename _Traits, typename _Alloc>
00239         std::basic_string<_CharT, _Traits, _Alloc>
00240         to_string() const
00241         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
00242 
00243       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00244       // 396. what are characters zero and one.
00245       template<class _CharT, class _Traits, class _Alloc>
00246     std::basic_string<_CharT, _Traits, _Alloc>
00247     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00248     {
00249       return _M_base().template
00250         to_string<_CharT, _Traits, _Alloc>(__zero, __one);
00251     }
00252 
00253       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00254       // 434. bitset::to_string() hard to use.
00255       template<typename _CharT, typename _Traits>
00256         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00257         to_string() const
00258         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
00259 
00260       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00261       // 853. to_string needs updating with zero and one.
00262       template<class _CharT, class _Traits>
00263     std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
00264     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00265     { return to_string<_CharT, _Traits,
00266                        std::allocator<_CharT> >(__zero, __one); }
00267 
00268       template<typename _CharT>
00269         std::basic_string<_CharT, std::char_traits<_CharT>,
00270                           std::allocator<_CharT> >
00271         to_string() const
00272         {
00273           return to_string<_CharT, std::char_traits<_CharT>,
00274                            std::allocator<_CharT> >();
00275         }
00276 
00277       template<class _CharT>
00278     std::basic_string<_CharT, std::char_traits<_CharT>,
00279                       std::allocator<_CharT> >
00280     to_string(_CharT __zero, _CharT __one = _CharT('1')) const
00281     {
00282       return to_string<_CharT, std::char_traits<_CharT>,
00283                        std::allocator<_CharT> >(__zero, __one);
00284     }
00285 
00286       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00287       to_string() const
00288       {
00289     return to_string<char,std::char_traits<char>,std::allocator<char> >();
00290       }
00291 
00292       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
00293       to_string(char __zero, char __one = '1') const
00294       {
00295     return to_string<char, std::char_traits<char>,
00296                      std::allocator<char> >(__zero, __one);
00297       }
00298 
00299       using _Base::count;
00300       using _Base::size;
00301 
00302       bool
00303       operator==(const bitset<_Nb>& __rhs) const
00304       { return _M_base() == __rhs; }
00305 
00306       bool
00307       operator!=(const bitset<_Nb>& __rhs) const
00308       { return _M_base() != __rhs; }
00309 
00310       using _Base::test;
00311       using _Base::all;
00312       using _Base::any;
00313       using _Base::none;
00314 
00315       bitset<_Nb>
00316       operator<<(size_t __pos) const
00317       { return bitset<_Nb>(_M_base() << __pos); }
00318 
00319       bitset<_Nb>
00320       operator>>(size_t __pos) const
00321       { return bitset<_Nb>(_M_base() >> __pos); }
00322 
00323       _Base&
00324       _M_base() { return *this; }
00325 
00326       const _Base&
00327       _M_base() const { return *this; }
00328     };
00329 
00330   template<size_t _Nb>
00331     bitset<_Nb>
00332     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
00333     { return bitset<_Nb>(__x) &= __y; }
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<typename _CharT, typename _Traits, size_t _Nb>
00346     std::basic_istream<_CharT, _Traits>&
00347     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
00348     { return __is >> __x._M_base(); }
00349 
00350   template<typename _CharT, typename _Traits, size_t _Nb>
00351     std::basic_ostream<_CharT, _Traits>&
00352     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00353            const bitset<_Nb>& __x)
00354     { return __os << __x._M_base(); }
00355 } // namespace __profile
00356 
00357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00358   // DR 1182.
00359   /// std::hash specialization for bitset.
00360   template<size_t _Nb>
00361     struct hash<__profile::bitset<_Nb>>
00362     : public std::unary_function<__profile::bitset<_Nb>, size_t>
00363     {
00364       size_t
00365       operator()(const __profile::bitset<_Nb>& __b) const
00366       { return std::hash<_GLIBCXX_STD_D::bitset<_Nb>>()(__b._M_base()); }
00367     };
00368 #endif
00369 
00370 } // namespace std
00371 
00372 #endif