bitset 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. // <bitset> -*- C++ -*-
  2. // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. // 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. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /*
  22. * Copyright (c) 1998
  23. * Silicon Graphics Computer Systems, Inc.
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Silicon Graphics makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. */
  33. /** @file include/bitset
  34. * This is a Standard C++ Library header.
  35. */
  36. #ifndef _GLIBCXX_BITSET
  37. #define _GLIBCXX_BITSET 1
  38. #pragma GCC system_header
  39. #include <cstddef> // For size_t
  40. #include <string>
  41. #include <bits/functexcept.h> // For invalid_argument, out_of_range,
  42. // overflow_error
  43. #include <iosfwd>
  44. #include <cxxabi-forced.h>
  45. #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
  46. #define _GLIBCXX_BITSET_WORDS(__n) \
  47. ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
  48. / _GLIBCXX_BITSET_BITS_PER_WORD)
  49. _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
  50. /**
  51. * Base class, general case. It is a class invariant that _Nw will be
  52. * nonnegative.
  53. *
  54. * See documentation for bitset.
  55. */
  56. template<size_t _Nw>
  57. struct _Base_bitset
  58. {
  59. typedef unsigned long _WordT;
  60. /// 0 is the least significant word.
  61. _WordT _M_w[_Nw];
  62. _Base_bitset()
  63. { _M_do_reset(); }
  64. _Base_bitset(unsigned long __val)
  65. {
  66. _M_do_reset();
  67. _M_w[0] = __val;
  68. }
  69. static size_t
  70. _S_whichword(size_t __pos )
  71. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  72. static size_t
  73. _S_whichbyte(size_t __pos )
  74. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  75. static size_t
  76. _S_whichbit(size_t __pos )
  77. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  78. static _WordT
  79. _S_maskbit(size_t __pos )
  80. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  81. _WordT&
  82. _M_getword(size_t __pos)
  83. { return _M_w[_S_whichword(__pos)]; }
  84. _WordT
  85. _M_getword(size_t __pos) const
  86. { return _M_w[_S_whichword(__pos)]; }
  87. _WordT&
  88. _M_hiword()
  89. { return _M_w[_Nw - 1]; }
  90. _WordT
  91. _M_hiword() const
  92. { return _M_w[_Nw - 1]; }
  93. void
  94. _M_do_and(const _Base_bitset<_Nw>& __x)
  95. {
  96. for (size_t __i = 0; __i < _Nw; __i++)
  97. _M_w[__i] &= __x._M_w[__i];
  98. }
  99. void
  100. _M_do_or(const _Base_bitset<_Nw>& __x)
  101. {
  102. for (size_t __i = 0; __i < _Nw; __i++)
  103. _M_w[__i] |= __x._M_w[__i];
  104. }
  105. void
  106. _M_do_xor(const _Base_bitset<_Nw>& __x)
  107. {
  108. for (size_t __i = 0; __i < _Nw; __i++)
  109. _M_w[__i] ^= __x._M_w[__i];
  110. }
  111. void
  112. _M_do_left_shift(size_t __shift);
  113. void
  114. _M_do_right_shift(size_t __shift);
  115. void
  116. _M_do_flip()
  117. {
  118. for (size_t __i = 0; __i < _Nw; __i++)
  119. _M_w[__i] = ~_M_w[__i];
  120. }
  121. void
  122. _M_do_set()
  123. {
  124. for (size_t __i = 0; __i < _Nw; __i++)
  125. _M_w[__i] = ~static_cast<_WordT>(0);
  126. }
  127. void
  128. _M_do_reset()
  129. { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
  130. bool
  131. _M_is_equal(const _Base_bitset<_Nw>& __x) const
  132. {
  133. for (size_t __i = 0; __i < _Nw; ++__i)
  134. if (_M_w[__i] != __x._M_w[__i])
  135. return false;
  136. return true;
  137. }
  138. size_t
  139. _M_are_all_aux() const
  140. {
  141. for (size_t __i = 0; __i < _Nw - 1; __i++)
  142. if (_M_w[__i] != ~static_cast<_WordT>(0))
  143. return 0;
  144. return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
  145. + __builtin_popcountl(_M_hiword()));
  146. }
  147. bool
  148. _M_is_any() const
  149. {
  150. for (size_t __i = 0; __i < _Nw; __i++)
  151. if (_M_w[__i] != static_cast<_WordT>(0))
  152. return true;
  153. return false;
  154. }
  155. size_t
  156. _M_do_count() const
  157. {
  158. size_t __result = 0;
  159. for (size_t __i = 0; __i < _Nw; __i++)
  160. __result += __builtin_popcountl(_M_w[__i]);
  161. return __result;
  162. }
  163. unsigned long
  164. _M_do_to_ulong() const;
  165. // find first "on" bit
  166. size_t
  167. _M_do_find_first(size_t __not_found) const;
  168. // find the next "on" bit that follows "prev"
  169. size_t
  170. _M_do_find_next(size_t __prev, size_t __not_found) const;
  171. };
  172. // Definitions of non-inline functions from _Base_bitset.
  173. template<size_t _Nw>
  174. void
  175. _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
  176. {
  177. if (__builtin_expect(__shift != 0, 1))
  178. {
  179. const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
  180. const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
  181. if (__offset == 0)
  182. for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
  183. _M_w[__n] = _M_w[__n - __wshift];
  184. else
  185. {
  186. const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
  187. - __offset);
  188. for (size_t __n = _Nw - 1; __n > __wshift; --__n)
  189. _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
  190. | (_M_w[__n - __wshift - 1] >> __sub_offset));
  191. _M_w[__wshift] = _M_w[0] << __offset;
  192. }
  193. std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
  194. }
  195. }
  196. template<size_t _Nw>
  197. void
  198. _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
  199. {
  200. if (__builtin_expect(__shift != 0, 1))
  201. {
  202. const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
  203. const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
  204. const size_t __limit = _Nw - __wshift - 1;
  205. if (__offset == 0)
  206. for (size_t __n = 0; __n <= __limit; ++__n)
  207. _M_w[__n] = _M_w[__n + __wshift];
  208. else
  209. {
  210. const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
  211. - __offset);
  212. for (size_t __n = 0; __n < __limit; ++__n)
  213. _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
  214. | (_M_w[__n + __wshift + 1] << __sub_offset));
  215. _M_w[__limit] = _M_w[_Nw-1] >> __offset;
  216. }
  217. std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
  218. }
  219. }
  220. template<size_t _Nw>
  221. unsigned long
  222. _Base_bitset<_Nw>::_M_do_to_ulong() const
  223. {
  224. for (size_t __i = 1; __i < _Nw; ++__i)
  225. if (_M_w[__i])
  226. __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
  227. return _M_w[0];
  228. }
  229. template<size_t _Nw>
  230. size_t
  231. _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
  232. {
  233. for (size_t __i = 0; __i < _Nw; __i++)
  234. {
  235. _WordT __thisword = _M_w[__i];
  236. if (__thisword != static_cast<_WordT>(0))
  237. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  238. + __builtin_ctzl(__thisword));
  239. }
  240. // not found, so return an indication of failure.
  241. return __not_found;
  242. }
  243. template<size_t _Nw>
  244. size_t
  245. _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
  246. {
  247. // make bound inclusive
  248. ++__prev;
  249. // check out of bounds
  250. if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
  251. return __not_found;
  252. // search first word
  253. size_t __i = _S_whichword(__prev);
  254. _WordT __thisword = _M_w[__i];
  255. // mask off bits below bound
  256. __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
  257. if (__thisword != static_cast<_WordT>(0))
  258. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  259. + __builtin_ctzl(__thisword));
  260. // check subsequent words
  261. __i++;
  262. for (; __i < _Nw; __i++)
  263. {
  264. __thisword = _M_w[__i];
  265. if (__thisword != static_cast<_WordT>(0))
  266. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  267. + __builtin_ctzl(__thisword));
  268. }
  269. // not found, so return an indication of failure.
  270. return __not_found;
  271. } // end _M_do_find_next
  272. /**
  273. * Base class, specialization for a single word.
  274. *
  275. * See documentation for bitset.
  276. */
  277. template<>
  278. struct _Base_bitset<1>
  279. {
  280. typedef unsigned long _WordT;
  281. _WordT _M_w;
  282. _Base_bitset(void)
  283. : _M_w(0)
  284. { }
  285. _Base_bitset(unsigned long __val)
  286. : _M_w(__val)
  287. { }
  288. static size_t
  289. _S_whichword(size_t __pos )
  290. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  291. static size_t
  292. _S_whichbyte(size_t __pos )
  293. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  294. static size_t
  295. _S_whichbit(size_t __pos )
  296. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  297. static _WordT
  298. _S_maskbit(size_t __pos )
  299. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  300. _WordT&
  301. _M_getword(size_t)
  302. { return _M_w; }
  303. _WordT
  304. _M_getword(size_t) const
  305. { return _M_w; }
  306. _WordT&
  307. _M_hiword()
  308. { return _M_w; }
  309. _WordT
  310. _M_hiword() const
  311. { return _M_w; }
  312. void
  313. _M_do_and(const _Base_bitset<1>& __x)
  314. { _M_w &= __x._M_w; }
  315. void
  316. _M_do_or(const _Base_bitset<1>& __x)
  317. { _M_w |= __x._M_w; }
  318. void
  319. _M_do_xor(const _Base_bitset<1>& __x)
  320. { _M_w ^= __x._M_w; }
  321. void
  322. _M_do_left_shift(size_t __shift)
  323. { _M_w <<= __shift; }
  324. void
  325. _M_do_right_shift(size_t __shift)
  326. { _M_w >>= __shift; }
  327. void
  328. _M_do_flip()
  329. { _M_w = ~_M_w; }
  330. void
  331. _M_do_set()
  332. { _M_w = ~static_cast<_WordT>(0); }
  333. void
  334. _M_do_reset()
  335. { _M_w = 0; }
  336. bool
  337. _M_is_equal(const _Base_bitset<1>& __x) const
  338. { return _M_w == __x._M_w; }
  339. size_t
  340. _M_are_all_aux() const
  341. { return __builtin_popcountl(_M_w); }
  342. bool
  343. _M_is_any() const
  344. { return _M_w != 0; }
  345. size_t
  346. _M_do_count() const
  347. { return __builtin_popcountl(_M_w); }
  348. unsigned long
  349. _M_do_to_ulong() const
  350. { return _M_w; }
  351. size_t
  352. _M_do_find_first(size_t __not_found) const
  353. {
  354. if (_M_w != 0)
  355. return __builtin_ctzl(_M_w);
  356. else
  357. return __not_found;
  358. }
  359. // find the next "on" bit that follows "prev"
  360. size_t
  361. _M_do_find_next(size_t __prev, size_t __not_found) const
  362. {
  363. ++__prev;
  364. if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
  365. return __not_found;
  366. _WordT __x = _M_w >> __prev;
  367. if (__x != 0)
  368. return __builtin_ctzl(__x) + __prev;
  369. else
  370. return __not_found;
  371. }
  372. };
  373. /**
  374. * Base class, specialization for no storage (zero-length %bitset).
  375. *
  376. * See documentation for bitset.
  377. */
  378. template<>
  379. struct _Base_bitset<0>
  380. {
  381. typedef unsigned long _WordT;
  382. _Base_bitset()
  383. { }
  384. _Base_bitset(unsigned long)
  385. { }
  386. static size_t
  387. _S_whichword(size_t __pos )
  388. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  389. static size_t
  390. _S_whichbyte(size_t __pos )
  391. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  392. static size_t
  393. _S_whichbit(size_t __pos )
  394. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  395. static _WordT
  396. _S_maskbit(size_t __pos )
  397. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  398. // This would normally give access to the data. The bounds-checking
  399. // in the bitset class will prevent the user from getting this far,
  400. // but (1) it must still return an lvalue to compile, and (2) the
  401. // user might call _Unchecked_set directly, in which case this /needs/
  402. // to fail. Let's not penalize zero-length users unless they actually
  403. // make an unchecked call; all the memory ugliness is therefore
  404. // localized to this single should-never-get-this-far function.
  405. _WordT&
  406. _M_getword(size_t) const
  407. {
  408. __throw_out_of_range(__N("_Base_bitset::_M_getword"));
  409. return *new _WordT;
  410. }
  411. _WordT
  412. _M_hiword() const
  413. { return 0; }
  414. void
  415. _M_do_and(const _Base_bitset<0>&)
  416. { }
  417. void
  418. _M_do_or(const _Base_bitset<0>&)
  419. { }
  420. void
  421. _M_do_xor(const _Base_bitset<0>&)
  422. { }
  423. void
  424. _M_do_left_shift(size_t)
  425. { }
  426. void
  427. _M_do_right_shift(size_t)
  428. { }
  429. void
  430. _M_do_flip()
  431. { }
  432. void
  433. _M_do_set()
  434. { }
  435. void
  436. _M_do_reset()
  437. { }
  438. // Are all empty bitsets equal to each other? Are they equal to
  439. // themselves? How to compare a thing which has no state? What is
  440. // the sound of one zero-length bitset clapping?
  441. bool
  442. _M_is_equal(const _Base_bitset<0>&) const
  443. { return true; }
  444. size_t
  445. _M_are_all_aux() const
  446. { return 0; }
  447. bool
  448. _M_is_any() const
  449. { return false; }
  450. size_t
  451. _M_do_count() const
  452. { return 0; }
  453. unsigned long
  454. _M_do_to_ulong() const
  455. { return 0; }
  456. // Normally "not found" is the size, but that could also be
  457. // misinterpreted as an index in this corner case. Oh well.
  458. size_t
  459. _M_do_find_first(size_t) const
  460. { return 0; }
  461. size_t
  462. _M_do_find_next(size_t, size_t) const
  463. { return 0; }
  464. };
  465. // Helper class to zero out the unused high-order bits in the highest word.
  466. template<size_t _Extrabits>
  467. struct _Sanitize
  468. {
  469. static void _S_do_sanitize(unsigned long& __val)
  470. { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
  471. };
  472. template<>
  473. struct _Sanitize<0>
  474. { static void _S_do_sanitize(unsigned long) {} };
  475. /**
  476. * @brief The %bitset class represents a @e fixed-size sequence of bits.
  477. *
  478. * @ingroup containers
  479. *
  480. * (Note that %bitset does @e not meet the formal requirements of a
  481. * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
  482. *
  483. * The template argument, @a Nb, may be any non-negative number,
  484. * specifying the number of bits (e.g., "0", "12", "1024*1024").
  485. *
  486. * In the general unoptimized case, storage is allocated in word-sized
  487. * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
  488. * words will be used for storage. B - Nb%B bits are unused. (They are
  489. * the high-order bits in the highest word.) It is a class invariant
  490. * that those unused bits are always zero.
  491. *
  492. * If you think of %bitset as "a simple array of bits," be aware that
  493. * your mental picture is reversed: a %bitset behaves the same way as
  494. * bits in integers do, with the bit at index 0 in the "least significant
  495. * / right-hand" position, and the bit at index Nb-1 in the "most
  496. * significant / left-hand" position. Thus, unlike other containers, a
  497. * %bitset's index "counts from right to left," to put it very loosely.
  498. *
  499. * This behavior is preserved when translating to and from strings. For
  500. * example, the first line of the following program probably prints
  501. * "b('a') is 0001100001" on a modern ASCII system.
  502. *
  503. * @code
  504. * #include <bitset>
  505. * #include <iostream>
  506. * #include <sstream>
  507. *
  508. * using namespace std;
  509. *
  510. * int main()
  511. * {
  512. * long a = 'a';
  513. * bitset<10> b(a);
  514. *
  515. * cout << "b('a') is " << b << endl;
  516. *
  517. * ostringstream s;
  518. * s << b;
  519. * string str = s.str();
  520. * cout << "index 3 in the string is " << str[3] << " but\n"
  521. * << "index 3 in the bitset is " << b[3] << endl;
  522. * }
  523. * @endcode
  524. *
  525. * Also see:
  526. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
  527. * for a description of extensions.
  528. *
  529. * Most of the actual code isn't contained in %bitset<> itself, but in the
  530. * base class _Base_bitset. The base class works with whole words, not with
  531. * individual bits. This allows us to specialize _Base_bitset for the
  532. * important special case where the %bitset is only a single word.
  533. *
  534. * Extra confusion can result due to the fact that the storage for
  535. * _Base_bitset @e is a regular array, and is indexed as such. This is
  536. * carefully encapsulated.
  537. */
  538. template<size_t _Nb>
  539. class bitset
  540. : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
  541. {
  542. private:
  543. typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
  544. typedef unsigned long _WordT;
  545. void
  546. _M_do_sanitize()
  547. {
  548. _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
  549. _S_do_sanitize(this->_M_hiword());
  550. }
  551. public:
  552. /**
  553. * This encapsulates the concept of a single bit. An instance of this
  554. * class is a proxy for an actual bit; this way the individual bit
  555. * operations are done as faster word-size bitwise instructions.
  556. *
  557. * Most users will never need to use this class directly; conversions
  558. * to and from bool are automatic and should be transparent. Overloaded
  559. * operators help to preserve the illusion.
  560. *
  561. * (On a typical system, this "bit %reference" is 64 times the size of
  562. * an actual bit. Ha.)
  563. */
  564. class reference
  565. {
  566. friend class bitset;
  567. _WordT *_M_wp;
  568. size_t _M_bpos;
  569. // left undefined
  570. reference();
  571. public:
  572. reference(bitset& __b, size_t __pos)
  573. {
  574. _M_wp = &__b._M_getword(__pos);
  575. _M_bpos = _Base::_S_whichbit(__pos);
  576. }
  577. ~reference()
  578. { }
  579. // For b[i] = __x;
  580. reference&
  581. operator=(bool __x)
  582. {
  583. if (__x)
  584. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  585. else
  586. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  587. return *this;
  588. }
  589. // For b[i] = b[__j];
  590. reference&
  591. operator=(const reference& __j)
  592. {
  593. if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
  594. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  595. else
  596. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  597. return *this;
  598. }
  599. // Flips the bit
  600. bool
  601. operator~() const
  602. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
  603. // For __x = b[i];
  604. operator bool() const
  605. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
  606. // For b[i].flip();
  607. reference&
  608. flip()
  609. {
  610. *_M_wp ^= _Base::_S_maskbit(_M_bpos);
  611. return *this;
  612. }
  613. };
  614. friend class reference;
  615. // 23.3.5.1 constructors:
  616. /// All bits set to zero.
  617. bitset()
  618. { }
  619. /// Initial bits bitwise-copied from a single word (others set to zero).
  620. bitset(unsigned long __val)
  621. : _Base(__val)
  622. { _M_do_sanitize(); }
  623. /**
  624. * @brief Use a subset of a string.
  625. * @param s A string of '0' and '1' characters.
  626. * @param position Index of the first character in @a s to use;
  627. * defaults to zero.
  628. * @throw std::out_of_range If @a pos is bigger the size of @a s.
  629. * @throw std::invalid_argument If a character appears in the string
  630. * which is neither '0' nor '1'.
  631. */
  632. template<class _CharT, class _Traits, class _Alloc>
  633. explicit
  634. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  635. size_t __position = 0)
  636. : _Base()
  637. {
  638. if (__position > __s.size())
  639. __throw_out_of_range(__N("bitset::bitset initial position "
  640. "not valid"));
  641. _M_copy_from_string(__s, __position,
  642. std::basic_string<_CharT, _Traits, _Alloc>::npos,
  643. _CharT('0'), _CharT('1'));
  644. }
  645. /**
  646. * @brief Use a subset of a string.
  647. * @param s A string of '0' and '1' characters.
  648. * @param position Index of the first character in @a s to use.
  649. * @param n The number of characters to copy.
  650. * @throw std::out_of_range If @a pos is bigger the size of @a s.
  651. * @throw std::invalid_argument If a character appears in the string
  652. * which is neither '0' nor '1'.
  653. */
  654. template<class _CharT, class _Traits, class _Alloc>
  655. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  656. size_t __position, size_t __n)
  657. : _Base()
  658. {
  659. if (__position > __s.size())
  660. __throw_out_of_range(__N("bitset::bitset initial position "
  661. "not valid"));
  662. _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
  663. }
  664. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  665. // 396. what are characters zero and one.
  666. template<class _CharT, class _Traits, class _Alloc>
  667. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  668. size_t __position, size_t __n,
  669. _CharT __zero, _CharT __one = _CharT('1'))
  670. : _Base()
  671. {
  672. if (__position > __s.size())
  673. __throw_out_of_range(__N("bitset::bitset initial position "
  674. "not valid"));
  675. _M_copy_from_string(__s, __position, __n, __zero, __one);
  676. }
  677. // 23.3.5.2 bitset operations:
  678. //@{
  679. /**
  680. * @brief Operations on bitsets.
  681. * @param rhs A same-sized bitset.
  682. *
  683. * These should be self-explanatory.
  684. */
  685. bitset<_Nb>&
  686. operator&=(const bitset<_Nb>& __rhs)
  687. {
  688. this->_M_do_and(__rhs);
  689. return *this;
  690. }
  691. bitset<_Nb>&
  692. operator|=(const bitset<_Nb>& __rhs)
  693. {
  694. this->_M_do_or(__rhs);
  695. return *this;
  696. }
  697. bitset<_Nb>&
  698. operator^=(const bitset<_Nb>& __rhs)
  699. {
  700. this->_M_do_xor(__rhs);
  701. return *this;
  702. }
  703. //@}
  704. //@{
  705. /**
  706. * @brief Operations on bitsets.
  707. * @param position The number of places to shift.
  708. *
  709. * These should be self-explanatory.
  710. */
  711. bitset<_Nb>&
  712. operator<<=(size_t __position)
  713. {
  714. if (__builtin_expect(__position < _Nb, 1))
  715. {
  716. this->_M_do_left_shift(__position);
  717. this->_M_do_sanitize();
  718. }
  719. else
  720. this->_M_do_reset();
  721. return *this;
  722. }
  723. bitset<_Nb>&
  724. operator>>=(size_t __position)
  725. {
  726. if (__builtin_expect(__position < _Nb, 1))
  727. {
  728. this->_M_do_right_shift(__position);
  729. this->_M_do_sanitize();
  730. }
  731. else
  732. this->_M_do_reset();
  733. return *this;
  734. }
  735. //@}
  736. //@{
  737. /**
  738. * These versions of single-bit set, reset, flip, and test are
  739. * extensions from the SGI version. They do no range checking.
  740. * @ingroup SGIextensions
  741. */
  742. bitset<_Nb>&
  743. _Unchecked_set(size_t __pos)
  744. {
  745. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  746. return *this;
  747. }
  748. bitset<_Nb>&
  749. _Unchecked_set(size_t __pos, int __val)
  750. {
  751. if (__val)
  752. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  753. else
  754. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  755. return *this;
  756. }
  757. bitset<_Nb>&
  758. _Unchecked_reset(size_t __pos)
  759. {
  760. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  761. return *this;
  762. }
  763. bitset<_Nb>&
  764. _Unchecked_flip(size_t __pos)
  765. {
  766. this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
  767. return *this;
  768. }
  769. bool
  770. _Unchecked_test(size_t __pos) const
  771. { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
  772. != static_cast<_WordT>(0)); }
  773. //@}
  774. // Set, reset, and flip.
  775. /**
  776. * @brief Sets every bit to true.
  777. */
  778. bitset<_Nb>&
  779. set()
  780. {
  781. this->_M_do_set();
  782. this->_M_do_sanitize();
  783. return *this;
  784. }
  785. /**
  786. * @brief Sets a given bit to a particular value.
  787. * @param position The index of the bit.
  788. * @param val Either true or false, defaults to true.
  789. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  790. */
  791. bitset<_Nb>&
  792. set(size_t __position, bool __val = true)
  793. {
  794. if (__position >= _Nb)
  795. __throw_out_of_range(__N("bitset::set"));
  796. return _Unchecked_set(__position, __val);
  797. }
  798. /**
  799. * @brief Sets every bit to false.
  800. */
  801. bitset<_Nb>&
  802. reset()
  803. {
  804. this->_M_do_reset();
  805. return *this;
  806. }
  807. /**
  808. * @brief Sets a given bit to false.
  809. * @param position The index of the bit.
  810. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  811. *
  812. * Same as writing @c set(pos,false).
  813. */
  814. bitset<_Nb>&
  815. reset(size_t __position)
  816. {
  817. if (__position >= _Nb)
  818. __throw_out_of_range(__N("bitset::reset"));
  819. return _Unchecked_reset(__position);
  820. }
  821. /**
  822. * @brief Toggles every bit to its opposite value.
  823. */
  824. bitset<_Nb>&
  825. flip()
  826. {
  827. this->_M_do_flip();
  828. this->_M_do_sanitize();
  829. return *this;
  830. }
  831. /**
  832. * @brief Toggles a given bit to its opposite value.
  833. * @param position The index of the bit.
  834. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  835. */
  836. bitset<_Nb>&
  837. flip(size_t __position)
  838. {
  839. if (__position >= _Nb)
  840. __throw_out_of_range(__N("bitset::flip"));
  841. return _Unchecked_flip(__position);
  842. }
  843. /// See the no-argument flip().
  844. bitset<_Nb>
  845. operator~() const
  846. { return bitset<_Nb>(*this).flip(); }
  847. //@{
  848. /**
  849. * @brief Array-indexing support.
  850. * @param position Index into the %bitset.
  851. * @return A bool for a 'const %bitset'. For non-const bitsets, an
  852. * instance of the reference proxy class.
  853. * @note These operators do no range checking and throw no exceptions,
  854. * as required by DR 11 to the standard.
  855. *
  856. * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
  857. * resolves DR 11 (items 1 and 2), but does not do the range-checking
  858. * required by that DR's resolution. -pme
  859. * The DR has since been changed: range-checking is a precondition
  860. * (users' responsibility), and these functions must not throw. -pme
  861. */
  862. reference
  863. operator[](size_t __position)
  864. { return reference(*this,__position); }
  865. bool
  866. operator[](size_t __position) const
  867. { return _Unchecked_test(__position); }
  868. //@}
  869. /**
  870. * @brief Returns a numerical interpretation of the %bitset.
  871. * @return The integral equivalent of the bits.
  872. * @throw std::overflow_error If there are too many bits to be
  873. * represented in an @c unsigned @c long.
  874. */
  875. unsigned long
  876. to_ulong() const
  877. { return this->_M_do_to_ulong(); }
  878. /**
  879. * @brief Returns a character interpretation of the %bitset.
  880. * @return The string equivalent of the bits.
  881. *
  882. * Note the ordering of the bits: decreasing character positions
  883. * correspond to increasing bit positions (see the main class notes for
  884. * an example).
  885. */
  886. template<class _CharT, class _Traits, class _Alloc>
  887. std::basic_string<_CharT, _Traits, _Alloc>
  888. to_string() const
  889. {
  890. std::basic_string<_CharT, _Traits, _Alloc> __result;
  891. _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
  892. return __result;
  893. }
  894. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  895. // 396. what are characters zero and one.
  896. template<class _CharT, class _Traits, class _Alloc>
  897. std::basic_string<_CharT, _Traits, _Alloc>
  898. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  899. {
  900. std::basic_string<_CharT, _Traits, _Alloc> __result;
  901. _M_copy_to_string(__result, __zero, __one);
  902. return __result;
  903. }
  904. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  905. // 434. bitset::to_string() hard to use.
  906. template<class _CharT, class _Traits>
  907. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  908. to_string() const
  909. { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
  910. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  911. // 853. to_string needs updating with zero and one.
  912. template<class _CharT, class _Traits>
  913. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  914. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  915. { return to_string<_CharT, _Traits,
  916. std::allocator<_CharT> >(__zero, __one); }
  917. template<class _CharT>
  918. std::basic_string<_CharT, std::char_traits<_CharT>,
  919. std::allocator<_CharT> >
  920. to_string() const
  921. {
  922. return to_string<_CharT, std::char_traits<_CharT>,
  923. std::allocator<_CharT> >();
  924. }
  925. template<class _CharT>
  926. std::basic_string<_CharT, std::char_traits<_CharT>,
  927. std::allocator<_CharT> >
  928. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  929. {
  930. return to_string<_CharT, std::char_traits<_CharT>,
  931. std::allocator<_CharT> >(__zero, __one);
  932. }
  933. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  934. to_string() const
  935. {
  936. return to_string<char, std::char_traits<char>,
  937. std::allocator<char> >();
  938. }
  939. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  940. to_string(char __zero, char __one = '1') const
  941. {
  942. return to_string<char, std::char_traits<char>,
  943. std::allocator<char> >(__zero, __one);
  944. }
  945. // Helper functions for string operations.
  946. template<class _CharT, class _Traits>
  947. void
  948. _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
  949. _CharT, _CharT);
  950. template<class _CharT, class _Traits, class _Alloc>
  951. void
  952. _M_copy_from_string(const std::basic_string<_CharT,
  953. _Traits, _Alloc>& __s, size_t __pos, size_t __n,
  954. _CharT __zero, _CharT __one)
  955. { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
  956. __zero, __one); }
  957. template<class _CharT, class _Traits, class _Alloc>
  958. void
  959. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
  960. _CharT, _CharT) const;
  961. // NB: Backward compat.
  962. template<class _CharT, class _Traits, class _Alloc>
  963. void
  964. _M_copy_from_string(const std::basic_string<_CharT,
  965. _Traits, _Alloc>& __s, size_t __pos, size_t __n)
  966. { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
  967. template<class _CharT, class _Traits, class _Alloc>
  968. void
  969. _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
  970. { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
  971. /// Returns the number of bits which are set.
  972. size_t
  973. count() const
  974. { return this->_M_do_count(); }
  975. /// Returns the total number of bits.
  976. size_t
  977. size() const
  978. { return _Nb; }
  979. //@{
  980. /// These comparisons for equality/inequality are, well, @e bitwise.
  981. bool
  982. operator==(const bitset<_Nb>& __rhs) const
  983. { return this->_M_is_equal(__rhs); }
  984. bool
  985. operator!=(const bitset<_Nb>& __rhs) const
  986. { return !this->_M_is_equal(__rhs); }
  987. //@}
  988. /**
  989. * @brief Tests the value of a bit.
  990. * @param position The index of a bit.
  991. * @return The value at @a pos.
  992. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  993. */
  994. bool
  995. test(size_t __position) const
  996. {
  997. if (__position >= _Nb)
  998. __throw_out_of_range(__N("bitset::test"));
  999. return _Unchecked_test(__position);
  1000. }
  1001. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1002. // DR 693. std::bitset::all() missing.
  1003. /**
  1004. * @brief Tests whether all the bits are on.
  1005. * @return True if all the bits are set.
  1006. */
  1007. bool
  1008. all() const
  1009. { return this->_M_are_all_aux() == _Nb; }
  1010. /**
  1011. * @brief Tests whether any of the bits are on.
  1012. * @return True if at least one bit is set.
  1013. */
  1014. bool
  1015. any() const
  1016. { return this->_M_is_any(); }
  1017. /**
  1018. * @brief Tests whether any of the bits are on.
  1019. * @return True if none of the bits are set.
  1020. */
  1021. bool
  1022. none() const
  1023. { return !this->_M_is_any(); }
  1024. //@{
  1025. /// Self-explanatory.
  1026. bitset<_Nb>
  1027. operator<<(size_t __position) const
  1028. { return bitset<_Nb>(*this) <<= __position; }
  1029. bitset<_Nb>
  1030. operator>>(size_t __position) const
  1031. { return bitset<_Nb>(*this) >>= __position; }
  1032. //@}
  1033. /**
  1034. * @brief Finds the index of the first "on" bit.
  1035. * @return The index of the first bit set, or size() if not found.
  1036. * @ingroup SGIextensions
  1037. * @sa _Find_next
  1038. */
  1039. size_t
  1040. _Find_first() const
  1041. { return this->_M_do_find_first(_Nb); }
  1042. /**
  1043. * @brief Finds the index of the next "on" bit after prev.
  1044. * @return The index of the next bit set, or size() if not found.
  1045. * @param prev Where to start searching.
  1046. * @ingroup SGIextensions
  1047. * @sa _Find_first
  1048. */
  1049. size_t
  1050. _Find_next(size_t __prev ) const
  1051. { return this->_M_do_find_next(__prev, _Nb); }
  1052. };
  1053. // Definitions of non-inline member functions.
  1054. template<size_t _Nb>
  1055. template<class _CharT, class _Traits>
  1056. void
  1057. bitset<_Nb>::
  1058. _M_copy_from_ptr(const _CharT* __s, size_t __len,
  1059. size_t __pos, size_t __n, _CharT __zero, _CharT __one)
  1060. {
  1061. reset();
  1062. const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
  1063. for (size_t __i = __nbits; __i > 0; --__i)
  1064. {
  1065. const _CharT __c = __s[__pos + __nbits - __i];
  1066. if (_Traits::eq(__c, __zero))
  1067. ;
  1068. else if (_Traits::eq(__c, __one))
  1069. _Unchecked_set(__i - 1);
  1070. else
  1071. __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
  1072. }
  1073. }
  1074. template<size_t _Nb>
  1075. template<class _CharT, class _Traits, class _Alloc>
  1076. void
  1077. bitset<_Nb>::
  1078. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
  1079. _CharT __zero, _CharT __one) const
  1080. {
  1081. __s.assign(_Nb, __zero);
  1082. for (size_t __i = _Nb; __i > 0; --__i)
  1083. if (_Unchecked_test(__i - 1))
  1084. _Traits::assign(__s[_Nb - __i], __one);
  1085. }
  1086. // 23.3.5.3 bitset operations:
  1087. //@{
  1088. /**
  1089. * @brief Global bitwise operations on bitsets.
  1090. * @param x A bitset.
  1091. * @param y A bitset of the same size as @a x.
  1092. * @return A new bitset.
  1093. *
  1094. * These should be self-explanatory.
  1095. */
  1096. template<size_t _Nb>
  1097. inline bitset<_Nb>
  1098. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  1099. {
  1100. bitset<_Nb> __result(__x);
  1101. __result &= __y;
  1102. return __result;
  1103. }
  1104. template<size_t _Nb>
  1105. inline bitset<_Nb>
  1106. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  1107. {
  1108. bitset<_Nb> __result(__x);
  1109. __result |= __y;
  1110. return __result;
  1111. }
  1112. template <size_t _Nb>
  1113. inline bitset<_Nb>
  1114. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  1115. {
  1116. bitset<_Nb> __result(__x);
  1117. __result ^= __y;
  1118. return __result;
  1119. }
  1120. //@}
  1121. //@{
  1122. /**
  1123. * @brief Global I/O operators for bitsets.
  1124. *
  1125. * Direct I/O between streams and bitsets is supported. Output is
  1126. * straightforward. Input will skip whitespace, only accept '0' and '1'
  1127. * characters, and will only extract as many digits as the %bitset will
  1128. * hold.
  1129. */
  1130. template<class _CharT, class _Traits, size_t _Nb>
  1131. std::basic_istream<_CharT, _Traits>&
  1132. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  1133. {
  1134. typedef typename _Traits::char_type char_type;
  1135. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1136. typedef typename __istream_type::ios_base __ios_base;
  1137. std::basic_string<_CharT, _Traits> __tmp;
  1138. __tmp.reserve(_Nb);
  1139. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1140. // 303. Bitset input operator underspecified
  1141. const char_type __zero = __is.widen('0');
  1142. const char_type __one = __is.widen('1');
  1143. typename __ios_base::iostate __state = __ios_base::goodbit;
  1144. typename __istream_type::sentry __sentry(__is);
  1145. if (__sentry)
  1146. {
  1147. __try
  1148. {
  1149. for (size_t __i = _Nb; __i > 0; --__i)
  1150. {
  1151. static typename _Traits::int_type __eof = _Traits::eof();
  1152. typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
  1153. if (_Traits::eq_int_type(__c1, __eof))
  1154. {
  1155. __state |= __ios_base::eofbit;
  1156. break;
  1157. }
  1158. else
  1159. {
  1160. const char_type __c2 = _Traits::to_char_type(__c1);
  1161. if (_Traits::eq(__c2, __zero))
  1162. __tmp.push_back(__zero);
  1163. else if (_Traits::eq(__c2, __one))
  1164. __tmp.push_back(__one);
  1165. else if (_Traits::
  1166. eq_int_type(__is.rdbuf()->sputbackc(__c2),
  1167. __eof))
  1168. {
  1169. __state |= __ios_base::failbit;
  1170. break;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. __catch(__cxxabiv1::__forced_unwind&)
  1176. {
  1177. __is._M_setstate(__ios_base::badbit);
  1178. __throw_exception_again;
  1179. }
  1180. __catch(...)
  1181. { __is._M_setstate(__ios_base::badbit); }
  1182. }
  1183. if (__tmp.empty() && _Nb)
  1184. __state |= __ios_base::failbit;
  1185. else
  1186. __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
  1187. __zero, __one);
  1188. if (__state)
  1189. __is.setstate(__state);
  1190. return __is;
  1191. }
  1192. template <class _CharT, class _Traits, size_t _Nb>
  1193. std::basic_ostream<_CharT, _Traits>&
  1194. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1195. const bitset<_Nb>& __x)
  1196. {
  1197. std::basic_string<_CharT, _Traits> __tmp;
  1198. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1199. // 396. what are characters zero and one.
  1200. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
  1201. __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
  1202. return __os << __tmp;
  1203. }
  1204. //@}
  1205. _GLIBCXX_END_NESTED_NAMESPACE
  1206. #undef _GLIBCXX_BITSET_WORDS
  1207. #undef _GLIBCXX_BITSET_BITS_PER_WORD
  1208. #ifdef _GLIBCXX_DEBUG
  1209. # include <debug/bitset>
  1210. #endif
  1211. #endif /* _GLIBCXX_BITSET */