dynamic_bitset.hpp 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. // -----------------------------------------------------------
  2. //
  3. // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
  4. // Copyright (c) 2003-2006, 2008, 2025 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 "boost/dynamic_bitset/config.hpp"
  21. #include "boost/dynamic_bitset/detail/dynamic_bitset.hpp"
  22. #include "boost/dynamic_bitset_fwd.hpp"
  23. #include "boost/limits.hpp"
  24. #include <iosfwd>
  25. #include <iterator>
  26. #include <string>
  27. #include <type_traits>
  28. #include <vector>
  29. #if defined( BOOST_DYNAMIC_BITSET_SPECIALIZE_STD_HASH )
  30. # include <functional>
  31. namespace std {
  32. //! Support for std::hash.
  33. //!
  34. //! You can exclude this support by defining the macro
  35. //! `BOOST_DYNAMIC_BITSET_NO_STD_HASH`.
  36. // -----------------------------------------------------------------------
  37. template< typename Block, typename AllocatorOrContainer >
  38. struct hash< boost::dynamic_bitset< Block, AllocatorOrContainer > >;
  39. }
  40. #endif
  41. namespace boost {
  42. template< typename Iterator >
  43. class bit_iterator_base;
  44. template< typename DynamicBitset >
  45. class bit_iterator;
  46. template< typename DynamicBitset >
  47. class const_bit_iterator;
  48. //! The `dynamic_bitset` template represents a set of bits.
  49. //!
  50. //! \par Template parameters
  51. //! - `Block`
  52. //! The integer type in which the bits are stored. Defaults to
  53. //! `unsigned long`.
  54. //!
  55. //! - `AllocatorOrContainer`
  56. //! Either an allocator (to use for all internal memory management) or
  57. //! a container of Block's. Defaults to `std::allocator< Block >`.
  58. //!
  59. //! \par Concepts modeled
  60. //! DefaultConstructible, CopyConstructible, CopyAssignable,
  61. //! MoveConstructible, MoveAssignable, EqualityComparable,
  62. //! LessThanComparable.
  63. //!
  64. //! \par Type requirements
  65. //! `Block` is a cv-unqualified unsigned integer type other than
  66. //! `bool`. `AllocatorOrContainer` satisfies the standard requirements for an
  67. //! <a href="https://en.cppreference.com/w/cpp/named_req/Allocator.html">allocator</a>
  68. //! or is a container-like type which provides at least bidirectional
  69. //! iterators.
  70. // ---------------------------------------------------------------------------
  71. template< typename Block, typename AllocatorOrContainer >
  72. class dynamic_bitset
  73. {
  74. static_assert( (bool)detail::dynamic_bitset_impl::allowed_block_type< Block >::value, "Block type not allowed" );
  75. static_assert( std::is_same< Block, typename AllocatorOrContainer::value_type >::value, "Block is not the same type as AllocatorOrContainer::value_type" );
  76. public:
  77. //! The same type as `Block`.
  78. // -----------------------------------------------------------------------
  79. typedef Block block_type;
  80. //! The allocator used for all memory allocations.
  81. // -----------------------------------------------------------------------
  82. typedef typename detail::dynamic_bitset_impl::allocator_type_extractor< AllocatorOrContainer, Block >::type
  83. allocator_type;
  84. //! An unsigned integral type that can represent the size of the
  85. //! bitset. See \ref size().
  86. // -----------------------------------------------------------------------
  87. typedef std::size_t size_type;
  88. //! Note: Made public to cope with failures from many GCC and
  89. //! Clang versions which seem to ignore the friend declarations
  90. //! of `bit_iterator` and `const_bit_iterator`.
  91. // -----------------------------------------------------------------------
  92. typedef typename std::conditional<
  93. detail::dynamic_bitset_impl::is_container< AllocatorOrContainer, Block >::value,
  94. AllocatorOrContainer,
  95. std::vector< Block, AllocatorOrContainer > >::type buffer_type;
  96. //! The number of bits the type `Block` uses to represent
  97. //! values, excluding any padding bits. Numerically equal to
  98. //! `std::numeric_limits< Block >::digits`.
  99. // -----------------------------------------------------------------------
  100. static constexpr int bits_per_block = std::numeric_limits< Block >::digits;
  101. //! The maximum value of `size_type`.
  102. // -----------------------------------------------------------------------
  103. static constexpr size_type npos = static_cast< size_type >( -1 );
  104. //! A proxy class to simulate lvalues of bit type.
  105. //!
  106. //! This class exists only as a helper class for
  107. //! `dynamic_bitset`'s `operator[]`. The following list
  108. //! describes the valid operations on the reference type. Assume
  109. //! that `b` is an instance of `dynamic_bitset`, `i`, `j` are of
  110. //! `size_type` and in the range `[0, b.size())`. Also, note
  111. //! that when we write `b[ i ]` we mean an object of type
  112. //! `reference` that was initialized from `b[ i ]`. The variable
  113. //! `x` is a `bool`.
  114. //!
  115. //! - `(bool)b[ i ]`
  116. //!
  117. //! Returns the i-th bit of `b`.
  118. //!
  119. //! - `(bool)~ b[ i ]`
  120. //!
  121. //! Returns the opposite of the i-th bit of `b`.
  122. //!
  123. //! - `b[ i ].flip()`
  124. //!
  125. //! Flips the i-th bit of `b` and returns `b[ i ]`.
  126. //!
  127. //! - `x = b[ i ]`
  128. //!
  129. //! Assigns the i-th bit of `b` to `x`.
  130. //!
  131. //! - `b[ i ] = x`
  132. //!
  133. //! Sets the i-th bit of `b` to the value of `x` and returns
  134. //! `b[ i ]`.
  135. //!
  136. //! - `b[ i ] = b[ j ]`
  137. //!
  138. //! Sets the i-th bit of `b` to the value of the j-th bit of
  139. //! `b` and returns `b[ i ]`.
  140. //!
  141. //! - `b[ i ] |= x`
  142. //!
  143. //! Does an OR of the i-th bit of `b` with the value of `x`
  144. //! and returns `b[ i ]`.
  145. //!
  146. //! - `b[ i ] &= x`
  147. //!
  148. //! Does an AND of the i-th bit of `b` with the value of `x`
  149. //! and returns `b[ i ]`.
  150. //!
  151. //! - `b[ i ] ^= x`
  152. //!
  153. //! Does a XOR of the i-th bit of `b` with the value of `x`
  154. //! and returns `b[ i ]`.
  155. //!
  156. //! - `b[ i ] -= x`
  157. //!
  158. //! If `x` is `true`, clears the i-th bit of `b`. Returns `b[
  159. //! i ]`.
  160. //!
  161. //! - `b[ i ] |= b[ j ]`
  162. //!
  163. //! Does an OR of the i-th bit of `b` with the j-th bit of `b`
  164. //! and returns `b[ i ]`.
  165. //!
  166. //! - `b[ i ] &= b[ j ]`
  167. //!
  168. //! Does an AND of the i-th bit of `b` with the j-th bit of
  169. //! `b` and returns `b[ i ]`.
  170. //!
  171. //! - `b[ i ] ^= b[ j ]`
  172. //!
  173. //! Does a XOR of the i-th bit of `b` with the j-th bit of `b`
  174. //! and returns `b[ i ]`.
  175. //!
  176. //! - `b[ i ] -= b[ j ]`
  177. //!
  178. //! If the j-th bit of `b` is set, clears the i-th bit of `b`.
  179. //! Returns `b[ i ]`.
  180. // -----------------------------------------------------------------------
  181. class reference
  182. {
  183. friend class dynamic_bitset< Block, AllocatorOrContainer >;
  184. friend class bit_iterator< dynamic_bitset >;
  185. //! The one and only non-copy ctor
  186. // -------------------------------------------------------------------
  187. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference( block_type & b, int pos );
  188. public:
  189. //! Deleted address-of operator.
  190. // -------------------------------------------------------------------
  191. void operator&() = delete;
  192. //! Copy constructor.
  193. //!
  194. //! Constructs a `reference` which refers to the same bit as
  195. //! `other`.
  196. // -------------------------------------------------------------------
  197. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference( const reference & other );
  198. //! See the class description.
  199. // -------------------------------------------------------------------
  200. BOOST_DYNAMIC_BITSET_CONSTEXPR20 operator bool() const;
  201. //! See the class description.
  202. //!
  203. //! \return The opposite of the value of `*this`.
  204. // -------------------------------------------------------------------
  205. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator~() const;
  206. //! See the class description.
  207. // -------------------------------------------------------------------
  208. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & flip();
  209. //! See the class description.
  210. // -------------------------------------------------------------------
  211. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator=( bool x );
  212. //! See the class description.
  213. // -------------------------------------------------------------------
  214. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator=( const reference & rhs );
  215. //! See the class description.
  216. // -------------------------------------------------------------------
  217. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator|=( bool x );
  218. //! See the class description.
  219. // -------------------------------------------------------------------
  220. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator&=( bool x );
  221. //! See the class description.
  222. // -------------------------------------------------------------------
  223. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator^=( bool x );
  224. //! See the class description.
  225. // -------------------------------------------------------------------
  226. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference & operator-=( bool x );
  227. private:
  228. block_type & m_block;
  229. const block_type m_mask;
  230. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void do_set();
  231. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void do_reset();
  232. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void do_flip();
  233. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void do_assign( bool x );
  234. };
  235. //! The type bool.
  236. // -----------------------------------------------------------------------
  237. typedef bool const_reference;
  238. friend class bit_iterator< dynamic_bitset >;
  239. friend class const_bit_iterator< dynamic_bitset >;
  240. //! A read/write iterator into the bitset.
  241. //!
  242. //! If `AllocatorOrContainer` is an allocator type, this is a
  243. //! C++20 RandomAccessIterator; otherwise, its category is the
  244. //! corresponding "non-legacy" category of the iterator type of
  245. //! the underlying container; for instance, if the underlying
  246. //! container provides LegacyBidirectionalIterators, this is a
  247. //! BidirectionalIterator.
  248. // -----------------------------------------------------------------------
  249. typedef bit_iterator< dynamic_bitset > iterator;
  250. //! A read-only iterator into the bitset.
  251. //!
  252. //! \copydetails iterator
  253. // -----------------------------------------------------------------------
  254. typedef const_bit_iterator< dynamic_bitset > const_iterator;
  255. //! A reverse read/write reverse iterator into the bitset.
  256. // -----------------------------------------------------------------------
  257. typedef std::reverse_iterator< iterator > reverse_iterator;
  258. //! A reverse read-only iterator into the bitset.
  259. // -----------------------------------------------------------------------
  260. typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
  261. #if defined( __cpp_lib_ranges )
  262. static_assert( std::bidirectional_iterator< typename buffer_type::iterator >, "AllocatorOrContainer doesn't provide at least BidirectionalIterators" );
  263. static_assert( std::bidirectional_iterator< iterator > );
  264. #endif
  265. //! Constructs a bitset of size zero.
  266. //!
  267. //! \post
  268. //! `this->size() == 0`.
  269. //!
  270. //! (Required by <a href="https://en.cppreference.com/w/cpp/named_req/DefaultConstructible">DefaultConstructible</a>.)
  271. // -----------------------------------------------------------------------
  272. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset();
  273. //! Constructs a bitset of size zero.
  274. //!
  275. //! \param alloc An allocator, a copy of which will be used to
  276. //! allocate memory when needed.
  277. //!
  278. //! \post
  279. //! `this->size() == 0`
  280. // -----------------------------------------------------------------------
  281. explicit BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset( const allocator_type & alloc );
  282. //! Constructs a bitset from an integer.
  283. //!
  284. //! The first `M` bits (where `M = min( num_bits,
  285. //! std::numeric_limits< unsigned long >::digits )`) are
  286. //! initialized to the corresponding bits in `value` and all
  287. //! other bits, if any, to zero. A copy of the `alloc` object
  288. //! will be used in subsequent bitset operations such as
  289. //! `resize()` to allocate memory. Note that, e.g., the
  290. //! following
  291. //!
  292. //! \code
  293. //! dynamic_bitset b<>( 16, 7 );
  294. //! \endcode
  295. //!
  296. //! will match the constructor from an iterator range (not this
  297. //! one), but the underlying implementation will still "do the
  298. //! right thing" and construct a bitset of 16 bits, from the
  299. //! value 7.
  300. //!
  301. //! \param num_bits The size of the constructed bitset.
  302. //! \param value The value to initialize the bitset from.
  303. //! \param alloc The allocator to use.
  304. //!
  305. //! \post
  306. //! - `this->size() == num_bits`
  307. //! - For all i in the range `[0, M)`, `( *this )[ i ] == (
  308. //! value >> i ) & 1`.
  309. //! - For all i in the range `[M, num_bits)`, `( *this )[ i ] ==
  310. //! false`.
  311. //!
  312. //! \par Throws
  313. //! An allocation error if memory is exhausted (`std::bad_alloc`
  314. //! if `allocator_type` is a `std::allocator`).
  315. // -----------------------------------------------------------------------
  316. explicit BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset( size_type num_bits, unsigned long value = 0, const allocator_type & alloc = allocator_type() );
  317. //! Constructs a bitset from a string of 0's and 1's.
  318. //!
  319. //! The size of the bitset is `num_bits` if `num_bits != npos`,
  320. //! otherwise `rlen = min( n, s.size() - pos )`. The first `M =
  321. //! min( num_bits, rlen )` bits are initialized to the
  322. //! corresponding characters in `s`. Note that the highest
  323. //! character position in `s`, not the lowest, corresponds to
  324. //! the least significant bit. So, for example, `dynamic_bitset(
  325. //! std::string( "1101" ))` is the same as `dynamic_bitset(
  326. //! 13ul)`.
  327. //!
  328. //! \pre
  329. //! `pos <= s.size()` and the characters used to initialize the
  330. //! bits compare equal to either `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '0' )`
  331. //! or `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '1' )`. E.g.:
  332. //! `dynamic_bitset<> b( std::string( "10xyz" ), 0, 2 ); // OK`.
  333. //!
  334. //! \param s The string to construct from.
  335. //! \param pos The start position in the string.
  336. //! \param n The maximum number of characters in the string to
  337. //! consider.
  338. //! \param num_bits The size of the bitset to construct, if
  339. //! different from `npos`.
  340. //! \param alloc The allocator to use.
  341. // -----------------------------------------------------------------------
  342. template< typename CharT, typename Traits, typename Alloc >
  343. explicit dynamic_bitset( const std::basic_string< CharT, Traits, Alloc > & s, typename std::basic_string< CharT, Traits, Alloc >::size_type pos = 0, typename std::basic_string< CharT, Traits, Alloc >::size_type n = ( std::basic_string< CharT, Traits, Alloc >::npos ), size_type num_bits = npos, const allocator_type & alloc = allocator_type() );
  344. //! Similar to the constructor from a `basic_string`, but takes
  345. //! a pointer to a C-style string (and doesn't take a `pos`).
  346. //!
  347. //! The size of the bitset is `num_bits` if `num_bits != npos`,
  348. //! otherwise `rlen = min( n, std::char_traits< CharT >::length( s ) )`.
  349. //! The first `M = min( num_bits, rlen )` bits are initialized
  350. //! to the corresponding characters in `s`.
  351. //!
  352. //! \pre
  353. //! The characters in `s` that are used to initialize the bits
  354. //! compare equal to either `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '0' )`
  355. //! or `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '1' )`. E.g.:
  356. //! `dynamic_bitset<> b( "10xyz", 2 ); // OK`.
  357. //!
  358. //! \param s The string to construct from.
  359. //! \param n The maximum number of characters in the string to
  360. //! consider.
  361. //! \param num_bits The size of the bitset to construct, if
  362. //! different from `npos`.
  363. //! \param alloc The allocator to use.
  364. // -----------------------------------------------------------------------
  365. template< typename CharT >
  366. explicit dynamic_bitset( const CharT * s, std::size_t n = std::size_t( -1 ), size_type num_bits = npos, const allocator_type & alloc = allocator_type() );
  367. #if defined( BOOST_DYNAMIC_BITSET_USE_CPP17_OR_LATER )
  368. //! Similar to the constructor from a pointer to a C-style
  369. //! string, but takes a `std::basic_string_view`. This
  370. //! constructor is only available if DynamicBitset is compiled
  371. //! as C++17 or later.
  372. //!
  373. //! \pre
  374. //! The characters in `sv` that are use to initialize the bits
  375. //! compare equal to either `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '0' )`
  376. //! or `std::use_facet< std::ctype< CharT > >( std::locale() ).widen( '1' )`. E.g.:
  377. //! `dynamic_bitset<> b( std::string_view( "10xyz", 2 ) ); // OK`.
  378. //!
  379. //! \param sv The basic_string_view to construct from.
  380. //! \param num_bits The size of the bitset to construct, if
  381. //! different from `npos`. (Otherwise the size of the bitset is
  382. //! `sv.length()`.)
  383. //! \param alloc The allocator to use.
  384. // -----------------------------------------------------------------------
  385. template< typename CharT, typename Traits >
  386. explicit dynamic_bitset( std::basic_string_view< CharT, Traits > sv, size_type num_bits = npos, const allocator_type & alloc = allocator_type() );
  387. #endif
  388. //! Constructs a bitset from a range of blocks or from an
  389. //! integer.
  390. //!
  391. //! If this constructor is called with a type
  392. //! `BlockInputIterator` which is actually an integral type, the
  393. //! library behaves as if the constructor from `unsigned long`
  394. //! were called, with arguments `static_cast< size_type >( first )`,
  395. //! `last` and `alloc`, in that order.
  396. //!
  397. //! \par Example
  398. //! Given:
  399. //!
  400. //! \code
  401. //! dynamic_bitset< unsigned short > b( 8, 7 );
  402. //! \endcode
  403. //!
  404. //! `b` is constructed as if by calling the constructor:
  405. //!
  406. //! \code
  407. //! dynamic_bitset(size_type num_bits,
  408. //! unsigned long value = 0,
  409. //! const allocator_type & alloc = allocator_type())
  410. //! \endcode
  411. //!
  412. //! with arguments:
  413. //!
  414. //! \code
  415. //! static_cast< dynamic_bitset< unsigned short >::size_type >( 8 ),
  416. //! 7,
  417. //! allocator_type()
  418. //! \endcode
  419. //!
  420. //! Note:
  421. //! At the time of writing (October 2008) this is aligned with
  422. //! the proposed resolution for library issue 438. That is a
  423. //! post C++03 change, and is currently in the working paper for
  424. //! C++0x. Informally speaking, the critical changes with
  425. //! respect to C++03 are the drop of a `static_cast` on the
  426. //! second argument, and more leeway as to when the templated
  427. //! constructor should have the same effect as the `(size, value)`
  428. //! one: Only when `InputIterator` is an integral type, in
  429. //! C++03; when it is either an integral type or any other type
  430. //! that the implementation might detect as impossible to be an
  431. //! input iterator, with the proposed resolution. For the
  432. //! purposes of dynamic_bitset we limit ourselves to the first
  433. //! of these two changes.
  434. //!
  435. //! Otherwise (i.e. if the template argument is not an integral
  436. //! type), constructs a bitset based on a range of blocks. Let
  437. //! `*first` be block number 0, `\*++first` block number 1, etc.
  438. //! Block number `b` is used to initialize the bits of the
  439. //! dynamic_bitset in the position range `[b * bits_per_block, (
  440. //! b + 1 ) * bits_per_block)`. For each block number `b` with
  441. //! value `bval`, the bit `( bval >> i ) & 1` corresponds to the
  442. //! bit at position `b * bits_per_block + i` in the bitset
  443. //! (where i goes through the range `[0, bits_per_block)`).
  444. //! \pre
  445. //! `BlockInputIterator` must be either an integral type or a
  446. //! model of <a href="https://en.cppreference.com/w/cpp/named_req/InputIterator">LegacyInputIterator</a>
  447. //! whose `value_type` is the same type as `Block`.
  448. //!
  449. //! \param first `numbits` if the template argument is an
  450. //! integral type, otherwise the start of the range.
  451. //! \param last `value` if the template argument is an integral
  452. //! type, otherwise the end of the range.
  453. //! \param alloc The allocator to use.
  454. //!
  455. //! \par Throws
  456. //! An allocation error if memory is exhausted (`std::bad_alloc`
  457. //! if `allocator_type` is a `std::allocator`).
  458. // -----------------------------------------------------------------------
  459. template< typename BlockInputIterator >
  460. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset( BlockInputIterator first, BlockInputIterator last, const allocator_type & alloc = allocator_type() );
  461. //! Copy constructor.
  462. //!
  463. //! Constructs a bitset that is a copy of the bitset `b`. The
  464. //! allocator for this bitset is a copy of the allocator of `b`.
  465. //!
  466. //! \post
  467. //! For all i in the range `[0, b.size())`, `( *this )[ i ] ==
  468. //! b[ i ]`.
  469. //!
  470. //! \par Throws
  471. //! An allocation error if memory is exhausted (`std::bad_alloc`
  472. //! if `allocator_type` is a `std::allocator`).
  473. //!
  474. //! (Required by <a href="https://en.cppreference.com/w/cpp/named_req/CopyConstructible">CopyConstructible</a>.)
  475. // -----------------------------------------------------------------------
  476. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset( const dynamic_bitset & b );
  477. //! Copy assignment operator.
  478. //!
  479. //! This bitset becomes a copy of the bitset `b`.
  480. //!
  481. //! \post
  482. //! For all `i` in the range `[0, x.size())`, `( *this )[ i ] ==
  483. //! b[ i ]`.
  484. //!
  485. //! \return
  486. //! `*this`.
  487. //!
  488. //! \par Throws
  489. //! An allocation error if memory is exhausted (`std::bad_alloc`
  490. //! if `allocator_type` is a `std::allocator`).
  491. //! (Required by <a href="https://en.cppreference.com/w/cpp/named_req/CopyAssignable">CopyAssignable</a>.)
  492. // -----------------------------------------------------------------------
  493. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator=( const dynamic_bitset & b );
  494. //! Destructor.
  495. //!
  496. //! Releases the memory associated with this bitset and destroys
  497. //! the bitset object itself.
  498. // -----------------------------------------------------------------------
  499. BOOST_DYNAMIC_BITSET_CONSTEXPR20 ~dynamic_bitset();
  500. //! Returns a read/write iterator that refers to the least
  501. //! significant bit in the bitset.
  502. // -----------------------------------------------------------------------
  503. BOOST_DYNAMIC_BITSET_CONSTEXPR20 iterator begin();
  504. //! Returns a read-only iterator that refers to the least
  505. //! significant bit in the bitset.
  506. // -----------------------------------------------------------------------
  507. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_iterator begin() const;
  508. //! Returns a read/write iterator that refers one past the most
  509. //! significant bit in the bitset.
  510. // -----------------------------------------------------------------------
  511. BOOST_DYNAMIC_BITSET_CONSTEXPR20 iterator end();
  512. //! Returns a read-only iterator that refers one past the most
  513. //! significant bit in the bitset.
  514. // -----------------------------------------------------------------------
  515. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_iterator end() const;
  516. //! Returns a read/write reverse iterator that refers to the
  517. //! most significant bit in the bitset.
  518. // -----------------------------------------------------------------------
  519. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reverse_iterator rbegin();
  520. //! Returns a read-only reverse iterator that refers to the most
  521. //! significant bit in the bitset.
  522. // -----------------------------------------------------------------------
  523. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reverse_iterator rbegin() const;
  524. //! Returns a read/write reverse iterator that refers to one
  525. //! before the least significant bit in the bitset.
  526. // -----------------------------------------------------------------------
  527. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reverse_iterator rend();
  528. //! Returns a read-only reverse iterator that refers to one
  529. //! before the least significant bit in the bitset.
  530. // -----------------------------------------------------------------------
  531. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reverse_iterator rend() const;
  532. //! Returns a read-only iterator that refers to the least
  533. //! significant bit in the bitset.
  534. // -----------------------------------------------------------------------
  535. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_iterator cbegin() const;
  536. //! Returns a read-only iterator that refers to one past the
  537. //! most significant bit in the bitset.
  538. // -----------------------------------------------------------------------
  539. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_iterator cend() const;
  540. //! Returns a read-only reverse iterator that refers to the most
  541. //! significant bit in the bitset.
  542. // -----------------------------------------------------------------------
  543. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reverse_iterator crbegin() const;
  544. //! Returns a read-only reverse iterator that refers to one
  545. //! before the least significant bit in the bitset.
  546. // -----------------------------------------------------------------------
  547. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reverse_iterator crend() const;
  548. //! Swaps the contents of this bitset and bitset `b`.
  549. //!
  550. //! \param b The bitset to be swapped with `*this`.
  551. //!
  552. //! \par Throws
  553. //! Nothing.
  554. // -----------------------------------------------------------------------
  555. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void swap( dynamic_bitset & b ) noexcept;
  556. //! Move constructor.
  557. //!
  558. //! Constructs a bitset that is the same as the bitset `src`,
  559. //! while using the resources from `src`. The allocator for this
  560. //! bitset is moved from the allocator in `src`.
  561. //!
  562. //! \par Throws
  563. //! An allocation error if memory is exhausted (`std::bad_alloc`
  564. //! if `allocator_type` is a `std::allocator`).
  565. // -----------------------------------------------------------------------
  566. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset( dynamic_bitset && src );
  567. //! Move assignment operator.
  568. //!
  569. //! This bitset becomes the same as the bitset `src`, while
  570. //! using the resources from `src`.
  571. //!
  572. //! \return
  573. //! `*this`.
  574. //!
  575. //! \par Throws
  576. //! Nothing.
  577. // -----------------------------------------------------------------------
  578. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator=( dynamic_bitset && src );
  579. //! Returns a copy of the allocator object used to construct
  580. //! `*this`.
  581. //!
  582. //! \return A copy of the said allocator.
  583. // -----------------------------------------------------------------------
  584. BOOST_DYNAMIC_BITSET_CONSTEXPR20 allocator_type get_allocator() const;
  585. //! Changes the number of bits of the bitset to `num_bits`.
  586. //!
  587. //! If `num_bits >= size()` then the bits in the range `[0,
  588. //! size())` remain the same, and the bits in `[size(), num_bits)`
  589. //! are all set to `value`. If `num_bits < size()` then the bits
  590. //! in the range `[0, num_bits)` stay the same (and the
  591. //! remaining bits are discarded).
  592. //!
  593. //! \param num_bits The new size of the bitset.
  594. //! \param value The value to set any new bit to.
  595. // -----------------------------------------------------------------------
  596. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void resize( size_type num_bits, bool value = false );
  597. //! Clears the bitset, i.e. makes its size zero.
  598. //!
  599. //! \par Throws
  600. //! Nothing.
  601. // -----------------------------------------------------------------------
  602. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void clear();
  603. //! Increases the size of the bitset by one, and sets the value
  604. //! of the new most significant bit to `bit`.
  605. //!
  606. //! \par Throws
  607. //! An allocation error if memory is exhausted (`std::bad_alloc`
  608. //! if `allocator_type` is a `std::allocator`).
  609. //!
  610. //! \param bit The value to set the most significant bit to.
  611. // -----------------------------------------------------------------------
  612. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void push_back( bool bit );
  613. //! Increases the size of the bitset by one, and sets the value
  614. //! of the new least significant bit to `bit`.
  615. //!
  616. //! \par Throws
  617. //! An allocation error if memory is exhausted (`std::bad_alloc`
  618. //! if `allocator_type` is a `std::allocator`).
  619. //!
  620. //! \param bit The value to set the least significant bit to.
  621. // -----------------------------------------------------------------------
  622. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void push_front( bool bit );
  623. //! Decreases the size of the bitset by one, removing the most
  624. //! significant bit.
  625. //!
  626. //! \pre
  627. //! `! this->empty()`.
  628. // -----------------------------------------------------------------------
  629. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void pop_back();
  630. //! Decreases the size of the bitset by one, removing the least
  631. //! significant bit.
  632. //!
  633. //! \pre
  634. //! `! this->empty()`.
  635. // -----------------------------------------------------------------------
  636. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void pop_front();
  637. //! Appends the bits in `block` to this bitset (appends to the
  638. //! most significant end). This increases the size of the bitset
  639. //! by `bits_per_block`. Let `s` be the old size of the bitset,
  640. //! then for `i` in the range `[0, bits_per_block)`, the bit at
  641. //! position `s + i` is set to `( block >> i ) & 1`.
  642. //!
  643. //! \param block The block to append.
  644. // -----------------------------------------------------------------------
  645. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void append( Block block );
  646. //! Appends a range of blocks to `*this`.
  647. //!
  648. //! This member provides the same end result as the following
  649. //! code, but is typically more efficient.
  650. //!
  651. //! \code
  652. //! for (; first != last; ++first) {
  653. //! append( *first );
  654. //! }
  655. //! \endcode
  656. //!
  657. //! \pre
  658. //! The `BlockInputIterator` type must be a model of
  659. //! <a href="https://en.cppreference.com/w/cpp/named_req/InputIterator">LegacyInputIterator</a>
  660. //! and its value_type must be the same type as Block.
  661. //!
  662. //! \param first The start of the range.
  663. //! \param last The end of the range.
  664. // -----------------------------------------------------------------------
  665. template< typename BlockInputIterator >
  666. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void append( BlockInputIterator first, BlockInputIterator last ); // strong guarantee
  667. //! Bitwise-ANDs all the bits in this bitset with the bits in
  668. //! `b`.
  669. //!
  670. //! This is equivalent to:
  671. //! \code
  672. //! for ( size_type i = 0; i != this->size(); ++ i ) {
  673. //! ( *this )[ i ] = ( *this )[ i ] & b[ i ];
  674. //! }
  675. //! \endcode
  676. //!
  677. //! \pre
  678. //! `this->size() == b.size()`.
  679. //!
  680. //! \return
  681. //! `*this`.
  682. // -----------------------------------------------------------------------
  683. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator&=( const dynamic_bitset & b );
  684. //! Bitwise-ORs all the bits in this bitset with the bits in
  685. //! `b`.
  686. //!
  687. //! This is equivalent to:
  688. //! \code
  689. //! for ( size_type i = 0; i != this->size(); ++ i ) {
  690. //! ( *this )[ i ] = ( *this )[ i ] | b[ i ];
  691. //! }
  692. //! \endcode
  693. //!
  694. //! \pre
  695. //! `this->size() == b.size()`.
  696. //!
  697. //! \return
  698. //! `*this`.
  699. // -----------------------------------------------------------------------
  700. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator|=( const dynamic_bitset & b );
  701. //! Bitwise-XORs all the bits in this bitset with the bits in
  702. //! `b`.
  703. //!
  704. //! This is equivalent to:
  705. //! \code
  706. //! for ( size_type i = 0; i != this->size(); ++ i ) {
  707. //! ( *this )[ i ] = ( *this )[ i ] ^ b[ i ];
  708. //! }
  709. //! \endcode
  710. //!
  711. //! \pre
  712. //! `this->size() == b.size()`.
  713. //!
  714. //! \return
  715. //! `*this`.
  716. // -----------------------------------------------------------------------
  717. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator^=( const dynamic_bitset & b );
  718. //! Computes the set difference of this bitset and the `b`
  719. //! bitset.
  720. //!
  721. //! This is equivalent to:
  722. //! \code
  723. //! for ( size_type i = 0; i != this->size(); ++ i ) {
  724. //! ( *this )[ i ] = ( *this )[ i ] && ! b[ i ];
  725. //! }
  726. //! \endcode
  727. //!
  728. //! \pre
  729. //! `this->size() == b.size()`.
  730. //!
  731. //! \return
  732. //! `*this`.
  733. // -----------------------------------------------------------------------
  734. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator-=( const dynamic_bitset & b );
  735. //! Shifts the bits in this bitset to the left by `n` positions.
  736. //!
  737. //! For each bit in the bitset, the bit at position `pos` takes
  738. //! on the previous value of the bit at position `pos - n`, or
  739. //! zero if no such bit exists.
  740. //!
  741. //! \return
  742. //! `*this`.
  743. //!
  744. //! \par Throws
  745. //! Nothing.
  746. // -----------------------------------------------------------------------
  747. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator<<=( size_type n );
  748. //! Shifts the bits in this bitset to the right by `n`
  749. //! positions.
  750. //!
  751. //! For each bit in the bitset, the bit at position `pos` takes
  752. //! on the previous value of the bit at position `pos + n`, or
  753. //! zero if no such bit exists.
  754. //!
  755. //! \return
  756. //! `*this`.
  757. //!
  758. //! \par Throws
  759. //! Nothing.
  760. // -----------------------------------------------------------------------
  761. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & operator>>=( size_type n );
  762. //! Returns a shifted copy of `*this`.
  763. //!
  764. //! \return
  765. //! A copy of `*this` shifted to the left by `n` positions. For
  766. //! each bit in the returned bitset, the bit at position `pos`
  767. //! takes on the value of the bit at position `pos - n` of this
  768. //! bitset, or zero if no such bit exists.
  769. //!
  770. //! \par Throws
  771. //! An allocation error if memory is exhausted (`std::bad_alloc`
  772. //! if `allocator_type` is a `std::allocator`).
  773. // -----------------------------------------------------------------------
  774. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset operator<<( size_type n ) const;
  775. //! Returns a shifted copy of `*this`.
  776. //!
  777. //! \return
  778. //! A copy of `*this` shifted to the right by `n` positions. For
  779. //! each bit in the returned bitset, the bit at position `pos`
  780. //! takes on the value of the bit at position `pos + n` of this
  781. //! bitset, or zero if no such bit exists.
  782. //!
  783. //! \par Throws
  784. //! An allocation error if memory is exhausted (`std::bad_alloc`
  785. //! if `allocator_type` is a `std::allocator`).
  786. // -----------------------------------------------------------------------
  787. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset operator>>( size_type n ) const;
  788. //! Sets the bits in the range `[pos, pos + len)` to `val`.
  789. //!
  790. //! If `len` is zero, does nothing. Otherwise, sets all the bits
  791. //! in this bitset which have a position in `[pos, pos + len -
  792. //! 1]` to `val`.
  793. //!
  794. //! \pre
  795. //! `pos + len <= this->size()`.
  796. //!
  797. //! \param pos The position of the first bit to set.
  798. //! \param len The number of bits to set.
  799. //! \param val The value to set the bits to.
  800. //!
  801. //! \return
  802. //! `*this`.
  803. // -----------------------------------------------------------------------
  804. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & set( size_type pos, size_type len, bool val /* = true */ ); // default would make it ambiguous
  805. //! Sets the bit at position `pos` in this bitset to `val`.
  806. //!
  807. //! \pre
  808. //! `pos < this->size()`.
  809. //!
  810. //! \param pos The position of the bit to set or clear.
  811. //! \param val The value to set the bit to.
  812. //!
  813. //! \return
  814. //! `*this`.
  815. // -----------------------------------------------------------------------
  816. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & set( size_type pos, bool val = true );
  817. //! Sets all the bits in this bitset.
  818. //!
  819. //! \return
  820. //! `*this`.
  821. //!
  822. //! \par Throws
  823. //! Nothing.
  824. // -----------------------------------------------------------------------
  825. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & set();
  826. //! If `len` is zero, does nothing. Otherwise, resets all the
  827. //! bits in this bitset which have a position in `[pos, pos +
  828. //! len - 1]`.
  829. //!
  830. //! \pre
  831. //! `pos + len <= this->size()`.
  832. //!
  833. //! \oaram pos The position of the lowest bit to reset.
  834. //! \param len The number of bits to reset.
  835. //!
  836. //! \return
  837. //! `*this`.
  838. // -----------------------------------------------------------------------
  839. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & reset( size_type pos, size_type len );
  840. //! Resets the bit in this bitset at position `pos`.
  841. //!
  842. //! \pre
  843. //! `pos < this->size()`.
  844. //!
  845. //! \param pos The position of the bit to reset.
  846. //!
  847. //! \return
  848. //! `this`.
  849. // -----------------------------------------------------------------------
  850. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & reset( size_type pos );
  851. //! Resets all the bits in this bitset.
  852. //!
  853. //! \return
  854. //! `*this`.
  855. // -----------------------------------------------------------------------
  856. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & reset();
  857. //! Toggles the bits in the range `[pos, pos + len)`.
  858. //!
  859. //! If `len` is zero, does nothing. Otherwise, toggles all the
  860. //! bits in this bitset which have a position in `[pos, pos +
  861. //! len - 1]`.
  862. //!
  863. //! \pre
  864. //! `pos + len <= this->size()`.
  865. //!
  866. //! \param pos The position of the lowest bit to toggle.
  867. //! \param len The number of bits to toggle.
  868. //!
  869. //! \return
  870. //! `*this`.
  871. // -----------------------------------------------------------------------
  872. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & flip( size_type pos, size_type len );
  873. //! Toggles the bit at position `pos` in this bitset.
  874. //!
  875. //! \pre
  876. //! `pos < this->size()`.
  877. //!
  878. //! \param pos The position of the bit to toggle.
  879. //!
  880. //! \return
  881. //! `*this`.
  882. // -----------------------------------------------------------------------
  883. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & flip( size_type pos );
  884. //! Toggles the value of every bit in this bitset.
  885. //!
  886. //! \return
  887. //! `*this`.
  888. //!
  889. //! \par Throws
  890. //! Nothing.
  891. // -----------------------------------------------------------------------
  892. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & flip();
  893. //! A checked version of `operator[]()`.
  894. //!
  895. //! \param pos The position of the bit to test.
  896. //!
  897. //! \return
  898. //! The same as `operator[]( pos )`.
  899. //!
  900. //! \par Throws
  901. //! `std::out_of_range` if `pos` is not within the range of the
  902. //! bitset.
  903. // -----------------------------------------------------------------------
  904. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference at( size_type pos );
  905. //! A checked version of `operator[]()`.
  906. //!
  907. //! \param pos The position of the bit to test.
  908. //!
  909. //! \return
  910. //! The same as `operator[]( pos )`.
  911. //!
  912. //! \par Throws
  913. //! `std::out_of_range` if `pos` is not within the range of the
  914. //! bitset.
  915. // -----------------------------------------------------------------------
  916. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool at( size_type pos ) const;
  917. //! Tests the bit at the given position.
  918. //!
  919. //! \pre
  920. //! `pos < this->size()`.
  921. //!
  922. //! \param pos The position of the bit to test.
  923. //!
  924. //! \return
  925. //! `true` if bit `pos` is set, and `false` if it is zero.
  926. // -----------------------------------------------------------------------
  927. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool test( size_type pos ) const;
  928. //! Sets bit `pos` if `val` is `true`, and clears it if `val` is
  929. //! `false`.
  930. //!
  931. //! \pre
  932. //! `pos < this->size()`.
  933. //!
  934. //! \param pos The position of the bit to set or clear.
  935. //! \param val The value to set the bit at position `pos` to.
  936. //!
  937. //! \return
  938. //! The previous state of bit `pos`.
  939. // -----------------------------------------------------------------------
  940. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool test_set( size_type pos, bool val = true );
  941. //! Checks whether all bits in `*this` are set.
  942. //!
  943. //! \return
  944. //! `true` if all bits in this bitset are set or if `size() ==
  945. //! 0`; otherwise `false`.
  946. //!
  947. //! \par Throws
  948. //! Nothing.
  949. // -----------------------------------------------------------------------
  950. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool all() const;
  951. //! Checks whether any bits in `*this` are set.
  952. //!
  953. //! \return
  954. //! `true` if any bits in this bitset are set, otherwise
  955. //! `false`.
  956. //!
  957. //! \par Throws
  958. //! Nothing.
  959. // -----------------------------------------------------------------------
  960. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool any() const;
  961. //! Checks whether this bitset has no set bit.
  962. //!
  963. //! \return
  964. //! `true` if no bits in this bitset are set, otherwise `false`.
  965. // -----------------------------------------------------------------------
  966. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool none() const;
  967. //! Returns a copy of `*this` with all of its bits toggled.
  968. //!
  969. //! \return A copy of `*this` with all of its bits toggled.
  970. //!
  971. //! \par Throws
  972. //! An allocation error if memory is exhausted (`std::bad_alloc`
  973. //! if `allocator_type` is a `std::allocator`).
  974. // -----------------------------------------------------------------------
  975. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset operator~() const;
  976. //! Returns the number of bits in this bitset that are set.
  977. //!
  978. //! \par Throws
  979. //! Nothing.
  980. // -----------------------------------------------------------------------
  981. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type count() const noexcept;
  982. //! Returns a `reference` to the bit at position `pos`.
  983. //!
  984. //! \pre
  985. //! `pos < this->size()`.
  986. //!
  987. //! \return
  988. //! A `reference` to bit `pos`. Note that `reference` is a proxy
  989. //! class with an assignment operator and a conversion to
  990. //! `bool`, which allows you to use `operator[]` for assignment.
  991. //! That is, you can write both `x = b[ n ]` and `b[ n ] = x`.
  992. //! However, in many other respects the proxy is not the same as
  993. //! the true reference type `bool &`.
  994. // -----------------------------------------------------------------------
  995. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference operator[]( size_type pos );
  996. //! The same as `test( pos )`.
  997. //!
  998. //! \pre
  999. //! `pos < this->size()`.
  1000. //!
  1001. //! \return
  1002. //! The same as `test( pos )`.
  1003. // -----------------------------------------------------------------------
  1004. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator[]( size_type pos ) const;
  1005. //! Returns the numeric value corresponding to the bits in
  1006. //! `*this`.
  1007. //!
  1008. //! \par Throws
  1009. //! `std::overflow_error` if that value is too large to be
  1010. //! represented in an `unsigned long`, i.e. if `*this` has any
  1011. //! non-zero bit at a position >= `std::numeric_limits< unsigned
  1012. //! long >::digits`.
  1013. //!
  1014. //! \return
  1015. //! The numeric value corresponding to the bits in `*this`.
  1016. // -----------------------------------------------------------------------
  1017. BOOST_DYNAMIC_BITSET_CONSTEXPR20 unsigned long to_ulong() const;
  1018. //! Returns the number of bits in this bitset.
  1019. //!
  1020. //! \par Throws
  1021. //! Nothing.
  1022. // -----------------------------------------------------------------------
  1023. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type size() const noexcept;
  1024. //! Returns the number of blocks in this bitset.
  1025. //!
  1026. //! \par Throws
  1027. //! Nothing.
  1028. // -----------------------------------------------------------------------
  1029. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type num_blocks() const noexcept;
  1030. //! Returns the maximum size of a bitset of this type.
  1031. //!
  1032. //! \par Throws
  1033. //! Nothing.
  1034. //!
  1035. //! \return
  1036. //! The maximum size of a `dynamic_bitset` object having the
  1037. //! same type as `*this`. Note that if any `dynamic_bitset`
  1038. //! operation causes `size()` to exceed `max_size()` then
  1039. //! <em>the behavior is undefined</em>.
  1040. //!
  1041. //! [The semantics of this function could change slightly when
  1042. //! lib issue 197 will be closed.]
  1043. // -----------------------------------------------------------------------
  1044. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type max_size() const noexcept;
  1045. //! Checks whether this bitset has size zero.
  1046. //!
  1047. //! \return
  1048. //! `this->size() == 0`.
  1049. //!
  1050. //! \par Note
  1051. //! Not to be confused with `none()`, which has different
  1052. //! semantics.
  1053. //!
  1054. //! \par Throws
  1055. //! Nothing.
  1056. // -----------------------------------------------------------------------
  1057. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool empty() const noexcept;
  1058. //! Returns the total number of elements that `*this` can hold
  1059. //! without requiring reallocation.
  1060. //!
  1061. //! \return The abovementioned number of elements.
  1062. //!
  1063. //! \par Throws
  1064. //! Nothing.
  1065. // -----------------------------------------------------------------------
  1066. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type capacity() const noexcept;
  1067. //! Informs the bitset of a planned change in size, so that it
  1068. //! can manage the storage allocation accordingly.
  1069. //!
  1070. //! After `reserve()`, `capacity()` is greater or equal to the
  1071. //! argument of `reserve()` if reallocation happens; and equal
  1072. //! to the previous value of `capacity()` otherwise.
  1073. //! Reallocation happens at this point if and only if the
  1074. //! current capacity is less than the argument of `reserve()`.
  1075. //!
  1076. //! \param num_bits The number of bits the bitset should be able
  1077. //! to store without reallocation.
  1078. //!
  1079. //! \par Note
  1080. //! It does not change the size of the bitset.
  1081. // -----------------------------------------------------------------------
  1082. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void reserve( size_type num_bits );
  1083. //! Requests the bitset to reduce memory use by removing unused
  1084. //! capacity.
  1085. //!
  1086. //! \par Note
  1087. //! It does not change the size of the bitset.
  1088. //!
  1089. //! \par Throws
  1090. //! An allocation error if memory is exhausted (`std::bad_alloc`
  1091. //! if `allocator_type` is a `std::allocator`).
  1092. // -----------------------------------------------------------------------
  1093. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void shrink_to_fit();
  1094. //! Checks whether `*this` is a subset of `b`.
  1095. //!
  1096. //! \pre
  1097. //! `this->size() == b.size()`.
  1098. //!
  1099. //! \param b The bitset to test `*this` against.
  1100. //!
  1101. //! \return
  1102. //! `true` if this bitset is a subset of bitset `b`. That is, it
  1103. //! returns `true` if, for every bit that is set in this bitset,
  1104. //! the corresponding bit in bitset `b` is also set. Otherwise
  1105. //! this function returns `false`.
  1106. // -----------------------------------------------------------------------
  1107. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool is_subset_of( const dynamic_bitset & b ) const;
  1108. //! Checks whether `*this` is a proper subset of `b`.
  1109. //!
  1110. //! \pre
  1111. //! `this->size() == b.size()`.
  1112. //!
  1113. //! \param b The bitset to test `*this` against.
  1114. //!
  1115. //! \return
  1116. //! `true` if this bitset is a proper subset of bitset `b`. That
  1117. //! is, it returns `true` if, for every bit that is set in this
  1118. //! bitset, the corresponding bit in bitset a is also set and if
  1119. //! `this->count() < b.count()`. Otherwise this function returns
  1120. //! `false`.
  1121. // -----------------------------------------------------------------------
  1122. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool is_proper_subset_of( const dynamic_bitset & b ) const;
  1123. //! Checks whether `*this` intersects with `b`.
  1124. //!
  1125. //! \pre
  1126. //! `this->size() == b.size()`.
  1127. //!
  1128. //! \param b The bitset to test `*this` against.
  1129. //!
  1130. //! \return
  1131. //! `true` if this bitset and `b` intersect. That is, it returns
  1132. //! `true` if there is a bit which is set in this bitset, such
  1133. //! that the corresponding bit in bitset `b` is also set.
  1134. //! Otherwise this function returns `false`.
  1135. // -----------------------------------------------------------------------
  1136. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool intersects( const dynamic_bitset & b ) const;
  1137. //! Finds the first set bit in `*this` with an index >= `pos`,
  1138. //! if any.
  1139. //!
  1140. //! \return
  1141. //! The lowest index `i` greater than or equal to `pos` such
  1142. //! that bit `i` is set in `*this`, or `npos` if no such index
  1143. //! exists.
  1144. //!
  1145. //! \par Throws
  1146. //! Nothing.
  1147. // -----------------------------------------------------------------------
  1148. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type find_first( size_type pos = 0 ) const;
  1149. //! Finds the first unset bit in `*this` with an index >= `pos`,
  1150. //! if any.
  1151. //!
  1152. //! \param pos The lower bound (inclusively) to start the search
  1153. //! from.
  1154. //!
  1155. //! \return
  1156. //! The lowest index `i` greater than or equal to `pos` such
  1157. //! that bit `i` is unset in `*this`, or `npos` if no such index
  1158. //! exists.
  1159. //!
  1160. //! \par Throws
  1161. //! Nothing.
  1162. // -----------------------------------------------------------------------
  1163. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type find_first_off( size_type pos = 0 ) const;
  1164. //! Finds the first bit set in `*this` with an index > `pos`, if
  1165. //! any.
  1166. //!
  1167. //! \param pos The lower bound (exclusively) to start the search
  1168. //! from.
  1169. //!
  1170. //! \return
  1171. //! The lowest index `i` greater than `pos` such that bit `i` is
  1172. //! set, or `npos` if no such index exists.
  1173. //!
  1174. //! \par Throws
  1175. //! Nothing.
  1176. // -----------------------------------------------------------------------
  1177. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type find_next( size_type pos ) const;
  1178. //! Finds the first unset bit in `*this` with an index > `pos`,
  1179. //! if any.
  1180. //!
  1181. //! \param pos The lower bound (exclusively) to start the search
  1182. //! from.
  1183. //!
  1184. //! \return
  1185. //! The lowest index `i` greater than `pos` such that bit `i` is
  1186. //! unset, or `npos` if no such index exists.
  1187. // -----------------------------------------------------------------------
  1188. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type find_next_off( size_type pos ) const;
  1189. template< typename B, typename A >
  1190. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator==( const dynamic_bitset< B, A > & a, const dynamic_bitset< B, A > & b );
  1191. template< typename B, typename A >
  1192. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator<( const dynamic_bitset< B, A > & a, const dynamic_bitset< B, A > & b );
  1193. template< typename B, typename A, typename BlockOutputIterator >
  1194. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 void to_block_range( const dynamic_bitset< B, A > & b, BlockOutputIterator result );
  1195. template< typename BlockIterator, typename B, typename A >
  1196. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 void from_block_range( BlockIterator first, BlockIterator last, dynamic_bitset< B, A > & result );
  1197. template< typename CharT, typename Traits, typename B, typename A >
  1198. friend std::basic_istream< CharT, Traits > & operator>>( std::basic_istream< CharT, Traits > & is, dynamic_bitset< B, A > & b );
  1199. template< typename B, typename A, typename StringT >
  1200. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 void to_string_helper( const dynamic_bitset< B, A > & b, StringT & s, bool dump_all );
  1201. //! Computes a hash value for a `dynamic_bitset`.
  1202. //!
  1203. //! This enables the use of `dynamic_bitset` in hash-based
  1204. //! containers such as `boost::unordered_map` or
  1205. //! `boost::unordered_set`.
  1206. //!
  1207. //! \return The computed hash value.
  1208. // -----------------------------------------------------------------------
  1209. template< typename B, typename A >
  1210. friend std::size_t hash_value( const dynamic_bitset< B, A > & a );
  1211. //! Optional zero-copy serialization support.
  1212. // -----------------------------------------------------------------------
  1213. class serialize_impl;
  1214. friend class serialize_impl;
  1215. private:
  1216. static constexpr int ulong_width = std::numeric_limits< unsigned long >::digits;
  1217. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset & range_operation( size_type pos, size_type len, Block ( *partial_block_operation )( Block, size_type, size_type ), Block ( *full_block_operation )( Block ) );
  1218. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void m_zero_unused_bits();
  1219. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool m_check_invariants() const;
  1220. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static bool m_not_empty( Block x );
  1221. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static bool m_not_full( Block x );
  1222. BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type m_do_find_from( size_type first_block, bool value ) const;
  1223. BOOST_DYNAMIC_BITSET_CONSTEXPR20 int count_extra_bits() const noexcept;
  1224. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static size_type block_index( size_type pos ) noexcept;
  1225. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static int bit_index( size_type pos ) noexcept;
  1226. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block bit_mask( size_type pos ) noexcept;
  1227. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block bit_mask( size_type first, size_type last ) noexcept;
  1228. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block set_block_bits( Block block, size_type first, size_type last, bool val ) noexcept;
  1229. // Functions for operations on ranges
  1230. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block set_block_partial( Block block, size_type first, size_type last ) noexcept;
  1231. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block set_block_full( Block ) noexcept;
  1232. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block reset_block_partial( Block block, size_type first, size_type last ) noexcept;
  1233. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block reset_block_full( Block ) noexcept;
  1234. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block flip_block_partial( Block block, size_type first, size_type last ) noexcept;
  1235. BOOST_DYNAMIC_BITSET_CONSTEXPR20 static Block flip_block_full( Block block ) noexcept;
  1236. template< typename T >
  1237. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void dispatch_init( T num_bits, unsigned long value, detail::dynamic_bitset_impl::value_to_type< true > );
  1238. template< typename T >
  1239. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void dispatch_init( T first, T last, detail::dynamic_bitset_impl::value_to_type< false > );
  1240. template< typename BlockIter >
  1241. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void init_from_block_range( BlockIter first, BlockIter last );
  1242. template< typename CharT, typename Traits = std::char_traits< CharT > >
  1243. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void init_from_string( const CharT * s, std::size_t string_length, std::size_t pos, std::size_t n, size_type num_bits );
  1244. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void init_from_unsigned_long( size_type num_bits, unsigned long value /*,
  1245. const allocator_type& alloc*/
  1246. );
  1247. template< typename BlockInputIterator >
  1248. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void m_append( BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag );
  1249. template< typename BlockInputIterator >
  1250. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void m_append( BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag );
  1251. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool m_unchecked_test( size_type pos ) const;
  1252. static BOOST_DYNAMIC_BITSET_CONSTEXPR20 size_type calc_num_blocks( size_type num_bits );
  1253. BOOST_DYNAMIC_BITSET_CONSTEXPR20 Block & m_highest_block();
  1254. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const Block & m_highest_block() const;
  1255. buffer_type m_bits;
  1256. size_type m_num_bits;
  1257. class bit_appender;
  1258. friend class bit_appender;
  1259. class bit_appender
  1260. {
  1261. // Helper for stream >>.
  1262. //
  1263. // Makes up for the lack of an efficient append at the least
  1264. // significant end: bits are actually appended "at left" but
  1265. // rearranged in the destructor.
  1266. //
  1267. dynamic_bitset & bs;
  1268. size_type n;
  1269. Block mask;
  1270. Block * current;
  1271. public:
  1272. bit_appender( const bit_appender & ) = delete;
  1273. bit_appender & operator=( const bit_appender & ) = delete;
  1274. bit_appender( dynamic_bitset & r );
  1275. ~bit_appender();
  1276. void do_append( bool value );
  1277. size_type get_count() const;
  1278. };
  1279. };
  1280. template< typename Iterator >
  1281. class bit_iterator_base
  1282. {
  1283. public:
  1284. typedef typename Iterator::iterator_category iterator_category;
  1285. typedef bool value_type;
  1286. typedef std::ptrdiff_t difference_type;
  1287. typedef value_type * pointer;
  1288. typedef value_type & reference;
  1289. static constexpr int bits_per_block = std::numeric_limits< typename Iterator::value_type >::digits;
  1290. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator_base( Iterator block_iterator, int bit_index );
  1291. template< typename Iter >
  1292. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator==( const bit_iterator_base< Iter > & lhs, const bit_iterator_base< Iter > & rhs );
  1293. template< typename Iter >
  1294. friend BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator<( const bit_iterator_base< Iter > & lhs, const bit_iterator_base< Iter > & rhs );
  1295. protected:
  1296. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void increment();
  1297. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void decrement();
  1298. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void add( typename Iterator::difference_type n );
  1299. Iterator m_block_iterator;
  1300. int m_bit_index = 0;
  1301. };
  1302. template< typename DynamicBitset >
  1303. class bit_iterator
  1304. : public bit_iterator_base< typename DynamicBitset::buffer_type::iterator >
  1305. {
  1306. public:
  1307. typedef typename DynamicBitset::reference reference;
  1308. typedef reference * pointer;
  1309. typedef typename bit_iterator_base< typename DynamicBitset::buffer_type::iterator >::difference_type difference_type;
  1310. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator();
  1311. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator( typename DynamicBitset::buffer_type::iterator block_iterator, int bit_index );
  1312. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference operator*() const;
  1313. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator & operator++();
  1314. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator operator++( int );
  1315. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator & operator--();
  1316. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator operator--( int );
  1317. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator & operator+=( difference_type n );
  1318. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bit_iterator & operator-=( difference_type n );
  1319. BOOST_DYNAMIC_BITSET_CONSTEXPR20 reference operator[]( difference_type n ) const;
  1320. };
  1321. template< typename DynamicBitset >
  1322. class const_bit_iterator
  1323. : public bit_iterator_base< typename DynamicBitset::buffer_type::const_iterator >
  1324. {
  1325. public:
  1326. typedef bool reference;
  1327. typedef bool const_reference;
  1328. typedef const bool * pointer;
  1329. typedef typename bit_iterator_base< typename DynamicBitset::buffer_type::const_iterator >::difference_type difference_type;
  1330. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator( typename DynamicBitset::buffer_type::const_iterator block_iterator, int bit_index );
  1331. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator( const bit_iterator< DynamicBitset > & it );
  1332. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reference operator*() const;
  1333. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator & operator++();
  1334. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator operator++( int );
  1335. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator & operator--();
  1336. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator operator--( int );
  1337. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator & operator+=( difference_type n );
  1338. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_bit_iterator & operator-=( difference_type n );
  1339. BOOST_DYNAMIC_BITSET_CONSTEXPR20 const_reference operator[]( difference_type n ) const;
  1340. };
  1341. //! Compares two bitsets.
  1342. //!
  1343. //! \return
  1344. //! `true` if `a.size() == b.size()` and for all `i` in the range
  1345. //! `[0, a.size())`, `a[ i ] == b[ i ]`. Otherwise `false`.
  1346. //!
  1347. //! \par Throws
  1348. //! Nothing.
  1349. //!
  1350. //! (Required by <a href="https://en.cppreference.com/w/cpp/named_req/EqualityComparable">EqualityComparable</a>.)
  1351. // -----------------------------------------------------------------------
  1352. template< typename Block, typename AllocatorOrContainer >
  1353. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator==( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1354. //! Compares two bitsets.
  1355. //!
  1356. //! \return
  1357. //! `! ( a == b )`.
  1358. //!
  1359. //! \par Throws
  1360. //! Nothing.
  1361. // -----------------------------------------------------------------------
  1362. template< typename Block, typename AllocatorOrContainer >
  1363. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator!=( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1364. //! Compares two bitsets.
  1365. //!
  1366. //! \return
  1367. //! `true` if `a` is lexicographically less than `b`, otherwise
  1368. //! `false`.
  1369. //!
  1370. //! \par Throws
  1371. //! Nothing.
  1372. //!
  1373. //! (Required by <a href="https://en.cppreference.com/w/cpp/named_req/LessThanComparable">LessThanComparable</a>.)
  1374. // -----------------------------------------------------------------------
  1375. template< typename Block, typename AllocatorOrContainer >
  1376. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator<( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1377. //! Compares two bitsets.
  1378. //!
  1379. //! \return
  1380. //! `a < b || a == b`.
  1381. //!
  1382. //! \par Throws
  1383. //! Nothing.
  1384. // -----------------------------------------------------------------------
  1385. template< typename Block, typename AllocatorOrContainer >
  1386. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator<=( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1387. //! Compares two bitsets.
  1388. //!
  1389. //! \return
  1390. //! `b < a`.
  1391. //!
  1392. //! \par Throws
  1393. //! Nothing.
  1394. // -----------------------------------------------------------------------
  1395. template< typename Block, typename AllocatorOrContainer >
  1396. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator>( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1397. //! Compares two bitsets.
  1398. //!
  1399. //! \return
  1400. //! `b <= a`.
  1401. //!
  1402. //! \par Throws
  1403. //! Nothing.
  1404. // -----------------------------------------------------------------------
  1405. template< typename Block, typename AllocatorOrContainer >
  1406. BOOST_DYNAMIC_BITSET_CONSTEXPR20 bool operator>=( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1407. //! Inserts a textual representation of `b` into the stream `os`,
  1408. //! highest bit first.
  1409. //!
  1410. //! Informally, the output is the same as:
  1411. //!
  1412. //! \code
  1413. //! std::basic_string<Char, Traits> s;
  1414. //! boost::to_string(x, s):
  1415. //! os << s;
  1416. //! \endcode
  1417. //!
  1418. //! except that the stream inserter takes into accout the locale
  1419. //! imbued into `os`, which `boost::to_string()` can't do. More
  1420. //! precisely: First, for each valid position `i` into the bitset b
  1421. //! let's put: `character_of( b[ i ) ] ) = b[ i ] ? os.widen( '1' )
  1422. //! : os.widen( '0' );`. Let also `s` be a `std::basic_string<Char,
  1423. //! Traits>` object, having length `b.size()` and such that, for
  1424. //! each `i` in `[0, b.size())`, `s[ i ]` is `character_of( b[ i ]
  1425. //! )`. Then, the output, the effects on `os` and the exception
  1426. //! behavior is the same as outputting the object `s` to `os` (same
  1427. //! width, same exception mask, same padding, same `setstate()`
  1428. //! logic.)
  1429. //!
  1430. //! \return
  1431. //! `os`.
  1432. // -----------------------------------------------------------------------
  1433. template< typename CharT, typename Traits, typename Block, typename AllocatorOrContainer >
  1434. std::basic_ostream< CharT, Traits > &
  1435. operator<<( std::basic_ostream< CharT, Traits > & os, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1436. //! Extracts a `dynamic_bitset` from an input stream.
  1437. //!
  1438. //! \par Definitions
  1439. //! - A (non-eof) character `c` extracted from `is` is a <em>bitset
  1440. //! digit</em> if and only if either `Traits::eq(c, is.widen('0'))`
  1441. //! or `Traits::eq(c, is.widen('1'))` return `true`.
  1442. //!
  1443. //! - If `c` is a bitset digit, its corresponding bit value is 0 if
  1444. //! `Tr::eq(c, is.widen('0'))` returns true, 1 otherwise.
  1445. //!
  1446. //! The extractor begins by constructing a `sentry` object `k` as if
  1447. //! by `typename std::basic_istream< Char, Traits >::sentry k( is
  1448. //! )`. If `bool( k )` is `true`, it calls `b.clear()` then attempts
  1449. //! to extract characters from `is`. For each character `c` that is
  1450. //! a bitset digit, the corresponding bit value is appended to the
  1451. //! less significant end of `b` (appending may throw). If
  1452. //! `is.width()` is greater than zero and smaller than
  1453. //! `b.max_size()` then the maximum number `n` of bits appended is
  1454. //! `is.width()`; otherwise `n = b.max_size()`. Unless the extractor
  1455. //! is exited via an exception, characters are extracted (and
  1456. //! corresponding bits appended) until any of the following occurs:
  1457. //!
  1458. //! - `n` bits are stored into the bitset;
  1459. //! - end-of-file, or an error, occurs on the input sequence;
  1460. //! - the next available input character isn't a bitset digit.
  1461. //!
  1462. //! If no exception caused the function to exit then `is.width( 0 )`
  1463. //! is called, regardless of how many characters were actually
  1464. //! extracted. The sentry object `k` is destroyed.
  1465. //!
  1466. //! If the function extracts no characters, it calls `is.setstate(
  1467. //! std::ios::failbit )`, which may throw `std::ios_base::failure`.
  1468. //!
  1469. //! \return
  1470. //! `is`.
  1471. // -----------------------------------------------------------------------
  1472. template< typename CharT, typename Traits, typename Block, typename AllocatorOrContainer >
  1473. std::basic_istream< CharT, Traits > &
  1474. operator>>( std::basic_istream< CharT, Traits > & is, dynamic_bitset< Block, AllocatorOrContainer > & b );
  1475. //! Performs a bitwise-AND of two bitsets.
  1476. //!
  1477. //! \pre
  1478. //! `a.size() == b.size()`.
  1479. //!
  1480. //! \return
  1481. //! A new bitset which is the bitwise-AND of the bitsets `a` and
  1482. //! `b`.
  1483. //!
  1484. //! \par Throws
  1485. //! An allocation error if memory is exhausted (`std::bad_alloc` if
  1486. //! `AllocatorOrContainer` is a `std::allocator`).
  1487. // -----------------------------------------------------------------------
  1488. template< typename Block, typename AllocatorOrContainer >
  1489. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset< Block, AllocatorOrContainer >
  1490. operator&( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1491. //! Performs a bitwise-OR of two bitsets.
  1492. //!
  1493. //! \pre
  1494. //! `a.size() == b.size()`.
  1495. //!
  1496. //! \return
  1497. //! A new bitset which is the bitwise-OR of the bitsets `a` and `b`.
  1498. //!
  1499. //! \par Throws
  1500. //! An allocation error if memory is exhausted (`std::bad_alloc` if
  1501. //! `allocator_type` is a `std::allocator`).
  1502. // -----------------------------------------------------------------------
  1503. template< typename Block, typename AllocatorOrContainer >
  1504. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset< Block, AllocatorOrContainer >
  1505. operator|( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1506. //! Performs a bitwise-XOR of two bitsets.
  1507. //!
  1508. //! \pre
  1509. //! `a.size() == b.size()`.
  1510. //!
  1511. //! \return
  1512. //! A new bitset which is the bitwise-XOR of the bitsets `a` and
  1513. //! `b`.
  1514. //!
  1515. //! \par Throws
  1516. //! An allocation error if memory is exhausted (`std::bad_alloc` if
  1517. //! `allocator_type` is a `std::allocator`).
  1518. // -----------------------------------------------------------------------
  1519. template< typename Block, typename AllocatorOrContainer >
  1520. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset< Block, AllocatorOrContainer >
  1521. operator^( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1522. //! Calculates the set difference of two bitsets.
  1523. //!
  1524. //! \pre
  1525. //! `a.size() == b.size()`.
  1526. //!
  1527. //! \return
  1528. //! A new bitset which is the set difference of the bitsets `a` and
  1529. //! `b`.
  1530. //!
  1531. //! \par Throws
  1532. //! An allocation error if memory is exhausted (`std::bad_alloc` if
  1533. //! `allocator_type` is a `std::allocator`).
  1534. // -----------------------------------------------------------------------
  1535. template< typename Block, typename AllocatorOrContainer >
  1536. BOOST_DYNAMIC_BITSET_CONSTEXPR20 dynamic_bitset< Block, AllocatorOrContainer >
  1537. operator-( const dynamic_bitset< Block, AllocatorOrContainer > & a, const dynamic_bitset< Block, AllocatorOrContainer > & b );
  1538. //! Exchanges the contents of `a` and `b`.
  1539. //!
  1540. //! \param a The bitset to exchange the contents of with `b`.
  1541. //! \param b The bitset to exchange the contents of with `a`.
  1542. //!
  1543. //! \par Throws
  1544. //! Nothing.
  1545. // -----------------------------------------------------------------------
  1546. template< typename Block, typename AllocatorOrContainer >
  1547. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void swap( dynamic_bitset< Block, AllocatorOrContainer > & a, dynamic_bitset< Block, AllocatorOrContainer > & b ) noexcept;
  1548. //! Copies a representation of `b` into the string `s`.
  1549. //!
  1550. //! Character position `i` in the string corresponds to bit position
  1551. //! `b.size() - 1 - i`.
  1552. //!
  1553. //! \par Throws
  1554. //! An allocation error from `s` if memory is exhausted.
  1555. //!
  1556. //! \par Rationale
  1557. //! This function is not a member function taking zero arguments and
  1558. //! returning a string for a couple of historical reasons. First, this
  1559. //! version could be slightly more efficient because the string is not
  1560. //! copied. Second, as a member function, to allow for flexibility with
  1561. //! regards to the template parameters of `basic_string`, the member
  1562. //! function would require explicit template parameters. Few C++ programmers
  1563. //! were familiar with explicit template parameters, and some C++ compilers
  1564. //! did not handle them properly.
  1565. //!
  1566. //! \param b The bitset of which to copy the representation.
  1567. //! \param s The string in which to copy the representation.
  1568. // -----------------------------------------------------------------------
  1569. template< typename Block, typename AllocatorOrContainer, typename StringT >
  1570. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void
  1571. to_string( const dynamic_bitset< Block, AllocatorOrContainer > & b, StringT & s );
  1572. //! Writes the bits of the bitset into the iterator `result`, a
  1573. //! block at a time.
  1574. //!
  1575. //! The first block written represents the bits in the position
  1576. //! range `[0, bits_per_block)` in the bitset, the second block
  1577. //! written the bits in the range `[bits_per_block, 2 \* bits_per_block)`,
  1578. //! and so on. For each block `bval` written, the bit
  1579. //! `( bval >> i ) & 1` corresponds to the bit at position
  1580. //! `b \* bits_per_block + i` in the bitset.
  1581. //!
  1582. //! \pre
  1583. //! The type `BlockOutputIterator` must be a model of
  1584. //! <a href="https://en.cppreference.com/w/cpp/named_req/OutputIterator">LegacyOutputIterator</a>
  1585. //! and its `value_type` must be the same type as `Block`.
  1586. //! Furthermore, the size of the output range must be greater than
  1587. //! or equal to `b.num_blocks()`.
  1588. //!
  1589. //! \param b The bitset of which to copy the bits.
  1590. //! \param result The start of the range to write to.
  1591. // -----------------------------------------------------------------------
  1592. template< typename Block, typename AllocatorOrContainer, typename BlockOutputIterator >
  1593. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void
  1594. to_block_range( const dynamic_bitset< Block, AllocatorOrContainer > & b, BlockOutputIterator result );
  1595. //! Reads blocks from the iterator range into the bitset.
  1596. //!
  1597. //! \pre
  1598. //! The type `BlockIterator` must be a model of
  1599. //! <a href="https://en.cppreference.com/w/cpp/named_req/InputIterator">LegacyInputIterator</a>
  1600. //! and its `value_type` must be the same type as `Block`. The size
  1601. //! of the iterator range must be less than or equal to
  1602. //! `b.num_blocks()`.
  1603. //!
  1604. //! \param first The start of the range.
  1605. //! \param last The end of the range.
  1606. //! \param result The resulting bitset.
  1607. // -----------------------------------------------------------------------
  1608. template< typename BlockIterator, typename Block, typename AllocatorOrContainer >
  1609. BOOST_DYNAMIC_BITSET_CONSTEXPR20 void
  1610. from_block_range( BlockIterator first, BlockIterator last, dynamic_bitset< Block, AllocatorOrContainer > & result );
  1611. }
  1612. #include "boost/dynamic_bitset/impl/dynamic_bitset.ipp"
  1613. #endif // include guard