libstdc++
profile/bitset
Go to the documentation of this file.
1 // Profiling bitset implementation -*- C++ -*-
2 
3 // Copyright (C) 2009-2015 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 
34 namespace std _GLIBCXX_VISIBILITY(default)
35 {
36 namespace __profile
37 {
38  /// Class std::bitset wrapper with performance instrumentation, none at the
39  /// moment.
40  template<size_t _Nb>
41  class bitset
42  : public _GLIBCXX_STD_C::bitset<_Nb>
43  {
44  typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
45 
46  public:
47  // 23.3.5.1 constructors:
48 #if __cplusplus < 201103L
49  bitset()
50  : _Base() { }
51 #else
52  constexpr bitset() = default;
53 #endif
54 
55 #if __cplusplus >= 201103L
56  constexpr bitset(unsigned long long __val) noexcept
57 #else
58  bitset(unsigned long __val)
59 #endif
60  : _Base(__val) { }
61 
62  template<typename _CharT, typename _Traits, typename _Alloc>
63  explicit
64  bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
65  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
66  __pos = 0,
67  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
68  __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
69  : _Base(__str, __pos, __n) { }
70 
71  // _GLIBCXX_RESOLVE_LIB_DEFECTS
72  // 396. what are characters zero and one.
73  template<class _CharT, class _Traits, class _Alloc>
74  bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
75  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
76  __pos,
77  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
78  __n,
79  _CharT __zero, _CharT __one = _CharT('1'))
80  : _Base(__str, __pos, __n, __zero, __one) { }
81 
82  bitset(const _Base& __x) : _Base(__x) { }
83 
84 #if __cplusplus >= 201103L
85  template<typename _CharT>
86  explicit
87  bitset(const _CharT* __str,
88  typename std::basic_string<_CharT>::size_type __n
89  = std::basic_string<_CharT>::npos,
90  _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
91  : _Base(__str, __n, __zero, __one) { }
92 #endif
93 
94  // 23.3.5.2 bitset operations:
95  bitset<_Nb>&
96  operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
97  {
98  _M_base() &= __rhs;
99  return *this;
100  }
101 
102  bitset<_Nb>&
103  operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
104  {
105  _M_base() |= __rhs;
106  return *this;
107  }
108 
109  bitset<_Nb>&
110  operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
111  {
112  _M_base() ^= __rhs;
113  return *this;
114  }
115 
116  bitset<_Nb>&
117  operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
118  {
119  _M_base() <<= __pos;
120  return *this;
121  }
122 
123  bitset<_Nb>&
124  operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
125  {
126  _M_base() >>= __pos;
127  return *this;
128  }
129 
130  bitset<_Nb>&
131  set() _GLIBCXX_NOEXCEPT
132  {
133  _Base::set();
134  return *this;
135  }
136 
137  // _GLIBCXX_RESOLVE_LIB_DEFECTS
138  // 186. bitset::set() second parameter should be bool
139  bitset<_Nb>&
140  set(size_t __pos, bool __val = true)
141  {
142  _Base::set(__pos, __val);
143  return *this;
144  }
145 
146  bitset<_Nb>&
147  reset() _GLIBCXX_NOEXCEPT
148  {
149  _Base::reset();
150  return *this;
151  }
152 
153  bitset<_Nb>&
154  reset(size_t __pos)
155  {
156  _Base::reset(__pos);
157  return *this;
158  }
159 
160  bitset<_Nb>
161  operator~() const _GLIBCXX_NOEXCEPT
162  { return bitset(~_M_base()); }
163 
164  bitset<_Nb>&
165  flip() _GLIBCXX_NOEXCEPT
166  {
167  _Base::flip();
168  return *this;
169  }
170 
171  bitset<_Nb>&
172  flip(size_t __pos)
173  {
174  _Base::flip(__pos);
175  return *this;
176  }
177 
178  bool
179  operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
180  { return _M_base() == __rhs; }
181 
182  bool
183  operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
184  { return _M_base() != __rhs; }
185 
186  bitset<_Nb>
187  operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
188  { return bitset<_Nb>(_M_base() << __pos); }
189 
190  bitset<_Nb>
191  operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
192  { return bitset<_Nb>(_M_base() >> __pos); }
193 
194  _Base&
195  _M_base() _GLIBCXX_NOEXCEPT
196  { return *this; }
197 
198  const _Base&
199  _M_base() const _GLIBCXX_NOEXCEPT
200  { return *this; }
201  };
202 
203  template<size_t _Nb>
204  bitset<_Nb>
205  operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
206  { return bitset<_Nb>(__x) &= __y; }
207 
208  template<size_t _Nb>
209  bitset<_Nb>
210  operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
211  { return bitset<_Nb>(__x) |= __y; }
212 
213  template<size_t _Nb>
214  bitset<_Nb>
215  operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
216  { return bitset<_Nb>(__x) ^= __y; }
217 
218  template<typename _CharT, typename _Traits, size_t _Nb>
219  std::basic_istream<_CharT, _Traits>&
220  operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
221  { return __is >> __x._M_base(); }
222 
223  template<typename _CharT, typename _Traits, size_t _Nb>
224  std::basic_ostream<_CharT, _Traits>&
225  operator<<(std::basic_ostream<_CharT, _Traits>& __os,
226  const bitset<_Nb>& __x)
227  { return __os << __x._M_base(); }
228 } // namespace __profile
229 
230 #if __cplusplus >= 201103L
231  // DR 1182.
232  /// std::hash specialization for bitset.
233  template<size_t _Nb>
234  struct hash<__profile::bitset<_Nb>>
235  : public __hash_base<size_t, __profile::bitset<_Nb>>
236  {
237  size_t
238  operator()(const __profile::bitset<_Nb>& __b) const noexcept
239  { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
240  };
241 #endif
242 
243 } // namespace std
244 
245 #endif