OSDN Git Service

2009-12-28 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
230       template <typename _CharT, typename _Traits, typename _Alloc>
231         std::basic_string<_CharT, _Traits, _Alloc>
232         to_string() const
233         { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
234
235       // _GLIBCXX_RESOLVE_LIB_DEFECTS
236       // 396. what are characters zero and one.
237       template<class _CharT, class _Traits, class _Alloc>
238         std::basic_string<_CharT, _Traits, _Alloc>
239         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
240         {
241           return _M_base().template
242             to_string<_CharT, _Traits, _Alloc>(__zero, __one);
243         }
244
245       // _GLIBCXX_RESOLVE_LIB_DEFECTS
246       // 434. bitset::to_string() hard to use.
247       template<typename _CharT, typename _Traits>
248         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
249         to_string() const
250         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
251
252       // _GLIBCXX_RESOLVE_LIB_DEFECTS
253       // 853. to_string needs updating with zero and one.
254       template<class _CharT, class _Traits>
255         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
256         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
257         { return to_string<_CharT, _Traits,
258                            std::allocator<_CharT> >(__zero, __one); }
259
260       template<typename _CharT>
261         std::basic_string<_CharT, std::char_traits<_CharT>,
262                           std::allocator<_CharT> >
263         to_string() const
264         {
265           return to_string<_CharT, std::char_traits<_CharT>,
266                            std::allocator<_CharT> >();
267         }
268
269       template<class _CharT>
270         std::basic_string<_CharT, std::char_traits<_CharT>,
271                           std::allocator<_CharT> >
272         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
273         {
274           return to_string<_CharT, std::char_traits<_CharT>,
275                            std::allocator<_CharT> >(__zero, __one);
276         }
277
278       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
279       to_string() const
280       {
281         return to_string<char,std::char_traits<char>,std::allocator<char> >();
282       }
283
284       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
285       to_string(char __zero, char __one = '1') const
286       {
287         return to_string<char, std::char_traits<char>,
288                          std::allocator<char> >(__zero, __one);
289       }
290
291       using _Base::count;
292       using _Base::size;
293
294       bool
295       operator==(const bitset<_Nb>& __rhs) const
296       { return _M_base() == __rhs; }
297
298       bool
299       operator!=(const bitset<_Nb>& __rhs) const
300       { return _M_base() != __rhs; }
301
302       using _Base::test;
303       using _Base::all;
304       using _Base::any;
305       using _Base::none;
306
307       bitset<_Nb>
308       operator<<(size_t __pos) const
309       { return bitset<_Nb>(_M_base() << __pos); }
310
311       bitset<_Nb>
312       operator>>(size_t __pos) const
313       { return bitset<_Nb>(_M_base() >> __pos); }
314
315       _Base&
316       _M_base() { return *this; }
317
318       const _Base&
319       _M_base() const { return *this; }
320     };
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<size_t _Nb>
333     bitset<_Nb>
334     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
335     { return bitset<_Nb>(__x) ^= __y; }
336
337   template<typename _CharT, typename _Traits, size_t _Nb>
338     std::basic_istream<_CharT, _Traits>&
339     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
340     { return __is >> __x._M_base(); }
341
342   template<typename _CharT, typename _Traits, size_t _Nb>
343     std::basic_ostream<_CharT, _Traits>&
344     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
345                const bitset<_Nb>& __x)
346     { return __os << __x._M_base(); }
347 } // namespace __profile
348 } // namespace std
349
350 #endif