OSDN Git Service

2009-12-29 Paolo Carlini <paolo.carlini@oracle.com>
[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   /// Class std::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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
125       explicit
126       bitset(const char* __str) : _Base(__str) { }
127 #endif
128
129       // 23.3.5.2 bitset operations:
130       bitset<_Nb>&
131       operator&=(const bitset<_Nb>& __rhs)
132       {
133         _M_base() &= __rhs;
134         return *this;
135       }
136
137       bitset<_Nb>&
138       operator|=(const bitset<_Nb>& __rhs)
139       {
140         _M_base() |= __rhs;
141         return *this;
142       }
143
144       bitset<_Nb>&
145       operator^=(const bitset<_Nb>& __rhs)
146       {
147         _M_base() ^= __rhs;
148         return *this;
149       }
150
151       bitset<_Nb>&
152       operator<<=(size_t __pos)
153       {
154         _M_base() <<= __pos;
155         return *this;
156       }
157
158       bitset<_Nb>&
159       operator>>=(size_t __pos)
160       {
161         _M_base() >>= __pos;
162         return *this;
163       }
164
165       bitset<_Nb>&
166       set()
167       {
168         _Base::set();
169         return *this;
170       }
171
172       // _GLIBCXX_RESOLVE_LIB_DEFECTS
173       // 186. bitset::set() second parameter should be bool
174       bitset<_Nb>&
175       set(size_t __pos, bool __val = true)
176       {
177         _Base::set(__pos, __val);
178         return *this;
179       }
180
181       bitset<_Nb>&
182       reset()
183       {
184         _Base::reset();
185         return *this;
186       }
187
188       bitset<_Nb>&
189       reset(size_t __pos)
190       {
191         _Base::reset(__pos);
192         return *this;
193       }
194
195       bitset<_Nb> operator~() const { return bitset(~_M_base()); }
196
197       bitset<_Nb>&
198       flip()
199       {
200         _Base::flip();
201         return *this;
202       }
203
204       bitset<_Nb>&
205       flip(size_t __pos)
206       {
207         _Base::flip(__pos);
208         return *this;
209       }
210
211       // element access:
212       // _GLIBCXX_RESOLVE_LIB_DEFECTS
213       // 11. Bitset minor problems
214       reference
215       operator[](size_t __pos)
216       {
217         return reference(_M_base()[__pos], this);
218       }
219
220       // _GLIBCXX_RESOLVE_LIB_DEFECTS
221       // 11. Bitset minor problems
222       bool
223       operator[](size_t __pos) const
224       {
225         return _M_base()[__pos];
226       }
227
228       using _Base::to_ulong;
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230       using _Base::to_ullong;
231 #endif
232
233       template <typename _CharT, typename _Traits, typename _Alloc>
234         std::basic_string<_CharT, _Traits, _Alloc>
235         to_string() const
236         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
237
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
243         {
244           return _M_base().template
245             to_string<_CharT, _Traits, _Alloc>(__zero, __one);
246         }
247
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> >
252         to_string() const
253         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
254
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); }
262
263       template<typename _CharT>
264         std::basic_string<_CharT, std::char_traits<_CharT>,
265                           std::allocator<_CharT> >
266         to_string() const
267         {
268           return to_string<_CharT, std::char_traits<_CharT>,
269                            std::allocator<_CharT> >();
270         }
271
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
276         {
277           return to_string<_CharT, std::char_traits<_CharT>,
278                            std::allocator<_CharT> >(__zero, __one);
279         }
280
281       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
282       to_string() const
283       {
284         return to_string<char,std::char_traits<char>,std::allocator<char> >();
285       }
286
287       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
288       to_string(char __zero, char __one = '1') const
289       {
290         return to_string<char, std::char_traits<char>,
291                          std::allocator<char> >(__zero, __one);
292       }
293
294       using _Base::count;
295       using _Base::size;
296
297       bool
298       operator==(const bitset<_Nb>& __rhs) const
299       { return _M_base() == __rhs; }
300
301       bool
302       operator!=(const bitset<_Nb>& __rhs) const
303       { return _M_base() != __rhs; }
304
305       using _Base::test;
306       using _Base::all;
307       using _Base::any;
308       using _Base::none;
309
310       bitset<_Nb>
311       operator<<(size_t __pos) const
312       { return bitset<_Nb>(_M_base() << __pos); }
313
314       bitset<_Nb>
315       operator>>(size_t __pos) const
316       { return bitset<_Nb>(_M_base() >> __pos); }
317
318       _Base&
319       _M_base() { return *this; }
320
321       const _Base&
322       _M_base() const { return *this; }
323     };
324
325   template<size_t _Nb>
326     bitset<_Nb>
327     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
328     { return bitset<_Nb>(__x) &= __y; }
329
330   template<size_t _Nb>
331     bitset<_Nb>
332     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
333     { return bitset<_Nb>(__x) |= __y; }
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<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(); }
344
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
351 } // namespace std
352
353 #endif