dynamic_bitset.hpp 65 KB

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