libstdc++
bitset
Go to the documentation of this file.
00001 // Profiling bitset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2015 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, none at the
00039   /// moment.
00040   template<size_t _Nb>
00041     class bitset
00042     : public _GLIBCXX_STD_C::bitset<_Nb>
00043     {
00044       typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
00045 
00046     public:
00047       // 23.3.5.1 constructors:
00048 #if __cplusplus < 201103L
00049       bitset()
00050       : _Base() { }
00051 #else
00052       constexpr bitset() = default;
00053 #endif
00054 
00055 #if __cplusplus >= 201103L
00056       constexpr bitset(unsigned long long __val) noexcept
00057 #else
00058       bitset(unsigned long __val)
00059 #endif
00060       : _Base(__val) { }
00061 
00062       template<typename _CharT, typename _Traits, typename _Alloc>
00063         explicit
00064         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00065                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00066                __pos = 0,
00067                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00068                __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
00069         : _Base(__str, __pos, __n) { }
00070 
00071       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00072       // 396. what are characters zero and one.
00073       template<class _CharT, class _Traits, class _Alloc>
00074         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
00075                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00076                __pos,
00077                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
00078                __n,
00079                _CharT __zero, _CharT __one = _CharT('1'))
00080         : _Base(__str, __pos, __n, __zero, __one) { }
00081 
00082       bitset(const _Base& __x) : _Base(__x) { }
00083 
00084 #if __cplusplus >= 201103L
00085       template<typename _CharT>
00086         explicit
00087         bitset(const _CharT* __str,
00088                typename std::basic_string<_CharT>::size_type __n
00089                = std::basic_string<_CharT>::npos,
00090                _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
00091         : _Base(__str, __n, __zero, __one) { }
00092 #endif
00093 
00094       // 23.3.5.2 bitset operations:
00095       bitset<_Nb>&
00096       operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00097       {
00098         _M_base() &= __rhs;
00099         return *this;
00100       }
00101 
00102       bitset<_Nb>&
00103       operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00104       {
00105         _M_base() |= __rhs;
00106         return *this;
00107       }
00108 
00109       bitset<_Nb>&
00110       operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00111       {
00112         _M_base() ^= __rhs;
00113         return *this;
00114       }
00115 
00116       bitset<_Nb>&
00117       operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
00118       {
00119         _M_base() <<= __pos;
00120         return *this;
00121       }
00122 
00123       bitset<_Nb>&
00124       operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
00125       {
00126         _M_base() >>= __pos;
00127         return *this;
00128       }
00129 
00130       bitset<_Nb>&
00131       set() _GLIBCXX_NOEXCEPT
00132       {
00133         _Base::set();
00134         return *this;
00135       }
00136 
00137       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00138       // 186. bitset::set() second parameter should be bool
00139       bitset<_Nb>&
00140       set(size_t __pos, bool __val = true)
00141       {
00142         _Base::set(__pos, __val);
00143         return *this;
00144       }
00145 
00146       bitset<_Nb>&
00147       reset() _GLIBCXX_NOEXCEPT
00148       {
00149         _Base::reset();
00150         return *this;
00151       }
00152 
00153       bitset<_Nb>&
00154       reset(size_t __pos)
00155       {
00156         _Base::reset(__pos);
00157         return *this;
00158       }
00159 
00160       bitset<_Nb>
00161       operator~() const _GLIBCXX_NOEXCEPT
00162       { return bitset(~_M_base()); }
00163 
00164       bitset<_Nb>&
00165       flip() _GLIBCXX_NOEXCEPT
00166       {
00167         _Base::flip();
00168         return *this;
00169       }
00170 
00171       bitset<_Nb>&
00172       flip(size_t __pos)
00173       {
00174         _Base::flip(__pos);
00175         return *this;
00176       }
00177 
00178       bool
00179       operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
00180       { return _M_base() == __rhs; }
00181 
00182       bool
00183       operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
00184       { return _M_base() != __rhs; }
00185 
00186       bitset<_Nb>
00187       operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
00188       { return bitset<_Nb>(_M_base() << __pos); }
00189 
00190       bitset<_Nb>
00191       operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
00192       { return bitset<_Nb>(_M_base() >> __pos); }
00193 
00194       _Base&
00195       _M_base() _GLIBCXX_NOEXCEPT
00196       { return *this; }
00197 
00198       const _Base&
00199       _M_base() const _GLIBCXX_NOEXCEPT
00200       { return *this; }
00201     };
00202 
00203   template<size_t _Nb>
00204     bitset<_Nb>
00205     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
00206     { return bitset<_Nb>(__x) &= __y; }
00207 
00208   template<size_t _Nb>
00209     bitset<_Nb>
00210     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
00211     { return bitset<_Nb>(__x) |= __y; }
00212 
00213   template<size_t _Nb>
00214     bitset<_Nb>
00215     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
00216     { return bitset<_Nb>(__x) ^= __y; }
00217 
00218   template<typename _CharT, typename _Traits, size_t _Nb>
00219     std::basic_istream<_CharT, _Traits>&
00220     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
00221     { return __is >> __x._M_base(); }
00222 
00223   template<typename _CharT, typename _Traits, size_t _Nb>
00224     std::basic_ostream<_CharT, _Traits>&
00225     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00226                const bitset<_Nb>& __x)
00227     { return __os << __x._M_base(); }
00228 } // namespace __profile
00229 
00230 #if __cplusplus >= 201103L
00231   // DR 1182.
00232   /// std::hash specialization for bitset.
00233   template<size_t _Nb>
00234     struct hash<__profile::bitset<_Nb>>
00235     : public __hash_base<size_t, __profile::bitset<_Nb>>
00236     {
00237       size_t
00238       operator()(const __profile::bitset<_Nb>& __b) const noexcept
00239       { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
00240     };
00241 #endif
00242 
00243 } // namespace std
00244 
00245 #endif