1 // Profiling bitset implementation -*- C++ -*-
3 // Copyright (C) 2009 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file profile/bitset
26 * This file is a GNU profile extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_PROFILE_BITSET
30 #define _GLIBCXX_PROFILE_BITSET
38 /// Class std::bitset wrapper with performance instrumentation.
41 : public _GLIBCXX_STD_D::bitset<_Nb>
43 typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
48 : private _Base::reference
50 typedef typename _Base::reference _Base_ref;
55 reference(const _Base_ref& __base, bitset* __seq)
60 reference(const reference& __x)
67 *static_cast<_Base_ref*>(this) = __x;
72 operator=(const reference& __x)
74 *static_cast<_Base_ref*>(this) = __x;
81 return ~(*static_cast<const _Base_ref*>(this));
86 return *static_cast<const _Base_ref*>(this);
97 // 23.3.5.1 constructors:
98 bitset() : _Base() { }
100 bitset(unsigned long __val) : _Base(__val) { }
102 template<typename _CharT, typename _Traits, typename _Alloc>
104 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
105 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
107 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
108 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
109 : _Base(__str, __pos, __n) { }
111 // _GLIBCXX_RESOLVE_LIB_DEFECTS
112 // 396. what are characters zero and one.
113 template<class _CharT, class _Traits, class _Alloc>
114 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
115 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
117 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
119 _CharT __zero, _CharT __one = _CharT('1'))
120 : _Base(__str, __pos, __n, __zero, __one) { }
122 bitset(const _Base& __x) : _Base(__x) { }
124 #ifdef __GXX_EXPERIMENTAL_CXX0X__
126 bitset(const char* __str) : _Base(__str) { }
129 // 23.3.5.2 bitset operations:
131 operator&=(const bitset<_Nb>& __rhs)
138 operator|=(const bitset<_Nb>& __rhs)
145 operator^=(const bitset<_Nb>& __rhs)
152 operator<<=(size_t __pos)
159 operator>>=(size_t __pos)
172 // _GLIBCXX_RESOLVE_LIB_DEFECTS
173 // 186. bitset::set() second parameter should be bool
175 set(size_t __pos, bool __val = true)
177 _Base::set(__pos, __val);
195 bitset<_Nb> operator~() const { return bitset(~_M_base()); }
212 // _GLIBCXX_RESOLVE_LIB_DEFECTS
213 // 11. Bitset minor problems
215 operator[](size_t __pos)
217 return reference(_M_base()[__pos], this);
220 // _GLIBCXX_RESOLVE_LIB_DEFECTS
221 // 11. Bitset minor problems
223 operator[](size_t __pos) const
225 return _M_base()[__pos];
228 using _Base::to_ulong;
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230 using _Base::to_ullong;
233 template <typename _CharT, typename _Traits, typename _Alloc>
234 std::basic_string<_CharT, _Traits, _Alloc>
236 { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
238 // _GLIBCXX_RESOLVE_LIB_DEFECTS
239 // 396. what are characters zero and one.
240 template<class _CharT, class _Traits, class _Alloc>
241 std::basic_string<_CharT, _Traits, _Alloc>
242 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
244 return _M_base().template
245 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
248 // _GLIBCXX_RESOLVE_LIB_DEFECTS
249 // 434. bitset::to_string() hard to use.
250 template<typename _CharT, typename _Traits>
251 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
253 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
255 // _GLIBCXX_RESOLVE_LIB_DEFECTS
256 // 853. to_string needs updating with zero and one.
257 template<class _CharT, class _Traits>
258 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
259 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
260 { return to_string<_CharT, _Traits,
261 std::allocator<_CharT> >(__zero, __one); }
263 template<typename _CharT>
264 std::basic_string<_CharT, std::char_traits<_CharT>,
265 std::allocator<_CharT> >
268 return to_string<_CharT, std::char_traits<_CharT>,
269 std::allocator<_CharT> >();
272 template<class _CharT>
273 std::basic_string<_CharT, std::char_traits<_CharT>,
274 std::allocator<_CharT> >
275 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
277 return to_string<_CharT, std::char_traits<_CharT>,
278 std::allocator<_CharT> >(__zero, __one);
281 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
284 return to_string<char,std::char_traits<char>,std::allocator<char> >();
287 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
288 to_string(char __zero, char __one = '1') const
290 return to_string<char, std::char_traits<char>,
291 std::allocator<char> >(__zero, __one);
298 operator==(const bitset<_Nb>& __rhs) const
299 { return _M_base() == __rhs; }
302 operator!=(const bitset<_Nb>& __rhs) const
303 { return _M_base() != __rhs; }
311 operator<<(size_t __pos) const
312 { return bitset<_Nb>(_M_base() << __pos); }
315 operator>>(size_t __pos) const
316 { return bitset<_Nb>(_M_base() >> __pos); }
319 _M_base() { return *this; }
322 _M_base() const { return *this; }
327 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
328 { return bitset<_Nb>(__x) &= __y; }
332 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
333 { return bitset<_Nb>(__x) |= __y; }
337 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
338 { return bitset<_Nb>(__x) ^= __y; }
340 template<typename _CharT, typename _Traits, size_t _Nb>
341 std::basic_istream<_CharT, _Traits>&
342 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
343 { return __is >> __x._M_base(); }
345 template<typename _CharT, typename _Traits, size_t _Nb>
346 std::basic_ostream<_CharT, _Traits>&
347 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
348 const bitset<_Nb>& __x)
349 { return __os << __x._M_base(); }
350 } // namespace __profile