OSDN Git Service

merge branch profile-stdlib
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / profile / bitset
1 // Profiling bitset implementation -*- C++ -*-
2
3 // Copyright (C) 2009 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
35 {
36 namespace __profile
37 {
38   /** @brief Bitset wrapper with performance instrumentation.  */
39   template<size_t _Nb>
40     class bitset
41     : public _GLIBCXX_STD_D::bitset<_Nb>
42     {
43       typedef _GLIBCXX_STD_D::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       bitset() : _Base() { }
99
100       bitset(unsigned long __val) : _Base(__val) { }
101
102       template<typename _CharT, typename _Traits, typename _Alloc>
103         explicit
104         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
105                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
106                __pos = 0,
107                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
108                __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
109         : _Base(__str, __pos, __n) { }
110
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
116                __pos,
117                typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
118                __n,
119                _CharT __zero, _CharT __one = _CharT('1'))
120         : _Base(__str, __pos, __n, __zero, __one) { }
121
122       bitset(const _Base& __x) : _Base(__x) { }
123
124       // 23.3.5.2 bitset operations:
125       bitset<_Nb>&
126       operator&=(const bitset<_Nb>& __rhs)
127       {
128         _M_base() &= __rhs;
129         return *this;
130       }
131
132       bitset<_Nb>&
133       operator|=(const bitset<_Nb>& __rhs)
134       {
135         _M_base() |= __rhs;
136         return *this;
137       }
138
139       bitset<_Nb>&
140       operator^=(const bitset<_Nb>& __rhs)
141       {
142         _M_base() ^= __rhs;
143         return *this;
144       }
145
146       bitset<_Nb>&
147       operator<<=(size_t __pos)
148       {
149         _M_base() <<= __pos;
150         return *this;
151       }
152
153       bitset<_Nb>&
154       operator>>=(size_t __pos)
155       {
156         _M_base() >>= __pos;
157         return *this;
158       }
159
160       bitset<_Nb>&
161       set()
162       {
163         _Base::set();
164         return *this;
165       }
166
167       // _GLIBCXX_RESOLVE_LIB_DEFECTS
168       // 186. bitset::set() second parameter should be bool
169       bitset<_Nb>&
170       set(size_t __pos, bool __val = true)
171       {
172         _Base::set(__pos, __val);
173         return *this;
174       }
175
176       bitset<_Nb>&
177       reset()
178       {
179         _Base::reset();
180         return *this;
181       }
182
183       bitset<_Nb>&
184       reset(size_t __pos)
185       {
186         _Base::reset(__pos);
187         return *this;
188       }
189
190       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
191
192       bitset<_Nb>&
193       flip()
194       {
195         _Base::flip();
196         return *this;
197       }
198
199       bitset<_Nb>&
200       flip(size_t __pos)
201       {
202         _Base::flip(__pos);
203         return *this;
204       }
205
206       // element access:
207       // _GLIBCXX_RESOLVE_LIB_DEFECTS
208       // 11. Bitset minor problems
209       reference
210       operator[](size_t __pos)
211       {
212         return reference(_M_base()[__pos], this);
213       }
214
215       // _GLIBCXX_RESOLVE_LIB_DEFECTS
216       // 11. Bitset minor problems
217       bool
218       operator[](size_t __pos) const
219       {
220         return _M_base()[__pos];
221       }
222
223       using _Base::to_ulong;
224
225       template <typename _CharT, typename _Traits, typename _Alloc>
226         std::basic_string<_CharT, _Traits, _Alloc>
227         to_string() const
228         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
229
230       // _GLIBCXX_RESOLVE_LIB_DEFECTS
231       // 396. what are characters zero and one.
232       template<class _CharT, class _Traits, class _Alloc>
233         std::basic_string<_CharT, _Traits, _Alloc>
234         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
235         {
236           return _M_base().template
237             to_string<_CharT, _Traits, _Alloc>(__zero, __one);
238         }
239
240       // _GLIBCXX_RESOLVE_LIB_DEFECTS
241       // 434. bitset::to_string() hard to use.
242       template<typename _CharT, typename _Traits>
243         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
244         to_string() const
245         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
246
247       // _GLIBCXX_RESOLVE_LIB_DEFECTS
248       // 853. to_string needs updating with zero and one.
249       template<class _CharT, class _Traits>
250         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
251         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
252         { return to_string<_CharT, _Traits,
253                            std::allocator<_CharT> >(__zero, __one); }
254
255       template<typename _CharT>
256         std::basic_string<_CharT, std::char_traits<_CharT>,
257                           std::allocator<_CharT> >
258         to_string() const
259         {
260           return to_string<_CharT, std::char_traits<_CharT>,
261                            std::allocator<_CharT> >();
262         }
263
264       template<class _CharT>
265         std::basic_string<_CharT, std::char_traits<_CharT>,
266                           std::allocator<_CharT> >
267         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
268         {
269           return to_string<_CharT, std::char_traits<_CharT>,
270                            std::allocator<_CharT> >(__zero, __one);
271         }
272
273       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
274       to_string() const
275       {
276         return to_string<char,std::char_traits<char>,std::allocator<char> >();
277       }
278
279       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
280       to_string(char __zero, char __one = '1') const
281       {
282         return to_string<char, std::char_traits<char>,
283                          std::allocator<char> >(__zero, __one);
284       }
285
286       using _Base::count;
287       using _Base::size;
288
289       bool
290       operator==(const bitset<_Nb>& __rhs) const
291       { return _M_base() == __rhs; }
292
293       bool
294       operator!=(const bitset<_Nb>& __rhs) const
295       { return _M_base() != __rhs; }
296
297       using _Base::test;
298       using _Base::all;
299       using _Base::any;
300       using _Base::none;
301
302       bitset<_Nb>
303       operator<<(size_t __pos) const
304       { return bitset<_Nb>(_M_base() << __pos); }
305
306       bitset<_Nb>
307       operator>>(size_t __pos) const
308       { return bitset<_Nb>(_M_base() >> __pos); }
309
310       _Base&
311       _M_base() { return *this; }
312
313       const _Base&
314       _M_base() const { return *this; }
315     };
316
317   template<size_t _Nb>
318     bitset<_Nb>
319     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
320     { return bitset<_Nb>(__x) &= __y; }
321
322   template<size_t _Nb>
323     bitset<_Nb>
324     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
325     { return bitset<_Nb>(__x) |= __y; }
326
327   template<size_t _Nb>
328     bitset<_Nb>
329     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
330     { return bitset<_Nb>(__x) ^= __y; }
331
332   template<typename _CharT, typename _Traits, size_t _Nb>
333     std::basic_istream<_CharT, _Traits>&
334     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
335     { return __is >> __x._M_base(); }
336
337   template<typename _CharT, typename _Traits, size_t _Nb>
338     std::basic_ostream<_CharT, _Traits>&
339     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
340                const bitset<_Nb>& __x)
341     { return __os << __x._M_base(); }
342 } // namespace __profile
343 } // namespace std
344
345 #endif