dynamic_bitset.hpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. // -----------------------------------------------------------
  2. //
  3. // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
  4. // Copyright (c) 2003-2006, 2008 Gennaro Prota
  5. // Copyright (c) 2014 Ahmed Charles
  6. //
  7. // Copyright (c) 2014 Glen Joseph Fernandes
  8. // glenfe at live dot com
  9. // Copyright (c) 2014 Riccardo Marcangelo
  10. //
  11. // Distributed under the Boost Software License, Version 1.0.
  12. // (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. //
  15. // -----------------------------------------------------------
  16. #ifndef BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
  17. #define BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
  18. #include <assert.h>
  19. #include <string>
  20. #include <stdexcept>
  21. #include <algorithm>
  22. #include <vector>
  23. #include <climits> // for CHAR_BIT
  24. #include "boost/dynamic_bitset/config.hpp"
  25. #ifndef BOOST_NO_STD_LOCALE
  26. # include <locale>
  27. #endif
  28. #if defined(BOOST_OLD_IOSTREAMS)
  29. # include <iostream.h>
  30. # include <ctype.h> // for isspace
  31. #else
  32. # include <istream>
  33. # include <ostream>
  34. #endif
  35. #include "boost/dynamic_bitset_fwd.hpp"
  36. #include "boost/detail/dynamic_bitset.hpp"
  37. #include "boost/detail/iterator.hpp" // used to implement append(Iter, Iter)
  38. #include "boost/move/move.hpp"
  39. #include "boost/limits.hpp"
  40. #include "boost/pending/lowest_bit.hpp"
  41. #include "boost/static_assert.hpp"
  42. #include "boost/utility/addressof.hpp"
  43. #include "boost/detail/no_exceptions_support.hpp"
  44. #include "boost/throw_exception.hpp"
  45. namespace boost {
  46. template <typename Block, typename Allocator>
  47. class dynamic_bitset
  48. {
  49. // Portability note: member function templates are defined inside
  50. // this class definition to avoid problems with VC++. Similarly,
  51. // with the member functions of nested classes.
  52. //
  53. // [October 2008: the note above is mostly historical; new versions
  54. // of VC++ are likely able to digest a more drinking form of the
  55. // code; but changing it now is probably not worth the risks...]
  56. BOOST_STATIC_ASSERT((bool)detail::dynamic_bitset_impl::allowed_block_type<Block>::value);
  57. typedef std::vector<Block, Allocator> buffer_type;
  58. public:
  59. typedef Block block_type;
  60. typedef Allocator allocator_type;
  61. typedef std::size_t size_type;
  62. typedef typename buffer_type::size_type block_width_type;
  63. BOOST_STATIC_CONSTANT(block_width_type, bits_per_block = (std::numeric_limits<Block>::digits));
  64. BOOST_STATIC_CONSTANT(size_type, npos = static_cast<size_type>(-1));
  65. public:
  66. // A proxy class to simulate lvalues of bit type.
  67. //
  68. class reference
  69. {
  70. friend class dynamic_bitset<Block, Allocator>;
  71. // the one and only non-copy ctor
  72. reference(block_type & b, block_width_type pos)
  73. :m_block(b),
  74. m_mask( (assert(pos < bits_per_block),
  75. block_type(1) << pos )
  76. )
  77. { }
  78. void operator&(); // left undefined
  79. public:
  80. // copy constructor: compiler generated
  81. operator bool() const { return (m_block & m_mask) != 0; }
  82. bool operator~() const { return (m_block & m_mask) == 0; }
  83. reference& flip() { do_flip(); return *this; }
  84. reference& operator=(bool x) { do_assign(x); return *this; } // for b[i] = x
  85. reference& operator=(const reference& rhs) { do_assign(rhs); return *this; } // for b[i] = b[j]
  86. reference& operator|=(bool x) { if (x) do_set(); return *this; }
  87. reference& operator&=(bool x) { if (!x) do_reset(); return *this; }
  88. reference& operator^=(bool x) { if (x) do_flip(); return *this; }
  89. reference& operator-=(bool x) { if (x) do_reset(); return *this; }
  90. private:
  91. block_type & m_block;
  92. const block_type m_mask;
  93. void do_set() { m_block |= m_mask; }
  94. void do_reset() { m_block &= ~m_mask; }
  95. void do_flip() { m_block ^= m_mask; }
  96. void do_assign(bool x) { x? do_set() : do_reset(); }
  97. };
  98. typedef bool const_reference;
  99. // constructors, etc.
  100. explicit
  101. dynamic_bitset(const Allocator& alloc = Allocator());
  102. explicit
  103. dynamic_bitset(size_type num_bits, unsigned long value = 0,
  104. const Allocator& alloc = Allocator());
  105. // WARNING: you should avoid using this constructor.
  106. //
  107. // A conversion from string is, in most cases, formatting,
  108. // and should be performed by using operator>>.
  109. //
  110. // NOTE:
  111. // Leave the parentheses around std::basic_string<CharT, Traits, Alloc>::npos.
  112. // g++ 3.2 requires them and probably the standard will - see core issue 325
  113. // NOTE 2:
  114. // split into two constructors because of bugs in MSVC 6.0sp5 with STLport
  115. template <typename CharT, typename Traits, typename Alloc>
  116. dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
  117. typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
  118. typename std::basic_string<CharT, Traits, Alloc>::size_type n,
  119. size_type num_bits = npos,
  120. const Allocator& alloc = Allocator())
  121. :m_bits(alloc),
  122. m_num_bits(0)
  123. {
  124. init_from_string(s, pos, n, num_bits);
  125. }
  126. template <typename CharT, typename Traits, typename Alloc>
  127. explicit
  128. dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
  129. typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0)
  130. :m_bits(Allocator()),
  131. m_num_bits(0)
  132. {
  133. init_from_string(s, pos, (std::basic_string<CharT, Traits, Alloc>::npos),
  134. npos);
  135. }
  136. // The first bit in *first is the least significant bit, and the
  137. // last bit in the block just before *last is the most significant bit.
  138. template <typename BlockInputIterator>
  139. dynamic_bitset(BlockInputIterator first, BlockInputIterator last,
  140. const Allocator& alloc = Allocator())
  141. :m_bits(alloc),
  142. m_num_bits(0)
  143. {
  144. using boost::detail::dynamic_bitset_impl::value_to_type;
  145. using boost::detail::dynamic_bitset_impl::is_numeric;
  146. const value_to_type<
  147. is_numeric<BlockInputIterator>::value> selector;
  148. dispatch_init(first, last, selector);
  149. }
  150. template <typename T>
  151. void dispatch_init(T num_bits, unsigned long value,
  152. detail::dynamic_bitset_impl::value_to_type<true>)
  153. {
  154. init_from_unsigned_long(static_cast<size_type>(num_bits), value);
  155. }
  156. template <typename T>
  157. void dispatch_init(T first, T last,
  158. detail::dynamic_bitset_impl::value_to_type<false>)
  159. {
  160. init_from_block_range(first, last);
  161. }
  162. template <typename BlockIter>
  163. void init_from_block_range(BlockIter first, BlockIter last)
  164. {
  165. assert(m_bits.size() == 0);
  166. m_bits.insert(m_bits.end(), first, last);
  167. m_num_bits = m_bits.size() * bits_per_block;
  168. }
  169. // copy constructor
  170. dynamic_bitset(const dynamic_bitset& b);
  171. ~dynamic_bitset();
  172. void swap(dynamic_bitset& b);
  173. dynamic_bitset& operator=(const dynamic_bitset& b);
  174. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  175. dynamic_bitset(dynamic_bitset&& src);
  176. dynamic_bitset& operator=(dynamic_bitset&& src);
  177. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  178. allocator_type get_allocator() const;
  179. // size changing operations
  180. void resize(size_type num_bits, bool value = false);
  181. void clear();
  182. void push_back(bool bit);
  183. void pop_back();
  184. void append(Block block);
  185. template <typename BlockInputIterator>
  186. void m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag)
  187. {
  188. std::vector<Block, Allocator> v(first, last);
  189. m_append(v.begin(), v.end(), std::random_access_iterator_tag());
  190. }
  191. template <typename BlockInputIterator>
  192. void m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag)
  193. {
  194. assert(first != last);
  195. block_width_type r = count_extra_bits();
  196. std::size_t d = boost::detail::distance(first, last);
  197. m_bits.reserve(num_blocks() + d);
  198. if (r == 0) {
  199. for( ; first != last; ++first)
  200. m_bits.push_back(*first); // could use vector<>::insert()
  201. }
  202. else {
  203. m_highest_block() |= (*first << r);
  204. do {
  205. Block b = *first >> (bits_per_block - r);
  206. ++first;
  207. m_bits.push_back(b | (first==last? 0 : *first << r));
  208. } while (first != last);
  209. }
  210. m_num_bits += bits_per_block * d;
  211. }
  212. template <typename BlockInputIterator>
  213. void append(BlockInputIterator first, BlockInputIterator last) // strong guarantee
  214. {
  215. if (first != last) {
  216. typename detail::iterator_traits<BlockInputIterator>::iterator_category cat;
  217. m_append(first, last, cat);
  218. }
  219. }
  220. // bitset operations
  221. dynamic_bitset& operator&=(const dynamic_bitset& b);
  222. dynamic_bitset& operator|=(const dynamic_bitset& b);
  223. dynamic_bitset& operator^=(const dynamic_bitset& b);
  224. dynamic_bitset& operator-=(const dynamic_bitset& b);
  225. dynamic_bitset& operator<<=(size_type n);
  226. dynamic_bitset& operator>>=(size_type n);
  227. dynamic_bitset operator<<(size_type n) const;
  228. dynamic_bitset operator>>(size_type n) const;
  229. // basic bit operations
  230. dynamic_bitset& set(size_type n, bool val = true);
  231. dynamic_bitset& set();
  232. dynamic_bitset& reset(size_type n);
  233. dynamic_bitset& reset();
  234. dynamic_bitset& flip(size_type n);
  235. dynamic_bitset& flip();
  236. bool test(size_type n) const;
  237. bool test_set(size_type n, bool val = true);
  238. bool all() const;
  239. bool any() const;
  240. bool none() const;
  241. dynamic_bitset operator~() const;
  242. size_type count() const BOOST_NOEXCEPT;
  243. // subscript
  244. reference operator[](size_type pos) {
  245. return reference(m_bits[block_index(pos)], bit_index(pos));
  246. }
  247. bool operator[](size_type pos) const { return test(pos); }
  248. unsigned long to_ulong() const;
  249. size_type size() const BOOST_NOEXCEPT;
  250. size_type num_blocks() const BOOST_NOEXCEPT;
  251. size_type max_size() const BOOST_NOEXCEPT;
  252. bool empty() const BOOST_NOEXCEPT;
  253. size_type capacity() const BOOST_NOEXCEPT;
  254. void reserve(size_type num_bits);
  255. void shrink_to_fit();
  256. bool is_subset_of(const dynamic_bitset& a) const;
  257. bool is_proper_subset_of(const dynamic_bitset& a) const;
  258. bool intersects(const dynamic_bitset & a) const;
  259. // lookup
  260. size_type find_first() const;
  261. size_type find_next(size_type pos) const;
  262. #if !defined BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
  263. // lexicographical comparison
  264. template <typename B, typename A>
  265. friend bool operator==(const dynamic_bitset<B, A>& a,
  266. const dynamic_bitset<B, A>& b);
  267. template <typename B, typename A>
  268. friend bool operator<(const dynamic_bitset<B, A>& a,
  269. const dynamic_bitset<B, A>& b);
  270. template <typename B, typename A>
  271. friend bool oplessthan(const dynamic_bitset<B, A>& a,
  272. const dynamic_bitset<B, A>& b);
  273. template <typename B, typename A, typename BlockOutputIterator>
  274. friend void to_block_range(const dynamic_bitset<B, A>& b,
  275. BlockOutputIterator result);
  276. template <typename BlockIterator, typename B, typename A>
  277. friend void from_block_range(BlockIterator first, BlockIterator last,
  278. dynamic_bitset<B, A>& result);
  279. template <typename CharT, typename Traits, typename B, typename A>
  280. friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,
  281. dynamic_bitset<B, A>& b);
  282. template <typename B, typename A, typename stringT>
  283. friend void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s, bool dump_all);
  284. #endif
  285. public:
  286. // forward declaration for optional zero-copy serialization support
  287. class serialize_impl;
  288. friend class serialize_impl;
  289. private:
  290. BOOST_STATIC_CONSTANT(block_width_type, ulong_width = std::numeric_limits<unsigned long>::digits);
  291. void m_zero_unused_bits();
  292. bool m_check_invariants() const;
  293. size_type m_do_find_from(size_type first_block) const;
  294. block_width_type count_extra_bits() const BOOST_NOEXCEPT { return bit_index(size()); }
  295. static size_type block_index(size_type pos) BOOST_NOEXCEPT { return pos / bits_per_block; }
  296. static block_width_type bit_index(size_type pos) BOOST_NOEXCEPT { return static_cast<block_width_type>(pos % bits_per_block); }
  297. static Block bit_mask(size_type pos) BOOST_NOEXCEPT { return Block(1) << bit_index(pos); }
  298. template <typename CharT, typename Traits, typename Alloc>
  299. void init_from_string(const std::basic_string<CharT, Traits, Alloc>& s,
  300. typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
  301. typename std::basic_string<CharT, Traits, Alloc>::size_type n,
  302. size_type num_bits)
  303. {
  304. assert(pos <= s.size());
  305. typedef typename std::basic_string<CharT, Traits, Alloc> StrT;
  306. typedef typename StrT::traits_type Tr;
  307. const typename StrT::size_type rlen = (std::min)(n, s.size() - pos);
  308. const size_type sz = ( num_bits != npos? num_bits : rlen);
  309. m_bits.resize(calc_num_blocks(sz));
  310. m_num_bits = sz;
  311. BOOST_DYNAMIC_BITSET_CTYPE_FACET(CharT, fac, std::locale());
  312. const CharT one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
  313. const size_type m = num_bits < rlen ? num_bits : rlen;
  314. typename StrT::size_type i = 0;
  315. for( ; i < m; ++i) {
  316. const CharT c = s[(pos + m - 1) - i];
  317. assert( Tr::eq(c, one)
  318. || Tr::eq(c, BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0')) );
  319. if (Tr::eq(c, one))
  320. set(i);
  321. }
  322. }
  323. void init_from_unsigned_long(size_type num_bits,
  324. unsigned long value/*,
  325. const Allocator& alloc*/)
  326. {
  327. assert(m_bits.size() == 0);
  328. m_bits.resize(calc_num_blocks(num_bits));
  329. m_num_bits = num_bits;
  330. typedef unsigned long num_type;
  331. typedef boost::detail::dynamic_bitset_impl
  332. ::shifter<num_type, bits_per_block, ulong_width> shifter;
  333. //if (num_bits == 0)
  334. // return;
  335. // zero out all bits at pos >= num_bits, if any;
  336. // note that: num_bits == 0 implies value == 0
  337. if (num_bits < static_cast<size_type>(ulong_width)) {
  338. const num_type mask = (num_type(1) << num_bits) - 1;
  339. value &= mask;
  340. }
  341. typename buffer_type::iterator it = m_bits.begin();
  342. for( ; value; shifter::left_shift(value), ++it) {
  343. *it = static_cast<block_type>(value);
  344. }
  345. }
  346. BOOST_DYNAMIC_BITSET_PRIVATE:
  347. bool m_unchecked_test(size_type pos) const;
  348. static size_type calc_num_blocks(size_type num_bits);
  349. Block& m_highest_block();
  350. const Block& m_highest_block() const;
  351. buffer_type m_bits;
  352. size_type m_num_bits;
  353. class bit_appender;
  354. friend class bit_appender;
  355. class bit_appender {
  356. // helper for stream >>
  357. // Supplies to the lack of an efficient append at the less
  358. // significant end: bits are actually appended "at left" but
  359. // rearranged in the destructor. From the perspective of
  360. // client code everything works *as if* dynamic_bitset<> had
  361. // an append_at_right() function (eventually throwing the same
  362. // exceptions as push_back) except that the function is in fact
  363. // called bit_appender::do_append().
  364. //
  365. dynamic_bitset & bs;
  366. size_type n;
  367. Block mask;
  368. Block * current;
  369. // not implemented
  370. bit_appender(const bit_appender &);
  371. bit_appender & operator=(const bit_appender &);
  372. public:
  373. bit_appender(dynamic_bitset & r) : bs(r), n(0), mask(0), current(0) {}
  374. ~bit_appender() {
  375. // reverse the order of blocks, shift
  376. // if needed, and then resize
  377. //
  378. std::reverse(bs.m_bits.begin(), bs.m_bits.end());
  379. const block_width_type offs = bit_index(n);
  380. if (offs)
  381. bs >>= (bits_per_block - offs);
  382. bs.resize(n); // doesn't enlarge, so can't throw
  383. assert(bs.m_check_invariants());
  384. }
  385. inline void do_append(bool value) {
  386. if (mask == 0) {
  387. bs.append(Block(0));
  388. current = &bs.m_highest_block();
  389. mask = Block(1) << (bits_per_block - 1);
  390. }
  391. if(value)
  392. *current |= mask;
  393. mask /= 2;
  394. ++n;
  395. }
  396. size_type get_count() const { return n; }
  397. };
  398. };
  399. #if !defined BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  400. template <typename Block, typename Allocator>
  401. const typename dynamic_bitset<Block, Allocator>::block_width_type
  402. dynamic_bitset<Block, Allocator>::bits_per_block;
  403. template <typename Block, typename Allocator>
  404. const typename dynamic_bitset<Block, Allocator>::size_type
  405. dynamic_bitset<Block, Allocator>::npos;
  406. template <typename Block, typename Allocator>
  407. const typename dynamic_bitset<Block, Allocator>::block_width_type
  408. dynamic_bitset<Block, Allocator>::ulong_width;
  409. #endif
  410. // Global Functions:
  411. // comparison
  412. template <typename Block, typename Allocator>
  413. bool operator!=(const dynamic_bitset<Block, Allocator>& a,
  414. const dynamic_bitset<Block, Allocator>& b);
  415. template <typename Block, typename Allocator>
  416. bool operator<=(const dynamic_bitset<Block, Allocator>& a,
  417. const dynamic_bitset<Block, Allocator>& b);
  418. template <typename Block, typename Allocator>
  419. bool operator>(const dynamic_bitset<Block, Allocator>& a,
  420. const dynamic_bitset<Block, Allocator>& b);
  421. template <typename Block, typename Allocator>
  422. bool operator>=(const dynamic_bitset<Block, Allocator>& a,
  423. const dynamic_bitset<Block, Allocator>& b);
  424. // stream operators
  425. #ifdef BOOST_OLD_IOSTREAMS
  426. template <typename Block, typename Allocator>
  427. std::ostream& operator<<(std::ostream& os,
  428. const dynamic_bitset<Block, Allocator>& b);
  429. template <typename Block, typename Allocator>
  430. std::istream& operator>>(std::istream& is, dynamic_bitset<Block,Allocator>& b);
  431. #else
  432. template <typename CharT, typename Traits, typename Block, typename Allocator>
  433. std::basic_ostream<CharT, Traits>&
  434. operator<<(std::basic_ostream<CharT, Traits>& os,
  435. const dynamic_bitset<Block, Allocator>& b);
  436. template <typename CharT, typename Traits, typename Block, typename Allocator>
  437. std::basic_istream<CharT, Traits>&
  438. operator>>(std::basic_istream<CharT, Traits>& is,
  439. dynamic_bitset<Block, Allocator>& b);
  440. #endif
  441. // bitset operations
  442. template <typename Block, typename Allocator>
  443. dynamic_bitset<Block, Allocator>
  444. operator&(const dynamic_bitset<Block, Allocator>& b1,
  445. const dynamic_bitset<Block, Allocator>& b2);
  446. template <typename Block, typename Allocator>
  447. dynamic_bitset<Block, Allocator>
  448. operator|(const dynamic_bitset<Block, Allocator>& b1,
  449. const dynamic_bitset<Block, Allocator>& b2);
  450. template <typename Block, typename Allocator>
  451. dynamic_bitset<Block, Allocator>
  452. operator^(const dynamic_bitset<Block, Allocator>& b1,
  453. const dynamic_bitset<Block, Allocator>& b2);
  454. template <typename Block, typename Allocator>
  455. dynamic_bitset<Block, Allocator>
  456. operator-(const dynamic_bitset<Block, Allocator>& b1,
  457. const dynamic_bitset<Block, Allocator>& b2);
  458. // namespace scope swap
  459. template<typename Block, typename Allocator>
  460. void swap(dynamic_bitset<Block, Allocator>& b1,
  461. dynamic_bitset<Block, Allocator>& b2);
  462. template <typename Block, typename Allocator, typename stringT>
  463. void
  464. to_string(const dynamic_bitset<Block, Allocator>& b, stringT & s);
  465. template <typename Block, typename Allocator, typename BlockOutputIterator>
  466. void
  467. to_block_range(const dynamic_bitset<Block, Allocator>& b,
  468. BlockOutputIterator result);
  469. template <typename BlockIterator, typename B, typename A>
  470. inline void
  471. from_block_range(BlockIterator first, BlockIterator last,
  472. dynamic_bitset<B, A>& result)
  473. {
  474. // PRE: distance(first, last) <= numblocks()
  475. std::copy (first, last, result.m_bits.begin());
  476. }
  477. //=============================================================================
  478. // dynamic_bitset implementation
  479. //-----------------------------------------------------------------------------
  480. // constructors, etc.
  481. template <typename Block, typename Allocator>
  482. dynamic_bitset<Block, Allocator>::dynamic_bitset(const Allocator& alloc)
  483. : m_bits(alloc), m_num_bits(0)
  484. {
  485. }
  486. template <typename Block, typename Allocator>
  487. dynamic_bitset<Block, Allocator>::
  488. dynamic_bitset(size_type num_bits, unsigned long value, const Allocator& alloc)
  489. : m_bits(alloc),
  490. m_num_bits(0)
  491. {
  492. init_from_unsigned_long(num_bits, value);
  493. }
  494. // copy constructor
  495. template <typename Block, typename Allocator>
  496. inline dynamic_bitset<Block, Allocator>::
  497. dynamic_bitset(const dynamic_bitset& b)
  498. : m_bits(b.m_bits), m_num_bits(b.m_num_bits)
  499. {
  500. }
  501. template <typename Block, typename Allocator>
  502. inline dynamic_bitset<Block, Allocator>::
  503. ~dynamic_bitset()
  504. {
  505. assert(m_check_invariants());
  506. }
  507. template <typename Block, typename Allocator>
  508. inline void dynamic_bitset<Block, Allocator>::
  509. swap(dynamic_bitset<Block, Allocator>& b) // no throw
  510. {
  511. std::swap(m_bits, b.m_bits);
  512. std::swap(m_num_bits, b.m_num_bits);
  513. }
  514. template <typename Block, typename Allocator>
  515. dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
  516. operator=(const dynamic_bitset<Block, Allocator>& b)
  517. {
  518. m_bits = b.m_bits;
  519. m_num_bits = b.m_num_bits;
  520. return *this;
  521. }
  522. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  523. template <typename Block, typename Allocator>
  524. inline dynamic_bitset<Block, Allocator>::
  525. dynamic_bitset(dynamic_bitset<Block, Allocator>&& b)
  526. : m_bits(boost::move(b.m_bits)), m_num_bits(boost::move(b.m_num_bits))
  527. {
  528. // Required so that assert(m_check_invariants()); works.
  529. assert((b.m_bits = buffer_type()).empty());
  530. b.m_num_bits = 0;
  531. }
  532. template <typename Block, typename Allocator>
  533. inline dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
  534. operator=(dynamic_bitset<Block, Allocator>&& b)
  535. {
  536. if (boost::addressof(b) == this) { return *this; }
  537. m_bits = boost::move(b.m_bits);
  538. m_num_bits = boost::move(b.m_num_bits);
  539. // Required so that assert(m_check_invariants()); works.
  540. assert((b.m_bits = buffer_type()).empty());
  541. b.m_num_bits = 0;
  542. return *this;
  543. }
  544. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  545. template <typename Block, typename Allocator>
  546. inline typename dynamic_bitset<Block, Allocator>::allocator_type
  547. dynamic_bitset<Block, Allocator>::get_allocator() const
  548. {
  549. return m_bits.get_allocator();
  550. }
  551. //-----------------------------------------------------------------------------
  552. // size changing operations
  553. template <typename Block, typename Allocator>
  554. void dynamic_bitset<Block, Allocator>::
  555. resize(size_type num_bits, bool value) // strong guarantee
  556. {
  557. const size_type old_num_blocks = num_blocks();
  558. const size_type required_blocks = calc_num_blocks(num_bits);
  559. const block_type v = value? ~Block(0) : Block(0);
  560. if (required_blocks != old_num_blocks) {
  561. m_bits.resize(required_blocks, v); // s.g. (copy)
  562. }
  563. // At this point:
  564. //
  565. // - if the buffer was shrunk, we have nothing more to do,
  566. // except a call to m_zero_unused_bits()
  567. //
  568. // - if it was enlarged, all the (used) bits in the new blocks have
  569. // the correct value, but we have not yet touched those bits, if
  570. // any, that were 'unused bits' before enlarging: if value == true,
  571. // they must be set.
  572. if (value && (num_bits > m_num_bits)) {
  573. const block_width_type extra_bits = count_extra_bits();
  574. if (extra_bits) {
  575. assert(old_num_blocks >= 1 && old_num_blocks <= m_bits.size());
  576. // Set them.
  577. m_bits[old_num_blocks - 1] |= (v << extra_bits);
  578. }
  579. }
  580. m_num_bits = num_bits;
  581. m_zero_unused_bits();
  582. }
  583. template <typename Block, typename Allocator>
  584. void dynamic_bitset<Block, Allocator>::
  585. clear() // no throw
  586. {
  587. m_bits.clear();
  588. m_num_bits = 0;
  589. }
  590. template <typename Block, typename Allocator>
  591. void dynamic_bitset<Block, Allocator>::
  592. push_back(bool bit)
  593. {
  594. const size_type sz = size();
  595. resize(sz + 1);
  596. set(sz, bit);
  597. }
  598. template <typename Block, typename Allocator>
  599. void dynamic_bitset<Block, Allocator>::
  600. pop_back()
  601. {
  602. const size_type old_num_blocks = num_blocks();
  603. const size_type required_blocks = calc_num_blocks(m_num_bits - 1);
  604. if (required_blocks != old_num_blocks) {
  605. m_bits.pop_back();
  606. }
  607. --m_num_bits;
  608. m_zero_unused_bits();
  609. }
  610. template <typename Block, typename Allocator>
  611. void dynamic_bitset<Block, Allocator>::
  612. append(Block value) // strong guarantee
  613. {
  614. const block_width_type r = count_extra_bits();
  615. if (r == 0) {
  616. // the buffer is empty, or all blocks are filled
  617. m_bits.push_back(value);
  618. }
  619. else {
  620. m_bits.push_back(value >> (bits_per_block - r));
  621. m_bits[m_bits.size() - 2] |= (value << r); // m_bits.size() >= 2
  622. }
  623. m_num_bits += bits_per_block;
  624. assert(m_check_invariants());
  625. }
  626. //-----------------------------------------------------------------------------
  627. // bitset operations
  628. template <typename Block, typename Allocator>
  629. dynamic_bitset<Block, Allocator>&
  630. dynamic_bitset<Block, Allocator>::operator&=(const dynamic_bitset& rhs)
  631. {
  632. assert(size() == rhs.size());
  633. for (size_type i = 0; i < num_blocks(); ++i)
  634. m_bits[i] &= rhs.m_bits[i];
  635. return *this;
  636. }
  637. template <typename Block, typename Allocator>
  638. dynamic_bitset<Block, Allocator>&
  639. dynamic_bitset<Block, Allocator>::operator|=(const dynamic_bitset& rhs)
  640. {
  641. assert(size() == rhs.size());
  642. for (size_type i = 0; i < num_blocks(); ++i)
  643. m_bits[i] |= rhs.m_bits[i];
  644. //m_zero_unused_bits();
  645. return *this;
  646. }
  647. template <typename Block, typename Allocator>
  648. dynamic_bitset<Block, Allocator>&
  649. dynamic_bitset<Block, Allocator>::operator^=(const dynamic_bitset& rhs)
  650. {
  651. assert(size() == rhs.size());
  652. for (size_type i = 0; i < this->num_blocks(); ++i)
  653. m_bits[i] ^= rhs.m_bits[i];
  654. //m_zero_unused_bits();
  655. return *this;
  656. }
  657. template <typename Block, typename Allocator>
  658. dynamic_bitset<Block, Allocator>&
  659. dynamic_bitset<Block, Allocator>::operator-=(const dynamic_bitset& rhs)
  660. {
  661. assert(size() == rhs.size());
  662. for (size_type i = 0; i < num_blocks(); ++i)
  663. m_bits[i] &= ~rhs.m_bits[i];
  664. //m_zero_unused_bits();
  665. return *this;
  666. }
  667. //
  668. // NOTE:
  669. // Note that the 'if (r != 0)' is crucial to avoid undefined
  670. // behavior when the left hand operand of >> isn't promoted to a
  671. // wider type (because rs would be too large).
  672. //
  673. template <typename Block, typename Allocator>
  674. dynamic_bitset<Block, Allocator>&
  675. dynamic_bitset<Block, Allocator>::operator<<=(size_type n)
  676. {
  677. if (n >= m_num_bits)
  678. return reset();
  679. //else
  680. if (n > 0) {
  681. size_type const last = num_blocks() - 1; // num_blocks() is >= 1
  682. size_type const div = n / bits_per_block; // div is <= last
  683. block_width_type const r = bit_index(n);
  684. block_type * const b = &m_bits[0];
  685. if (r != 0) {
  686. block_width_type const rs = bits_per_block - r;
  687. for (size_type i = last-div; i>0; --i) {
  688. b[i+div] = (b[i] << r) | (b[i-1] >> rs);
  689. }
  690. b[div] = b[0] << r;
  691. }
  692. else {
  693. for (size_type i = last-div; i>0; --i) {
  694. b[i+div] = b[i];
  695. }
  696. b[div] = b[0];
  697. }
  698. // zero out div blocks at the less significant end
  699. std::fill_n(m_bits.begin(), div, static_cast<block_type>(0));
  700. // zero out any 1 bit that flowed into the unused part
  701. m_zero_unused_bits(); // thanks to Lester Gong
  702. }
  703. return *this;
  704. }
  705. //
  706. // NOTE:
  707. // see the comments to operator <<=
  708. //
  709. template <typename B, typename A>
  710. dynamic_bitset<B, A> & dynamic_bitset<B, A>::operator>>=(size_type n) {
  711. if (n >= m_num_bits) {
  712. return reset();
  713. }
  714. //else
  715. if (n>0) {
  716. size_type const last = num_blocks() - 1; // num_blocks() is >= 1
  717. size_type const div = n / bits_per_block; // div is <= last
  718. block_width_type const r = bit_index(n);
  719. block_type * const b = &m_bits[0];
  720. if (r != 0) {
  721. block_width_type const ls = bits_per_block - r;
  722. for (size_type i = div; i < last; ++i) {
  723. b[i-div] = (b[i] >> r) | (b[i+1] << ls);
  724. }
  725. // r bits go to zero
  726. b[last-div] = b[last] >> r;
  727. }
  728. else {
  729. for (size_type i = div; i <= last; ++i) {
  730. b[i-div] = b[i];
  731. }
  732. // note the '<=': the last iteration 'absorbs'
  733. // b[last-div] = b[last] >> 0;
  734. }
  735. // div blocks are zero filled at the most significant end
  736. std::fill_n(m_bits.begin() + (num_blocks()-div), div, static_cast<block_type>(0));
  737. }
  738. return *this;
  739. }
  740. template <typename Block, typename Allocator>
  741. dynamic_bitset<Block, Allocator>
  742. dynamic_bitset<Block, Allocator>::operator<<(size_type n) const
  743. {
  744. dynamic_bitset r(*this);
  745. return r <<= n;
  746. }
  747. template <typename Block, typename Allocator>
  748. dynamic_bitset<Block, Allocator>
  749. dynamic_bitset<Block, Allocator>::operator>>(size_type n) const
  750. {
  751. dynamic_bitset r(*this);
  752. return r >>= n;
  753. }
  754. //-----------------------------------------------------------------------------
  755. // basic bit operations
  756. template <typename Block, typename Allocator>
  757. dynamic_bitset<Block, Allocator>&
  758. dynamic_bitset<Block, Allocator>::set(size_type pos, bool val)
  759. {
  760. assert(pos < m_num_bits);
  761. if (val)
  762. m_bits[block_index(pos)] |= bit_mask(pos);
  763. else
  764. reset(pos);
  765. return *this;
  766. }
  767. template <typename Block, typename Allocator>
  768. dynamic_bitset<Block, Allocator>&
  769. dynamic_bitset<Block, Allocator>::set()
  770. {
  771. std::fill(m_bits.begin(), m_bits.end(), static_cast<Block>(~0));
  772. m_zero_unused_bits();
  773. return *this;
  774. }
  775. template <typename Block, typename Allocator>
  776. dynamic_bitset<Block, Allocator>&
  777. dynamic_bitset<Block, Allocator>::reset(size_type pos)
  778. {
  779. assert(pos < m_num_bits);
  780. #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
  781. // CodeWarrior 8 generates incorrect code when the &=~ is compiled,
  782. // use the |^ variation instead.. <grafik>
  783. m_bits[block_index(pos)] |= bit_mask(pos);
  784. m_bits[block_index(pos)] ^= bit_mask(pos);
  785. #else
  786. m_bits[block_index(pos)] &= ~bit_mask(pos);
  787. #endif
  788. return *this;
  789. }
  790. template <typename Block, typename Allocator>
  791. dynamic_bitset<Block, Allocator>&
  792. dynamic_bitset<Block, Allocator>::reset()
  793. {
  794. std::fill(m_bits.begin(), m_bits.end(), Block(0));
  795. return *this;
  796. }
  797. template <typename Block, typename Allocator>
  798. dynamic_bitset<Block, Allocator>&
  799. dynamic_bitset<Block, Allocator>::flip(size_type pos)
  800. {
  801. assert(pos < m_num_bits);
  802. m_bits[block_index(pos)] ^= bit_mask(pos);
  803. return *this;
  804. }
  805. template <typename Block, typename Allocator>
  806. dynamic_bitset<Block, Allocator>&
  807. dynamic_bitset<Block, Allocator>::flip()
  808. {
  809. for (size_type i = 0; i < num_blocks(); ++i)
  810. m_bits[i] = ~m_bits[i];
  811. m_zero_unused_bits();
  812. return *this;
  813. }
  814. template <typename Block, typename Allocator>
  815. bool dynamic_bitset<Block, Allocator>::m_unchecked_test(size_type pos) const
  816. {
  817. return (m_bits[block_index(pos)] & bit_mask(pos)) != 0;
  818. }
  819. template <typename Block, typename Allocator>
  820. bool dynamic_bitset<Block, Allocator>::test(size_type pos) const
  821. {
  822. assert(pos < m_num_bits);
  823. return m_unchecked_test(pos);
  824. }
  825. template <typename Block, typename Allocator>
  826. bool dynamic_bitset<Block, Allocator>::test_set(size_type pos, bool val)
  827. {
  828. bool const b = test(pos);
  829. if (b != val) {
  830. set(pos, val);
  831. }
  832. return b;
  833. }
  834. template <typename Block, typename Allocator>
  835. bool dynamic_bitset<Block, Allocator>::all() const
  836. {
  837. if (empty()) {
  838. return true;
  839. }
  840. const block_width_type extra_bits = count_extra_bits();
  841. block_type const all_ones = static_cast<Block>(~0);
  842. if (extra_bits == 0) {
  843. for (size_type i = 0, e = num_blocks(); i < e; ++i) {
  844. if (m_bits[i] != all_ones) {
  845. return false;
  846. }
  847. }
  848. } else {
  849. for (size_type i = 0, e = num_blocks() - 1; i < e; ++i) {
  850. if (m_bits[i] != all_ones) {
  851. return false;
  852. }
  853. }
  854. const block_type mask = (block_type(1) << extra_bits) - 1;
  855. if (m_highest_block() != mask) {
  856. return false;
  857. }
  858. }
  859. return true;
  860. }
  861. template <typename Block, typename Allocator>
  862. bool dynamic_bitset<Block, Allocator>::any() const
  863. {
  864. for (size_type i = 0; i < num_blocks(); ++i)
  865. if (m_bits[i])
  866. return true;
  867. return false;
  868. }
  869. template <typename Block, typename Allocator>
  870. inline bool dynamic_bitset<Block, Allocator>::none() const
  871. {
  872. return !any();
  873. }
  874. template <typename Block, typename Allocator>
  875. dynamic_bitset<Block, Allocator>
  876. dynamic_bitset<Block, Allocator>::operator~() const
  877. {
  878. dynamic_bitset b(*this);
  879. b.flip();
  880. return b;
  881. }
  882. template <typename Block, typename Allocator>
  883. typename dynamic_bitset<Block, Allocator>::size_type
  884. dynamic_bitset<Block, Allocator>::count() const BOOST_NOEXCEPT
  885. {
  886. using detail::dynamic_bitset_impl::table_width;
  887. using detail::dynamic_bitset_impl::access_by_bytes;
  888. using detail::dynamic_bitset_impl::access_by_blocks;
  889. using detail::dynamic_bitset_impl::value_to_type;
  890. #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 3) && (__GNUC_PATCHLEVEL__ == 3)
  891. // NOTE: Explicit qualification of "bits_per_block"
  892. // breaks compilation on gcc 4.3.3
  893. enum { no_padding = bits_per_block == CHAR_BIT * sizeof(Block) };
  894. #else
  895. // NOTE: Explicitly qualifying "bits_per_block" to workaround
  896. // regressions of gcc 3.4.x
  897. enum { no_padding =
  898. dynamic_bitset<Block, Allocator>::bits_per_block
  899. == CHAR_BIT * sizeof(Block) };
  900. #endif
  901. enum { enough_table_width = table_width >= CHAR_BIT };
  902. enum { mode = (no_padding && enough_table_width)
  903. ? access_by_bytes
  904. : access_by_blocks };
  905. return do_count(m_bits.begin(), num_blocks(), Block(0),
  906. static_cast<value_to_type<(bool)mode> *>(0));
  907. }
  908. //-----------------------------------------------------------------------------
  909. // conversions
  910. template <typename B, typename A, typename stringT>
  911. void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s,
  912. bool dump_all)
  913. {
  914. typedef typename stringT::traits_type Tr;
  915. typedef typename stringT::value_type Ch;
  916. BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, std::locale());
  917. const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
  918. const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
  919. // Note that this function may access (when
  920. // dump_all == true) bits beyond position size() - 1
  921. typedef typename dynamic_bitset<B, A>::size_type size_type;
  922. const size_type len = dump_all?
  923. dynamic_bitset<B, A>::bits_per_block * b.num_blocks():
  924. b.size();
  925. s.assign (len, zero);
  926. for (size_type i = 0; i < len; ++i) {
  927. if (b.m_unchecked_test(i))
  928. Tr::assign(s[len - 1 - i], one);
  929. }
  930. }
  931. // A comment similar to the one about the constructor from
  932. // basic_string can be done here. Thanks to James Kanze for
  933. // making me (Gennaro) realize this important separation of
  934. // concerns issue, as well as many things about i18n.
  935. //
  936. template <typename Block, typename Allocator, typename stringT>
  937. inline void
  938. to_string(const dynamic_bitset<Block, Allocator>& b, stringT& s)
  939. {
  940. to_string_helper(b, s, false);
  941. }
  942. // Differently from to_string this function dumps out
  943. // every bit of the internal representation (may be
  944. // useful for debugging purposes)
  945. //
  946. template <typename B, typename A, typename stringT>
  947. inline void
  948. dump_to_string(const dynamic_bitset<B, A>& b, stringT& s)
  949. {
  950. to_string_helper(b, s, true /* =dump_all*/);
  951. }
  952. template <typename Block, typename Allocator, typename BlockOutputIterator>
  953. inline void
  954. to_block_range(const dynamic_bitset<Block, Allocator>& b,
  955. BlockOutputIterator result)
  956. {
  957. // note how this copies *all* bits, including the
  958. // unused ones in the last block (which are zero)
  959. std::copy(b.m_bits.begin(), b.m_bits.end(), result);
  960. }
  961. template <typename Block, typename Allocator>
  962. unsigned long dynamic_bitset<Block, Allocator>::
  963. to_ulong() const
  964. {
  965. if (m_num_bits == 0)
  966. return 0; // convention
  967. // Check for overflows. This may be a performance burden on very
  968. // large bitsets but is required by the specification, sorry
  969. if (find_next(ulong_width - 1) != npos)
  970. BOOST_THROW_EXCEPTION(std::overflow_error("boost::dynamic_bitset::to_ulong overflow"));
  971. // Ok, from now on we can be sure there's no "on" bit
  972. // beyond the "allowed" positions
  973. typedef unsigned long result_type;
  974. const size_type maximum_size =
  975. (std::min)(m_num_bits, static_cast<size_type>(ulong_width));
  976. const size_type last_block = block_index( maximum_size - 1 );
  977. assert((last_block * bits_per_block) < static_cast<size_type>(ulong_width));
  978. result_type result = 0;
  979. for (size_type i = 0; i <= last_block; ++i) {
  980. const size_type offset = i * bits_per_block;
  981. result |= (static_cast<result_type>(m_bits[i]) << offset);
  982. }
  983. return result;
  984. }
  985. template <typename Block, typename Allocator>
  986. inline typename dynamic_bitset<Block, Allocator>::size_type
  987. dynamic_bitset<Block, Allocator>::size() const BOOST_NOEXCEPT
  988. {
  989. return m_num_bits;
  990. }
  991. template <typename Block, typename Allocator>
  992. inline typename dynamic_bitset<Block, Allocator>::size_type
  993. dynamic_bitset<Block, Allocator>::num_blocks() const BOOST_NOEXCEPT
  994. {
  995. return m_bits.size();
  996. }
  997. template <typename Block, typename Allocator>
  998. inline typename dynamic_bitset<Block, Allocator>::size_type
  999. dynamic_bitset<Block, Allocator>::max_size() const BOOST_NOEXCEPT
  1000. {
  1001. // Semantics of vector<>::max_size() aren't very clear
  1002. // (see lib issue 197) and many library implementations
  1003. // simply return dummy values, _unrelated_ to the underlying
  1004. // allocator.
  1005. //
  1006. // Given these problems, I was tempted to not provide this
  1007. // function at all but the user could need it if he provides
  1008. // his own allocator.
  1009. //
  1010. const size_type m = detail::dynamic_bitset_impl::
  1011. vector_max_size_workaround(m_bits);
  1012. return m <= (size_type(-1)/bits_per_block) ?
  1013. m * bits_per_block :
  1014. size_type(-1);
  1015. }
  1016. template <typename Block, typename Allocator>
  1017. inline bool dynamic_bitset<Block, Allocator>::empty() const BOOST_NOEXCEPT
  1018. {
  1019. return size() == 0;
  1020. }
  1021. template <typename Block, typename Allocator>
  1022. inline typename dynamic_bitset<Block, Allocator>::size_type
  1023. dynamic_bitset<Block, Allocator>::capacity() const BOOST_NOEXCEPT
  1024. {
  1025. return m_bits.capacity() * bits_per_block;
  1026. }
  1027. template <typename Block, typename Allocator>
  1028. inline void dynamic_bitset<Block, Allocator>::reserve(size_type num_bits)
  1029. {
  1030. m_bits.reserve(calc_num_blocks(num_bits));
  1031. }
  1032. template <typename Block, typename Allocator>
  1033. void dynamic_bitset<Block, Allocator>::shrink_to_fit()
  1034. {
  1035. if (m_bits.size() < m_bits.capacity()) {
  1036. buffer_type(m_bits).swap(m_bits);
  1037. }
  1038. }
  1039. template <typename Block, typename Allocator>
  1040. bool dynamic_bitset<Block, Allocator>::
  1041. is_subset_of(const dynamic_bitset<Block, Allocator>& a) const
  1042. {
  1043. assert(size() == a.size());
  1044. for (size_type i = 0; i < num_blocks(); ++i)
  1045. if (m_bits[i] & ~a.m_bits[i])
  1046. return false;
  1047. return true;
  1048. }
  1049. template <typename Block, typename Allocator>
  1050. bool dynamic_bitset<Block, Allocator>::
  1051. is_proper_subset_of(const dynamic_bitset<Block, Allocator>& a) const
  1052. {
  1053. assert(size() == a.size());
  1054. assert(num_blocks() == a.num_blocks());
  1055. bool proper = false;
  1056. for (size_type i = 0; i < num_blocks(); ++i) {
  1057. const Block & bt = m_bits[i];
  1058. const Block & ba = a.m_bits[i];
  1059. if (bt & ~ba)
  1060. return false; // not a subset at all
  1061. if (ba & ~bt)
  1062. proper = true;
  1063. }
  1064. return proper;
  1065. }
  1066. template <typename Block, typename Allocator>
  1067. bool dynamic_bitset<Block, Allocator>::intersects(const dynamic_bitset & b) const
  1068. {
  1069. size_type common_blocks = num_blocks() < b.num_blocks()
  1070. ? num_blocks() : b.num_blocks();
  1071. for(size_type i = 0; i < common_blocks; ++i) {
  1072. if(m_bits[i] & b.m_bits[i])
  1073. return true;
  1074. }
  1075. return false;
  1076. }
  1077. // --------------------------------
  1078. // lookup
  1079. // look for the first bit "on", starting
  1080. // from the block with index first_block
  1081. //
  1082. template <typename Block, typename Allocator>
  1083. typename dynamic_bitset<Block, Allocator>::size_type
  1084. dynamic_bitset<Block, Allocator>::m_do_find_from(size_type first_block) const
  1085. {
  1086. size_type i = first_block;
  1087. // skip null blocks
  1088. while (i < num_blocks() && m_bits[i] == 0)
  1089. ++i;
  1090. if (i >= num_blocks())
  1091. return npos; // not found
  1092. return i * bits_per_block + static_cast<size_type>(boost::lowest_bit(m_bits[i]));
  1093. }
  1094. template <typename Block, typename Allocator>
  1095. typename dynamic_bitset<Block, Allocator>::size_type
  1096. dynamic_bitset<Block, Allocator>::find_first() const
  1097. {
  1098. return m_do_find_from(0);
  1099. }
  1100. template <typename Block, typename Allocator>
  1101. typename dynamic_bitset<Block, Allocator>::size_type
  1102. dynamic_bitset<Block, Allocator>::find_next(size_type pos) const
  1103. {
  1104. const size_type sz = size();
  1105. if (pos >= (sz-1) || sz == 0)
  1106. return npos;
  1107. ++pos;
  1108. const size_type blk = block_index(pos);
  1109. const block_width_type ind = bit_index(pos);
  1110. // shift bits upto one immediately after current
  1111. const Block fore = m_bits[blk] >> ind;
  1112. return fore?
  1113. pos + static_cast<size_type>(lowest_bit(fore))
  1114. :
  1115. m_do_find_from(blk + 1);
  1116. }
  1117. //-----------------------------------------------------------------------------
  1118. // comparison
  1119. template <typename Block, typename Allocator>
  1120. bool operator==(const dynamic_bitset<Block, Allocator>& a,
  1121. const dynamic_bitset<Block, Allocator>& b)
  1122. {
  1123. return (a.m_num_bits == b.m_num_bits)
  1124. && (a.m_bits == b.m_bits);
  1125. }
  1126. template <typename Block, typename Allocator>
  1127. inline bool operator!=(const dynamic_bitset<Block, Allocator>& a,
  1128. const dynamic_bitset<Block, Allocator>& b)
  1129. {
  1130. return !(a == b);
  1131. }
  1132. template <typename Block, typename Allocator>
  1133. bool operator<(const dynamic_bitset<Block, Allocator>& a,
  1134. const dynamic_bitset<Block, Allocator>& b)
  1135. {
  1136. // assert(a.size() == b.size());
  1137. typedef BOOST_DEDUCED_TYPENAME dynamic_bitset<Block, Allocator>::size_type size_type;
  1138. size_type asize(a.size());
  1139. size_type bsize(b.size());
  1140. if (!bsize)
  1141. {
  1142. return false;
  1143. }
  1144. else if (!asize)
  1145. {
  1146. return true;
  1147. }
  1148. else if (asize == bsize)
  1149. {
  1150. for (size_type ii = a.num_blocks(); ii > 0; --ii)
  1151. {
  1152. size_type i = ii-1;
  1153. if (a.m_bits[i] < b.m_bits[i])
  1154. return true;
  1155. else if (a.m_bits[i] > b.m_bits[i])
  1156. return false;
  1157. }
  1158. return false;
  1159. }
  1160. else
  1161. {
  1162. size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize));
  1163. for (size_type ii = 0; ii < leqsize; ++ii,--asize,--bsize)
  1164. {
  1165. size_type i = asize-1;
  1166. size_type j = bsize-1;
  1167. if (a[i] < b[j])
  1168. return true;
  1169. else if (a[i] > b[j])
  1170. return false;
  1171. }
  1172. return (a.size() < b.size());
  1173. }
  1174. }
  1175. template <typename Block, typename Allocator>
  1176. bool oplessthan(const dynamic_bitset<Block, Allocator>& a,
  1177. const dynamic_bitset<Block, Allocator>& b)
  1178. {
  1179. // assert(a.size() == b.size());
  1180. typedef BOOST_DEDUCED_TYPENAME dynamic_bitset<Block, Allocator>::size_type size_type;
  1181. size_type asize(a.num_blocks());
  1182. size_type bsize(b.num_blocks());
  1183. assert(asize == 3);
  1184. assert(bsize == 4);
  1185. if (!bsize)
  1186. {
  1187. return false;
  1188. }
  1189. else if (!asize)
  1190. {
  1191. return true;
  1192. }
  1193. else
  1194. {
  1195. size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize));
  1196. assert(leqsize == 3);
  1197. //if (a.size() == 0)
  1198. // return false;
  1199. // Since we are storing the most significant bit
  1200. // at pos == size() - 1, we need to do the comparisons in reverse.
  1201. //
  1202. for (size_type ii = 0; ii < leqsize; ++ii,--asize,--bsize)
  1203. {
  1204. size_type i = asize-1;
  1205. size_type j = bsize-1;
  1206. if (a.m_bits[i] < b.m_bits[j])
  1207. return true;
  1208. else if (a.m_bits[i] > b.m_bits[j])
  1209. return false;
  1210. }
  1211. return (a.num_blocks() < b.num_blocks());
  1212. }
  1213. }
  1214. template <typename Block, typename Allocator>
  1215. inline bool operator<=(const dynamic_bitset<Block, Allocator>& a,
  1216. const dynamic_bitset<Block, Allocator>& b)
  1217. {
  1218. return !(a > b);
  1219. }
  1220. template <typename Block, typename Allocator>
  1221. inline bool operator>(const dynamic_bitset<Block, Allocator>& a,
  1222. const dynamic_bitset<Block, Allocator>& b)
  1223. {
  1224. return b < a;
  1225. }
  1226. template <typename Block, typename Allocator>
  1227. inline bool operator>=(const dynamic_bitset<Block, Allocator>& a,
  1228. const dynamic_bitset<Block, Allocator>& b)
  1229. {
  1230. return !(a < b);
  1231. }
  1232. //-----------------------------------------------------------------------------
  1233. // stream operations
  1234. #ifdef BOOST_OLD_IOSTREAMS
  1235. template < typename Block, typename Alloc>
  1236. std::ostream&
  1237. operator<<(std::ostream& os, const dynamic_bitset<Block, Alloc>& b)
  1238. {
  1239. // NOTE: since this is aimed at "classic" iostreams, exception
  1240. // masks on the stream are not supported. The library that
  1241. // ships with gcc 2.95 has an exceptions() member function but
  1242. // nothing is actually implemented; not even the class ios::failure.
  1243. using namespace std;
  1244. const ios::iostate ok = ios::goodbit;
  1245. ios::iostate err = ok;
  1246. if (os.opfx()) {
  1247. //try
  1248. typedef typename dynamic_bitset<Block, Alloc>::size_type bitsetsize_type;
  1249. const bitsetsize_type sz = b.size();
  1250. std::streambuf * buf = os.rdbuf();
  1251. size_t npad = os.width() <= 0 // careful: os.width() is signed (and can be < 0)
  1252. || (bitsetsize_type) os.width() <= sz? 0 : os.width() - sz;
  1253. const char fill_char = os.fill();
  1254. const ios::fmtflags adjustfield = os.flags() & ios::adjustfield;
  1255. // if needed fill at left; pad is decresed along the way
  1256. if (adjustfield != ios::left) {
  1257. for (; 0 < npad; --npad)
  1258. if (fill_char != buf->sputc(fill_char)) {
  1259. err |= ios::failbit;
  1260. break;
  1261. }
  1262. }
  1263. if (err == ok) {
  1264. // output the bitset
  1265. for (bitsetsize_type i = b.size(); 0 < i; --i) {
  1266. const char dig = b.test(i-1)? '1' : '0';
  1267. if (EOF == buf->sputc(dig)) {
  1268. err |= ios::failbit;
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. if (err == ok) {
  1274. // if needed fill at right
  1275. for (; 0 < npad; --npad) {
  1276. if (fill_char != buf->sputc(fill_char)) {
  1277. err |= ios::failbit;
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. os.osfx();
  1283. os.width(0);
  1284. } // if opfx
  1285. if(err != ok)
  1286. os.setstate(err); // assume this does NOT throw
  1287. return os;
  1288. }
  1289. #else
  1290. template <typename Ch, typename Tr, typename Block, typename Alloc>
  1291. std::basic_ostream<Ch, Tr>&
  1292. operator<<(std::basic_ostream<Ch, Tr>& os,
  1293. const dynamic_bitset<Block, Alloc>& b)
  1294. {
  1295. using namespace std;
  1296. const ios_base::iostate ok = ios_base::goodbit;
  1297. ios_base::iostate err = ok;
  1298. typename basic_ostream<Ch, Tr>::sentry cerberos(os);
  1299. if (cerberos) {
  1300. BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, os.getloc());
  1301. const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
  1302. const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
  1303. BOOST_TRY {
  1304. typedef typename dynamic_bitset<Block, Alloc>::size_type bitset_size_type;
  1305. typedef basic_streambuf<Ch, Tr> buffer_type;
  1306. buffer_type * buf = os.rdbuf();
  1307. // careful: os.width() is signed (and can be < 0)
  1308. const bitset_size_type width = (os.width() <= 0) ? 0 : static_cast<bitset_size_type>(os.width());
  1309. streamsize npad = (width <= b.size()) ? 0 : width - b.size();
  1310. const Ch fill_char = os.fill();
  1311. const ios_base::fmtflags adjustfield = os.flags() & ios_base::adjustfield;
  1312. // if needed fill at left; pad is decreased along the way
  1313. if (adjustfield != ios_base::left) {
  1314. for (; 0 < npad; --npad)
  1315. if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
  1316. err |= ios_base::failbit;
  1317. break;
  1318. }
  1319. }
  1320. if (err == ok) {
  1321. // output the bitset
  1322. for (bitset_size_type i = b.size(); 0 < i; --i) {
  1323. typename buffer_type::int_type
  1324. ret = buf->sputc(b.test(i-1)? one : zero);
  1325. if (Tr::eq_int_type(Tr::eof(), ret)) {
  1326. err |= ios_base::failbit;
  1327. break;
  1328. }
  1329. }
  1330. }
  1331. if (err == ok) {
  1332. // if needed fill at right
  1333. for (; 0 < npad; --npad) {
  1334. if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
  1335. err |= ios_base::failbit;
  1336. break;
  1337. }
  1338. }
  1339. }
  1340. os.width(0);
  1341. } BOOST_CATCH (...) { // see std 27.6.1.1/4
  1342. bool rethrow = false;
  1343. BOOST_TRY { os.setstate(ios_base::failbit); } BOOST_CATCH (...) { rethrow = true; } BOOST_CATCH_END
  1344. if (rethrow)
  1345. BOOST_RETHROW;
  1346. }
  1347. BOOST_CATCH_END
  1348. }
  1349. if(err != ok)
  1350. os.setstate(err); // may throw exception
  1351. return os;
  1352. }
  1353. #endif
  1354. #ifdef BOOST_OLD_IOSTREAMS
  1355. // A sentry-like class that calls isfx in its destructor.
  1356. // "Necessary" because bit_appender::do_append may throw.
  1357. class pseudo_sentry {
  1358. std::istream & m_r;
  1359. const bool m_ok;
  1360. public:
  1361. explicit pseudo_sentry(std::istream & r) : m_r(r), m_ok(r.ipfx(0)) { }
  1362. ~pseudo_sentry() { m_r.isfx(); }
  1363. operator bool() const { return m_ok; }
  1364. };
  1365. template <typename Block, typename Alloc>
  1366. std::istream&
  1367. operator>>(std::istream& is, dynamic_bitset<Block, Alloc>& b)
  1368. {
  1369. // Extractor for classic IO streams (libstdc++ < 3.0)
  1370. // ----------------------------------------------------//
  1371. // It's assumed that the stream buffer functions, and
  1372. // the stream's setstate() _cannot_ throw.
  1373. typedef dynamic_bitset<Block, Alloc> bitset_type;
  1374. typedef typename bitset_type::size_type size_type;
  1375. std::ios::iostate err = std::ios::goodbit;
  1376. pseudo_sentry cerberos(is); // skips whitespaces
  1377. if(cerberos) {
  1378. b.clear();
  1379. const std::streamsize w = is.width();
  1380. const size_type limit = w > 0 && static_cast<size_type>(w) < b.max_size()
  1381. ? static_cast<size_type>(w) : b.max_size();
  1382. typename bitset_type::bit_appender appender(b);
  1383. std::streambuf * buf = is.rdbuf();
  1384. for(int c = buf->sgetc(); appender.get_count() < limit; c = buf->snextc() ) {
  1385. if (c == EOF) {
  1386. err |= std::ios::eofbit;
  1387. break;
  1388. }
  1389. else if (char(c) != '0' && char(c) != '1')
  1390. break; // non digit character
  1391. else {
  1392. BOOST_TRY {
  1393. appender.do_append(char(c) == '1');
  1394. }
  1395. BOOST_CATCH(...) {
  1396. is.setstate(std::ios::failbit); // assume this can't throw
  1397. BOOST_RETHROW;
  1398. }
  1399. BOOST_CATCH_END
  1400. }
  1401. } // for
  1402. }
  1403. is.width(0);
  1404. if (b.size() == 0)
  1405. err |= std::ios::failbit;
  1406. if (err != std::ios::goodbit)
  1407. is.setstate (err); // may throw
  1408. return is;
  1409. }
  1410. #else // BOOST_OLD_IOSTREAMS
  1411. template <typename Ch, typename Tr, typename Block, typename Alloc>
  1412. std::basic_istream<Ch, Tr>&
  1413. operator>>(std::basic_istream<Ch, Tr>& is, dynamic_bitset<Block, Alloc>& b)
  1414. {
  1415. using namespace std;
  1416. typedef dynamic_bitset<Block, Alloc> bitset_type;
  1417. typedef typename bitset_type::size_type size_type;
  1418. const streamsize w = is.width();
  1419. const size_type limit = 0 < w && static_cast<size_type>(w) < b.max_size()?
  1420. static_cast<size_type>(w) : b.max_size();
  1421. ios_base::iostate err = ios_base::goodbit;
  1422. typename basic_istream<Ch, Tr>::sentry cerberos(is); // skips whitespaces
  1423. if(cerberos) {
  1424. // in accordance with prop. resol. of lib DR 303 [last checked 4 Feb 2004]
  1425. BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, is.getloc());
  1426. const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
  1427. const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
  1428. b.clear();
  1429. BOOST_TRY {
  1430. typename bitset_type::bit_appender appender(b);
  1431. basic_streambuf <Ch, Tr> * buf = is.rdbuf();
  1432. typename Tr::int_type c = buf->sgetc();
  1433. for( ; appender.get_count() < limit; c = buf->snextc() ) {
  1434. if (Tr::eq_int_type(Tr::eof(), c)) {
  1435. err |= ios_base::eofbit;
  1436. break;
  1437. }
  1438. else {
  1439. const Ch to_c = Tr::to_char_type(c);
  1440. const bool is_one = Tr::eq(to_c, one);
  1441. if (!is_one && !Tr::eq(to_c, zero))
  1442. break; // non digit character
  1443. appender.do_append(is_one);
  1444. }
  1445. } // for
  1446. }
  1447. BOOST_CATCH (...) {
  1448. // catches from stream buf, or from vector:
  1449. //
  1450. // bits_stored bits have been extracted and stored, and
  1451. // either no further character is extractable or we can't
  1452. // append to the underlying vector (out of memory)
  1453. bool rethrow = false; // see std 27.6.1.1/4
  1454. BOOST_TRY { is.setstate(ios_base::badbit); }
  1455. BOOST_CATCH(...) { rethrow = true; }
  1456. BOOST_CATCH_END
  1457. if (rethrow)
  1458. BOOST_RETHROW;
  1459. }
  1460. BOOST_CATCH_END
  1461. }
  1462. is.width(0);
  1463. if (b.size() == 0 /*|| !cerberos*/)
  1464. err |= ios_base::failbit;
  1465. if (err != ios_base::goodbit)
  1466. is.setstate (err); // may throw
  1467. return is;
  1468. }
  1469. #endif
  1470. //-----------------------------------------------------------------------------
  1471. // bitset operations
  1472. template <typename Block, typename Allocator>
  1473. dynamic_bitset<Block, Allocator>
  1474. operator&(const dynamic_bitset<Block, Allocator>& x,
  1475. const dynamic_bitset<Block, Allocator>& y)
  1476. {
  1477. dynamic_bitset<Block, Allocator> b(x);
  1478. return b &= y;
  1479. }
  1480. template <typename Block, typename Allocator>
  1481. dynamic_bitset<Block, Allocator>
  1482. operator|(const dynamic_bitset<Block, Allocator>& x,
  1483. const dynamic_bitset<Block, Allocator>& y)
  1484. {
  1485. dynamic_bitset<Block, Allocator> b(x);
  1486. return b |= y;
  1487. }
  1488. template <typename Block, typename Allocator>
  1489. dynamic_bitset<Block, Allocator>
  1490. operator^(const dynamic_bitset<Block, Allocator>& x,
  1491. const dynamic_bitset<Block, Allocator>& y)
  1492. {
  1493. dynamic_bitset<Block, Allocator> b(x);
  1494. return b ^= y;
  1495. }
  1496. template <typename Block, typename Allocator>
  1497. dynamic_bitset<Block, Allocator>
  1498. operator-(const dynamic_bitset<Block, Allocator>& x,
  1499. const dynamic_bitset<Block, Allocator>& y)
  1500. {
  1501. dynamic_bitset<Block, Allocator> b(x);
  1502. return b -= y;
  1503. }
  1504. //-----------------------------------------------------------------------------
  1505. // namespace scope swap
  1506. template<typename Block, typename Allocator>
  1507. inline void
  1508. swap(dynamic_bitset<Block, Allocator>& left,
  1509. dynamic_bitset<Block, Allocator>& right) // no throw
  1510. {
  1511. left.swap(right);
  1512. }
  1513. //-----------------------------------------------------------------------------
  1514. // private (on conforming compilers) member functions
  1515. template <typename Block, typename Allocator>
  1516. inline typename dynamic_bitset<Block, Allocator>::size_type
  1517. dynamic_bitset<Block, Allocator>::calc_num_blocks(size_type num_bits)
  1518. {
  1519. return num_bits / bits_per_block
  1520. + static_cast<size_type>( num_bits % bits_per_block != 0 );
  1521. }
  1522. // gives a reference to the highest block
  1523. //
  1524. template <typename Block, typename Allocator>
  1525. inline Block& dynamic_bitset<Block, Allocator>::m_highest_block()
  1526. {
  1527. return const_cast<Block &>
  1528. (static_cast<const dynamic_bitset *>(this)->m_highest_block());
  1529. }
  1530. // gives a const-reference to the highest block
  1531. //
  1532. template <typename Block, typename Allocator>
  1533. inline const Block& dynamic_bitset<Block, Allocator>::m_highest_block() const
  1534. {
  1535. assert(size() > 0 && num_blocks() > 0);
  1536. return m_bits.back();
  1537. }
  1538. // If size() is not a multiple of bits_per_block
  1539. // then not all the bits in the last block are used.
  1540. // This function resets the unused bits (convenient
  1541. // for the implementation of many member functions)
  1542. //
  1543. template <typename Block, typename Allocator>
  1544. inline void dynamic_bitset<Block, Allocator>::m_zero_unused_bits()
  1545. {
  1546. assert (num_blocks() == calc_num_blocks(m_num_bits));
  1547. // if != 0 this is the number of bits used in the last block
  1548. const block_width_type extra_bits = count_extra_bits();
  1549. if (extra_bits != 0)
  1550. m_highest_block() &= (Block(1) << extra_bits) - 1;
  1551. }
  1552. // check class invariants
  1553. template <typename Block, typename Allocator>
  1554. bool dynamic_bitset<Block, Allocator>::m_check_invariants() const
  1555. {
  1556. const block_width_type extra_bits = count_extra_bits();
  1557. if (extra_bits > 0) {
  1558. const block_type mask = block_type(~0) << extra_bits;
  1559. if ((m_highest_block() & mask) != 0)
  1560. return false;
  1561. }
  1562. if (m_bits.size() > m_bits.capacity() || num_blocks() != calc_num_blocks(size()))
  1563. return false;
  1564. return true;
  1565. }
  1566. } // namespace boost
  1567. #undef BOOST_BITSET_CHAR
  1568. #endif // include guard