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