31 #ifndef ETL_BITSET_INCLUDED
32 #define ETL_BITSET_INCLUDED
51 #include "static_assert.h"
60 #if defined(ETL_COMPILER_KEIL)
61 #pragma diag_suppress 1300
80 bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
81 :
exception(reason_, file_name_, line_number_)
95 :
bitset_exception(ETL_ERROR_TEXT(
"bitset:null pointer", ETL_FILE
"A"), file_name_, line_number_)
109 :
bitset_exception(ETL_ERROR_TEXT(
"bitset:type_too_small", ETL_FILE
"B"), file_name_, line_number_)
123 #if !defined(ETL_BITSET_ELEMENT_TYPE)
124 typedef uint_least8_t element_t;
126 typedef ETL_BITSET_ELEMENT_TYPE element_t;
132 static const element_t ALL_CLEAR = 0;
156 operator bool()
const
158 return p_bitset->
test(position);
166 p_bitset->
set(position, b);
175 p_bitset->
set(position,
bool(r));
184 p_bitset->
flip(position);
193 return !p_bitset->
test(position);
202 : p_bitset(ETL_NULLPTR),
210 bit_reference(
ibitset& r_bitset,
size_t position_)
211 : p_bitset(&r_bitset),
235 for (
size_t i = 0; i < SIZE; ++i)
247 bool test(
size_t position)
const
255 mask = element_t(1) << position;
260 mask = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
263 return (pdata[index] & mask) != 0;
271 for (
size_t i = 0; i < SIZE; ++i)
276 pdata[SIZE - 1] &= TOP_MASK;
292 bit = element_t(1) << position;
297 bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
306 pdata[index] &= ~
bit;
323 set(--i, *text++ ==
'1');
340 set(--i, *text++ == L
'1');
357 set(--i, *text++ == L
'1');
374 set(--i, *text++ == u
'1');
391 set(--i, *text++ == U
'1');
400 template <
typename T>
406 const bool OK = (
sizeof(T) * CHAR_BIT) >= (SIZE * BITS_PER_ELEMENT);
414 for (
size_t i = 0; i < SIZE; ++i)
416 v |= T(pdata[i]) << shift;
417 shift += T(BITS_PER_ELEMENT);
429 for (
size_t i = 0; i < SIZE; ++i)
431 pdata[i] = ALL_CLEAR;
448 bit = element_t(1) << position;
453 bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
456 pdata[index] &= ~
bit;
466 for (
size_t i = 0; i < SIZE; ++i)
468 pdata[i] = ~pdata[i];
471 pdata[SIZE - 1] &= TOP_MASK;
481 if (position < NBITS)
489 bit = element_t(1) << position;
494 bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
509 for (
size_t i = 0; i < (SIZE - 1); ++i)
511 if (pdata[i] != ALL_SET)
518 if (pdata[SIZE - 1] != (ALL_SET & TOP_MASK))
539 for (
size_t i = 0; i < SIZE; ++i)
580 bit = position & (BITS_PER_ELEMENT - 1);
583 element_t mask = 1 <<
bit;
588 element_t
value = pdata[index];
591 if ((state && (
value != ALL_CLEAR)) ||
592 (!state && (
value != ALL_SET)))
595 while ((
bit < BITS_PER_ELEMENT) && (position < NBITS))
598 if (((
value & mask) != 0) == state)
611 position += BITS_PER_ELEMENT;
621 return ibitset::npos;
629 return test(position);
645 for (
size_t i = 0; i < SIZE; ++i)
647 pdata[i] &= other.pdata[i];
658 for (
size_t i = 0; i < SIZE; ++i)
660 pdata[i] |= other.pdata[i];
671 for (
size_t i = 0; i < SIZE; ++i)
673 pdata[i] ^= other.pdata[i];
690 size_t source = NBITS - shift - 1;
691 size_t destination = NBITS - 1;
693 for (
size_t i = 0; i < (NBITS - shift); ++i)
695 set(destination--,
test(source--));
698 for (
size_t i = 0; i < shift; ++i)
700 reset(destination--);
718 size_t source = shift;
719 size_t destination = 0;
721 for (
size_t i = 0; i < (NBITS - shift); ++i)
723 set(destination++,
test(source++));
726 for (
size_t i = 0; i < shift; ++i)
728 reset(destination++);
742 etl::copy_n(other.pdata, SIZE, pdata);
753 etl::swap_ranges(pdata, pdata + SIZE, other.pdata);
788 pdata[0] = element_t(
value);
794 while ((
value != 0) && (i < SIZE))
796 pdata[i++] =
value & ALL_SET;
801 pdata[SIZE - 1] &= TOP_MASK;
811 for (
size_t i = 0; i < SIZE; ++i)
813 pdata[i] = ~pdata[i];
828 ibitset(
size_t nbits_,
size_t size_, element_t* pdata_)
833 size_t allocated_bits = SIZE * BITS_PER_ELEMENT;
834 size_t top_mask_shift = ((BITS_PER_ELEMENT - (allocated_bits - NBITS)) % BITS_PER_ELEMENT);
835 TOP_MASK = element_t(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
843 return etl::equal(lhs.pdata, lhs.pdata + lhs.SIZE, rhs.pdata);
860 #if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
880 template <const
size_t MAXN>
884 static const size_t ARRAY_SIZE = (MAXN % BITS_PER_ELEMENT == 0) ? MAXN / BITS_PER_ELEMENT : MAXN / BITS_PER_ELEMENT + 1;
888 static const size_t ALLOCATED_BITS = ARRAY_SIZE * BITS_PER_ELEMENT;
907 etl::copy_n(other.data, ARRAY_SIZE, data);
1000 template <
typename T>
1004 ETL_STATIC_ASSERT((
sizeof(T) * CHAR_BIT) >= (ARRAY_SIZE * BITS_PER_ELEMENT),
"Type too small");
1006 return ibitset::value<T>();
1052 etl::copy_n(other.data, ARRAY_SIZE, data);
1149 element_t data[ARRAY_SIZE];
1156 template <const
size_t MAXN>
1168 template<const
size_t MAXN>
1180 template<const
size_t MAXN>
1192 template<const
size_t MAXN>
1195 return !(lhs == rhs);
1202 template <const
size_t MAXN>
void swap(etl::bitset< MAXN > &lhs, etl::bitset< MAXN > &rhs)
swap
Definition: bitset.h:1203
The reference type returned.
Definition: bitset.h:148
bit_reference & operator=(bool b)
Assignment operator.
Definition: bitset.h:164
bool operator~() const
Return the logical inverse of the bit.
Definition: bitset.h:191
bit_reference & flip()
Flip the bit.
Definition: bitset.h:182
Array view.
Definition: span.h:59
ETL_CONSTEXPR14 uint_least8_t count_bits(uint16_t value)
Definition: binary.h:736
bitset< MAXN > & operator=(const bitset< MAXN > &other)
operator =
Definition: bitset.h:1048
size_t find_first(bool state) const
Definition: bitset.h:555
size_t find_next(bool state, size_t position) const
Definition: bitset.h:566
bitset< MAXN > & flip()
Flip all of the bits.
Definition: bitset.h:1030
bool any() const
Are any of the bits set?
Definition: bitset.h:529
~ibitset()
Destructor.
Definition: bitset.h:867
ibitset & set(const char *text)
Set from a string.
Definition: bitset.h:315
ibitset & flip(size_t position)
Flip the bit at the position.
Definition: bitset.h:479
bitset< MAXN > & operator<<=(size_t shift)
operator <<=
Definition: bitset.h:1112
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition: bitset.h:820
ibitset & set()
Set the bit at the position.
Definition: bitset.h:269
bitset(const char *text)
Construct from a string.
Definition: bitset.h:922
ibitset & operator^=(const ibitset &other)
operator ^=
Definition: bitset.h:669
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset.h:383
bitset< MAXN > & reset()
Reset all of the bits.
Definition: bitset.h:1012
ibitset & reset(size_t position)
Reset the bit at the position.
Definition: bitset.h:440
ibitset & operator|=(const ibitset &other)
operator |=
Definition: bitset.h:656
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset.h:402
bitset()
Default constructor.
Definition: bitset.h:895
bitset< MAXN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset.h:940
bitset< MAXN > operator<<(size_t shift) const
operator <<
Definition: bitset.h:1100
bitset< MAXN > & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset.h:970
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset.h:366
void swap(ibitset &other)
swap
Definition: bitset.h:751
ibitset(size_t nbits_, size_t size_, element_t *pdata_)
Constructor.
Definition: bitset.h:828
bitset< MAXN > & set(const char *text)
Set from a string.
Definition: bitset.h:949
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset.h:1002
bitset< MAXN > & operator|=(const bitset< MAXN > &other)
operator |=
Definition: bitset.h:1070
bitset< MAXN > & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset.h:990
bitset< MAXN > & set()
Set all of the bits.
Definition: bitset.h:931
size_t count() const
Count the number of bits set.
Definition: bitset.h:231
ibitset & operator>>=(size_t shift)
operator >>=
Definition: bitset.h:710
bitset< MAXN > & reset(size_t position)
Reset the bit at the position.
Definition: bitset.h:1021
bool operator[](size_t position) const
Read [] operator.
Definition: bitset.h:627
ibitset & reset()
Resets the bitset.
Definition: bitset.h:427
bitset< MAXN > & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset.h:980
void invert()
Invert.
Definition: bitset.h:809
const_span_type span() const
Definition: bitset.h:769
ibitset & operator&=(const ibitset &other)
operator &=
Definition: bitset.h:643
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset.h:349
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition: bitset.h:779
bitset< MAXN > operator~() const
operator ~
Definition: bitset.h:1088
bitset< MAXN > & flip(size_t position)
Flip the bit at the position.
Definition: bitset.h:1039
bitset(const bitset< MAXN > &other)
Copy constructor.
Definition: bitset.h:904
bitset< MAXN > & operator&=(const bitset< MAXN > &other)
operator &=
Definition: bitset.h:1061
ibitset & operator=(const ibitset &other)
operator =
Definition: bitset.h:738
ibitset & from_string(const char *text)
Set from a string.
Definition: bitset.h:332
friend bool operator==(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
operator ==
Definition: bitset.h:1142
bitset(unsigned long long value)
Construct from a value.
Definition: bitset.h:913
bitset< MAXN > operator>>(size_t shift) const
operator >>
Definition: bitset.h:1121
bool none() const
Are none of the bits set?
Definition: bitset.h:537
bitset< MAXN > & operator>>=(size_t shift)
operator >>=
Definition: bitset.h:1133
ibitset & flip()
Flip all of the bits.
Definition: bitset.h:464
ibitset & operator<<=(size_t shift)
operator <<=
Definition: bitset.h:682
bool test(size_t position) const
Definition: bitset.h:247
bitset< MAXN > & operator^=(const bitset< MAXN > &other)
operator ^=
Definition: bitset.h:1079
size_t size() const
The size of the bitset.
Definition: bitset.h:223
bitset< MAXN > & from_string(const char *text)
Set from a string.
Definition: bitset.h:960
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset.h:284
span_type span()
Definition: bitset.h:760
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition: bitset.h:841
bitset< MAXN > operator^(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1181
bitset< MAXN > operator|(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1169
bitset< MAXN > operator&(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1157
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: integral_limits.h:54
enable_if
Definition: type_traits_generator.h:1228
Definition: absolute.h:37
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:247