_bitset.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * Copyright (c) 1998
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_BITSET_H
  19. #define _STLP_BITSET_H
  20. // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused
  21. // bits. (They are the high- order bits in the highest word.) It is
  22. // a class invariant of class bitset<> that those unused bits are
  23. // always zero.
  24. // Most of the actual code isn't contained in bitset<> itself, but in the
  25. // base class _Base_bitset. The base class works with whole words, not with
  26. // individual bits. This allows us to specialize _Base_bitset for the
  27. // important special case where the bitset is only a single word.
  28. // The C++ standard does not define the precise semantics of operator[].
  29. // In this implementation the const version of operator[] is equivalent
  30. // to test(), except that it does no range checking. The non-const version
  31. // returns a reference to a bit, again without doing any range checking.
  32. #ifndef _STLP_INTERNAL_ALGOBASE_H
  33. # include <stl/_algobase.h>
  34. #endif
  35. #ifndef _STLP_INTERNAL_ALLOC_H
  36. # include <stl/_alloc.h>
  37. #endif
  38. #ifndef _STLP_INTERNAL_ITERATOR_H
  39. # include <stl/_iterator.h>
  40. #endif
  41. #ifndef _STLP_INTERNAL_UNINITIALIZED_H
  42. # include <stl/_uninitialized.h>
  43. #endif
  44. #ifndef _STLP_RANGE_ERRORS_H
  45. # include <stl/_range_errors.h>
  46. #endif
  47. #ifndef _STLP_INTERNAL_STRING_H
  48. # include <stl/_string.h>
  49. #endif
  50. #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
  51. #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
  52. _STLP_BEGIN_NAMESPACE
  53. _STLP_MOVE_TO_PRIV_NAMESPACE
  54. // structure to aid in counting bits
  55. class _STLP_CLASS_DECLSPEC _Bs_G
  56. {
  57. public:
  58. //returns the number of bit set within the buffer between __beg and __end.
  59. static size_t _S_count(const unsigned char *__beg, const unsigned char *__end)
  60. #if defined (_STLP_USE_NO_IOSTREAMS)
  61. {
  62. size_t __result = 0;
  63. for (; __beg != __end; ++__beg) {
  64. for (size_t i = 0; i < (sizeof(unsigned char) * 8); ++i) {
  65. if ((*__beg & (1 << i)) != 0) { ++__result; }
  66. }
  67. }
  68. return __result;
  69. }
  70. #else
  71. ;
  72. #endif
  73. // Mapping from 8 bit unsigned integers to the index of the first one bit set:
  74. static unsigned char _S_first_one(unsigned char __x)
  75. #if defined (_STLP_USE_NO_IOSTREAMS)
  76. {
  77. for (unsigned char i = 0; i < (sizeof(unsigned char) * 8); ++i) {
  78. if ((__x & (1 << i)) != 0) { return i; }
  79. }
  80. return 0;
  81. }
  82. #else
  83. ;
  84. #endif
  85. };
  86. //
  87. // Base class: general case.
  88. //
  89. template<size_t _Nw>
  90. struct _Base_bitset {
  91. typedef unsigned long _WordT;
  92. _WordT _M_w[_Nw]; // 0 is the least significant word.
  93. _Base_bitset() { _M_do_reset(); }
  94. _Base_bitset(unsigned long __val) {
  95. _M_do_reset();
  96. _M_w[0] = __val;
  97. }
  98. static size_t _STLP_CALL _S_whichword( size_t __pos ) {
  99. return __pos / __BITS_PER_WORD;
  100. }
  101. static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
  102. return (__pos % __BITS_PER_WORD) / CHAR_BIT;
  103. }
  104. static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
  105. return __pos % __BITS_PER_WORD;
  106. }
  107. static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
  108. return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos);
  109. }
  110. _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; }
  111. _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
  112. _WordT& _M_hiword() { return _M_w[_Nw - 1]; }
  113. _WordT _M_hiword() const { return _M_w[_Nw - 1]; }
  114. void _M_do_and(const _Base_bitset<_Nw>& __x) {
  115. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  116. _M_w[__i] &= __x._M_w[__i];
  117. }
  118. }
  119. void _M_do_or(const _Base_bitset<_Nw>& __x) {
  120. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  121. _M_w[__i] |= __x._M_w[__i];
  122. }
  123. }
  124. void _M_do_xor(const _Base_bitset<_Nw>& __x) {
  125. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  126. _M_w[__i] ^= __x._M_w[__i];
  127. }
  128. }
  129. void _M_do_left_shift(size_t __shift);
  130. void _M_do_right_shift(size_t __shift);
  131. void _M_do_flip() {
  132. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  133. _M_w[__i] = ~_M_w[__i];
  134. }
  135. }
  136. void _M_do_set() {
  137. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  138. _M_w[__i] = ~__STATIC_CAST(_WordT,0);
  139. }
  140. }
  141. void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
  142. bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
  143. for (size_t __i = 0; __i < _Nw; ++__i) {
  144. if (_M_w[__i] != __x._M_w[__i])
  145. return false;
  146. }
  147. return true;
  148. }
  149. bool _M_is_any() const {
  150. for ( size_t __i = 0; __i < _Nw ; __i++ ) {
  151. if ( _M_w[__i] != __STATIC_CAST(_WordT,0) )
  152. return true;
  153. }
  154. return false;
  155. }
  156. size_t _M_do_count() const {
  157. const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
  158. const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
  159. return _Bs_G::_S_count(__byte_ptr, __end_ptr);
  160. }
  161. unsigned long _M_do_to_ulong() const;
  162. // find first "on" bit
  163. size_t _M_do_find_first(size_t __not_found) const;
  164. // find the next "on" bit that follows "prev"
  165. size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
  166. };
  167. //
  168. // Base class: specialization for a single word.
  169. //
  170. _STLP_TEMPLATE_NULL
  171. struct _Base_bitset<1UL> {
  172. typedef unsigned long _WordT;
  173. typedef _Base_bitset<1UL> _Self;
  174. _WordT _M_w;
  175. _Base_bitset( void ) : _M_w(0) {}
  176. _Base_bitset(unsigned long __val) : _M_w(__val) {}
  177. static size_t _STLP_CALL _S_whichword( size_t __pos ) {
  178. return __pos / __BITS_PER_WORD ;
  179. }
  180. static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
  181. return (__pos % __BITS_PER_WORD) / CHAR_BIT;
  182. }
  183. static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
  184. return __pos % __BITS_PER_WORD;
  185. }
  186. static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
  187. return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
  188. }
  189. _WordT& _M_getword(size_t) { return _M_w; }
  190. _WordT _M_getword(size_t) const { return _M_w; }
  191. _WordT& _M_hiword() { return _M_w; }
  192. _WordT _M_hiword() const { return _M_w; }
  193. void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; }
  194. void _M_do_or(const _Self& __x) { _M_w |= __x._M_w; }
  195. void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; }
  196. void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
  197. void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
  198. void _M_do_flip() { _M_w = ~_M_w; }
  199. void _M_do_set() { _M_w = ~__STATIC_CAST(_WordT,0); }
  200. void _M_do_reset() { _M_w = 0; }
  201. bool _M_is_equal(const _Self& __x) const {
  202. return _M_w == __x._M_w;
  203. }
  204. bool _M_is_any() const {
  205. return _M_w != 0;
  206. }
  207. size_t _M_do_count() const {
  208. const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
  209. const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
  210. return _Bs_G::_S_count(__byte_ptr, __end_ptr);
  211. }
  212. unsigned long _M_do_to_ulong() const { return _M_w; }
  213. inline size_t _M_do_find_first(size_t __not_found) const;
  214. // find the next "on" bit that follows "prev"
  215. inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
  216. };
  217. // ------------------------------------------------------------
  218. //
  219. // Definitions of should-be-non-inline functions from the single-word version of
  220. // _Base_bitset.
  221. //
  222. inline size_t
  223. _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const {
  224. // typedef unsigned long _WordT;
  225. _WordT __thisword = _M_w;
  226. if ( __thisword != __STATIC_CAST(_WordT,0) ) {
  227. // find byte within word
  228. for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
  229. unsigned char __this_byte
  230. = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
  231. if ( __this_byte )
  232. return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
  233. __thisword >>= CHAR_BIT;
  234. }
  235. }
  236. // not found, so return a value that indicates failure.
  237. return __not_found;
  238. }
  239. inline size_t
  240. _Base_bitset<1UL>::_M_do_find_next(size_t __prev,
  241. size_t __not_found ) const {
  242. // make bound inclusive
  243. ++__prev;
  244. // check out of bounds
  245. if ( __prev >= __BITS_PER_WORD )
  246. return __not_found;
  247. // search first (and only) word
  248. _WordT __thisword = _M_w;
  249. // mask off bits below bound
  250. __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
  251. if ( __thisword != __STATIC_CAST(_WordT,0) ) {
  252. // find byte within word
  253. // get first byte into place
  254. __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
  255. for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
  256. unsigned char __this_byte
  257. = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
  258. if ( __this_byte )
  259. return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
  260. __thisword >>= CHAR_BIT;
  261. }
  262. }
  263. // not found, so return a value that indicates failure.
  264. return __not_found;
  265. } // end _M_do_find_next
  266. //
  267. // Base class, specialization for no storage (zero-length %bitset).
  268. //
  269. _STLP_TEMPLATE_NULL
  270. struct _Base_bitset<0UL> {
  271. typedef unsigned long _WordT;
  272. typedef _Base_bitset<0UL> _Self;
  273. _Base_bitset(void) {}
  274. _Base_bitset(unsigned long) {}
  275. static size_t _STLP_CALL _S_whichword(size_t __pos) {
  276. return __pos / __BITS_PER_WORD;
  277. }
  278. static size_t _STLP_CALL _S_whichbyte(size_t __pos) {
  279. return (__pos % __BITS_PER_WORD) / CHAR_BIT;
  280. }
  281. static size_t _STLP_CALL _S_whichbit(size_t __pos) {
  282. return __pos % __BITS_PER_WORD;
  283. }
  284. static _WordT _STLP_CALL _S_maskbit(size_t __pos) {
  285. return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
  286. }
  287. _WordT& _M_getword(size_t) {
  288. __stl_throw_out_of_range("_Base_bitset::_M_getword");
  289. return *new _WordT;
  290. }
  291. _WordT _M_getword(size_t) const { return 0; }
  292. _WordT _M_hiword() const { return 0; }
  293. void _M_do_and(const _Self&) {}
  294. void _M_do_or(const _Self&) {}
  295. void _M_do_xor(const _Self&) {}
  296. void _M_do_left_shift(size_t) {}
  297. void _M_do_right_shift(size_t) {}
  298. void _M_do_flip() {}
  299. void _M_do_set() {}
  300. void _M_do_reset() {}
  301. bool _M_is_equal(const _Self&) const { return true; }
  302. bool _M_is_any() const { return false; }
  303. size_t _M_do_count() const { return 0; }
  304. unsigned long _M_do_to_ulong() const { return 0; }
  305. size_t _M_do_find_first(size_t) const { return 0; }
  306. size_t _M_do_find_next(size_t, size_t) const { return 0; }
  307. };
  308. // ------------------------------------------------------------
  309. // Helper class to zero out the unused high-order bits in the highest word.
  310. template <size_t _Extrabits> struct _Sanitize {
  311. static void _STLP_CALL _M_do_sanitize(unsigned long& __val)
  312. { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); }
  313. };
  314. _STLP_TEMPLATE_NULL struct _Sanitize<0UL> {
  315. static void _STLP_CALL _M_do_sanitize(unsigned long) {}
  316. };
  317. _STLP_MOVE_TO_STD_NAMESPACE
  318. // ------------------------------------------------------------
  319. // Class bitset.
  320. // _Nb may be any nonzero number of type size_t.
  321. template<size_t _Nb>
  322. class bitset : public _STLP_PRIV _Base_bitset<__BITSET_WORDS(_Nb) > {
  323. public:
  324. enum { _Words = __BITSET_WORDS(_Nb) } ;
  325. private:
  326. typedef _STLP_PRIV _Base_bitset< _Words > _Base;
  327. void _M_do_sanitize() {
  328. _STLP_PRIV _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword());
  329. }
  330. public:
  331. typedef unsigned long _WordT;
  332. struct reference;
  333. friend struct reference;
  334. // bit reference:
  335. struct reference {
  336. typedef _STLP_PRIV _Base_bitset<_Words > _Bitset_base;
  337. typedef bitset<_Nb> _Bitset;
  338. // friend _Bitset;
  339. _WordT *_M_wp;
  340. size_t _M_bpos;
  341. // should be left undefined
  342. reference() {}
  343. reference( _Bitset& __b, size_t __pos ) {
  344. _M_wp = &__b._M_getword(__pos);
  345. _M_bpos = _Bitset_base::_S_whichbit(__pos);
  346. }
  347. public:
  348. ~reference() {}
  349. // for b[i] = __x;
  350. reference& operator=(bool __x) {
  351. if ( __x )
  352. *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
  353. else
  354. *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
  355. return *this;
  356. }
  357. // for b[i] = b[__j];
  358. reference& operator=(const reference& __j) {
  359. if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) )
  360. *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
  361. else
  362. *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
  363. return *this;
  364. }
  365. // flips the bit
  366. bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; }
  367. // for __x = b[i];
  368. operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; }
  369. // for b[i].flip();
  370. reference& flip() {
  371. *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos);
  372. return *this;
  373. }
  374. };
  375. // 23.3.5.1 constructors:
  376. bitset() {}
  377. bitset(unsigned long __val) : _STLP_PRIV _Base_bitset<_Words>(__val) { _M_do_sanitize(); }
  378. #if defined (_STLP_MEMBER_TEMPLATES)
  379. template<class _CharT, class _Traits, class _Alloc>
  380. explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
  381. size_t __pos = 0)
  382. : _STLP_PRIV _Base_bitset<_Words >() {
  383. if (__pos > __s.size())
  384. __stl_throw_out_of_range("bitset");
  385. _M_copy_from_string(__s, __pos,
  386. basic_string<_CharT, _Traits, _Alloc>::npos);
  387. }
  388. template<class _CharT, class _Traits, class _Alloc>
  389. bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
  390. size_t __pos,
  391. size_t __n)
  392. : _STLP_PRIV _Base_bitset<_Words >() {
  393. if (__pos > __s.size())
  394. __stl_throw_out_of_range("bitset");
  395. _M_copy_from_string(__s, __pos, __n);
  396. }
  397. #else /* _STLP_MEMBER_TEMPLATES */
  398. explicit bitset(const string& __s,
  399. size_t __pos = 0,
  400. size_t __n = (size_t)-1)
  401. : _STLP_PRIV _Base_bitset<_Words >() {
  402. if (__pos > __s.size())
  403. __stl_throw_out_of_range("bitset");
  404. _M_copy_from_string(__s, __pos, __n);
  405. }
  406. #endif /* _STLP_MEMBER_TEMPLATES */
  407. // 23.3.5.2 bitset operations:
  408. bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {
  409. this->_M_do_and(__rhs);
  410. return *this;
  411. }
  412. bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {
  413. this->_M_do_or(__rhs);
  414. return *this;
  415. }
  416. bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {
  417. this->_M_do_xor(__rhs);
  418. return *this;
  419. }
  420. bitset<_Nb>& operator<<=(size_t __pos) {
  421. this->_M_do_left_shift(__pos);
  422. this->_M_do_sanitize();
  423. return *this;
  424. }
  425. bitset<_Nb>& operator>>=(size_t __pos) {
  426. this->_M_do_right_shift(__pos);
  427. this->_M_do_sanitize();
  428. return *this;
  429. }
  430. //
  431. // Extension:
  432. // Versions of single-bit set, reset, flip, test with no range checking.
  433. //
  434. bitset<_Nb>& _Unchecked_set(size_t __pos) {
  435. this->_M_getword(__pos) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos);
  436. return *this;
  437. }
  438. bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {
  439. if (__val)
  440. this->_M_getword(__pos) |= this->_S_maskbit(__pos);
  441. else
  442. this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
  443. return *this;
  444. }
  445. bitset<_Nb>& _Unchecked_reset(size_t __pos) {
  446. this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
  447. return *this;
  448. }
  449. bitset<_Nb>& _Unchecked_flip(size_t __pos) {
  450. this->_M_getword(__pos) ^= this->_S_maskbit(__pos);
  451. return *this;
  452. }
  453. bool _Unchecked_test(size_t __pos) const {
  454. return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0);
  455. }
  456. // Set, reset, and flip.
  457. bitset<_Nb>& set() {
  458. this->_M_do_set();
  459. this->_M_do_sanitize();
  460. return *this;
  461. }
  462. bitset<_Nb>& set(size_t __pos) {
  463. if (__pos >= _Nb)
  464. __stl_throw_out_of_range("bitset");
  465. return _Unchecked_set(__pos);
  466. }
  467. bitset<_Nb>& set(size_t __pos, int __val) {
  468. if (__pos >= _Nb)
  469. __stl_throw_out_of_range("bitset");
  470. return _Unchecked_set(__pos, __val);
  471. }
  472. bitset<_Nb>& reset() {
  473. this->_M_do_reset();
  474. return *this;
  475. }
  476. bitset<_Nb>& reset(size_t __pos) {
  477. if (__pos >= _Nb)
  478. __stl_throw_out_of_range("bitset");
  479. return _Unchecked_reset(__pos);
  480. }
  481. bitset<_Nb>& flip() {
  482. this->_M_do_flip();
  483. this->_M_do_sanitize();
  484. return *this;
  485. }
  486. bitset<_Nb>& flip(size_t __pos) {
  487. if (__pos >= _Nb)
  488. __stl_throw_out_of_range("bitset");
  489. return _Unchecked_flip(__pos);
  490. }
  491. bitset<_Nb> operator~() const {
  492. return bitset<_Nb>(*this).flip();
  493. }
  494. // element access:
  495. //for b[i];
  496. reference operator[](size_t __pos) { return reference(*this,__pos); }
  497. bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
  498. unsigned long to_ulong() const { return this->_M_do_to_ulong(); }
  499. #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
  500. template <class _CharT, class _Traits, class _Alloc>
  501. basic_string<_CharT, _Traits, _Alloc> to_string() const {
  502. basic_string<_CharT, _Traits, _Alloc> __result;
  503. _M_copy_to_string(__result);
  504. return __result;
  505. }
  506. #else
  507. string to_string() const {
  508. string __result;
  509. _M_copy_to_string(__result);
  510. return __result;
  511. }
  512. #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */
  513. size_t count() const { return this->_M_do_count(); }
  514. size_t size() const { return _Nb; }
  515. bool operator==(const bitset<_Nb>& __rhs) const {
  516. return this->_M_is_equal(__rhs);
  517. }
  518. bool operator!=(const bitset<_Nb>& __rhs) const {
  519. return !this->_M_is_equal(__rhs);
  520. }
  521. bool test(size_t __pos) const {
  522. if (__pos >= _Nb)
  523. __stl_throw_out_of_range("bitset");
  524. return _Unchecked_test(__pos);
  525. }
  526. bool any() const { return this->_M_is_any(); }
  527. bool none() const { return !this->_M_is_any(); }
  528. bitset<_Nb> operator<<(size_t __pos) const {
  529. bitset<_Nb> __result(*this);
  530. __result <<= __pos ; return __result;
  531. }
  532. bitset<_Nb> operator>>(size_t __pos) const {
  533. bitset<_Nb> __result(*this);
  534. __result >>= __pos ; return __result;
  535. }
  536. #if !defined (_STLP_NO_EXTENSIONS)
  537. //
  538. // EXTENSIONS: bit-find operations. These operations are
  539. // experimental, and are subject to change or removal in future
  540. // versions.
  541. //
  542. // find the index of the first "on" bit
  543. size_t _Find_first() const
  544. { return this->_M_do_find_first(_Nb); }
  545. // find the index of the next "on" bit after prev
  546. size_t _Find_next( size_t __prev ) const
  547. { return this->_M_do_find_next(__prev, _Nb); }
  548. #endif
  549. //
  550. // Definitions of should-be non-inline member functions.
  551. //
  552. #if defined (_STLP_MEMBER_TEMPLATES)
  553. template<class _CharT, class _Traits, class _Alloc>
  554. void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
  555. size_t __pos, size_t __n) {
  556. #else
  557. void _M_copy_from_string(const string& __s,
  558. size_t __pos, size_t __n) {
  559. typedef typename string::traits_type _Traits;
  560. #endif
  561. reset();
  562. size_t __tmp = _Nb;
  563. const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));
  564. for ( size_t __i= 0; __i < __Nbits; ++__i) {
  565. typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);
  566. // boris : widen() ?
  567. if (__k == '1')
  568. set(__i);
  569. else if (__k != '0')
  570. __stl_throw_invalid_argument("bitset");
  571. }
  572. }
  573. #if defined (_STLP_MEMBER_TEMPLATES)
  574. template <class _CharT, class _Traits, class _Alloc>
  575. void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
  576. #else
  577. void _M_copy_to_string(string& __s) const
  578. #endif
  579. {
  580. __s.assign(_Nb, '0');
  581. for (size_t __i = 0; __i < _Nb; ++__i) {
  582. if (_Unchecked_test(__i))
  583. __s[_Nb - 1 - __i] = '1';
  584. }
  585. }
  586. #if !defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_WCHAR_T)
  587. void _M_copy_to_string(wstring& __s) const {
  588. __s.assign(_Nb, '0');
  589. for (size_t __i = 0; __i < _Nb; ++__i) {
  590. if (_Unchecked_test(__i))
  591. __s[_Nb - 1 - __i] = '1';
  592. }
  593. }
  594. #endif
  595. #if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
  596. bitset<_Nb> operator&(const bitset<_Nb>& __y) const {
  597. bitset<_Nb> __result(*this);
  598. __result &= __y;
  599. return __result;
  600. }
  601. bitset<_Nb> operator|(const bitset<_Nb>& __y) const {
  602. bitset<_Nb> __result(*this);
  603. __result |= __y;
  604. return __result;
  605. }
  606. bitset<_Nb> operator^(const bitset<_Nb>& __y) const {
  607. bitset<_Nb> __result(*this);
  608. __result ^= __y;
  609. return __result;
  610. }
  611. #endif
  612. };
  613. // ------------------------------------------------------------
  614. //
  615. // 23.3.5.3 bitset operations:
  616. //
  617. #if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
  618. template <size_t _Nb>
  619. inline bitset<_Nb> _STLP_CALL
  620. operator&(const bitset<_Nb>& __x,
  621. const bitset<_Nb>& __y) {
  622. bitset<_Nb> __result(__x);
  623. __result &= __y;
  624. return __result;
  625. }
  626. template <size_t _Nb>
  627. inline bitset<_Nb> _STLP_CALL
  628. operator|(const bitset<_Nb>& __x,
  629. const bitset<_Nb>& __y) {
  630. bitset<_Nb> __result(__x);
  631. __result |= __y;
  632. return __result;
  633. }
  634. template <size_t _Nb>
  635. inline bitset<_Nb> _STLP_CALL
  636. operator^(const bitset<_Nb>& __x,
  637. const bitset<_Nb>& __y) {
  638. bitset<_Nb> __result(__x);
  639. __result ^= __y;
  640. return __result;
  641. }
  642. #if !defined (_STLP_USE_NO_IOSTREAMS)
  643. _STLP_END_NAMESPACE
  644. # if !(defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) && \
  645. !(defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x500))
  646. #ifndef _STLP_INTERNAL_IOSFWD
  647. # include <stl/_iosfwd.h>
  648. #endif
  649. _STLP_BEGIN_NAMESPACE
  650. template <class _CharT, class _Traits, size_t _Nb>
  651. basic_istream<_CharT, _Traits>& _STLP_CALL
  652. operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);
  653. template <class _CharT, class _Traits, size_t _Nb>
  654. basic_ostream<_CharT, _Traits>& _STLP_CALL
  655. operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);
  656. # else
  657. #ifndef _STLP_STRING_IO_H
  658. # include <stl/_string_io.h> //includes _istream.h and _ostream.h
  659. #endif
  660. _STLP_BEGIN_NAMESPACE
  661. template <size_t _Nb>
  662. istream& _STLP_CALL
  663. operator>>(istream& __is, bitset<_Nb>& __x) {
  664. typedef typename string::traits_type _Traits;
  665. string __tmp;
  666. __tmp.reserve(_Nb);
  667. // Skip whitespace
  668. typename istream::sentry __sentry(__is);
  669. if (__sentry) {
  670. streambuf* __buf = __is.rdbuf();
  671. for (size_t __i = 0; __i < _Nb; ++__i) {
  672. static typename _Traits::int_type __eof = _Traits::eof();
  673. typename _Traits::int_type __c1 = __buf->sbumpc();
  674. if (_Traits::eq_int_type(__c1, __eof)) {
  675. __is.setstate(ios_base::eofbit);
  676. break;
  677. }
  678. else {
  679. typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
  680. char __c = __is.narrow(__c2, '*');
  681. if (__c == '0' || __c == '1')
  682. __tmp.push_back(__c);
  683. else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
  684. __is.setstate(ios_base::failbit);
  685. break;
  686. }
  687. }
  688. }
  689. if (__tmp.empty())
  690. __is.setstate(ios_base::failbit);
  691. else
  692. __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
  693. }
  694. return __is;
  695. }
  696. template <size_t _Nb>
  697. ostream& _STLP_CALL
  698. operator<<(ostream& __os, const bitset<_Nb>& __x) {
  699. string __tmp;
  700. __x._M_copy_to_string(__tmp);
  701. return __os << __tmp;
  702. }
  703. # if !defined (_STLP_NO_WCHAR_T)
  704. template <size_t _Nb>
  705. wistream& _STLP_CALL
  706. operator>>(wistream& __is, bitset<_Nb>& __x) {
  707. typedef typename wstring::traits_type _Traits;
  708. wstring __tmp;
  709. __tmp.reserve(_Nb);
  710. // Skip whitespace
  711. typename wistream::sentry __sentry(__is);
  712. if (__sentry) {
  713. wstreambuf* __buf = __is.rdbuf();
  714. for (size_t __i = 0; __i < _Nb; ++__i) {
  715. static typename _Traits::int_type __eof = _Traits::eof();
  716. typename _Traits::int_type __c1 = __buf->sbumpc();
  717. if (_Traits::eq_int_type(__c1, __eof)) {
  718. __is.setstate(ios_base::eofbit);
  719. break;
  720. }
  721. else {
  722. typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
  723. char __c = __is.narrow(__c2, '*');
  724. if (__c == '0' || __c == '1')
  725. __tmp.push_back(__c);
  726. else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
  727. __is.setstate(ios_base::failbit);
  728. break;
  729. }
  730. }
  731. }
  732. if (__tmp.empty())
  733. __is.setstate(ios_base::failbit);
  734. else
  735. __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
  736. }
  737. return __is;
  738. }
  739. template <size_t _Nb>
  740. wostream& _STLP_CALL
  741. operator<<(wostream& __os, const bitset<_Nb>& __x) {
  742. wstring __tmp;
  743. __x._M_copy_to_string(__tmp);
  744. return __os << __tmp;
  745. }
  746. # endif /* _STLP_NO_WCHAR_T */
  747. # endif
  748. #endif
  749. #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  750. #undef bitset
  751. _STLP_END_NAMESPACE
  752. #undef __BITS_PER_WORD
  753. #undef __BITSET_WORDS
  754. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  755. # include <stl/_bitset.c>
  756. #endif
  757. #endif /* _STLP_BITSET_H */
  758. // Local Variables:
  759. // mode:C++
  760. // End: