dynamic_bitset 1.3.1
Simple Useful Libraries: C++17/20 header-only dynamic bitset
Loading...
Searching...
No Matches
dynamic_bitset.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2019 Maxime Pinard
3//
4// Distributed under the MIT license
5// See accompanying file LICENSE or copy at
6// https://opensource.org/licenses/MIT
7//
8#ifndef SUL_DYNAMIC_BITSET_HPP
9#define SUL_DYNAMIC_BITSET_HPP
10
16#define SUL_DYNAMIC_BITSET_VERSION_MAJOR 1
17
23#define SUL_DYNAMIC_BITSET_VERSION_MINOR 3
24
30#define SUL_DYNAMIC_BITSET_VERSION_PATCH 1
31
46#include <algorithm>
47#include <cassert>
48#include <cmath>
49#include <functional>
50#include <limits>
51#include <memory>
52#include <stdexcept>
53#include <string>
54#include <string_view>
55#include <type_traits>
56#include <vector>
57
58// define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT
59#if !defined(DYNAMIC_BITSET_NO_LIBPOPCNT)
60// https://github.com/kimwalisch/libpopcnt
61# if __has_include(<libpopcnt.h>)
62# include <libpopcnt.h>
63# define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT true
64# endif
65#endif
66#if !defined(DYNAMIC_BITSET_CAN_USE_LIBPOPCNT)
67# define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT false
68#endif
69
70// define DYNAMIC_BITSET_CAN_USE_STD_BITOPS
71#if !defined(DYNAMIC_BITSET_NO_STD_BITOPS)
72// https://en.cppreference.com/w/cpp/header/bit
73# if __has_include(<bit>)
74# include <bit>
75# ifdef __cpp_lib_bitops
76# define DYNAMIC_BITSET_CAN_USE_STD_BITOPS true
77# endif
78# endif
79#endif
80#if !defined(DYNAMIC_BITSET_CAN_USE_STD_BITOPS)
81# define DYNAMIC_BITSET_CAN_USE_STD_BITOPS false
82#endif
83
84// define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT
85// define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ
86// define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN
87// define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD
88// define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64
89#if !DYNAMIC_BITSET_CAN_USE_STD_BITOPS && !defined(DYNAMIC_BITSET_NO_COMPILER_BUILTIN)
90# if defined(__clang__)
91// https://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
92// https://clang.llvm.org/docs/LanguageExtensions.html#intrinsics-support-within-constant-expressions
93# ifdef __has_builtin
94# if __has_builtin(__builtin_popcount) && __has_builtin(__builtin_popcountl) \
95 && __has_builtin(__builtin_popcountll)
96# define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT true
97# endif
98# if __has_builtin(__builtin_ctz) && __has_builtin(__builtin_ctzl) && __has_builtin(__builtin_ctzll)
99# define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ true
100# endif
101# endif
102# elif defined(__GNUC__) // also defined by clang
103// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
104# define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN true
105# elif defined(_MSC_VER)
106// https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64
107// __popcnt16, __popcnt, __popcnt64 not used because it require to check the hardware support at runtime
108// (https://docs.microsoft.com/fr-fr/cpp/intrinsics/popcnt16-popcnt-popcnt64?view=msvc-160#remarks)
109# if defined(_M_IX86) || defined(_M_ARM) || defined(_M_X64) || defined(_M_ARM64)
110# include <intrin.h>
111# pragma intrinsic(_BitScanForward)
112# define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD true
113# endif
114# if(defined(_M_X64) || defined(_M_ARM64)) \
115 && !defined(DYNAMIC_BITSET_NO_MSVC_BUILTIN_BITSCANFORWARD64) // for testing purposes
116# pragma intrinsic(_BitScanForward64)
117# define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64 true
118# endif
119# endif
120#endif
121#if !defined(DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT)
122# define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT false
123#endif
124#if !defined(DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ)
125# define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ false
126#endif
127#if !defined(DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN)
128# define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN false
129#endif
130#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD)
131# define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD false
132#endif
133#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64)
134# define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64 false
135#endif
136#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN)
137# define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN false
138#endif
139
140#ifndef DYNAMIC_BITSET_NO_NAMESPACE
146namespace sul
147{
148#endif
149
169 template<typename Block = unsigned long long, typename Allocator = std::allocator<Block>>
171 {
172 static_assert(std::is_unsigned<Block>::value, "Block is not an unsigned integral type");
173
174 public:
180 typedef size_t size_type;
181
187 typedef Block block_type;
188
194 typedef Allocator allocator_type;
195
201 static constexpr size_type bits_per_block = std::numeric_limits<block_type>::digits;
202
208 static constexpr size_type npos = std::numeric_limits<size_type>::max();
209
221 {
222 public:
234 constexpr reference(dynamic_bitset<Block, Allocator>& bitset, size_type bit_pos);
235
241 constexpr reference(const reference&) noexcept = default;
242
248 constexpr reference(reference&&) noexcept = default;
249
255 ~reference() noexcept = default;
256
268 constexpr reference& operator=(bool v);
269
281 constexpr reference& operator=(const reference& rhs);
282
294 constexpr reference& operator=(reference&& rhs) noexcept;
295
308 constexpr reference& operator&=(bool v);
309
322 constexpr reference& operator|=(bool v);
323
336 constexpr reference& operator^=(bool v);
337
355 constexpr reference& operator-=(bool v);
356
366 [[nodiscard]] constexpr bool operator~() const;
367
375 [[nodiscard]] constexpr operator bool() const;
376
382 constexpr void operator&() = delete;
383
393 constexpr reference& set();
394
404 constexpr reference& reset();
405
415 constexpr reference& flip();
416
428 constexpr reference& assign(bool v);
429
430 private:
431 block_type& m_block;
432 block_type m_mask;
433 };
434
440 typedef bool const_reference;
441
447 constexpr dynamic_bitset(const dynamic_bitset<Block, Allocator>& other) = default;
448
454 constexpr dynamic_bitset(dynamic_bitset<Block, Allocator>&& other) noexcept = default;
455
461 constexpr dynamic_bitset<Block, Allocator>& operator=(const dynamic_bitset<Block, Allocator>& other) = default;
462
468 constexpr dynamic_bitset<Block, Allocator>&
469 operator=(dynamic_bitset<Block, Allocator>&& other) noexcept = default;
470
482 constexpr explicit dynamic_bitset(const allocator_type& allocator = allocator_type());
483
500 constexpr explicit dynamic_bitset(size_type nbits,
501 unsigned long long init_val = 0,
502 const allocator_type& allocator = allocator_type());
503
518 constexpr dynamic_bitset(std::initializer_list<block_type> init_vals,
519 const allocator_type& allocator = allocator_type());
520
546 template<typename _CharT, typename _Traits>
547 constexpr explicit dynamic_bitset(
548 std::basic_string_view<_CharT, _Traits> str,
549 typename std::basic_string_view<_CharT, _Traits>::size_type pos = 0,
550 typename std::basic_string_view<_CharT, _Traits>::size_type n = std::basic_string_view<_CharT, _Traits>::npos,
551 _CharT zero = _CharT('0'),
552 _CharT one = _CharT('1'),
553 const allocator_type& allocator = allocator_type());
554
581 template<typename _CharT, typename _Traits, typename _Alloc>
582 constexpr explicit dynamic_bitset(const std::basic_string<_CharT, _Traits, _Alloc>& str,
583 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type pos = 0,
584 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type n =
585 std::basic_string<_CharT, _Traits, _Alloc>::npos,
586 _CharT zero = _CharT('0'),
587 _CharT one = _CharT('1'),
588 const allocator_type& allocator = allocator_type());
589
615 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
616 constexpr explicit dynamic_bitset(
617 const _CharT* str,
618 typename std::basic_string<_CharT>::size_type pos = 0,
619 typename std::basic_string<_CharT>::size_type n = std::basic_string<_CharT>::npos,
620 _CharT zero = _CharT('0'),
621 _CharT one = _CharT('1'),
622 const allocator_type& allocator = allocator_type());
623
629 ~dynamic_bitset() noexcept = default;
630
646 constexpr void resize(size_type nbits, bool value = false);
647
660 constexpr void clear();
661
674 constexpr void push_back(bool value);
675
686 constexpr void pop_back();
687
699 constexpr void append(block_type block);
700
712 constexpr void append(std::initializer_list<block_type> blocks);
713
729 template<typename BlockInputIterator>
730 constexpr void append(BlockInputIterator first, BlockInputIterator last);
731
748 constexpr dynamic_bitset<Block, Allocator>& operator&=(const dynamic_bitset<Block, Allocator>& rhs);
749
766 constexpr dynamic_bitset<Block, Allocator>& operator|=(const dynamic_bitset<Block, Allocator>& rhs);
767
784 constexpr dynamic_bitset<Block, Allocator>& operator^=(const dynamic_bitset<Block, Allocator>& rhs);
785
807 constexpr dynamic_bitset<Block, Allocator>& operator-=(const dynamic_bitset<Block, Allocator>& rhs);
808
822 constexpr dynamic_bitset<Block, Allocator>& operator<<=(size_type shift);
823
837 constexpr dynamic_bitset<Block, Allocator>& operator>>=(size_type shift);
838
857 [[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator<<(size_type shift) const;
858
877 [[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator>>(size_type shift) const;
878
894 [[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator~() const;
895
915 constexpr dynamic_bitset<Block, Allocator>& set(size_type pos, size_type len, bool value);
916
933 constexpr dynamic_bitset<Block, Allocator>& set(size_type pos, bool value = true);
934
944 constexpr dynamic_bitset<Block, Allocator>& set();
945
962 constexpr dynamic_bitset<Block, Allocator>& reset(size_type pos, size_type len);
963
979 constexpr dynamic_bitset<Block, Allocator>& reset(size_type pos);
980
990 constexpr dynamic_bitset<Block, Allocator>& reset();
991
1008 constexpr dynamic_bitset<Block, Allocator>& flip(size_type pos, size_type len);
1009
1025 constexpr dynamic_bitset<Block, Allocator>& flip(size_type pos);
1026
1036 constexpr dynamic_bitset<Block, Allocator>& flip();
1037
1053 [[nodiscard]] constexpr bool test(size_type pos) const;
1054
1072 [[nodiscard]] constexpr bool test_set(size_type pos, bool value = true);
1073
1087 [[nodiscard]] constexpr bool all() const;
1088
1102 [[nodiscard]] constexpr bool any() const;
1103
1117 [[nodiscard]] constexpr bool none() const;
1118
1130 [[nodiscard]] constexpr size_type count() const noexcept;
1131
1147 [[nodiscard]] constexpr reference operator[](size_type pos);
1148
1164 [[nodiscard]] constexpr const_reference operator[](size_type pos) const;
1165
1175 [[nodiscard]] constexpr size_type size() const noexcept;
1176
1186 [[nodiscard]] constexpr size_type num_blocks() const noexcept;
1187
1202 [[nodiscard]] constexpr bool empty() const noexcept;
1203
1214 [[nodiscard]] constexpr size_type capacity() const noexcept;
1215
1230 constexpr void reserve(size_type num_bits);
1231
1244 constexpr void shrink_to_fit();
1245
1274 [[nodiscard]] constexpr bool is_subset_of(const dynamic_bitset<Block, Allocator>& bitset) const;
1275
1304 [[nodiscard]] constexpr bool is_proper_subset_of(const dynamic_bitset<Block, Allocator>& bitset) const;
1305
1331 [[nodiscard]] constexpr bool intersects(const dynamic_bitset<Block, Allocator>& bitset) const;
1332
1346 [[nodiscard]] constexpr size_type find_first() const;
1347
1365 [[nodiscard]] constexpr size_type find_next(size_type prev) const;
1366
1378 constexpr void
1379 swap(dynamic_bitset<Block, Allocator>& other) noexcept(noexcept(std::swap(m_blocks, other.m_blocks)));
1380
1390 [[nodiscard]] constexpr allocator_type get_allocator() const;
1391
1414 template<typename _CharT = char,
1415 typename _Traits = std::char_traits<_CharT>,
1416 typename _Alloc = std::allocator<_CharT>>
1417 [[nodiscard]] constexpr std::basic_string<_CharT, _Traits, _Alloc> to_string(_CharT zero = _CharT('0'),
1418 _CharT one = _CharT('1')) const;
1419
1435 [[nodiscard]] constexpr unsigned long to_ulong() const;
1436
1452 [[nodiscard]] constexpr unsigned long long to_ullong() const;
1453
1481 template<typename Function, typename... Parameters>
1482 constexpr void iterate_bits_on(Function&& function, Parameters&&... parameters) const;
1483
1518 [[nodiscard]] constexpr block_type* data() noexcept;
1519
1554 [[nodiscard]] constexpr const block_type* data() const noexcept;
1555
1571 template<typename Block_, typename Allocator_>
1572 friend constexpr bool operator==(const dynamic_bitset<Block_, Allocator_>& lhs,
1573 const dynamic_bitset<Block_, Allocator_>& rhs);
1574
1621 template<typename Block_, typename Allocator_>
1622 friend constexpr bool operator<(const dynamic_bitset<Block_, Allocator_>& lhs,
1623 const dynamic_bitset<Block_, Allocator_>& rhs);
1624
1625 private:
1626 template<typename T>
1627 struct dependent_false : public std::false_type
1628 {
1629 };
1630
1631 std::vector<Block, Allocator> m_blocks;
1632 size_type m_bits_number;
1633
1634 static constexpr block_type zero_block = block_type(0);
1635 static constexpr block_type one_block = block_type(~zero_block);
1636 static constexpr size_type block_last_bit_index = bits_per_block - 1;
1637
1638 static constexpr size_type blocks_required(size_type nbits) noexcept;
1639
1640 static constexpr size_type block_index(size_type pos) noexcept;
1641 static constexpr size_type bit_index(size_type pos) noexcept;
1642
1643 static constexpr block_type bit_mask(size_type pos) noexcept;
1644 static constexpr block_type bit_mask(size_type first, size_type last) noexcept;
1645
1646 static constexpr void
1647 set_block_bits(block_type& block, size_type first, size_type last, bool val = true) noexcept;
1648 static constexpr void flip_block_bits(block_type& block, size_type first, size_type last) noexcept;
1649
1650 static constexpr size_type block_count(const block_type& block) noexcept;
1651 static constexpr size_type block_count(const block_type& block, size_type nbits) noexcept;
1652
1653 static constexpr size_type count_block_trailing_zero(const block_type& block) noexcept;
1654
1655 template<typename _CharT, typename _Traits>
1656 constexpr void init_from_string(std::basic_string_view<_CharT, _Traits> str,
1657 typename std::basic_string_view<_CharT, _Traits>::size_type pos,
1658 typename std::basic_string_view<_CharT, _Traits>::size_type n,
1659 _CharT zero,
1660 _CharT one);
1661
1662 constexpr block_type& get_block(size_type pos);
1663 constexpr const block_type& get_block(size_type pos) const;
1664 constexpr block_type& last_block();
1665 constexpr block_type last_block() const;
1666
1667 // used bits in the last block
1668 constexpr size_type extra_bits_number() const noexcept;
1669 // unused bits in the last block
1670 constexpr size_type unused_bits_number() const noexcept;
1671
1672 template<typename BinaryOperation>
1673 constexpr void apply(const dynamic_bitset<Block, Allocator>& other, BinaryOperation binary_op);
1674 template<typename UnaryOperation>
1675 constexpr void apply(UnaryOperation unary_op);
1676 constexpr void apply_left_shift(size_type shift);
1677 constexpr void apply_right_shift(size_type shift);
1678
1679 // reset unused bits to 0
1680 constexpr void sanitize();
1681
1682 // check functions used in asserts
1683 constexpr bool check_unused_bits() const noexcept;
1684 constexpr bool check_size() const noexcept;
1685 constexpr bool check_consistency() const noexcept;
1686 };
1687
1688 // Deduction guideline for expressions like "dynamic_bitset a(32);" with an integral type as parameter
1689 // to use the constructor with the initial size instead of the constructor with the allocator.
1690 template<typename integral_type, typename = std::enable_if_t<std::is_integral_v<integral_type>>>
1691 dynamic_bitset(integral_type) -> dynamic_bitset<>;
1692
1693 //=================================================================================================
1694 // dynamic_bitset external functions declarations
1695 //=================================================================================================
1696
1720 template<typename Block, typename Allocator>
1721 constexpr bool operator!=(const dynamic_bitset<Block, Allocator>& lhs, const dynamic_bitset<Block, Allocator>& rhs);
1722
1747 template<typename Block, typename Allocator>
1748 constexpr bool operator<=(const dynamic_bitset<Block, Allocator>& lhs, const dynamic_bitset<Block, Allocator>& rhs);
1749
1775 template<typename Block, typename Allocator>
1776 constexpr bool operator>(const dynamic_bitset<Block, Allocator>& lhs, const dynamic_bitset<Block, Allocator>& rhs);
1777
1803 template<typename Block, typename Allocator>
1804 constexpr bool operator>=(const dynamic_bitset<Block, Allocator>& lhs, const dynamic_bitset<Block, Allocator>& rhs);
1805
1835 template<typename Block, typename Allocator>
1836 constexpr dynamic_bitset<Block, Allocator> operator&(const dynamic_bitset<Block, Allocator>& lhs,
1837 const dynamic_bitset<Block, Allocator>& rhs);
1838
1868 template<typename Block, typename Allocator>
1869 constexpr dynamic_bitset<Block, Allocator> operator|(const dynamic_bitset<Block, Allocator>& lhs,
1870 const dynamic_bitset<Block, Allocator>& rhs);
1871
1901 template<typename Block, typename Allocator>
1902 constexpr dynamic_bitset<Block, Allocator> operator^(const dynamic_bitset<Block, Allocator>& lhs,
1903 const dynamic_bitset<Block, Allocator>& rhs);
1904
1934 template<typename Block, typename Allocator>
1935 constexpr dynamic_bitset<Block, Allocator> operator-(const dynamic_bitset<Block, Allocator>& lhs,
1936 const dynamic_bitset<Block, Allocator>& rhs);
1937
1963 template<typename _CharT, typename _Traits, typename Block, typename Allocator>
1964 constexpr std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& os,
1965 const dynamic_bitset<Block, Allocator>& bitset);
1966
1995 template<typename _CharT, typename _Traits, typename Block, typename Allocator>
1996 constexpr std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& is,
1997 dynamic_bitset<Block, Allocator>& bitset);
1998
2020 template<typename Block, typename Allocator>
2021 constexpr void swap(dynamic_bitset<Block, Allocator>& bitset1,
2022 dynamic_bitset<Block, Allocator>& bitset2) noexcept(noexcept(bitset1.swap(bitset2)));
2023
2024 //=================================================================================================
2025 // dynamic_bitset::reference functions implementations
2026 //=================================================================================================
2027
2028 template<typename Block, typename Allocator>
2029 constexpr dynamic_bitset<Block, Allocator>::reference::reference(dynamic_bitset<Block, Allocator>& bitset,
2030 size_type bit_pos)
2031 : m_block(bitset.get_block(bit_pos))
2032 , m_mask(dynamic_bitset<Block, Allocator>::bit_mask(bit_pos))
2033 {
2034 }
2035
2036 template<typename Block, typename Allocator>
2039 {
2040 assign(v);
2041 return *this;
2042 }
2043
2044 template<typename Block, typename Allocator>
2047 {
2048 assign(rhs);
2049 return *this;
2050 }
2051
2052 template<typename Block, typename Allocator>
2055 {
2056 assign(rhs);
2057 return *this;
2058 }
2059
2060 template<typename Block, typename Allocator>
2063 {
2064 if(!v)
2065 {
2066 reset();
2067 }
2068 return *this;
2069 }
2070
2071 template<typename Block, typename Allocator>
2074 {
2075 if(v)
2076 {
2077 set();
2078 }
2079 return *this;
2080 }
2081
2082 template<typename Block, typename Allocator>
2085 {
2086 if(v)
2087 {
2088 flip();
2089 }
2090 return *this;
2091 }
2092
2093 template<typename Block, typename Allocator>
2096 {
2097 if(v)
2098 {
2099 reset();
2100 }
2101 return *this;
2102 }
2103
2104 template<typename Block, typename Allocator>
2106 {
2107 return (m_block & m_mask) == zero_block;
2108 }
2109
2110 template<typename Block, typename Allocator>
2112 {
2113 return (m_block & m_mask) != zero_block;
2114 }
2115
2116 template<typename Block, typename Allocator>
2118 {
2119 m_block |= m_mask;
2120 return *this;
2121 }
2122
2123 template<typename Block, typename Allocator>
2125 {
2126 m_block &= static_cast<block_type>(~m_mask);
2127 return *this;
2128 }
2129
2130 template<typename Block, typename Allocator>
2132 {
2133 m_block ^= m_mask;
2134 return *this;
2135 }
2136
2137 template<typename Block, typename Allocator>
2140 {
2141 if(v)
2142 {
2143 set();
2144 }
2145 else
2146 {
2147 reset();
2148 }
2149 return *this;
2150 }
2151
2152 //=================================================================================================
2153 // dynamic_bitset public functions implementations
2154 //=================================================================================================
2155
2156 template<typename Block, typename Allocator>
2158 : m_blocks(allocator)
2159 , m_bits_number(0)
2160 {
2161 }
2162
2163 template<typename Block, typename Allocator>
2165 unsigned long long init_val,
2166 const allocator_type& allocator)
2167 : m_blocks(blocks_required(nbits), allocator)
2168 , m_bits_number(nbits)
2169 {
2170 if(nbits == 0 || init_val == 0)
2171 {
2172 return;
2173 }
2174
2175 constexpr size_t ull_bits_number = std::numeric_limits<unsigned long long>::digits;
2176 constexpr size_t init_val_required_blocks = ull_bits_number / bits_per_block;
2177 if constexpr(init_val_required_blocks == 1)
2178 {
2179 m_blocks[0] = init_val;
2180 }
2181 else
2182 {
2183 const unsigned long long block_mask = static_cast<unsigned long long>(one_block);
2184 const size_t blocks_to_init = std::min(m_blocks.size(), init_val_required_blocks);
2185 for(size_t i = 0; i < blocks_to_init; ++i)
2186 {
2187 m_blocks[i] = block_type((init_val >> (i * bits_per_block) & block_mask));
2188 }
2189 }
2190 sanitize();
2191 }
2192
2193 template<typename Block, typename Allocator>
2194 constexpr dynamic_bitset<Block, Allocator>::dynamic_bitset(std::initializer_list<block_type> init_vals,
2195 const allocator_type& allocator)
2196 : m_blocks(allocator)
2197 , m_bits_number(0)
2198 {
2199 append(init_vals);
2200 }
2201
2202 template<typename Block, typename Allocator>
2203 template<typename _CharT, typename _Traits>
2205 std::basic_string_view<_CharT, _Traits> str,
2206 typename std::basic_string_view<_CharT, _Traits>::size_type pos,
2207 typename std::basic_string_view<_CharT, _Traits>::size_type n,
2208 _CharT zero,
2209 _CharT one,
2210 const allocator_type& allocator)
2211 : m_blocks(allocator)
2212 , m_bits_number(0)
2213 {
2214 assert(pos < str.size());
2215 init_from_string(str, pos, n, zero, one);
2216 }
2217
2218 template<typename Block, typename Allocator>
2219 template<typename _CharT, typename _Traits, typename _Alloc>
2221 const std::basic_string<_CharT, _Traits, _Alloc>& str,
2222 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type pos,
2223 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type n,
2224 _CharT zero,
2225 _CharT one,
2226 const allocator_type& allocator)
2227 : m_blocks(allocator)
2228 , m_bits_number(0)
2229 {
2230 assert(pos < str.size());
2231 init_from_string(std::basic_string_view<_CharT, _Traits>(str), pos, n, zero, one);
2232 }
2233
2234 template<typename Block, typename Allocator>
2235 template<typename _CharT, typename _Traits>
2237 typename std::basic_string<_CharT>::size_type pos,
2238 typename std::basic_string<_CharT>::size_type n,
2239 _CharT zero,
2240 _CharT one,
2241 const allocator_type& allocator)
2242 : m_blocks(allocator)
2243 , m_bits_number(0)
2244 {
2245 init_from_string(std::basic_string_view<_CharT, _Traits>(str), pos, n, zero, one);
2246 }
2247
2248 template<typename Block, typename Allocator>
2249 constexpr void dynamic_bitset<Block, Allocator>::resize(size_type nbits, bool value)
2250 {
2251 if(nbits == m_bits_number)
2252 {
2253 return;
2254 }
2255
2256 const size_type old_num_blocks = num_blocks();
2257 const size_type new_num_blocks = blocks_required(nbits);
2258
2259 const block_type init_value = value ? one_block : zero_block;
2260 if(new_num_blocks != old_num_blocks)
2261 {
2262 m_blocks.resize(new_num_blocks, init_value);
2263 }
2264
2265 if(value && nbits > m_bits_number && old_num_blocks > 0)
2266 {
2267 // set value of the new bits in the old last block
2268 const size_type extra_bits = extra_bits_number();
2269 if(extra_bits > 0)
2270 {
2271 m_blocks[old_num_blocks - 1] |= static_cast<block_type>(init_value << extra_bits);
2272 }
2273 }
2274
2275 m_bits_number = nbits;
2276 sanitize();
2277 assert(check_consistency());
2278 }
2279
2280 template<typename Block, typename Allocator>
2282 {
2283 m_blocks.clear();
2284 m_bits_number = 0;
2285 }
2286
2287 template<typename Block, typename Allocator>
2289 {
2290 const size_type new_last_bit = m_bits_number++;
2291 if(m_bits_number <= m_blocks.size() * bits_per_block)
2292 {
2293 if(value)
2294 {
2295 set(new_last_bit, value);
2296 }
2297 }
2298 else
2299 {
2300 m_blocks.push_back(block_type(value));
2301 }
2302 assert(operator[](new_last_bit) == value);
2303 assert(check_consistency());
2304 }
2305
2306 template<typename Block, typename Allocator>
2308 {
2309 if(empty())
2310 {
2311 return;
2312 }
2313
2314 --m_bits_number;
2315 if(m_blocks.size() > blocks_required(m_bits_number))
2316 {
2317 m_blocks.pop_back();
2318 // no extra bits: sanitize not required
2319 assert(extra_bits_number() == 0);
2320 }
2321 else
2322 {
2323 sanitize();
2324 }
2325 assert(check_consistency());
2326 }
2327
2328 template<typename Block, typename Allocator>
2330 {
2331 const size_type extra_bits = extra_bits_number();
2332 if(extra_bits == 0)
2333 {
2334 m_blocks.push_back(block);
2335 }
2336 else
2337 {
2338 last_block() |= static_cast<block_type>(block << extra_bits);
2339 m_blocks.push_back(block_type(block >> (bits_per_block - extra_bits)));
2340 }
2341
2342 m_bits_number += bits_per_block;
2343 assert(check_consistency());
2344 }
2345
2346 template<typename Block, typename Allocator>
2347 constexpr void dynamic_bitset<Block, Allocator>::append(std::initializer_list<block_type> blocks)
2348 {
2349 if(blocks.size() == 0)
2350 {
2351 return;
2352 }
2353
2354 append(std::cbegin(blocks), std::cend(blocks));
2355 }
2356
2357 template<typename Block, typename Allocator>
2358 template<typename BlockInputIterator>
2359 constexpr void dynamic_bitset<Block, Allocator>::append(BlockInputIterator first, BlockInputIterator last)
2360 {
2361 if(first == last)
2362 {
2363 return;
2364 }
2365
2366 // if random access iterators, std::distance complexity is constant
2367 if constexpr(std::is_same_v<typename std::iterator_traits<BlockInputIterator>::iterator_category,
2368 std::random_access_iterator_tag>)
2369 {
2370 assert(std::distance(first, last) > 0);
2371 m_blocks.reserve(m_blocks.size() + static_cast<size_type>(std::distance(first, last)));
2372 }
2373
2374 const size_type extra_bits = extra_bits_number();
2375 const size_type unused_bits = unused_bits_number();
2376 if(extra_bits == 0)
2377 {
2378 auto pos = m_blocks.insert(std::end(m_blocks), first, last);
2379 assert(std::distance(pos, std::end(m_blocks)) > 0);
2380 m_bits_number += static_cast<size_type>(std::distance(pos, std::end(m_blocks))) * bits_per_block;
2381 }
2382 else
2383 {
2384 last_block() |= static_cast<block_type>(*first << extra_bits);
2385 block_type block = block_type(*first >> unused_bits);
2386 ++first;
2387 while(first != last)
2388 {
2389 block |= static_cast<block_type>(*first << extra_bits);
2390 m_blocks.push_back(block);
2391 m_bits_number += bits_per_block;
2392 block = block_type(*first >> unused_bits);
2393 ++first;
2394 }
2395 m_blocks.push_back(block);
2396 m_bits_number += bits_per_block;
2397 }
2398
2399 assert(check_consistency());
2400 }
2401
2402 template<typename Block, typename Allocator>
2405 {
2406 assert(size() == rhs.size());
2407 // apply(rhs, std::bit_and());
2408 for(size_type i = 0; i < m_blocks.size(); ++i)
2409 {
2410 m_blocks[i] &= rhs.m_blocks[i];
2411 }
2412 return *this;
2413 }
2414
2415 template<typename Block, typename Allocator>
2418 {
2419 assert(size() == rhs.size());
2420 // apply(rhs, std::bit_or());
2421 for(size_type i = 0; i < m_blocks.size(); ++i)
2422 {
2423 m_blocks[i] |= rhs.m_blocks[i];
2424 }
2425 return *this;
2426 }
2427
2428 template<typename Block, typename Allocator>
2431 {
2432 assert(size() == rhs.size());
2433 // apply(rhs, std::bit_xor());
2434 for(size_type i = 0; i < m_blocks.size(); ++i)
2435 {
2436 m_blocks[i] ^= rhs.m_blocks[i];
2437 }
2438 return *this;
2439 }
2440
2441 template<typename Block, typename Allocator>
2444 {
2445 assert(size() == rhs.size());
2446 // apply(rhs, [](const block_type& x, const block_type& y) { return (x & ~y); });
2447 for(size_type i = 0; i < m_blocks.size(); ++i)
2448 {
2449 m_blocks[i] &= static_cast<block_type>(~rhs.m_blocks[i]);
2450 }
2451 return *this;
2452 }
2453
2454 template<typename Block, typename Allocator>
2456 {
2457 if(shift != 0)
2458 {
2459 if(shift >= m_bits_number)
2460 {
2461 reset();
2462 }
2463 else
2464 {
2465 apply_left_shift(shift);
2466 sanitize(); // unused bits can have changed, reset them to 0
2467 }
2468 }
2469 return *this;
2470 }
2471
2472 template<typename Block, typename Allocator>
2474 {
2475 if(shift != 0)
2476 {
2477 if(shift >= m_bits_number)
2478 {
2479 reset();
2480 }
2481 else
2482 {
2483 apply_right_shift(shift);
2484 }
2485 }
2486 return *this;
2487 }
2488
2489 template<typename Block, typename Allocator>
2494
2495 template<typename Block, typename Allocator>
2500
2501 template<typename Block, typename Allocator>
2503 {
2505 bitset.flip();
2506 return bitset;
2507 }
2508
2509 template<typename Block, typename Allocator>
2512 {
2513 assert(pos < size());
2514 if(len == 0)
2515 {
2516 return *this;
2517 }
2518 assert(pos + len - 1 < size());
2519
2520 const size_type first_block = block_index(pos);
2521 const size_type last_block = block_index(pos + len - 1);
2522 const size_type first_bit_index = bit_index(pos);
2523 const size_type last_bit_index = bit_index(pos + len - 1);
2524
2525 if(first_block == last_block)
2526 {
2527 set_block_bits(m_blocks[first_block], first_bit_index, last_bit_index, value);
2528 }
2529 else
2530 {
2531 size_type first_full_block = first_block;
2532 size_type last_full_block = last_block;
2533
2534 if(first_bit_index != 0)
2535 {
2536 ++first_full_block; // first block is not full
2537 set_block_bits(m_blocks[first_block], first_bit_index, block_last_bit_index, value);
2538 }
2539
2540 if(last_bit_index != block_last_bit_index)
2541 {
2542 --last_full_block; // last block is not full
2543 set_block_bits(m_blocks[last_block], 0, last_bit_index, value);
2544 }
2545
2546 const block_type full_block = value ? one_block : zero_block;
2547 for(size_type i = first_full_block; i <= last_full_block; ++i)
2548 {
2549 m_blocks[i] = full_block;
2550 }
2551 }
2552
2553 return *this;
2554 }
2555
2556 template<typename Block, typename Allocator>
2558 {
2559 assert(pos < size());
2560
2561 if(value)
2562 {
2563 m_blocks[block_index(pos)] |= bit_mask(pos);
2564 }
2565 else
2566 {
2567 m_blocks[block_index(pos)] &= static_cast<block_type>(~bit_mask(pos));
2568 }
2569
2570 return *this;
2571 }
2572
2573 template<typename Block, typename Allocator>
2575 {
2576 std::fill(std::begin(m_blocks), std::end(m_blocks), one_block);
2577 sanitize();
2578 return *this;
2579 }
2580
2581 template<typename Block, typename Allocator>
2583 {
2584 return set(pos, len, false);
2585 }
2586
2587 template<typename Block, typename Allocator>
2589 {
2590 return set(pos, false);
2591 }
2592
2593 template<typename Block, typename Allocator>
2595 {
2596 std::fill(std::begin(m_blocks), std::end(m_blocks), zero_block);
2597 return *this;
2598 }
2599
2600 template<typename Block, typename Allocator>
2602 {
2603 assert(pos < size());
2604 if(len == 0)
2605 {
2606 return *this;
2607 }
2608 assert(pos + len - 1 < size());
2609
2610 const size_type first_block = block_index(pos);
2611 const size_type last_block = block_index(pos + len - 1);
2612 const size_type first_bit_index = bit_index(pos);
2613 const size_type last_bit_index = bit_index(pos + len - 1);
2614
2615 if(first_block == last_block)
2616 {
2617 flip_block_bits(m_blocks[first_block], first_bit_index, last_bit_index);
2618 }
2619 else
2620 {
2621 size_type first_full_block = first_block;
2622 size_type last_full_block = last_block;
2623
2624 if(first_bit_index != 0)
2625 {
2626 ++first_full_block; // first block is not full
2627 flip_block_bits(m_blocks[first_block], first_bit_index, block_last_bit_index);
2628 }
2629
2630 if(last_bit_index != block_last_bit_index)
2631 {
2632 --last_full_block; // last block is not full
2633 flip_block_bits(m_blocks[last_block], 0, last_bit_index);
2634 }
2635
2636 for(size_type i = first_full_block; i <= last_full_block; ++i)
2637 {
2638 m_blocks[i] = block_type(~m_blocks[i]);
2639 }
2640 }
2641
2642 return *this;
2643 }
2644
2645 template<typename Block, typename Allocator>
2647 {
2648 assert(pos < size());
2649 m_blocks[block_index(pos)] ^= bit_mask(pos);
2650 return *this;
2651 }
2652
2653 template<typename Block, typename Allocator>
2655 {
2656 std::transform(std::cbegin(m_blocks), std::cend(m_blocks), std::begin(m_blocks), std::bit_not<block_type>());
2657 sanitize();
2658 return *this;
2659 }
2660
2661 template<typename Block, typename Allocator>
2663 {
2664 assert(pos < size());
2665 return (m_blocks[block_index(pos)] & bit_mask(pos)) != zero_block;
2666 }
2667
2668 template<typename Block, typename Allocator>
2670 {
2671 bool const result = test(pos);
2672 if(result != value)
2673 {
2674 set(pos, value);
2675 }
2676 return result;
2677 }
2678
2679 template<typename Block, typename Allocator>
2681 {
2682 if(empty())
2683 {
2684 return true;
2685 }
2686
2687 const block_type full_block = one_block;
2688 if(extra_bits_number() == 0)
2689 {
2690 for(const block_type& block: m_blocks)
2691 {
2692 if(block != full_block)
2693 {
2694 return false;
2695 }
2696 }
2697 }
2698 else
2699 {
2700 for(size_type i = 0; i < m_blocks.size() - 1; ++i)
2701 {
2702 if(m_blocks[i] != full_block)
2703 {
2704 return false;
2705 }
2706 }
2707 if(last_block() != (full_block >> unused_bits_number()))
2708 {
2709 return false;
2710 }
2711 }
2712 return true;
2713 }
2714
2715 template<typename Block, typename Allocator>
2717 {
2718 for(const block_type& block: m_blocks)
2719 {
2720 if(block != zero_block)
2721 {
2722 return true;
2723 }
2724 }
2725 return false;
2726 }
2727
2728 template<typename Block, typename Allocator>
2730 {
2731 return !any();
2732 }
2733
2734 template<typename Block, typename Allocator>
2737 {
2738 if(empty())
2739 {
2740 return 0;
2741 }
2742
2743#if DYNAMIC_BITSET_CAN_USE_LIBPOPCNT
2744 const size_type count = static_cast<size_type>(popcnt(m_blocks.data(), m_blocks.size() * sizeof(block_type)));
2745#else
2746 size_type count = 0;
2747
2748 // full blocks
2749 for(size_type i = 0; i < m_blocks.size() - 1; ++i)
2750 {
2751 count += block_count(m_blocks[i]);
2752 }
2753
2754 // last block
2755 const block_type& block = last_block();
2756 const size_type extra_bits = extra_bits_number();
2757 if(extra_bits == 0)
2758 {
2759 count += block_count(block);
2760 }
2761 else
2762 {
2763 count += block_count(block, extra_bits);
2764 }
2765#endif
2766 return count;
2767 }
2768
2769 template<typename Block, typename Allocator>
2772 {
2773 assert(pos < size());
2775 }
2776
2777 template<typename Block, typename Allocator>
2780 {
2781 return test(pos);
2782 }
2783
2784 template<typename Block, typename Allocator>
2787 {
2788 return m_bits_number;
2789 }
2790
2791 template<typename Block, typename Allocator>
2794 {
2795 return m_blocks.size();
2796 }
2797
2798 template<typename Block, typename Allocator>
2799 constexpr bool dynamic_bitset<Block, Allocator>::empty() const noexcept
2800 {
2801 return size() == 0;
2802 }
2803
2804 template<typename Block, typename Allocator>
2807 {
2808 return m_blocks.capacity() * bits_per_block;
2809 }
2810
2811 template<typename Block, typename Allocator>
2813 {
2814 m_blocks.reserve(blocks_required(num_bits));
2815 }
2816
2817 template<typename Block, typename Allocator>
2819 {
2820 m_blocks.shrink_to_fit();
2821 }
2822
2823 template<typename Block, typename Allocator>
2825 {
2826 assert(size() == bitset.size());
2827 for(size_type i = 0; i < m_blocks.size(); ++i)
2828 {
2829 if((m_blocks[i] & ~bitset.m_blocks[i]) != zero_block)
2830 {
2831 return false;
2832 }
2833 }
2834 return true;
2835 }
2836
2837 template<typename Block, typename Allocator>
2838 constexpr bool
2840 {
2841 assert(size() == bitset.size());
2842 bool is_proper = false;
2843 for(size_type i = 0; i < m_blocks.size(); ++i)
2844 {
2845 const block_type& self_block = m_blocks[i];
2846 const block_type& other_block = bitset.m_blocks[i];
2847
2848 if((self_block & ~other_block) != zero_block)
2849 {
2850 return false;
2851 }
2852 if((~self_block & other_block) != zero_block)
2853 {
2854 is_proper = true;
2855 }
2856 }
2857 return is_proper;
2858 }
2859
2860 template<typename Block, typename Allocator>
2862 {
2863 const size_type min_blocks_number = std::min(m_blocks.size(), bitset.m_blocks.size());
2864 for(size_type i = 0; i < min_blocks_number; ++i)
2865 {
2866 if((m_blocks[i] & bitset.m_blocks[i]) != zero_block)
2867 {
2868 return true;
2869 }
2870 }
2871 return false;
2872 }
2873
2874 template<typename Block, typename Allocator>
2876 {
2877 for(size_type i = 0; i < m_blocks.size(); ++i)
2878 {
2879 if(m_blocks[i] != zero_block)
2880 {
2881 return i * bits_per_block + count_block_trailing_zero(m_blocks[i]);
2882 }
2883 }
2884 return npos;
2885 }
2886
2887 template<typename Block, typename Allocator>
2890 {
2891 if(empty() || prev >= (size() - 1))
2892 {
2893 return npos;
2894 }
2895
2896 const size_type first_bit = prev + 1;
2897 const size_type first_block = block_index(first_bit);
2898 const size_type first_bit_index = bit_index(first_bit);
2899 const block_type first_block_shifted = block_type(m_blocks[first_block] >> first_bit_index);
2900
2901 if(first_block_shifted != zero_block)
2902 {
2903 return first_bit + count_block_trailing_zero(first_block_shifted);
2904 }
2905 else
2906 {
2907 for(size_type i = first_block + 1; i < m_blocks.size(); ++i)
2908 {
2909 if(m_blocks[i] != zero_block)
2910 {
2911 return i * bits_per_block + count_block_trailing_zero(m_blocks[i]);
2912 }
2913 }
2914 }
2915 return npos;
2916 }
2917
2918 template<typename Block, typename Allocator>
2920 noexcept(std::swap(m_blocks, other.m_blocks)))
2921 {
2922 std::swap(m_blocks, other.m_blocks);
2923 std::swap(m_bits_number, other.m_bits_number);
2924 }
2925
2926 template<typename Block, typename Allocator>
2929 {
2930 return m_blocks.get_allocator();
2931 }
2932
2933 template<typename Block, typename Allocator>
2934 template<typename _CharT, typename _Traits, typename _Alloc>
2935 constexpr std::basic_string<_CharT, _Traits, _Alloc> dynamic_bitset<Block, Allocator>::to_string(_CharT zero,
2936 _CharT one) const
2937 {
2938 const size_type len = size();
2939 std::basic_string<_CharT, _Traits, _Alloc> str(len, zero);
2940 for(size_type i_block = 0; i_block < m_blocks.size(); ++i_block)
2941 {
2942 if(m_blocks[i_block] == zero_block)
2943 {
2944 continue;
2945 }
2946 block_type mask = block_type(1);
2947 const size_type limit = i_block * bits_per_block < len ? len - i_block * bits_per_block : bits_per_block;
2948 for(size_type i_bit = 0; i_bit < limit; ++i_bit)
2949 {
2950 if((m_blocks[i_block] & mask) != zero_block)
2951 {
2952 _Traits::assign(str[len - (i_block * bits_per_block + i_bit + 1)], one);
2953 }
2954 // mask <<= 1; not used because it trigger -Wconversion
2955 // because of integral promotion for block_type smaller than int
2956 mask = static_cast<block_type>(mask << 1);
2957 }
2958 }
2959 return str;
2960 }
2961
2962 template<typename Block, typename Allocator>
2963 constexpr unsigned long dynamic_bitset<Block, Allocator>::to_ulong() const
2964 {
2965 if(m_bits_number == 0)
2966 {
2967 return 0;
2968 }
2969
2970 constexpr size_t ul_bits_number = std::numeric_limits<unsigned long>::digits;
2971 if(find_next(ul_bits_number - 1) != npos)
2972 {
2973 throw std::overflow_error("sul::dynamic_bitset::to_ulong");
2974 }
2975
2976 unsigned long result = 0;
2977 const size_type result_bits_number = std::min(ul_bits_number, m_bits_number);
2978 for(size_type i_block = 0; i_block <= block_index(result_bits_number - 1); ++i_block)
2979 {
2980 result |= (static_cast<unsigned long>(m_blocks[i_block]) << (i_block * bits_per_block));
2981 }
2982
2983 return result;
2984 }
2985
2986 template<typename Block, typename Allocator>
2987 constexpr unsigned long long dynamic_bitset<Block, Allocator>::to_ullong() const
2988 {
2989 if(m_bits_number == 0)
2990 {
2991 return 0;
2992 }
2993
2994 constexpr size_t ull_bits_number = std::numeric_limits<unsigned long long>::digits;
2995 if(find_next(ull_bits_number - 1) != npos)
2996 {
2997 throw std::overflow_error("sul::dynamic_bitset::to_ullong");
2998 }
2999
3000 unsigned long long result = 0;
3001 const size_type result_bits_number = std::min(ull_bits_number, m_bits_number);
3002 for(size_type i_block = 0; i_block <= block_index(result_bits_number - 1); ++i_block)
3003 {
3004 result |= (static_cast<unsigned long long>(m_blocks[i_block]) << (i_block * bits_per_block));
3005 }
3006
3007 return result;
3008 }
3009
3010 template<typename Block, typename Allocator>
3011 template<typename Function, typename... Parameters>
3012 constexpr void dynamic_bitset<Block, Allocator>::iterate_bits_on(Function&& function,
3013 Parameters&&... parameters) const
3014 {
3015 if constexpr(!std::is_invocable_v<Function, size_t, Parameters...>)
3016 {
3017 static_assert(dependent_false<Function>::value, "Function take invalid arguments");
3018 // function should take (size_t, parameters...) as arguments
3019 }
3020
3021 if constexpr(std::is_same_v<std::invoke_result_t<Function, size_t, Parameters...>, void>)
3022 {
3023 size_type i_bit = find_first();
3024 while(i_bit != npos)
3025 {
3026 std::invoke(std::forward<Function>(function), i_bit, std::forward<Parameters>(parameters)...);
3027 i_bit = find_next(i_bit);
3028 }
3029 }
3030 else if constexpr(std::is_convertible_v<std::invoke_result_t<Function, size_t, Parameters...>, bool>)
3031 {
3032 size_type i_bit = find_first();
3033 while(i_bit != npos)
3034 {
3035 if(!std::invoke(std::forward<Function>(function), i_bit, std::forward<Parameters>(parameters)...))
3036 {
3037 break;
3038 }
3039 i_bit = find_next(i_bit);
3040 }
3041 }
3042 else
3043 {
3044 static_assert(dependent_false<Function>::value, "Function have invalid return type");
3045 // return type should be void, or convertible to bool
3046 }
3047 }
3048
3049 template<typename Block, typename Allocator>
3051 {
3052 return m_blocks.data();
3053 }
3054
3055 template<typename Block, typename Allocator>
3056 constexpr const typename dynamic_bitset<Block, Allocator>::block_type*
3058 {
3059 return m_blocks.data();
3060 }
3061
3062 template<typename Block_, typename Allocator_>
3063 [[nodiscard]] constexpr bool operator==(const dynamic_bitset<Block_, Allocator_>& lhs,
3065 {
3066 return (lhs.m_bits_number == rhs.m_bits_number) && (lhs.m_blocks == rhs.m_blocks);
3067 }
3068
3069 template<typename Block_, typename Allocator_>
3070 [[nodiscard]] constexpr bool operator<(const dynamic_bitset<Block_, Allocator_>& lhs,
3072 {
3075 const size_type lhs_size = lhs.size();
3076 const size_type rhs_size = rhs.size();
3077 const size_type lhs_blocks_size = lhs.m_blocks.size();
3078 const size_type rhs_blocks_size = rhs.m_blocks.size();
3079
3080 if(lhs_size == rhs_size)
3081 {
3082 // if comparison of two empty bitsets
3083 if(lhs_size == 0)
3084 {
3085 return false;
3086 }
3087
3088 for(size_type i = lhs_blocks_size - 1; i > 0; --i)
3089 {
3090 if(lhs.m_blocks[i] != rhs.m_blocks[i])
3091 {
3092 return lhs.m_blocks[i] < rhs.m_blocks[i];
3093 }
3094 }
3095 return lhs.m_blocks[0] < rhs.m_blocks[0];
3096 }
3097
3098 // empty bitset inferior to 0-only bitset
3099 if(lhs_size == 0)
3100 {
3101 return true;
3102 }
3103 if(rhs_size == 0)
3104 {
3105 return false;
3106 }
3107
3108 const bool rhs_longer = rhs_size > lhs_size;
3109 const dynamic_bitset<Block_, Allocator_>& longest_bitset = rhs_longer ? rhs : lhs;
3110 const size_type longest_blocks_size = std::max(lhs_blocks_size, rhs_blocks_size);
3111 const size_type shortest_blocks_size = std::min(lhs_blocks_size, rhs_blocks_size);
3112 for(size_type i = longest_blocks_size - 1; i >= shortest_blocks_size; --i)
3113 {
3114 if(longest_bitset.m_blocks[i] != block_type(0))
3115 {
3116 return rhs_longer;
3117 }
3118 }
3119
3120 for(size_type i = shortest_blocks_size - 1; i > 0; --i)
3121 {
3122 if(lhs.m_blocks[i] != rhs.m_blocks[i])
3123 {
3124 return lhs.m_blocks[i] < rhs.m_blocks[i];
3125 }
3126 }
3127 if(lhs.m_blocks[0] != rhs.m_blocks[0])
3128 {
3129 return lhs.m_blocks[0] < rhs.m_blocks[0];
3130 }
3131 return lhs_size < rhs_size;
3132 }
3133
3134 //=================================================================================================
3135 // dynamic_bitset private functions implementations
3136 //=================================================================================================
3137
3138 template<typename Block, typename Allocator>
3141 {
3142 return nbits / bits_per_block + static_cast<size_type>(nbits % bits_per_block > 0);
3143 }
3144
3145 template<typename Block, typename Allocator>
3147 dynamic_bitset<Block, Allocator>::block_index(size_type pos) noexcept
3148 {
3149 return pos / bits_per_block;
3150 }
3151
3152 template<typename Block, typename Allocator>
3154 dynamic_bitset<Block, Allocator>::bit_index(size_type pos) noexcept
3155 {
3156 return pos % bits_per_block;
3157 }
3158
3159 template<typename Block, typename Allocator>
3161 dynamic_bitset<Block, Allocator>::bit_mask(size_type pos) noexcept
3162 {
3163 return block_type(block_type(1) << bit_index(pos));
3164 }
3165
3166 template<typename Block, typename Allocator>
3168 dynamic_bitset<Block, Allocator>::bit_mask(size_type first, size_type last) noexcept
3169 {
3170 first = bit_index(first);
3171 last = bit_index(last);
3172 if(last == (block_last_bit_index))
3173 {
3174 return block_type(one_block << first);
3175 }
3176 else
3177 {
3178 return block_type(((block_type(1) << (last + 1)) - 1) ^ ((block_type(1) << first) - 1));
3179 }
3180 }
3181
3182 template<typename Block, typename Allocator>
3183 constexpr void dynamic_bitset<Block, Allocator>::set_block_bits(block_type& block,
3184 size_type first,
3185 size_type last,
3186 bool val) noexcept
3187 {
3188 if(val)
3189 {
3190 block |= bit_mask(first, last);
3191 }
3192 else
3193 {
3194 block &= static_cast<block_type>(~bit_mask(first, last));
3195 }
3196 }
3197
3198 template<typename Block, typename Allocator>
3199 constexpr void
3200 dynamic_bitset<Block, Allocator>::flip_block_bits(block_type& block, size_type first, size_type last) noexcept
3201 {
3202 block ^= bit_mask(first, last);
3203 }
3204
3205 template<typename Block, typename Allocator>
3207 dynamic_bitset<Block, Allocator>::block_count(const block_type& block) noexcept
3208 {
3209#if DYNAMIC_BITSET_CAN_USE_STD_BITOPS
3210 return static_cast<size_type>(std::popcount(block));
3211#else
3212 if(block == zero_block)
3213 {
3214 return 0;
3215 }
3216
3217# if DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN || DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT
3218 constexpr size_t u_bits_number = std::numeric_limits<unsigned>::digits;
3219 constexpr size_t ul_bits_number = std::numeric_limits<unsigned long>::digits;
3220 constexpr size_t ull_bits_number = std::numeric_limits<unsigned long long>::digits;
3221 if constexpr(bits_per_block <= u_bits_number)
3222 {
3223 return static_cast<size_type>(__builtin_popcount(static_cast<unsigned int>(block)));
3224 }
3225 else if constexpr(bits_per_block <= ul_bits_number)
3226 {
3227 return static_cast<size_type>(__builtin_popcountl(static_cast<unsigned long>(block)));
3228 }
3229 else if constexpr(bits_per_block <= ull_bits_number)
3230 {
3231 return static_cast<size_type>(__builtin_popcountll(static_cast<unsigned long long>(block)));
3232 }
3233# endif
3234
3235 size_type count = 0;
3236 block_type mask = 1;
3237 for(size_type bit_index = 0; bit_index < bits_per_block; ++bit_index)
3238 {
3239 count += static_cast<size_type>((block & mask) != zero_block);
3240 // mask <<= 1; not used because it trigger -Wconversion
3241 // because of integral promotion for block_type smaller than int
3242 mask = static_cast<block_type>(mask << 1);
3243 }
3244 return count;
3245#endif
3246 }
3247
3248 template<typename Block, typename Allocator>
3250 dynamic_bitset<Block, Allocator>::block_count(const block_type& block, size_type nbits) noexcept
3251 {
3252 assert(nbits <= bits_per_block);
3253#if DYNAMIC_BITSET_CAN_USE_STD_BITOPS
3254 const block_type shifted_block = block_type(block << (bits_per_block - nbits));
3255 return static_cast<size_type>(std::popcount(shifted_block));
3256#else
3257 const block_type shifted_block = block_type(block << (bits_per_block - nbits));
3258 if(shifted_block == zero_block)
3259 {
3260 return 0;
3261 }
3262
3263# if DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN || DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT
3264 constexpr size_t u_bits_number = std::numeric_limits<unsigned>::digits;
3265 constexpr size_t ul_bits_number = std::numeric_limits<unsigned long>::digits;
3266 constexpr size_t ull_bits_number = std::numeric_limits<unsigned long long>::digits;
3267 if constexpr(bits_per_block <= u_bits_number)
3268 {
3269 return static_cast<size_type>(__builtin_popcount(static_cast<unsigned int>(shifted_block)));
3270 }
3271 else if constexpr(bits_per_block <= ul_bits_number)
3272 {
3273 return static_cast<size_type>(__builtin_popcountl(static_cast<unsigned long>(shifted_block)));
3274 }
3275 else if constexpr(bits_per_block <= ull_bits_number)
3276 {
3277 return static_cast<size_type>(__builtin_popcountll(static_cast<unsigned long long>(shifted_block)));
3278 }
3279# endif
3280
3281 size_type count = 0;
3282 block_type mask = 1;
3283 for(size_type bit_index = 0; bit_index < nbits; ++bit_index)
3284 {
3285 count += static_cast<size_type>((block & mask) != zero_block);
3286 // mask <<= 1; not used because it trigger -Wconversion
3287 // because of integral promotion for block_type smaller than int
3288 mask = static_cast<block_type>(mask << 1);
3289 }
3290
3291 return count;
3292#endif
3293 }
3294
3295 template<typename Block, typename Allocator>
3297 dynamic_bitset<Block, Allocator>::count_block_trailing_zero(const block_type& block) noexcept
3298 {
3299 assert(block != zero_block);
3300#if DYNAMIC_BITSET_CAN_USE_STD_BITOPS
3301 return static_cast<size_type>(std::countr_zero(block));
3302#else
3303# if DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN || DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ
3304 constexpr size_t u_bits_number = std::numeric_limits<unsigned>::digits;
3305 constexpr size_t ul_bits_number = std::numeric_limits<unsigned long>::digits;
3306 constexpr size_t ull_bits_number = std::numeric_limits<unsigned long long>::digits;
3307 if constexpr(bits_per_block <= u_bits_number)
3308 {
3309 return static_cast<size_type>(__builtin_ctz(static_cast<unsigned int>(block)));
3310 }
3311 else if constexpr(bits_per_block <= ul_bits_number)
3312 {
3313 return static_cast<size_type>(__builtin_ctzl(static_cast<unsigned long>(block)));
3314 }
3315 else if constexpr(bits_per_block <= ull_bits_number)
3316 {
3317 return static_cast<size_type>(__builtin_ctzll(static_cast<unsigned long long>(block)));
3318 }
3319
3320# elif DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD
3321 constexpr size_t ul_bits_number = std::numeric_limits<unsigned long>::digits;
3322 constexpr size_t ui64_bits_number = std::numeric_limits<unsigned __int64>::digits;
3323 if constexpr(bits_per_block <= ul_bits_number)
3324 {
3325 unsigned long index = std::numeric_limits<unsigned long>::max();
3326 _BitScanForward(&index, static_cast<unsigned long>(block));
3327 return static_cast<size_type>(index);
3328 }
3329 else if constexpr(bits_per_block <= ui64_bits_number)
3330 {
3331# if DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64
3332 unsigned long index = std::numeric_limits<unsigned long>::max();
3333 _BitScanForward64(&index, static_cast<unsigned __int64>(block));
3334 return static_cast<size_type>(index);
3335# else
3336 constexpr unsigned long max_ul = std::numeric_limits<unsigned long>::max();
3337 unsigned long low = block & max_ul;
3338 if(low != 0)
3339 {
3340 unsigned long index = std::numeric_limits<unsigned long>::max();
3341 _BitScanForward(&index, low);
3342 return static_cast<size_type>(index);
3343 }
3344 unsigned long high = block >> ul_bits_number;
3345 unsigned long index = std::numeric_limits<unsigned long>::max();
3346 _BitScanForward(&index, high);
3347 return static_cast<size_type>(ul_bits_number + index);
3348# endif
3349 }
3350# endif
3351
3352 block_type mask = block_type(1);
3353 for(size_type i = 0; i < bits_per_block; ++i)
3354 {
3355 if((block & mask) != zero_block)
3356 {
3357 return i;
3358 }
3359 // mask <<= 1; not used because it trigger -Wconversion
3360 // because of integral promotion for block_type smaller than int
3361 mask = static_cast<block_type>(mask << 1);
3362 }
3363 assert(false); // LCOV_EXCL_LINE: unreachable
3364 return npos; // LCOV_EXCL_LINE: unreachable
3365#endif
3366 }
3367
3368 template<typename Block, typename Allocator>
3369 template<typename _CharT, typename _Traits>
3370 constexpr void
3371 dynamic_bitset<Block, Allocator>::init_from_string(std::basic_string_view<_CharT, _Traits> str,
3372 typename std::basic_string_view<_CharT, _Traits>::size_type pos,
3373 typename std::basic_string_view<_CharT, _Traits>::size_type n,
3374 [[maybe_unused]] _CharT zero,
3375 _CharT one)
3376 {
3377 assert(pos < str.size());
3378
3379 const size_type size = std::min(n, str.size() - pos);
3380 m_bits_number = size;
3381
3382 m_blocks.clear();
3383 m_blocks.resize(blocks_required(size));
3384 for(size_t i = 0; i < size; ++i)
3385 {
3386 const _CharT c = str[(pos + size - 1) - i];
3387 assert(c == zero || c == one);
3388 if(c == one)
3389 {
3390 set(i);
3391 }
3392 }
3393 }
3394
3395 template<typename Block, typename Allocator>
3397 dynamic_bitset<Block, Allocator>::get_block(size_type pos)
3398 {
3399 return m_blocks[block_index(pos)];
3400 }
3401
3402 template<typename Block, typename Allocator>
3403 constexpr const typename dynamic_bitset<Block, Allocator>::block_type&
3404 dynamic_bitset<Block, Allocator>::get_block(size_type pos) const
3405 {
3406 return m_blocks[block_index(pos)];
3407 }
3408
3409 template<typename Block, typename Allocator>
3410 constexpr typename dynamic_bitset<Block, Allocator>::block_type& dynamic_bitset<Block, Allocator>::last_block()
3411 {
3412 return m_blocks[m_blocks.size() - 1];
3413 }
3414
3415 template<typename Block, typename Allocator>
3416 constexpr typename dynamic_bitset<Block, Allocator>::block_type dynamic_bitset<Block, Allocator>::last_block() const
3417 {
3418 return m_blocks[m_blocks.size() - 1];
3419 }
3420
3421 template<typename Block, typename Allocator>
3423 dynamic_bitset<Block, Allocator>::extra_bits_number() const noexcept
3424 {
3425 return bit_index(m_bits_number);
3426 }
3427
3428 template<typename Block, typename Allocator>
3430 dynamic_bitset<Block, Allocator>::unused_bits_number() const noexcept
3431 {
3432 return bits_per_block - extra_bits_number();
3433 }
3434
3435 template<typename Block, typename Allocator>
3436 template<typename BinaryOperation>
3437 constexpr void dynamic_bitset<Block, Allocator>::apply(const dynamic_bitset<Block, Allocator>& other,
3438 BinaryOperation binary_op)
3439 {
3440 assert(num_blocks() == other.num_blocks());
3441 std::transform(
3442 std::cbegin(m_blocks), std::cend(m_blocks), std::cbegin(other.m_blocks), std::begin(m_blocks), binary_op);
3443 }
3444
3445 template<typename Block, typename Allocator>
3446 template<typename UnaryOperation>
3447 constexpr void dynamic_bitset<Block, Allocator>::apply(UnaryOperation unary_op)
3448 {
3449 std::transform(std::cbegin(m_blocks), std::cend(m_blocks), std::begin(m_blocks), unary_op);
3450 }
3451
3452 template<typename Block, typename Allocator>
3453 constexpr void dynamic_bitset<Block, Allocator>::apply_left_shift(size_type shift)
3454 {
3455 assert(shift > 0);
3456 assert(shift < capacity());
3457
3458 const size_type blocks_shift = shift / bits_per_block;
3459 const size_type bits_offset = shift % bits_per_block;
3460
3461 if(bits_offset == 0)
3462 {
3463 for(size_type i = m_blocks.size() - 1; i >= blocks_shift; --i)
3464 {
3465 m_blocks[i] = m_blocks[i - blocks_shift];
3466 }
3467 }
3468 else
3469 {
3470 const size_type reverse_bits_offset = bits_per_block - bits_offset;
3471 for(size_type i = m_blocks.size() - 1; i > blocks_shift; --i)
3472 {
3473 m_blocks[i] = block_type((m_blocks[i - blocks_shift] << bits_offset)
3474 | block_type(m_blocks[i - blocks_shift - 1] >> reverse_bits_offset));
3475 }
3476 m_blocks[blocks_shift] = block_type(m_blocks[0] << bits_offset);
3477 }
3478
3479 // set bit that came at the right to 0 in unmodified blocks
3480 std::fill(std::begin(m_blocks),
3481 std::begin(m_blocks) + static_cast<typename decltype(m_blocks)::difference_type>(blocks_shift),
3482 zero_block);
3483 }
3484
3485 template<typename Block, typename Allocator>
3486 constexpr void dynamic_bitset<Block, Allocator>::apply_right_shift(size_type shift)
3487 {
3488 assert(shift > 0);
3489 assert(shift < capacity());
3490
3491 const size_type blocks_shift = shift / bits_per_block;
3492 const size_type bits_offset = shift % bits_per_block;
3493 const size_type last_block_to_shift = m_blocks.size() - blocks_shift - 1;
3494
3495 if(bits_offset == 0)
3496 {
3497 for(size_type i = 0; i <= last_block_to_shift; ++i)
3498 {
3499 m_blocks[i] = m_blocks[i + blocks_shift];
3500 }
3501 }
3502 else
3503 {
3504 const size_type reverse_bits_offset = bits_per_block - bits_offset;
3505 for(size_type i = 0; i < last_block_to_shift; ++i)
3506 {
3507 m_blocks[i] = block_type((m_blocks[i + blocks_shift] >> bits_offset)
3508 | block_type(m_blocks[i + blocks_shift + 1] << reverse_bits_offset));
3509 }
3510 m_blocks[last_block_to_shift] = block_type(m_blocks[m_blocks.size() - 1] >> bits_offset);
3511 }
3512
3513 // set bit that came at the left to 0 in unmodified blocks
3514 std::fill(std::begin(m_blocks)
3515 + static_cast<typename decltype(m_blocks)::difference_type>(last_block_to_shift + 1),
3516 std::end(m_blocks),
3517 zero_block);
3518 }
3519
3520 template<typename Block, typename Allocator>
3521 constexpr void dynamic_bitset<Block, Allocator>::sanitize()
3522 {
3523 size_type shift = m_bits_number % bits_per_block;
3524 if(shift > 0)
3525 {
3526 last_block() &= static_cast<block_type>(~(one_block << shift));
3527 }
3528 }
3529
3530 template<typename Block, typename Allocator>
3531 constexpr bool dynamic_bitset<Block, Allocator>::check_unused_bits() const noexcept
3532 {
3533 const size_type extra_bits = extra_bits_number();
3534 if(extra_bits > 0)
3535 {
3536 return (last_block() & (one_block << extra_bits)) == zero_block;
3537 }
3538 return true;
3539 }
3540
3541 template<typename Block, typename Allocator>
3542 constexpr bool dynamic_bitset<Block, Allocator>::check_size() const noexcept
3543 {
3544 return blocks_required(size()) == m_blocks.size();
3545 }
3546
3547 template<typename Block, typename Allocator>
3548 constexpr bool dynamic_bitset<Block, Allocator>::check_consistency() const noexcept
3549 {
3550 return check_unused_bits() && check_size();
3551 }
3552
3553 //=================================================================================================
3554 // dynamic_bitset external functions implementations
3555 //=================================================================================================
3556
3557 template<typename Block, typename Allocator>
3559 {
3560 return !(lhs == rhs);
3561 }
3562
3563 template<typename Block, typename Allocator>
3565 {
3566 return !(rhs < lhs);
3567 }
3568
3569 template<typename Block, typename Allocator>
3571 {
3572 return rhs < lhs;
3573 }
3574
3575 template<typename Block, typename Allocator>
3577 {
3578 return !(lhs < rhs);
3579 }
3580
3581 template<typename Block, typename Allocator>
3584 {
3586 return result &= rhs;
3587 }
3588
3589 template<typename Block, typename Allocator>
3592 {
3594 return result |= rhs;
3595 }
3596
3597 template<typename Block, typename Allocator>
3600 {
3602 return result ^= rhs;
3603 }
3604
3605 template<typename Block, typename Allocator>
3608 {
3610 return result -= rhs;
3611 }
3612
3613 template<typename _CharT, typename _Traits, typename Block, typename Allocator>
3614 constexpr std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& os,
3616 {
3617 // A better implementation is possible
3618 return os << bitset.template to_string<_CharT, _Traits>();
3619 }
3620
3621 template<typename _CharT, typename _Traits, typename Block, typename Allocator>
3622 constexpr std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& is,
3624 {
3625 // A better implementation is possible
3626 constexpr _CharT zero = _CharT('0');
3627 constexpr _CharT one = _CharT('1');
3628 typename std::basic_istream<_CharT, _Traits>::sentry s(is);
3629 if(!s)
3630 {
3631 return is;
3632 }
3633
3634 dynamic_bitset<Block, Allocator> reverse_bitset;
3635 _CharT val;
3636 is.get(val);
3637 while(is.good())
3638 {
3639 if(val == one)
3640 {
3641 reverse_bitset.push_back(true);
3642 }
3643 else if(val == zero)
3644 {
3645 reverse_bitset.push_back(false);
3646 }
3647 else
3648 {
3649 is.unget();
3650 break;
3651 }
3652 is.get(val);
3653 }
3654
3655 bitset.clear();
3656 if(!reverse_bitset.empty())
3657 {
3658 for(typename dynamic_bitset<Block, Allocator>::size_type i = reverse_bitset.size() - 1; i > 0; --i)
3659 {
3660 bitset.push_back(reverse_bitset.test(i));
3661 }
3662 bitset.push_back(reverse_bitset.test(0));
3663 }
3664
3665 return is;
3666 }
3667
3668 template<typename Block, typename Allocator>
3670 dynamic_bitset<Block, Allocator>& bitset2) noexcept(noexcept(bitset1.swap(bitset2)))
3671 {
3672 bitset1.swap(bitset2);
3673 }
3674
3675#ifndef DYNAMIC_BITSET_NO_NAMESPACE
3676} // namespace sul
3677#endif
3678
3679#endif // SUL_DYNAMIC_BITSET_HPP
Reference to a sul::dynamic_bitset bit.
Definition dynamic_bitset.hpp:221
constexpr reference & reset()
Reset the referenced bit to false.
Definition dynamic_bitset.hpp:2124
constexpr reference & operator-=(bool v)
Apply binary difference to the referenced bit and a value, and assign the result to the referenced bi...
Definition dynamic_bitset.hpp:2095
constexpr reference & assign(bool v)
Assign the value v to the referenced bit.
Definition dynamic_bitset.hpp:2139
constexpr reference(dynamic_bitset< Block, Allocator > &bitset, size_type bit_pos)
Constructs a reference to a bit from a sul::dynamic_bitset and a bit position.
Definition dynamic_bitset.hpp:2029
constexpr reference & operator|=(bool v)
Apply binary operator OR to the referenced bit and a value, and assign the result to the referenced b...
Definition dynamic_bitset.hpp:2073
constexpr reference(const reference &) noexcept=default
Copy constructor.
constexpr reference(reference &&) noexcept=default
Move constructor.
constexpr reference & operator&=(bool v)
Apply binary operator AND to the referenced bit and a value, and assign the result to the referenced ...
Definition dynamic_bitset.hpp:2062
constexpr bool operator~() const
Return the result of applying unary NOT operator.
Definition dynamic_bitset.hpp:2105
constexpr reference & operator=(bool v)
Assign a value to the referenced bit.
Definition dynamic_bitset.hpp:2038
constexpr reference & flip()
Flip the referenced bit.
Definition dynamic_bitset.hpp:2131
constexpr reference & set()
Set the referenced bit to true.
Definition dynamic_bitset.hpp:2117
constexpr reference & operator^=(bool v)
Apply binary operator XOR to the referenced bit and a value, and assign the result to the referenced ...
Definition dynamic_bitset.hpp:2084
Dynamic bitset.
Definition dynamic_bitset.hpp:171
constexpr dynamic_bitset< Block, Allocator > & operator<<=(size_type shift)
Performs binary shift left of shift bits.
Definition dynamic_bitset.hpp:2455
constexpr size_type size() const noexcept
Give the number of bits of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2786
constexpr dynamic_bitset< Block, Allocator > & operator^=(const dynamic_bitset< Block, Allocator > &rhs)
Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and rhs.
Definition dynamic_bitset.hpp:2430
constexpr void push_back(bool value)
Add a new bit with the value value at the end of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2288
constexpr bool test_set(size_type pos, bool value=true)
Test the value of the bit at position pos and set it to true or value value.
Definition dynamic_bitset.hpp:2669
constexpr dynamic_bitset< Block, Allocator > & set()
Set all the bits of the sul::dynamic_bitset to true.
Definition dynamic_bitset.hpp:2574
Block block_type
Same type as Block.
Definition dynamic_bitset.hpp:187
constexpr void swap(dynamic_bitset< Block, Allocator > &other) noexcept(noexcept(std::swap(m_blocks, other.m_blocks)))
Exchanges the bits of this sul::dynamic_bitset with those of other.
Definition dynamic_bitset.hpp:2919
constexpr void iterate_bits_on(Function &&function, Parameters &&... parameters) const
Iterate on the sul::dynamic_bitset and call function with the position of the bits on.
Definition dynamic_bitset.hpp:3012
constexpr bool empty() const noexcept
Checks if the sul::dynamic_bitset is empty.
Definition dynamic_bitset.hpp:2799
constexpr bool any() const
Checks if any bits are set to true.
Definition dynamic_bitset.hpp:2716
constexpr dynamic_bitset< Block, Allocator > operator<<(size_type shift) const
Performs binary shift right of shift bits.
Definition dynamic_bitset.hpp:2490
constexpr void clear()
Clears the sul::dynamic_bitset, resize it to 0.
Definition dynamic_bitset.hpp:2281
constexpr size_type num_blocks() const noexcept
Give the number of blocks used by the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2793
constexpr bool is_subset_of(const dynamic_bitset< Block, Allocator > &bitset) const
Determines if this sul::dynamic_bitset is a subset of bitset.
Definition dynamic_bitset.hpp:2824
constexpr std::basic_string< _CharT, _Traits, _Alloc > to_string(_CharT zero=_CharT('0'), _CharT one=_CharT('1')) const
Generate a string representation of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2935
constexpr bool all() const
Checks if all bits are set to true.
Definition dynamic_bitset.hpp:2680
constexpr dynamic_bitset< Block, Allocator > & reset()
Reset all the bits of the sul::dynamic_bitset to false.
Definition dynamic_bitset.hpp:2594
constexpr dynamic_bitset< Block, Allocator > & operator&=(const dynamic_bitset< Block, Allocator > &rhs)
Sets the bits to the result of binary AND on corresponding pairs of bits of *this and rhs.
Definition dynamic_bitset.hpp:2404
constexpr bool is_proper_subset_of(const dynamic_bitset< Block, Allocator > &bitset) const
Determines if this sul::dynamic_bitset is a proper subset of bitset.
Definition dynamic_bitset.hpp:2839
constexpr dynamic_bitset< Block, Allocator > & operator-=(const dynamic_bitset< Block, Allocator > &rhs)
Sets the bits to the result of the binary difference between the bits of *this and rhs.
Definition dynamic_bitset.hpp:2443
constexpr size_type count() const noexcept
Count the number of bits set to true.
Definition dynamic_bitset.hpp:2736
size_t size_type
Type used to represent the size of a sul::dynamic_bitset.
Definition dynamic_bitset.hpp:180
constexpr dynamic_bitset(const dynamic_bitset< Block, Allocator > &other)=default
Copy constructor.
constexpr size_type capacity() const noexcept
Give the number of bits that the sul::dynamic_bitset has currently allocated space for.
Definition dynamic_bitset.hpp:2806
constexpr dynamic_bitset< Block, Allocator > & operator|=(const dynamic_bitset< Block, Allocator > &rhs)
Sets the bits to the result of binary OR on corresponding pairs of bits of *this and rhs.
Definition dynamic_bitset.hpp:2417
constexpr unsigned long to_ulong() const
Converts the contents of the bitset to an unsigned long integer.
Definition dynamic_bitset.hpp:2963
bool const_reference
Const reference to a sul::dynamic_bitset bit, type bool.
Definition dynamic_bitset.hpp:440
constexpr dynamic_bitset< Block, Allocator > & flip()
Flip all the bits of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2654
constexpr size_type find_next(size_type prev) const
Find the position of the first bit set in the range [prev + 1, size()[ of the sul::dynamic_bitset sta...
Definition dynamic_bitset.hpp:2889
constexpr bool test(size_type pos) const
Test the value of the bit at position pos.
Definition dynamic_bitset.hpp:2662
constexpr void reserve(size_type num_bits)
Increase the capacity of the sul::dynamic_bitset to a value that's greater or equal to num_bits.
Definition dynamic_bitset.hpp:2812
constexpr void shrink_to_fit()
Requests the removal of unused capacity.
Definition dynamic_bitset.hpp:2818
constexpr bool none() const
Checks if none of the bits are set to true.
Definition dynamic_bitset.hpp:2729
constexpr size_type find_first() const
Find the position of the first bit set in the sul::dynamic_bitset starting from the least-significant...
Definition dynamic_bitset.hpp:2875
static constexpr size_type bits_per_block
Number of bits that can be stored in a block.
Definition dynamic_bitset.hpp:201
constexpr bool intersects(const dynamic_bitset< Block, Allocator > &bitset) const
Determines if this sul::dynamic_bitset and bitset intersect.
Definition dynamic_bitset.hpp:2861
Allocator allocator_type
Same type as Allocator.
Definition dynamic_bitset.hpp:194
constexpr void append(block_type block)
Append a block of bits block at the end of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2329
constexpr allocator_type get_allocator() const
Gets the associated allocator.
Definition dynamic_bitset.hpp:2928
constexpr block_type * data() noexcept
Return a pointer to the underlying array serving as blocks storage.
Definition dynamic_bitset.hpp:3050
constexpr reference operator[](size_type pos)
Accesses the bit at position pos.
Definition dynamic_bitset.hpp:2771
constexpr dynamic_bitset< Block, Allocator > & flip(size_type pos, size_type len)
Flip the bits of the range [pos, pos + len[.
Definition dynamic_bitset.hpp:2601
static constexpr size_type npos
Maximum value of size_type, returned for invalid positions.
Definition dynamic_bitset.hpp:208
constexpr void pop_back()
Remove the last bit of the sul::dynamic_bitset.
Definition dynamic_bitset.hpp:2307
constexpr void resize(size_type nbits, bool value=false)
Resize the sul::dynamic_bitset to contain nbits bits.
Definition dynamic_bitset.hpp:2249
constexpr dynamic_bitset< Block, Allocator > operator~() const
Performs a unary NOT on all bits.
Definition dynamic_bitset.hpp:2502
constexpr unsigned long long to_ullong() const
Converts the contents of the bitset to an unsigned long long integer.
Definition dynamic_bitset.hpp:2987
constexpr dynamic_bitset< Block, Allocator > operator>>(size_type shift) const
Performs binary shift left of shift bits.
Definition dynamic_bitset.hpp:2496
constexpr dynamic_bitset< Block, Allocator > & operator>>=(size_type shift)
Performs binary shift right of shift bits.
Definition dynamic_bitset.hpp:2473
Simple Useful Libraries.
Definition dynamic_bitset.hpp:147
constexpr bool operator>=(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Test if lhs is "greater than or equal to" rhs. The comparison of the two sul::dynamic_bitset is first...
Definition dynamic_bitset.hpp:3576
constexpr bool operator<=(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Test if lhs is "less than or equal to" rhs. The comparison of the two sul::dynamic_bitset is first on...
Definition dynamic_bitset.hpp:3564
constexpr bool operator<(const dynamic_bitset< Block_, Allocator_ > &lhs, const dynamic_bitset< Block_, Allocator_ > &rhs)
Definition dynamic_bitset.hpp:3070
constexpr bool operator>(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Test if lhs is "greater than" rhs. The comparison of the two sul::dynamic_bitset is first on numbers ...
Definition dynamic_bitset.hpp:3570
constexpr std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &is, dynamic_bitset< Block, Allocator > &bitset)
Extract a sul::dynamic_bitset from a character stream using its string representation.
Definition dynamic_bitset.hpp:3622
constexpr dynamic_bitset< Block, Allocator > operator|(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Performs binary OR on corresponding pairs of bits of lhs and rhs.
Definition dynamic_bitset.hpp:3590
constexpr dynamic_bitset< Block, Allocator > operator&(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Performs binary AND on corresponding pairs of bits of lhs and rhs.
Definition dynamic_bitset.hpp:3582
constexpr dynamic_bitset< Block, Allocator > operator-(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Performs binary difference between bits of lhs and rhs.
Definition dynamic_bitset.hpp:3606
constexpr dynamic_bitset< Block, Allocator > operator^(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Performs binary XOR on corresponding pairs of bits of lhs and rhs.
Definition dynamic_bitset.hpp:3598
constexpr std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &os, const dynamic_bitset< Block, Allocator > &bitset)
Insert a string representation of this sul::dynamic_bitset to a character stream.
Definition dynamic_bitset.hpp:3614
constexpr bool operator==(const dynamic_bitset< Block_, Allocator_ > &lhs, const dynamic_bitset< Block_, Allocator_ > &rhs)
Definition dynamic_bitset.hpp:3063
constexpr bool operator!=(const dynamic_bitset< Block, Allocator > &lhs, const dynamic_bitset< Block, Allocator > &rhs)
Test if two sul::dynamic_bitset content are different.
Definition dynamic_bitset.hpp:3558
constexpr void swap(dynamic_bitset< Block, Allocator > &bitset1, dynamic_bitset< Block, Allocator > &bitset2) noexcept(noexcept(bitset1.swap(bitset2)))
Exchange the content of bitset1 and bitset2.
Definition dynamic_bitset.hpp:3669