Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
4 * Silicon Graphics Computer Systems, Inc.
9 * This material is provided "as is", with absolutely no warranty expressed
10 * or implied. Any use is at your own risk.
12 * Permission to use or copy this software for any purpose is hereby granted
13 * without fee, provided the above notices are retained on all copies.
14 * Permission to modify the code and to distribute modified code is granted,
15 * provided the above notices are retained, and a notice that the code was
16 * modified is included with the above copyright notice.
20 #ifndef _STLP_BITSET_H
21 #define _STLP_BITSET_H
23 // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused
24 // bits. (They are the high- order bits in the highest word.) It is
25 // a class invariant of class bitset<> that those unused bits are
28 // Most of the actual code isn't contained in bitset<> itself, but in the
29 // base class _Base_bitset. The base class works with whole words, not with
30 // individual bits. This allows us to specialize _Base_bitset for the
31 // important special case where the bitset is only a single word.
33 // The C++ standard does not define the precise semantics of operator[].
34 // In this implementation the const version of operator[] is equivalent
35 // to test(), except that it does no range checking. The non-const version
36 // returns a reference to a bit, again without doing any range checking.
39 # ifndef _STLP_INTERNAL_ALGOBASE_H
40 # include <stl/_algobase.h>
43 # ifndef _STLP_INTERNAL_ALLOC_H
44 # include <stl/_alloc.h>
47 # ifndef _STLP_INTERNAL_ITERATOR_H
48 # include <stl/_iterator.h>
51 # ifndef _STLP_INTERNAL_UNINITIALIZED_H
52 # include <stl/_uninitialized.h>
55 # ifndef _STLP_RANGE_ERRORS_H
56 # include <stl/_range_errors.h>
63 # ifndef _STLP_ISTREAM
67 #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
68 #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
72 // structure to aid in counting bits
73 template<class _Dummy>
76 static const unsigned char _S_bit_count[256];
77 // Mapping from 8 bit unsigned integers to the index of the first one
79 static const unsigned char _S_first_one[256];
83 // Base class: general case.
88 typedef unsigned long _WordT;
90 _WordT _M_w[_Nw]; // 0 is the least significant word.
92 _Base_bitset( void ) { _M_do_reset(); }
94 _Base_bitset(unsigned long __val) {
99 static size_t _STLP_CALL _S_whichword( size_t __pos ) {
100 return __pos / __BITS_PER_WORD;
102 static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
103 return (__pos % __BITS_PER_WORD) / CHAR_BIT;
105 static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
106 return __pos % __BITS_PER_WORD;
108 static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
109 return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos);
112 _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; }
113 _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
115 _WordT& _M_hiword() { return _M_w[_Nw - 1]; }
116 _WordT _M_hiword() const { return _M_w[_Nw - 1]; }
118 void _M_do_and(const _Base_bitset<_Nw>& __x) {
119 for ( size_t __i = 0; __i < _Nw; __i++ ) {
120 _M_w[__i] &= __x._M_w[__i];
124 void _M_do_or(const _Base_bitset<_Nw>& __x) {
125 for ( size_t __i = 0; __i < _Nw; __i++ ) {
126 _M_w[__i] |= __x._M_w[__i];
130 void _M_do_xor(const _Base_bitset<_Nw>& __x) {
131 for ( size_t __i = 0; __i < _Nw; __i++ ) {
132 _M_w[__i] ^= __x._M_w[__i];
136 void _M_do_left_shift(size_t __shift);
138 void _M_do_right_shift(size_t __shift);
141 for ( size_t __i = 0; __i < _Nw; __i++ ) {
142 _M_w[__i] = ~_M_w[__i];
147 for ( size_t __i = 0; __i < _Nw; __i++ ) {
148 _M_w[__i] = ~__STATIC_CAST(_WordT,0);
153 void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
155 bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
156 for (size_t __i = 0; __i < _Nw; ++__i) {
157 if (_M_w[__i] != __x._M_w[__i])
163 bool _M_is_any() const {
164 for ( size_t __i = 0; __i < _Nw ; __i++ ) {
165 if ( _M_w[__i] != __STATIC_CAST(_WordT,0) )
171 size_t _M_do_count() const {
173 const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
174 const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
176 while ( __byte_ptr < __end_ptr ) {
177 __result += _Bs_G<bool>::_S_bit_count[*__byte_ptr];
183 unsigned long _M_do_to_ulong() const;
185 // find first "on" bit
186 size_t _M_do_find_first(size_t __not_found) const;
188 // find the next "on" bit that follows "prev"
189 size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
193 // Base class: specialization for a single word.
197 struct _Base_bitset<1UL> {
198 typedef unsigned long _WordT;
199 typedef _Base_bitset<1UL> _Self;
203 _Base_bitset( void ) : _M_w(0) {}
204 _Base_bitset(unsigned long __val) : _M_w(__val) {}
206 static size_t _STLP_CALL _S_whichword( size_t __pos ) {
207 return __pos / __BITS_PER_WORD ;
209 static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
210 return (__pos % __BITS_PER_WORD) / CHAR_BIT;
212 static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
213 return __pos % __BITS_PER_WORD;
215 static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
216 return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
219 _WordT& _M_getword(size_t) { return _M_w; }
220 _WordT _M_getword(size_t) const { return _M_w; }
222 _WordT& _M_hiword() { return _M_w; }
223 _WordT _M_hiword() const { return _M_w; }
226 void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; }
227 void _M_do_or(const _Self& __x) { _M_w |= __x._M_w; }
228 void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; }
229 void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
230 void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
231 void _M_do_flip() { _M_w = ~_M_w; }
232 void _M_do_set() { _M_w = ~__STATIC_CAST(_WordT,0); }
233 void _M_do_reset() { _M_w = 0; }
235 bool _M_is_equal(const _Self& __x) const {
236 return _M_w == __x._M_w;
238 bool _M_is_any() const {
242 size_t _M_do_count() const {
244 const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
245 const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
246 while ( __byte_ptr < __end_ptr ) {
247 __result += _Bs_G<bool>::_S_bit_count[*__byte_ptr];
253 unsigned long _M_do_to_ulong() const { return _M_w; }
255 inline size_t _M_do_find_first(size_t __not_found) const;
257 // find the next "on" bit that follows "prev"
258 inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
263 // ------------------------------------------------------------
265 // Definitions of should-be-non-inline functions from the single-word version of
270 _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const
272 // typedef unsigned long _WordT;
273 _WordT __thisword = _M_w;
275 if ( __thisword != __STATIC_CAST(_WordT,0) ) {
276 // find byte within word
277 for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
278 unsigned char __this_byte
279 = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
281 return __j*CHAR_BIT + _Bs_G<bool>::_S_first_one[__this_byte];
283 __thisword >>= CHAR_BIT;
286 // not found, so return a value that indicates failure.
291 _Base_bitset<1UL>::_M_do_find_next(size_t __prev,
292 size_t __not_found ) const
294 // make bound inclusive
297 // check out of bounds
298 if ( __prev >= __BITS_PER_WORD )
301 // search first (and only) word
302 _WordT __thisword = _M_w;
304 // mask off bits below bound
305 __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
307 if ( __thisword != __STATIC_CAST(_WordT,0) ) {
308 // find byte within word
309 // get first byte into place
310 __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
311 for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
312 unsigned char __this_byte
313 = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
315 return __j*CHAR_BIT + _Bs_G<bool>::_S_first_one[__this_byte];
317 __thisword >>= CHAR_BIT;
321 // not found, so return a value that indicates failure.
323 } // end _M_do_find_next
326 // ------------------------------------------------------------
327 // Helper class to zero out the unused high-order bits in the highest word.
329 template <size_t _Extrabits> struct _Sanitize {
330 static void _STLP_CALL _M_do_sanitize(unsigned long& __val)
331 { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); }
334 _STLP_TEMPLATE_NULL struct _Sanitize<0UL> {
335 static void _STLP_CALL _M_do_sanitize(unsigned long) {}
338 // ------------------------------------------------------------
340 // _Nb may be any nonzero number of type size_t.
344 class bitset : public _Base_bitset<__BITSET_WORDS(_Nb) >
347 enum { _Words = __BITSET_WORDS(_Nb) } ;
350 typedef _Base_bitset< _Words > _Base;
352 void _M_do_sanitize() {
353 _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword());
356 typedef unsigned long _WordT;
358 friend struct reference;
362 typedef _Base_bitset<_Words > _Bitset_base;
363 typedef bitset<_Nb> _Bitset;
368 // should be left undefined
371 reference( _Bitset& __b, size_t __pos ) {
372 _M_wp = &__b._M_getword(__pos);
373 _M_bpos = _Bitset_base::_S_whichbit(__pos);
380 reference& operator=(bool __x) {
382 *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
384 *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
389 // for b[i] = b[__j];
390 reference& operator=(const reference& __j) {
391 if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) )
392 *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
394 *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
400 bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; }
403 operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; }
407 *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos);
412 // 23.3.5.1 constructors:
415 bitset(unsigned long __val) : _Base_bitset<_Words>(__val) { _M_do_sanitize(); }
417 # ifdef _STLP_MEMBER_TEMPLATES
418 template<class _CharT, class _Traits, class _Alloc>
419 explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
421 : _Base_bitset<_Words >()
423 if (__pos > __s.size())
424 __stl_throw_out_of_range("bitset");
425 _M_copy_from_string(__s, __pos,
426 basic_string<_CharT, _Traits, _Alloc>::npos);
428 template<class _CharT, class _Traits, class _Alloc>
429 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
432 : _Base_bitset<_Words >()
434 if (__pos > __s.size())
435 __stl_throw_out_of_range("bitset");
436 _M_copy_from_string(__s, __pos, __n);
438 #else /* _STLP_MEMBER_TEMPLATES */
439 explicit bitset(const string& __s,
441 size_t __n = (size_t)-1)
442 : _Base_bitset<_Words >()
444 if (__pos > __s.size())
445 __stl_throw_out_of_range("bitset");
446 _M_copy_from_string(__s, __pos, __n);
448 #endif /* _STLP_MEMBER_TEMPLATES */
450 // 23.3.5.2 bitset operations:
451 bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {
452 this->_M_do_and(__rhs);
456 bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {
457 this->_M_do_or(__rhs);
461 bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {
462 this->_M_do_xor(__rhs);
466 bitset<_Nb>& operator<<=(size_t __pos) {
470 this->_M_do_left_shift(__pos);
471 this->_M_do_sanitize();
476 this->_M_do_left_shift(__pos);
477 this->_M_do_sanitize();
483 bitset<_Nb>& operator>>=(size_t __pos) {
487 this->_M_do_right_shift(__pos);
488 this->_M_do_sanitize();
493 this->_M_do_right_shift(__pos);
494 this->_M_do_sanitize();
502 // Versions of single-bit set, reset, flip, test with no range checking.
505 bitset<_Nb>& _Unchecked_set(size_t __pos) {
506 this->_M_getword(__pos) |= _Base_bitset<_Words > ::_S_maskbit(__pos);
510 bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {
512 this->_M_getword(__pos) |= this->_S_maskbit(__pos);
514 this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
519 bitset<_Nb>& _Unchecked_reset(size_t __pos) {
520 this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
524 bitset<_Nb>& _Unchecked_flip(size_t __pos) {
525 this->_M_getword(__pos) ^= this->_S_maskbit(__pos);
529 bool _Unchecked_test(size_t __pos) const {
530 return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0);
533 // Set, reset, and flip.
537 this->_M_do_sanitize();
541 bitset<_Nb>& set(size_t __pos) {
543 __stl_throw_out_of_range("bitset");
544 return _Unchecked_set(__pos);
547 bitset<_Nb>& set(size_t __pos, int __val) {
549 __stl_throw_out_of_range("bitset");
550 return _Unchecked_set(__pos, __val);
553 bitset<_Nb>& reset() {
558 bitset<_Nb>& reset(size_t __pos) {
560 __stl_throw_out_of_range("bitset");
562 return _Unchecked_reset(__pos);
565 bitset<_Nb>& flip() {
567 this->_M_do_sanitize();
571 bitset<_Nb>& flip(size_t __pos) {
573 __stl_throw_out_of_range("bitset");
575 return _Unchecked_flip(__pos);
578 bitset<_Nb> operator~() const {
579 return bitset<_Nb>(*this).flip();
584 reference operator[](size_t __pos) { return reference(*this,__pos); }
585 bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
587 unsigned long to_ulong() const { return this->_M_do_to_ulong(); }
589 #if defined (_STLP_MEMBER_TEMPLATES) && ! defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
590 template <class _CharT, class _Traits, class _Alloc>
591 basic_string<_CharT, _Traits, _Alloc> to_string() const {
592 basic_string<_CharT, _Traits, _Alloc> __result;
593 _M_copy_to_string(__result);
597 string to_string() const {
599 _M_copy_to_string(__result);
602 #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */
604 size_t count() const { return this->_M_do_count(); }
606 size_t size() const { return _Nb; }
608 bool operator==(const bitset<_Nb>& __rhs) const {
609 return this->_M_is_equal(__rhs);
611 bool operator!=(const bitset<_Nb>& __rhs) const {
612 return !this->_M_is_equal(__rhs);
615 bool test(size_t __pos) const {
617 __stl_throw_out_of_range("bitset");
619 return _Unchecked_test(__pos);
622 bool any() const { return this->_M_is_any(); }
623 bool none() const { return !this->_M_is_any(); }
625 bitset<_Nb> operator<<(size_t __pos) const {
626 bitset<_Nb> __result(*this);
627 __result <<= __pos ; return __result;
629 bitset<_Nb> operator>>(size_t __pos) const {
630 bitset<_Nb> __result(*this);
631 __result >>= __pos ; return __result;
635 // EXTENSIONS: bit-find operations. These operations are
636 // experimental, and are subject to change or removal in future
640 // find the index of the first "on" bit
641 size_t _Find_first() const
642 { return this->_M_do_find_first(_Nb); }
644 // find the index of the next "on" bit after prev
645 size_t _Find_next( size_t __prev ) const
646 { return this->_M_do_find_next(__prev, _Nb); }
649 // Definitions of should-be non-inline member functions.
651 # if defined (_STLP_MEMBER_TEMPLATES)
652 template<class _CharT, class _Traits, class _Alloc>
653 void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
657 void _M_copy_from_string(const string& __s,
660 typedef char_traits<char> _Traits;
664 const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));
665 for ( size_t __i= 0; __i < __Nbits; ++__i) {
666 typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);
671 __stl_throw_invalid_argument("bitset");
675 # if defined (_STLP_MEMBER_TEMPLATES)
676 template <class _CharT, class _Traits, class _Alloc>
677 void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
679 void _M_copy_to_string(string& __s) const
682 __s.assign(_Nb, '0');
684 for (size_t __i = 0; __i < _Nb; ++__i)
685 if (_Unchecked_test(__i))
686 __s[_Nb - 1 - __i] = '1';
689 # if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
690 bitset<_Nb> operator&(const bitset<_Nb>& __y) const {
691 bitset<_Nb> __result(*this);
695 bitset<_Nb> operator|(const bitset<_Nb>& __y) const {
696 bitset<_Nb> __result(*this);
700 bitset<_Nb> operator^(const bitset<_Nb>& __y) const {
701 bitset<_Nb> __result(*this);
709 // ------------------------------------------------------------
711 // 23.3.5.3 bitset operations:
714 # if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
716 template <size_t _Nb>
717 inline bitset<_Nb> _STLP_CALL
718 operator&(const bitset<_Nb>& __x,
719 const bitset<_Nb>& __y) {
720 bitset<_Nb> __result(__x);
726 template <size_t _Nb>
727 inline bitset<_Nb> _STLP_CALL
728 operator|(const bitset<_Nb>& __x,
729 const bitset<_Nb>& __y) {
730 bitset<_Nb> __result(__x);
735 template <size_t _Nb>
736 inline bitset<_Nb> _STLP_CALL
737 operator^(const bitset<_Nb>& __x,
738 const bitset<_Nb>& __y) {
739 bitset<_Nb> __result(__x);
744 #if defined ( _STLP_USE_NEW_IOSTREAMS )
746 template <class _CharT, class _Traits, size_t _Nb>
747 basic_istream<_CharT, _Traits>& _STLP_CALL
748 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);
751 template <class _CharT, class _Traits, size_t _Nb>
752 basic_ostream<_CharT, _Traits>& _STLP_CALL
753 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);
755 #elif ! defined ( _STLP_USE_NO_IOSTREAMS )
757 // (reg) For Watcom IO, this tells if ostream class is in .exe or in .dll
758 template <size_t _Nb>
759 _ISTREAM_DLL& _STLP_CALL
760 operator>>(_ISTREAM_DLL& __is, bitset<_Nb>& __x);
762 template <size_t _Nb>
763 inline _OSTREAM_DLL& _STLP_CALL operator<<(_OSTREAM_DLL& __os, const bitset<_Nb>& __x) {
765 __x._M_copy_to_string(__tmp);
766 return __os << __tmp;
771 # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
778 # undef __BITS_PER_WORD
779 # undef __BITSET_WORDS
781 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
782 # include <stl/_bitset.c>
785 #endif /* _STLP_BITSET_H */