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