libstdc++
dynamic_bitset
Go to the documentation of this file.
1// TR2 <dynamic_bitset> -*- C++ -*-
2
3// Copyright (C) 2009-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file tr2/dynamic_bitset
26 * This is a TR2 C++ Library header.
27 */
28
29#ifndef _GLIBCXX_TR2_DYNAMIC_BITSET
30#define _GLIBCXX_TR2_DYNAMIC_BITSET 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#include <limits>
37#include <vector>
38#include <string>
39#include <istream>
40#include <bits/functexcept.h>
41#include <bits/stl_algo.h> // For fill
42#include <bits/cxxabi_forced.h>
43
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48namespace tr2
49{
50 /**
51 * @defgroup dynamic_bitset Dynamic Bitset.
52 * @ingroup extensions
53 *
54 * @{
55 */
56
57 /**
58 * Base class, general case.
59 *
60 * See documentation for dynamic_bitset.
61 */
62 template<typename _WordT = unsigned long long,
63 typename _Alloc = std::allocator<_WordT>>
64 struct __dynamic_bitset_base
65 {
66 static_assert(std::is_unsigned<_WordT>::value, "template argument "
67 "_WordT not an unsigned integral type");
68
69 typedef _WordT block_type;
70 typedef _Alloc allocator_type;
71 typedef size_t size_type;
72
73 static const size_type _S_bits_per_block = __CHAR_BIT__ * sizeof(block_type);
74 static const size_type npos = static_cast<size_type>(-1);
75
76 /// 0 is the least significant word.
77 std::vector<block_type, allocator_type> _M_w;
78
79 explicit
80 __dynamic_bitset_base(const allocator_type& __alloc)
81 : _M_w(__alloc)
82 { }
83
84 __dynamic_bitset_base() = default;
85 __dynamic_bitset_base(const __dynamic_bitset_base&) = default;
86 __dynamic_bitset_base(__dynamic_bitset_base&& __b) = default;
87 __dynamic_bitset_base& operator=(const __dynamic_bitset_base&) = default;
88 __dynamic_bitset_base& operator=(__dynamic_bitset_base&&) = default;
89 ~__dynamic_bitset_base() = default;
90
91 explicit
92 __dynamic_bitset_base(size_type __nbits, unsigned long long __val = 0ULL,
93 const allocator_type& __alloc = allocator_type())
94 : _M_w(__nbits / _S_bits_per_block + (__nbits % _S_bits_per_block > 0),
95 block_type(0), __alloc)
96 {
97 if (__nbits < std::numeric_limits<decltype(__val)>::digits)
98 __val &= ~(-1ULL << __nbits);
99 if (__val == 0)
100 return;
101
102#pragma GCC diagnostic push
103#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
104 if constexpr (sizeof(__val) == sizeof(block_type))
105 _M_w[0] = __val;
106 else
107 {
108 const size_t __n
109 = std::min(_M_w.size(), sizeof(__val) / sizeof(block_type));
110 for (size_t __i = 0; __val && __i < __n; ++__i)
111 {
112 _M_w[__i] = static_cast<block_type>(__val);
113 __val >>= _S_bits_per_block;
114 }
115 }
116#pragma GCC diagnostic pop
117 }
118
119 void
120 _M_swap(__dynamic_bitset_base& __b) noexcept
121 { this->_M_w.swap(__b._M_w); }
122
123 void
124 _M_clear() noexcept
125 { this->_M_w.clear(); }
126
127 void
128 _M_resize(size_t __nbits, bool __value)
129 {
130 size_t __sz = __nbits / _S_bits_per_block;
131 if (__nbits % _S_bits_per_block > 0)
132 ++__sz;
133 if (__sz != this->_M_w.size())
134 {
135 block_type __val = 0;
136 if (__value)
137 __val = std::numeric_limits<block_type>::max();
138 this->_M_w.resize(__sz, __val);
139 }
140 }
141
142 allocator_type
143 _M_get_allocator() const noexcept
144 { return this->_M_w.get_allocator(); }
145
146 static size_type
147 _S_whichword(size_type __pos) noexcept
148 { return __pos / _S_bits_per_block; }
149
150 static size_type
151 _S_whichbyte(size_type __pos) noexcept
152 { return (__pos % _S_bits_per_block) / __CHAR_BIT__; }
153
154 static size_type
155 _S_whichbit(size_type __pos) noexcept
156 { return __pos % _S_bits_per_block; }
157
158 static block_type
159 _S_maskbit(size_type __pos) noexcept
160 { return (static_cast<block_type>(1)) << _S_whichbit(__pos); }
161
162 block_type&
163 _M_getword(size_type __pos) noexcept
164 { return this->_M_w[_S_whichword(__pos)]; }
165
166 block_type
167 _M_getword(size_type __pos) const noexcept
168 { return this->_M_w[_S_whichword(__pos)]; }
169
170 block_type&
171 _M_hiword() noexcept
172 { return this->_M_w[_M_w.size() - 1]; }
173
174 block_type
175 _M_hiword() const noexcept
176 { return this->_M_w[_M_w.size() - 1]; }
177
178 void
179 _M_do_and(const __dynamic_bitset_base& __x) noexcept
180 {
181 if (__x._M_w.size() == this->_M_w.size())
182 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
183 this->_M_w[__i] &= __x._M_w[__i];
184 else
185 return;
186 }
187
188 void
189 _M_do_or(const __dynamic_bitset_base& __x) noexcept
190 {
191 if (__x._M_w.size() == this->_M_w.size())
192 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
193 this->_M_w[__i] |= __x._M_w[__i];
194 else
195 return;
196 }
197
198 void
199 _M_do_xor(const __dynamic_bitset_base& __x) noexcept
200 {
201 if (__x._M_w.size() == this->_M_w.size())
202 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
203 this->_M_w[__i] ^= __x._M_w[__i];
204 else
205 return;
206 }
207
208 void
209 _M_do_dif(const __dynamic_bitset_base& __x) noexcept
210 {
211 if (__x._M_w.size() == this->_M_w.size())
212 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
213 this->_M_w[__i] &= ~__x._M_w[__i];
214 else
215 return;
216 }
217
218 void
219 _M_do_left_shift(size_t __shift);
220
221 void
222 _M_do_right_shift(size_t __shift);
223
224 void
225 _M_do_flip() noexcept
226 {
227 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
228 this->_M_w[__i] = ~this->_M_w[__i];
229 }
230
231 void
232 _M_do_set() noexcept
233 {
234 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
235 this->_M_w[__i] = static_cast<block_type>(-1);
236 }
237
238 void
239 _M_do_reset() noexcept
240 {
241 std::fill(_M_w.begin(), _M_w.end(), static_cast<block_type>(0));
242 }
243
244 bool
245 _M_is_equal(const __dynamic_bitset_base& __x) const noexcept
246 {
247 if (__x._M_w.size() == this->_M_w.size())
248 {
249 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
250 if (this->_M_w[__i] != __x._M_w[__i])
251 return false;
252 return true;
253 }
254 else
255 return false;
256 }
257
258 bool
259 _M_is_less(const __dynamic_bitset_base& __x) const noexcept
260 {
261 if (__x._M_w.size() == this->_M_w.size())
262 {
263 for (size_t __i = this->_M_w.size(); __i > 0; --__i)
264 {
265 if (this->_M_w[__i-1] < __x._M_w[__i-1])
266 return true;
267 else if (this->_M_w[__i-1] > __x._M_w[__i-1])
268 return false;
269 }
270 return false;
271 }
272 else
273 return false;
274 }
275
276 size_t
277 _M_are_all_aux() const noexcept
278 {
279 for (size_t __i = 0; __i < this->_M_w.size() - 1; ++__i)
280 if (_M_w[__i] != static_cast<block_type>(-1))
281 return 0;
282 return ((this->_M_w.size() - 1) * _S_bits_per_block
283 + __builtin_popcountll(this->_M_hiword()));
284 }
285
286 bool
287 _M_is_any() const noexcept
288 {
289 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
290 if (this->_M_w[__i] != static_cast<block_type>(0))
291 return true;
292 return false;
293 }
294
295 bool
296 _M_is_subset_of(const __dynamic_bitset_base& __b) noexcept
297 {
298 if (__b._M_w.size() == this->_M_w.size())
299 {
300 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
301 if (this->_M_w[__i] != (this->_M_w[__i] | __b._M_w[__i]))
302 return false;
303 return true;
304 }
305 else
306 return false;
307 }
308
309 bool
310 _M_is_proper_subset_of(const __dynamic_bitset_base& __b) const noexcept
311 {
312 if (this->_M_is_subset_of(__b))
313 {
314 if (*this == __b)
315 return false;
316 else
317 return true;
318 }
319 else
320 return false;
321 }
322
323 size_t
324 _M_do_count() const noexcept
325 {
326 size_t __result = 0;
327 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
328 __result += __builtin_popcountll(this->_M_w[__i]);
329 return __result;
330 }
331
332 size_type
333 _M_size() const noexcept
334 { return this->_M_w.size(); }
335
336 unsigned long
337 _M_do_to_ulong() const;
338
339 unsigned long long
340 _M_do_to_ullong() const;
341
342 // find first "on" bit
343 size_type
344 _M_do_find_first(size_t __not_found) const;
345
346 // find the next "on" bit that follows "prev"
347 size_type
348 _M_do_find_next(size_t __prev, size_t __not_found) const;
349
350 // do append of block
351 void
352 _M_do_append_block(block_type __block, size_type __pos)
353 {
354 size_t __offset = __pos % _S_bits_per_block;
355 if (__offset == 0)
356 this->_M_w.push_back(__block);
357 else
358 {
359 this->_M_hiword() |= (__block << __offset);
360 this->_M_w.push_back(__block >> (_S_bits_per_block - __offset));
361 }
362 }
363 };
364
365 /**
366 * @brief The %dynamic_bitset class represents a sequence of bits.
367 *
368 * See N2050,
369 * Proposal to Add a Dynamically Sizeable Bitset to the Standard Library.
370 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf
371 *
372 * In the general unoptimized case, storage is allocated in
373 * word-sized blocks. Let B be the number of bits in a word, then
374 * (Nb+(B-1))/B words will be used for storage. B - Nb%B bits are
375 * unused. (They are the high-order bits in the highest word.) It
376 * is a class invariant that those unused bits are always zero.
377 *
378 * If you think of %dynamic_bitset as "a simple array of bits," be
379 * aware that your mental picture is reversed: a %dynamic_bitset
380 * behaves the same way as bits in integers do, with the bit at
381 * index 0 in the "least significant / right-hand" position, and
382 * the bit at index Nb-1 in the "most significant / left-hand"
383 * position. Thus, unlike other containers, a %dynamic_bitset's
384 * index "counts from right to left," to put it very loosely.
385 *
386 * This behavior is preserved when translating to and from strings.
387 * For example, the first line of the following program probably
388 * prints "b('a') is 0001100001" on a modern ASCII system.
389 *
390 * @code
391 * #include <dynamic_bitset>
392 * #include <iostream>
393 * #include <sstream>
394 *
395 * using namespace std;
396 *
397 * int main()
398 * {
399 * long a = 'a';
400 * dynamic_bitset<> b(a);
401 *
402 * cout << "b('a') is " << b << endl;
403 *
404 * ostringstream s;
405 * s << b;
406 * string str = s.str();
407 * cout << "index 3 in the string is " << str[3] << " but\n"
408 * << "index 3 in the bitset is " << b[3] << endl;
409 * }
410 * @endcode
411 *
412 * Most of the actual code isn't contained in %dynamic_bitset<>
413 * itself, but in the base class __dynamic_bitset_base. The base
414 * class works with whole words, not with individual bits. This
415 * allows us to specialize __dynamic_bitset_base for the important
416 * special case where the %dynamic_bitset is only a single word.
417 *
418 * Extra confusion can result due to the fact that the storage for
419 * __dynamic_bitset_base @e is a vector, and is indexed as such. This is
420 * carefully encapsulated.
421 */
422 template<typename _WordT = unsigned long long,
423 typename _Alloc = std::allocator<_WordT>>
424 class dynamic_bitset
425 : private __dynamic_bitset_base<_WordT, _Alloc>
426 {
427 static_assert(std::is_unsigned<_WordT>::value, "template argument "
428 "_WordT not an unsigned integral type");
429
430 public:
431
432 typedef __dynamic_bitset_base<_WordT, _Alloc> _Base;
433 typedef _WordT block_type;
434 typedef _Alloc allocator_type;
435 typedef size_t size_type;
436
437 static const size_type bits_per_block = __CHAR_BIT__ * sizeof(block_type);
438 // Use this: constexpr size_type std::numeric_limits<size_type>::max().
439 static const size_type npos = static_cast<size_type>(-1);
440
441 private:
442
443 // Clear the unused bits in the uppermost word.
444 void
445 _M_do_sanitize()
446 {
447 size_type __shift = this->_M_Nb % bits_per_block;
448 if (__shift > 0)
449 this->_M_hiword() &= block_type(~(block_type(-1) << __shift));
450 }
451
452 // Set the unused bits in the uppermost word.
453 void
454 _M_do_fill()
455 {
456 size_type __shift = this->_M_Nb % bits_per_block;
457 if (__shift > 0)
458 this->_M_hiword() |= block_type(block_type(-1) << __shift);
459 }
460
461 /**
462 * These versions of single-bit set, reset, flip, and test
463 * do no range checking.
464 */
465 dynamic_bitset&
466 _M_unchecked_set(size_type __pos) noexcept
467 {
468 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
469 return *this;
470 }
471
472 dynamic_bitset&
473 _M_unchecked_set(size_type __pos, int __val) noexcept
474 {
475 if (__val)
476 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
477 else
478 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
479 return *this;
480 }
481
482 dynamic_bitset&
483 _M_unchecked_reset(size_type __pos) noexcept
484 {
485 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
486 return *this;
487 }
488
489 dynamic_bitset&
490 _M_unchecked_flip(size_type __pos) noexcept
491 {
492 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
493 return *this;
494 }
495
496 bool
497 _M_unchecked_test(size_type __pos) const noexcept
498 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
499 != static_cast<_WordT>(0)); }
500
501 size_type _M_Nb = 0;
502
503 public:
504 /**
505 * This encapsulates the concept of a single bit. An instance
506 * of this class is a proxy for an actual bit; this way the
507 * individual bit operations are done as faster word-size
508 * bitwise instructions.
509 *
510 * Most users will never need to use this class directly;
511 * conversions to and from bool are automatic and should be
512 * transparent. Overloaded operators help to preserve the
513 * illusion.
514 *
515 * (On a typical system, this "bit %reference" is 64 times the
516 * size of an actual bit. Ha.)
517 */
518 class reference
519 {
520 friend class dynamic_bitset;
521
522 block_type *_M_wp;
523 size_type _M_bpos;
524
525 public:
526 reference(dynamic_bitset& __b, size_type __pos) noexcept
527 {
528 this->_M_wp = &__b._M_getword(__pos);
529 this->_M_bpos = _Base::_S_whichbit(__pos);
530 }
531
532 // For b[i] = __x;
533 reference&
534 operator=(bool __x) noexcept
535 {
536 if (__x)
537 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
538 else
539 *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
540 return *this;
541 }
542
543 // For b[i] = b[__j];
544 reference&
545 operator=(const reference& __j) noexcept
546 {
547 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
548 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
549 else
550 *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
551 return *this;
552 }
553
554 // Flips the bit
555 bool
556 operator~() const noexcept
557 { return (*(_M_wp) & _Base::_S_maskbit(this->_M_bpos)) == 0; }
558
559 // For __x = b[i];
560 operator bool() const noexcept
561 { return (*(this->_M_wp) & _Base::_S_maskbit(this->_M_bpos)) != 0; }
562
563 // For b[i].flip();
564 reference&
565 flip() noexcept
566 {
567 *this->_M_wp ^= _Base::_S_maskbit(this->_M_bpos);
568 return *this;
569 }
570 };
571
572 friend class reference;
573
574 typedef bool const_reference;
575
576 // 23.3.5.1 constructors:
577
578 /// All bits set to zero.
579 dynamic_bitset() = default;
580
581 /// All bits set to zero.
582 explicit
583 dynamic_bitset(const allocator_type& __alloc)
584 : _Base(__alloc)
585 { }
586
587 /// Initial bits bitwise-copied from a single word (others set to zero).
588 explicit
589 dynamic_bitset(size_type __nbits, unsigned long long __val = 0ULL,
590 const allocator_type& __alloc = allocator_type())
591 : _Base(__nbits, __val, __alloc),
592 _M_Nb(__nbits)
593 { }
594
595 dynamic_bitset(initializer_list<block_type> __il,
596 const allocator_type& __alloc = allocator_type())
597 : _Base(__alloc)
598 { this->append(__il); }
599
600 /**
601 * @brief Use a subset of a string.
602 * @param __str A string of '0' and '1' characters.
603 * @param __pos Index of the first character in @p __str to use.
604 * @param __n The number of characters to copy.
605 * @param __zero The character to use for unset bits.
606 * @param __one The character to use for set bits.
607 * @param __alloc An allocator.
608 * @throw std::out_of_range If @p __pos is bigger the size of @p __str.
609 * @throw std::invalid_argument If a character appears in the string
610 * which is neither '0' nor '1'.
611 */
612 template<typename _CharT, typename _Traits, typename _Alloc1>
613 explicit
614 dynamic_bitset(const std::basic_string<_CharT, _Traits, _Alloc1>& __str,
615 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
616 __pos = 0,
617 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
618 __n = std::basic_string<_CharT, _Traits, _Alloc1>::npos,
619 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'),
620 const allocator_type& __alloc = allocator_type())
621 : _Base(__alloc)
622 {
623 if (__pos > __str.size())
624 __throw_out_of_range(__N("dynamic_bitset::bitset initial position "
625 "not valid"));
626
627 // Watch for npos.
628 this->_M_Nb = (__n > __str.size() ? __str.size() - __pos : __n);
629 this->resize(this->_M_Nb);
630 this->_M_copy_from_string(__str, __pos, __n, __zero, __one);
631 }
632
633 /**
634 * @brief Construct from a string.
635 * @param __str A string of '0' and '1' characters.
636 * @param __alloc An allocator.
637 * @throw std::invalid_argument If a character appears in the string
638 * which is neither '0' nor '1'.
639 */
640 explicit
641 dynamic_bitset(const char* __str,
642 const allocator_type& __alloc = allocator_type())
643 : _Base(__builtin_strlen(__str), 0ULL, __alloc),
644 _M_Nb(__builtin_strlen(__str))
645 {
646 this->_M_copy_from_ptr(__str, _M_Nb, 0, _M_Nb);
647 }
648
649 /// Copy constructor.
650 dynamic_bitset(const dynamic_bitset&) = default;
651
652 /// Move constructor.
653 dynamic_bitset(dynamic_bitset&& __b) noexcept
654 : _Base(std::move(__b)), _M_Nb(__b._M_Nb)
655 { __b.clear(); }
656
657 /// Swap with another bitset.
658 void
659 swap(dynamic_bitset& __b) noexcept
660 {
661 this->_M_swap(__b);
662 std::swap(this->_M_Nb, __b._M_Nb);
663 }
664
665 /// Copy assignment operator.
666 dynamic_bitset& operator=(const dynamic_bitset&) = default;
667
668 /// Move assignment operator.
669 dynamic_bitset&
670 operator=(dynamic_bitset&& __b)
671 noexcept(std::is_nothrow_move_assignable<_Base>::value)
672 {
673#pragma GCC diagnostic push
674#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
675 static_cast<_Base&>(*this) = static_cast<_Base&&>(__b);
676 _M_Nb = __b._M_Nb;
677 if constexpr (std::is_nothrow_move_assignable<_Base>::value)
678 __b._M_Nb = 0;
679 else if (get_allocator() == __b.get_allocator())
680 __b._M_Nb = 0;
681 return *this;
682#pragma GCC diagnostic pop
683 }
684
685 /**
686 * @brief Return the allocator for the bitset.
687 */
688 allocator_type
689 get_allocator() const noexcept
690 { return this->_M_get_allocator(); }
691
692 /**
693 * @brief Resize the bitset.
694 */
695 void
696 resize(size_type __nbits, bool __value = false)
697 {
698 if (__value)
699 this->_M_do_fill();
700 this->_M_resize(__nbits, __value);
701 this->_M_Nb = __nbits;
702 this->_M_do_sanitize();
703 }
704
705 /**
706 * @brief Clear the bitset.
707 */
708 void
709 clear()
710 {
711 this->_M_clear();
712 this->_M_Nb = 0;
713 }
714
715 /**
716 * @brief Push a bit onto the high end of the bitset.
717 */
718 void
719 push_back(bool __bit)
720 {
721 if (this->size() % bits_per_block == 0)
722 this->_M_do_append_block(block_type(__bit), this->_M_Nb);
723 else
724 this->_M_unchecked_set(this->_M_Nb, __bit);
725 ++this->_M_Nb;
726 }
727
728 // XXX why is there no pop_back() member in the proposal?
729
730 /**
731 * @brief Append a block.
732 */
733 void
734 append(block_type __block)
735 {
736 this->_M_do_append_block(__block, this->_M_Nb);
737 this->_M_Nb += bits_per_block;
738 }
739
740 /**
741 * @brief
742 */
743 void
744 append(initializer_list<block_type> __il)
745 { this->append(__il.begin(), __il.end()); }
746
747 /**
748 * @brief Append an iterator range of blocks.
749 */
750 template <typename _BlockInputIterator>
751 void
752 append(_BlockInputIterator __first, _BlockInputIterator __last)
753 {
754 for (; __first != __last; ++__first)
755 this->append(*__first);
756 }
757
758 // 23.3.5.2 dynamic_bitset operations:
759 ///@{
760 /**
761 * @brief Operations on dynamic_bitsets.
762 * @param __rhs A same-sized dynamic_bitset.
763 *
764 * These should be self-explanatory.
765 */
766 dynamic_bitset&
767 operator&=(const dynamic_bitset& __rhs)
768 {
769 this->_M_do_and(__rhs);
770 return *this;
771 }
772
773 dynamic_bitset&
774 operator&=(dynamic_bitset&& __rhs)
775 {
776 this->_M_do_and(std::move(__rhs));
777 return *this;
778 }
779
780 dynamic_bitset&
781 operator|=(const dynamic_bitset& __rhs)
782 {
783 this->_M_do_or(__rhs);
784 return *this;
785 }
786
787 dynamic_bitset&
788 operator^=(const dynamic_bitset& __rhs)
789 {
790 this->_M_do_xor(__rhs);
791 return *this;
792 }
793
794 dynamic_bitset&
795 operator-=(const dynamic_bitset& __rhs)
796 {
797 this->_M_do_dif(__rhs);
798 return *this;
799 }
800 ///@}
801
802 ///@{
803 /**
804 * @brief Operations on dynamic_bitsets.
805 * @param __pos The number of places to shift.
806 *
807 * These should be self-explanatory.
808 */
809 dynamic_bitset&
810 operator<<=(size_type __pos)
811 {
812 if (__builtin_expect(__pos < this->_M_Nb, 1))
813 {
814 this->_M_do_left_shift(__pos);
815 this->_M_do_sanitize();
816 }
817 else
818 this->_M_do_reset();
819 return *this;
820 }
821
822 dynamic_bitset&
823 operator>>=(size_type __pos)
824 {
825 if (__builtin_expect(__pos < this->_M_Nb, 1))
826 this->_M_do_right_shift(__pos);
827 else
828 this->_M_do_reset();
829 return *this;
830 }
831 ///@}
832
833 // Set, reset, and flip.
834 /**
835 * @brief Sets every bit to true.
836 */
837 dynamic_bitset&
838 set()
839 {
840 this->_M_do_set();
841 this->_M_do_sanitize();
842 return *this;
843 }
844
845 /**
846 * @brief Sets a given bit to a particular value.
847 * @param __pos The index of the bit.
848 * @param __val Either true or false, defaults to true.
849 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
850 */
851 dynamic_bitset&
852 set(size_type __pos, bool __val = true)
853 {
854 if (__pos >= _M_Nb)
855 __throw_out_of_range(__N("dynamic_bitset::set"));
856 return this->_M_unchecked_set(__pos, __val);
857 }
858
859 /**
860 * @brief Sets every bit to false.
861 */
862 dynamic_bitset&
863 reset()
864 {
865 this->_M_do_reset();
866 return *this;
867 }
868
869 /**
870 * @brief Sets a given bit to false.
871 * @param __pos The index of the bit.
872 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
873 *
874 * Same as writing @c set(__pos, false).
875 */
876 dynamic_bitset&
877 reset(size_type __pos)
878 {
879 if (__pos >= _M_Nb)
880 __throw_out_of_range(__N("dynamic_bitset::reset"));
881 return this->_M_unchecked_reset(__pos);
882 }
883
884 /**
885 * @brief Toggles every bit to its opposite value.
886 */
887 dynamic_bitset&
888 flip()
889 {
890 this->_M_do_flip();
891 this->_M_do_sanitize();
892 return *this;
893 }
894
895 /**
896 * @brief Toggles a given bit to its opposite value.
897 * @param __pos The index of the bit.
898 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
899 */
900 dynamic_bitset&
901 flip(size_type __pos)
902 {
903 if (__pos >= _M_Nb)
904 __throw_out_of_range(__N("dynamic_bitset::flip"));
905 return this->_M_unchecked_flip(__pos);
906 }
907
908 /// See the no-argument flip().
909 dynamic_bitset
910 operator~() const
911 { return dynamic_bitset<_WordT, _Alloc>(*this).flip(); }
912
913 ///@{
914 /**
915 * @brief Array-indexing support.
916 * @param __pos Index into the %dynamic_bitset.
917 * @return A bool for a 'const %dynamic_bitset'. For non-const
918 * bitsets, an instance of the reference proxy class.
919 * @note These operators do no range checking and throw no
920 * exceptions, as required by DR 11 to the standard.
921 */
922 reference
923 operator[](size_type __pos)
924 { return reference(*this,__pos); }
925
926 const_reference
927 operator[](size_type __pos) const
928 { return _M_unchecked_test(__pos); }
929 ///@}
930
931 /**
932 * @brief Returns a numerical interpretation of the %dynamic_bitset.
933 * @return The integral equivalent of the bits.
934 * @throw std::overflow_error If there are too many bits to be
935 * represented in an @c unsigned @c long.
936 */
937 unsigned long
938 to_ulong() const
939 { return this->_M_do_to_ulong(); }
940
941 /**
942 * @brief Returns a numerical interpretation of the %dynamic_bitset.
943 * @return The integral equivalent of the bits.
944 * @throw std::overflow_error If there are too many bits to be
945 * represented in an @c unsigned @c long.
946 */
947 unsigned long long
948 to_ullong() const
949 { return this->_M_do_to_ullong(); }
950
951 /**
952 * @brief Returns a character interpretation of the %dynamic_bitset.
953 * @return The string equivalent of the bits.
954 *
955 * Note the ordering of the bits: decreasing character positions
956 * correspond to increasing bit positions (see the main class notes for
957 * an example).
958 */
959 template<typename _CharT = char,
960 typename _Traits = std::char_traits<_CharT>,
961 typename _Alloc1 = std::allocator<_CharT>>
962 std::basic_string<_CharT, _Traits, _Alloc1>
963 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const
964 {
965 std::basic_string<_CharT, _Traits, _Alloc1> __result;
966 _M_copy_to_string(__result, __zero, __one);
967 return __result;
968 }
969
970 // Helper functions for string operations.
971 template<typename _Traits = std::char_traits<char>,
972 typename _CharT = typename _Traits::char_type>
973 void
974 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
975 _CharT __zero = _CharT('0'),
976 _CharT __one = _CharT('1'));
977
978 template<typename _CharT, typename _Traits, typename _Alloc1>
979 void
980 _M_copy_from_string(const basic_string<_CharT, _Traits, _Alloc1>& __str,
981 size_t __pos, size_t __n,
982 _CharT __zero = _CharT('0'),
983 _CharT __one = _CharT('1'))
984 {
985 _M_copy_from_ptr<_Traits>(__str.data(), __str.size(), __pos, __n,
986 __zero, __one);
987 }
988
989 template<typename _CharT, typename _Traits, typename _Alloc1>
990 void
991 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
992 _CharT __zero = _CharT('0'),
993 _CharT __one = _CharT('1')) const;
994
995 /// Returns the number of bits which are set.
996 size_type
997 count() const noexcept
998 { return this->_M_do_count(); }
999
1000 /// Returns the total number of bits.
1001 size_type
1002 size() const noexcept
1003 { return this->_M_Nb; }
1004
1005 /// Returns the total number of blocks.
1006 size_type
1007 num_blocks() const noexcept
1008 { return this->_M_size(); }
1009
1010 /// Returns true if the dynamic_bitset is empty.
1011 _GLIBCXX_NODISCARD bool
1012 empty() const noexcept
1013 { return (this->_M_Nb == 0); }
1014
1015 /// Returns the maximum size of a dynamic_bitset object having the same
1016 /// type as *this.
1017 /// The real answer is max() * bits_per_block but is likely to overflow.
1018 constexpr size_type
1019 max_size() noexcept
1020 { return std::numeric_limits<block_type>::max(); }
1021
1022 /**
1023 * @brief Tests the value of a bit.
1024 * @param __pos The index of a bit.
1025 * @return The value at @a __pos.
1026 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
1027 */
1028 bool
1029 test(size_type __pos) const
1030 {
1031 if (__pos >= _M_Nb)
1032 __throw_out_of_range(__N("dynamic_bitset::test"));
1033 return _M_unchecked_test(__pos);
1034 }
1035
1036 /**
1037 * @brief Tests whether all the bits are on.
1038 * @return True if all the bits are set.
1039 */
1040 bool
1041 all() const
1042 { return this->_M_are_all_aux() == _M_Nb; }
1043
1044 /**
1045 * @brief Tests whether any of the bits are on.
1046 * @return True if at least one bit is set.
1047 */
1048 bool
1049 any() const
1050 { return this->_M_is_any(); }
1051
1052 /**
1053 * @brief Tests whether any of the bits are on.
1054 * @return True if none of the bits are set.
1055 */
1056 bool
1057 none() const
1058 { return !this->_M_is_any(); }
1059
1060 ///@{
1061 /// Self-explanatory.
1062 dynamic_bitset
1063 operator<<(size_type __pos) const
1064 { return dynamic_bitset(*this) <<= __pos; }
1065
1066 dynamic_bitset
1067 operator>>(size_type __pos) const
1068 { return dynamic_bitset(*this) >>= __pos; }
1069 ///@}
1070
1071 /**
1072 * @brief Finds the index of the first "on" bit.
1073 * @return The index of the first bit set, or size() if not found.
1074 * @sa find_next
1075 */
1076 size_type
1077 find_first() const
1078 { return this->_M_do_find_first(this->_M_Nb); }
1079
1080 /**
1081 * @brief Finds the index of the next "on" bit after prev.
1082 * @return The index of the next bit set, or size() if not found.
1083 * @param __prev Where to start searching.
1084 * @sa find_first
1085 */
1086 size_type
1087 find_next(size_t __prev) const
1088 { return this->_M_do_find_next(__prev, this->_M_Nb); }
1089
1090 bool
1091 is_subset_of(const dynamic_bitset& __b) const
1092 { return this->_M_is_subset_of(__b); }
1093
1094 bool
1095 is_proper_subset_of(const dynamic_bitset& __b) const
1096 { return this->_M_is_proper_subset_of(__b); }
1097
1098 friend bool
1099 operator==(const dynamic_bitset& __lhs,
1100 const dynamic_bitset& __rhs) noexcept
1101 { return __lhs._M_Nb == __rhs._M_Nb && __lhs._M_is_equal(__rhs); }
1102
1103 friend bool
1104 operator<(const dynamic_bitset& __lhs,
1105 const dynamic_bitset& __rhs) noexcept
1106 { return __lhs._M_is_less(__rhs) || __lhs._M_Nb < __rhs._M_Nb; }
1107 };
1108
1109 template<typename _WordT, typename _Alloc>
1110 template<typename _CharT, typename _Traits, typename _Alloc1>
1111 inline void
1112 dynamic_bitset<_WordT, _Alloc>::
1113 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
1114 _CharT __zero, _CharT __one) const
1115 {
1116 __str.assign(_M_Nb, __zero);
1117 for (size_t __i = _M_Nb; __i > 0; --__i)
1118 if (_M_unchecked_test(__i - 1))
1119 _Traits::assign(__str[_M_Nb - __i], __one);
1120 }
1121
1122
1123 ///@{
1124 /// These comparisons for equality/inequality are, well, @e bitwise.
1125
1126 template<typename _WordT, typename _Alloc>
1127 inline bool
1128 operator!=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1129 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1130 { return !(__lhs == __rhs); }
1131
1132 template<typename _WordT, typename _Alloc>
1133 inline bool
1134 operator<=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1135 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1136 { return !(__lhs > __rhs); }
1137
1138 template<typename _WordT, typename _Alloc>
1139 inline bool
1140 operator>(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1141 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1142 { return __rhs < __lhs; }
1143
1144 template<typename _WordT, typename _Alloc>
1145 inline bool
1146 operator>=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1147 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1148 { return !(__lhs < __rhs); }
1149 ///@}
1150
1151 // 23.3.5.3 bitset operations:
1152 ///@{
1153 /**
1154 * @brief Global bitwise operations on bitsets.
1155 * @param __x A bitset.
1156 * @param __y A bitset of the same size as @a __x.
1157 * @return A new bitset.
1158 *
1159 * These should be self-explanatory.
1160 */
1161 template<typename _WordT, typename _Alloc>
1162 inline dynamic_bitset<_WordT, _Alloc>
1163 operator&(const dynamic_bitset<_WordT, _Alloc>& __x,
1164 const dynamic_bitset<_WordT, _Alloc>& __y)
1165 {
1166 dynamic_bitset<_WordT, _Alloc> __result(__x);
1167 __result &= __y;
1168 return __result;
1169 }
1170
1171 template<typename _WordT, typename _Alloc>
1172 inline dynamic_bitset<_WordT, _Alloc>
1173 operator|(const dynamic_bitset<_WordT, _Alloc>& __x,
1174 const dynamic_bitset<_WordT, _Alloc>& __y)
1175 {
1176 dynamic_bitset<_WordT, _Alloc> __result(__x);
1177 __result |= __y;
1178 return __result;
1179 }
1180
1181 template <typename _WordT, typename _Alloc>
1182 inline dynamic_bitset<_WordT, _Alloc>
1183 operator^(const dynamic_bitset<_WordT, _Alloc>& __x,
1184 const dynamic_bitset<_WordT, _Alloc>& __y)
1185 {
1186 dynamic_bitset<_WordT, _Alloc> __result(__x);
1187 __result ^= __y;
1188 return __result;
1189 }
1190
1191 template <typename _WordT, typename _Alloc>
1192 inline dynamic_bitset<_WordT, _Alloc>
1193 operator-(const dynamic_bitset<_WordT, _Alloc>& __x,
1194 const dynamic_bitset<_WordT, _Alloc>& __y)
1195 {
1196 dynamic_bitset<_WordT, _Alloc> __result(__x);
1197 __result -= __y;
1198 return __result;
1199 }
1200 ///@}
1201
1202 /// Stream output operator for dynamic_bitset.
1203 template <typename _CharT, typename _Traits,
1204 typename _WordT, typename _Alloc>
1205 inline std::basic_ostream<_CharT, _Traits>&
1206 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1207 const dynamic_bitset<_WordT, _Alloc>& __x)
1208 {
1209 std::basic_string<_CharT, _Traits> __tmp;
1210
1211 const ctype<_CharT>& __ct = use_facet<ctype<_CharT>>(__os.getloc());
1212 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1213 return __os << __tmp;
1214 }
1215 /**
1216 * @}
1217 */
1218} // tr2
1219
1220_GLIBCXX_END_NAMESPACE_VERSION
1221} // std
1222
1223#include <tr2/dynamic_bitset.tcc>
1224
1225#endif /* _GLIBCXX_TR2_DYNAMIC_BITSET */