libstdc++

profile/bitset
Go to the documentation of this file.
1 // Profiling bitset implementation -*- C++ -*-
2
3 // Copyright (C) 2009-2013 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.
39 template<size_t _Nb>
40 class bitset
41 : public _GLIBCXX_STD_C::bitset<_Nb>
42 {
43 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
44
45 public:
46 // bit reference:
47 class reference
48 : private _Base::reference
49 {
50 typedef typename _Base::reference _Base_ref;
51
52 friend class bitset;
53 reference();
54
55 reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
56 : _Base_ref(__base)
57 { }
58
59 public:
60 reference(const reference& __x) _GLIBCXX_NOEXCEPT
61 : _Base_ref(__x)
62 { }
63
64 reference&
65 operator=(bool __x) _GLIBCXX_NOEXCEPT
66 {
67 *static_cast<_Base_ref*>(this) = __x;
68 return *this;
69 }
70
71 reference&
72 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
73 {
74 *static_cast<_Base_ref*>(this) = __x;
75 return *this;
76 }
77
78 bool
79 operator~() const _GLIBCXX_NOEXCEPT
80 {
81 return ~(*static_cast<const _Base_ref*>(this));
82 }
83
84 operator bool() const _GLIBCXX_NOEXCEPT
85 {
86 return *static_cast<const _Base_ref*>(this);
87 }
88
89 reference&
90 flip() _GLIBCXX_NOEXCEPT
91 {
92 _Base_ref::flip();
93 return *this;
94 }
95 };
96
97 // 23.3.5.1 constructors:
98 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
99 : _Base() { }
100
101 #if __cplusplus >= 201103L
102 constexpr bitset(unsigned long long __val) noexcept
103 #else
104 bitset(unsigned long __val)
105 #endif
106 : _Base(__val) { }
107
108 template<typename _CharT, typename _Traits, typename _Alloc>
109 explicit
110 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
111 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
112 __pos = 0,
113 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
114 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
115 : _Base(__str, __pos, __n) { }
116
117 // _GLIBCXX_RESOLVE_LIB_DEFECTS
118 // 396. what are characters zero and one.
119 template<class _CharT, class _Traits, class _Alloc>
120 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
121 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
122 __pos,
123 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
124 __n,
125 _CharT __zero, _CharT __one = _CharT('1'))
126 : _Base(__str, __pos, __n, __zero, __one) { }
127
128 bitset(const _Base& __x) : _Base(__x) { }
129
130 #if __cplusplus >= 201103L
131 template<typename _CharT>
132 explicit
133 bitset(const _CharT* __str,
134 typename std::basic_string<_CharT>::size_type __n
135 = std::basic_string<_CharT>::npos,
136 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
137 : _Base(__str, __n, __zero, __one) { }
138 #endif
139
140 // 23.3.5.2 bitset operations:
141 bitset<_Nb>&
142 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
143 {
144 _M_base() &= __rhs;
145 return *this;
146 }
147
148 bitset<_Nb>&
149 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
150 {
151 _M_base() |= __rhs;
152 return *this;
153 }
154
155 bitset<_Nb>&
156 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
157 {
158 _M_base() ^= __rhs;
159 return *this;
160 }
161
162 bitset<_Nb>&
163 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
164 {
165 _M_base() <<= __pos;
166 return *this;
167 }
168
169 bitset<_Nb>&
170 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
171 {
172 _M_base() >>= __pos;
173 return *this;
174 }
175
176 bitset<_Nb>&
177 set() _GLIBCXX_NOEXCEPT
178 {
179 _Base::set();
180 return *this;
181 }
182
183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 // 186. bitset::set() second parameter should be bool
185 bitset<_Nb>&
186 set(size_t __pos, bool __val = true)
187 {
188 _Base::set(__pos, __val);
189 return *this;
190 }
191
192 bitset<_Nb>&
193 reset() _GLIBCXX_NOEXCEPT
194 {
195 _Base::reset();
196 return *this;
197 }
198
199 bitset<_Nb>&
200 reset(size_t __pos)
201 {
202 _Base::reset(__pos);
203 return *this;
204 }
205
206 bitset<_Nb>
207 operator~() const _GLIBCXX_NOEXCEPT
208 { return bitset(~_M_base()); }
209
210 bitset<_Nb>&
211 flip() _GLIBCXX_NOEXCEPT
212 {
213 _Base::flip();
214 return *this;
215 }
216
217 bitset<_Nb>&
218 flip(size_t __pos)
219 {
220 _Base::flip(__pos);
221 return *this;
222 }
223
224 // element access:
225 // _GLIBCXX_RESOLVE_LIB_DEFECTS
226 // 11. Bitset minor problems
227 reference
228 operator[](size_t __pos)
229 {
230 return reference(_M_base()[__pos], this);
231 }
232
233 // _GLIBCXX_RESOLVE_LIB_DEFECTS
234 // 11. Bitset minor problems
235 _GLIBCXX_CONSTEXPR bool
236 operator[](size_t __pos) const
237 {
238 return _Base::operator[](__pos);
239 }
240
241 using _Base::to_ulong;
242 #if __cplusplus >= 201103L
243 using _Base::to_ullong;
244 #endif
245
246 template <typename _CharT, typename _Traits, typename _Alloc>
247 std::basic_string<_CharT, _Traits, _Alloc>
248 to_string() const
249 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
250
251 // _GLIBCXX_RESOLVE_LIB_DEFECTS
252 // 396. what are characters zero and one.
253 template<class _CharT, class _Traits, class _Alloc>
254 std::basic_string<_CharT, _Traits, _Alloc>
255 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
256 {
257 return _M_base().template
258 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
259 }
260
261 // _GLIBCXX_RESOLVE_LIB_DEFECTS
262 // 434. bitset::to_string() hard to use.
263 template<typename _CharT, typename _Traits>
264 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
265 to_string() const
266 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
267
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // 853. to_string needs updating with zero and one.
270 template<class _CharT, class _Traits>
271 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
272 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
273 { return to_string<_CharT, _Traits,
274 std::allocator<_CharT> >(__zero, __one); }
275
276 template<typename _CharT>
277 std::basic_string<_CharT, std::char_traits<_CharT>,
278 std::allocator<_CharT> >
279 to_string() const
280 {
281 return to_string<_CharT, std::char_traits<_CharT>,
282 std::allocator<_CharT> >();
283 }
284
285 template<class _CharT>
286 std::basic_string<_CharT, std::char_traits<_CharT>,
287 std::allocator<_CharT> >
288 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
289 {
290 return to_string<_CharT, std::char_traits<_CharT>,
291 std::allocator<_CharT> >(__zero, __one);
292 }
293
294 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
295 to_string() const
296 {
297 return to_string<char,std::char_traits<char>,std::allocator<char> >();
298 }
299
300 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
301 to_string(char __zero, char __one = '1') const
302 {
303 return to_string<char, std::char_traits<char>,
304 std::allocator<char> >(__zero, __one);
305 }
306
307 using _Base::count;
308 using _Base::size;
309
310 bool
311 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
312 { return _M_base() == __rhs; }
313
314 bool
315 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
316 { return _M_base() != __rhs; }
317
318 using _Base::test;
319 using _Base::all;
320 using _Base::any;
321 using _Base::none;
322
323 bitset<_Nb>
324 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
325 { return bitset<_Nb>(_M_base() << __pos); }
326
327 bitset<_Nb>
328 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
329 { return bitset<_Nb>(_M_base() >> __pos); }
330
331 _Base&
332 _M_base() _GLIBCXX_NOEXCEPT
333 { return *this; }
334
335 const _Base&
336 _M_base() const _GLIBCXX_NOEXCEPT
337 { return *this; }
338 };
339
340 template<size_t _Nb>
341 bitset<_Nb>
342 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
343 { return bitset<_Nb>(__x) &= __y; }
344
345 template<size_t _Nb>
346 bitset<_Nb>
347 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
348 { return bitset<_Nb>(__x) |= __y; }
349
350 template<size_t _Nb>
351 bitset<_Nb>
352 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
353 { return bitset<_Nb>(__x) ^= __y; }
354
355 template<typename _CharT, typename _Traits, size_t _Nb>
356 std::basic_istream<_CharT, _Traits>&
357 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
358 { return __is >> __x._M_base(); }
359
360 template<typename _CharT, typename _Traits, size_t _Nb>
361 std::basic_ostream<_CharT, _Traits>&
362 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
363 const bitset<_Nb>& __x)
364 { return __os << __x._M_base(); }
365 } // namespace __profile
366
367 #if __cplusplus >= 201103L
368 // DR 1182.
369 /// std::hash specialization for bitset.
370 template<size_t _Nb>
371 struct hash<__profile::bitset<_Nb>>
372 : public __hash_base<size_t, __profile::bitset<_Nb>>
373 {
374 size_t
375 operator()(const __profile::bitset<_Nb>& __b) const noexcept
376 { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
377 };
378 #endif
379
380 } // namespace std
381
382 #endif