dynamic_bitset.hpp 64 KB

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