31#ifndef ETL_BITSET_LEGACY_INCLUDED
32#define ETL_BITSET_LEGACY_INCLUDED
45#include "../static_assert.h"
56#if defined(ETL_COMPILER_KEIL)
57#pragma diag_suppress 1300
62 #define ETL_STRL(x) L##x
63 #define ETL_STRu(x) u##x
64 #define ETL_STRU(x) U##x
88 bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
89 :
exception(reason_, file_name_, line_number_)
98 class bitset_nullptr :
public bitset_exception
102 bitset_nullptr(string_type file_name_, numeric_type line_number_)
103 : bitset_exception(ETL_ERROR_TEXT(
"bitset:null pointer", ETL_BITSET_FILE_ID
"A"), file_name_, line_number_)
112 class bitset_type_too_small :
public bitset_exception
116 bitset_type_too_small(string_type file_name_, numeric_type line_number_)
117 : bitset_exception(ETL_ERROR_TEXT(
"bitset:type_too_small", ETL_BITSET_FILE_ID
"B"), file_name_, line_number_)
126 class bitset_overflow :
public bitset_exception
130 bitset_overflow(string_type file_name_, numeric_type line_number_)
131 : bitset_exception(ETL_ERROR_TEXT(
"bitset:overflow", ETL_BITSET_FILE_ID
"C"), file_name_, line_number_)
145#if !defined(ETL_BITSET_ELEMENT_TYPE)
146 #define ETL_BITSET_ELEMENT_TYPE uint_least8_t
151 typedef size_t size_type;
154 typedef element_type element_t;
157 static ETL_CONSTANT element_type ALL_CLEAR = 0;
178 friend class ibitset;
183 operator bool()
const
185 return p_bitset->test(position);
192 : p_bitset(other.p_bitset)
193 , position(other.position)
202 p_bitset->set(position, b);
211 p_bitset->set(position,
bool(r));
220 p_bitset->
flip(position);
229 return !p_bitset->test(position);
238 : p_bitset(ETL_NULLPTR)
247 : p_bitset(&r_bitset)
248 , position(position_)
271 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
283 bool test(
size_t position)
const
285 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow),
false);
289 if (position >= Active_Bits)
293 if (Number_Of_Elements == 0)
297 else if (Number_Of_Elements == 1)
300 mask = element_type(1) << position;
304 index = position >> etl::log2<Bits_Per_Element>::value;
305 mask = element_type(1) << (position & (Bits_Per_Element - 1));
308 return (pdata[index] & mask) != 0;
316 etl::fill_n(pdata, Number_Of_Elements - 1U, ALL_SET);
317 pdata[Number_Of_Elements - 1U] = Top_Mask;
327 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
331 if (position < Active_Bits)
333 if (Number_Of_Elements == 0)
337 else if (Number_Of_Elements == 1)
340 bit = element_type(1) << position;
344 index = position >> etl::log2<Bits_Per_Element>::value;
345 bit = element_type(1) << (position & (Bits_Per_Element - 1));
354 pdata[index] &=
~bit;
368 size_t i = etl::min(Active_Bits,
etl::strlen(text));
372 set(--i, *text++ == ETL_STRL(
'1'));
385 size_t i = etl::min(Active_Bits,
etl::strlen(text));
389 set(--i, *text++ == ETL_STRL(
'1'));
402 size_t i = etl::min(Active_Bits,
etl::strlen(text));
406 set(--i, *text++ == ETL_STRu(
'1'));
419 size_t i = etl::min(Active_Bits,
etl::strlen(text));
423 set(--i, *text++ == ETL_STRU(
'1'));
434 if (text == ETL_NULLPTR)
451 if (text == ETL_NULLPTR)
468 if (text == ETL_NULLPTR)
485 if (text == ETL_NULLPTR)
500 template <
typename T>
506 const bool OK = (
sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
512 uint_least8_t shift = 0U;
514 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
517 shift += uint_least8_t(Bits_Per_Element);
545 etl::fill_n(pdata, Number_Of_Elements, ALL_CLEAR);
555 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
559 if (position < Active_Bits)
561 if (Number_Of_Elements == 0)
565 else if (Number_Of_Elements == 1)
568 bit = element_type(1) << position;
572 index = position >> etl::log2<Bits_Per_Element>::value;
573 bit = element_type(1) << (position & (Bits_Per_Element - 1));
576 pdata[index] &=
~bit;
592 clear_unused_bits_in_msb();
602 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
606 if (Number_Of_Elements == 0)
610 else if (Number_Of_Elements == 1)
613 bit = element_type(1) << position;
617 index = position >> log2<Bits_Per_Element>::value;
618 bit = element_type(1) << (position & (Bits_Per_Element - 1));
631 if (Number_Of_Elements == 0UL)
637 for (
size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
639 if (pdata[i] != ALL_SET)
646 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
667 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
700 if (Number_Of_Elements == 0)
702 return ibitset::npos;
704 else if (Number_Of_Elements == 1)
711 index = position >> log2<Bits_Per_Element>::value;
712 bit = position & (Bits_Per_Element - 1);
715 element_type mask = 1 <<
bit;
718 while (index < Number_Of_Elements)
720 element_type
value = pdata[index];
723 if ((state && (
value != ALL_CLEAR)) ||
724 (!state && (
value != ALL_SET)))
727 while ((
bit < Bits_Per_Element) && (position < Active_Bits))
730 if (((
value & mask) != 0) == state)
743 position += (Bits_Per_Element -
bit);
753 return ibitset::npos;
761 return test(position);
777 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
779 pdata[i] &= other.pdata[i];
790 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
792 pdata[i] |= other.pdata[i];
803 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
805 pdata[i] ^= other.pdata[i];
816 if (shift >= Active_Bits)
820 else if (Number_Of_Elements != 0UL)
823 if (Number_Of_Elements == 1UL)
827 else if (shift == Bits_Per_Element)
829 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
835 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
838 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
841 int dst_index = int(Number_Of_Elements - 1U);
844 const size_t lsb_shift = Bits_Per_Element - split_position;
845 const size_t msb_shift = split_position;
849 const element_type lsb_shifted_mask = element_type(
lsb_mask << lsb_shift);
852 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
853 pdata[dst_index] = lsb;
857 while (src_index >= 0)
860 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
861 pdata[dst_index] = pdata[dst_index] | msb;
865 lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
866 pdata[dst_index] = lsb;
872 pdata[dst_index] &= lsb_shifted_mask;
876 for (
int i = 0; i <= dst_index; ++i)
883 clear_unused_bits_in_msb();
894 if (shift >= Active_Bits)
898 else if (Number_Of_Elements != 0UL)
901 if (Number_Of_Elements == 1UL)
906 else if (shift == Bits_Per_Element)
908 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
909 pdata[Number_Of_Elements - 1U] = 0;
914 const size_t split_position = shift % Bits_Per_Element;
917 int src_index = int(shift / Bits_Per_Element);
923 const size_t lsb_shift = Bits_Per_Element - split_position;
924 const size_t msb_shift = split_position;
928 const element_type msb_shifted_mask = element_type(
msb_mask >> msb_shift);
931 while (src_index <
int(Number_Of_Elements - 1))
934 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
938 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
941 pdata[dst_index] = lsb | msb;
946 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
947 pdata[dst_index] = msb;
951 pdata[dst_index] &= msb_shifted_mask;
955 for (
int i = dst_index; i < int(Number_Of_Elements); ++i)
972 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
983 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
993 return span_type(pdata, pdata + Number_Of_Elements);
1000 const_span_type span()
const
1002 return const_span_type(pdata, pdata + Number_Of_Elements);
1020 pdata[0] = element_type(
value);
1026 while ((
value != 0) && (i < Number_Of_Elements))
1028 pdata[i++] =
value & ALL_SET;
1033 clear_unused_bits_in_msb();
1043 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
1045 pdata[i] = ~pdata[i];
1048 clear_unused_bits_in_msb();
1062 ibitset(
size_t nbits_,
size_t size_, element_type* pdata_)
1063 : Active_Bits(nbits_)
1064 , Number_Of_Elements(size_)
1067 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1068 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1069 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1077 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1080 element_type Top_Mask;
1087 void clear_unused_bits_in_msb()
1089 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1095 const size_t Active_Bits;
1096 const size_t Number_Of_Elements;
1097 element_type* pdata;
1102#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1115 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1117 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1119 ETL_CONSTANT
size_t ibitset::Bits_Per_Element;
1128 template <
size_t MaxN>
1132 static ETL_CONSTANT
size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1136 static ETL_CONSTANT
size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1137 static ETL_CONSTANT
size_t Allocated_Bits = ALLOCATED_BITS;
1156 etl::copy_n(other.data, Array_Size, data);
1305 template <
typename T>
1309 ETL_STATIC_ASSERT(etl::is_integral<T>::value,
"Only integral types are supported");
1310 ETL_STATIC_ASSERT((
sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element),
"Type too small");
1355 template <
typename TString = etl::
string<MaxN>>
1357 template <
typename TString>
1359 TString
to_string(
typename TString::value_type zero =
typename TString::value_type(
'0'),
typename TString::value_type one =
typename TString::value_type(
'1'))
const
1363 result.resize(MaxN,
'\0');
1367 for (
size_t i = MaxN; i > 0; --i)
1369 result[MaxN - i] =
test(i - 1) ? one : zero;
1382 etl::copy_n(other.data, Array_Size, data);
1479 element_type data[Array_Size > 0U ? Array_Size : 1U];
1482 template <
size_t MaxN>
1483 ETL_CONSTANT
size_t bitset<MaxN>::ALLOCATED_BITS;
1485 template <
size_t MaxN>
1486 ETL_CONSTANT
size_t bitset<MaxN>::Allocated_Bits;
1492 template <
size_t MaxN>
1504 template<
size_t MaxN>
1516 template<
size_t MaxN>
1528 template<
size_t MaxN>
1531 return !(lhs == rhs);
1538template <
size_t MaxN>
void swap(etl::bitset< MaxN > &lhs, etl::bitset< MaxN > &rhs)
swap
Definition bitset_legacy.h:1539
The reference type returned.
Definition bitset_legacy.h:175
bool operator~() const
Return the logical inverse of the bit.
Definition bitset_legacy.h:227
bit_reference(const bit_reference &other)
Copy constructor.
Definition bitset_legacy.h:191
bit_reference & operator=(bool b)
Assignment operator.
Definition bitset_legacy.h:200
bit_reference & flip()
Flip the bit.
Definition bitset_legacy.h:218
Span - Fixed Extent.
Definition span.h:138
ETL_CONSTEXPR14 void transform_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function)
Definition algorithm.h:2771
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_bits(T value)
Definition binary.h:923
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:502
ibitset & set()
Set all bits.
Definition bitset_legacy.h:314
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:1216
ibitset & reset()
Resets the bitset.
Definition bitset_legacy.h:543
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition bitset_legacy.h:1391
bitset(const char16_t *text)
Construct from a string.
Definition bitset_legacy.h:1189
size_t find_first(bool state) const
Definition bitset_legacy.h:683
size_t find_next(bool state, size_t position) const
Definition bitset_legacy.h:694
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition bitset_legacy.h:1011
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:398
bool any() const
Are any of the bits set?
Definition bitset_legacy.h:657
~ibitset()
Destructor.
Definition bitset_legacy.h:1109
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition bitset_legacy.h:1472
bitset< MaxN > & flip()
Flip all of the bits.
Definition bitset_legacy.h:1336
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition bitset_legacy.h:1054
ibitset & operator|=(const ibitset &other)
operator |=
Definition bitset_legacy.h:788
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition bitset_legacy.h:1153
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition bitset_legacy.h:1062
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition bitset_legacy.h:1255
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition bitset_legacy.h:1235
bitset(const char *text)
Construct from a string.
Definition bitset_legacy.h:1171
void swap(ibitset &other)
swap
Definition bitset_legacy.h:981
ibitset & operator=(const ibitset &other)
operator =
Definition bitset_legacy.h:968
bitset< MaxN > & reset()
Reset all of the bits.
Definition bitset_legacy.h:1318
bitset(const wchar_t *text)
Construct from a string.
Definition bitset_legacy.h:1180
ibitset & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:892
ibitset & operator^=(const ibitset &other)
operator ^=
Definition bitset_legacy.h:801
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:1307
bitset(unsigned long long value)
Construct from a value.
Definition bitset_legacy.h:1162
ETL_CONSTEXPR14 void swap(etl::bitset< Active_Bits, TElement > &other) ETL_NOEXCEPT
swap
Definition bitset_new.h:2726
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:1327
size_t count() const
Count the number of bits set.
Definition bitset_legacy.h:267
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:381
TString to_string(typename TString::value_type zero=typename TString::value_type('0'), typename TString::value_type one=typename TString::value_type('1')) const
Returns a string representing the bitset.
Definition bitset_legacy.h:1359
ibitset & set(const char32_t *text)
Set from a u32string.
Definition bitset_legacy.h:483
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:415
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:325
ibitset & flip()
Flip all of the bits.
Definition bitset_legacy.h:585
bitset(const char32_t *text)
Construct from a string.
Definition bitset_legacy.h:1198
ibitset & operator&=(const ibitset &other)
operator &=
Definition bitset_legacy.h:775
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:1275
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:1442
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition bitset_legacy.h:1430
unsigned long to_ulong() const
Put to a unsigned long.
Definition bitset_legacy.h:527
bool operator[](size_t position) const
Read [] operator.
Definition bitset_legacy.h:759
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition bitset_legacy.h:535
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition bitset_legacy.h:1400
void invert()
Invert.
Definition bitset_legacy.h:1041
bitset()
Default constructor.
Definition bitset_legacy.h:1144
bitset< MaxN > operator~() const
operator ~
Definition bitset_legacy.h:1418
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition bitset_legacy.h:1451
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:1285
ibitset & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:553
bitset< MaxN > & set(const char *text)
Set from a string.
Definition bitset_legacy.h:1225
ibitset & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:364
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition bitset_legacy.h:1378
ibitset & set(const char16_t *text)
Set from a u16string.
Definition bitset_legacy.h:466
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition bitset_legacy.h:449
ibitset & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:814
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition bitset_legacy.h:1409
ETL_CONSTEXPR14 bool test() const
Definition bitset_new.h:2363
bool none() const
Are none of the bits set?
Definition bitset_legacy.h:665
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:1295
bool test(size_t position) const
Definition bitset_legacy.h:283
size_t size() const
The number of bits in the bitset.
Definition bitset_legacy.h:259
bitset< MaxN > & set(const char16_t *text)
Set from a string.
Definition bitset_legacy.h:1245
ibitset & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:600
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:1345
ibitset & set(const char *text)
Set from a string.
Definition bitset_legacy.h:432
bitset< MaxN > & set()
Set all of the bits.
Definition bitset_legacy.h:1207
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:1463
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition bitset_legacy.h:1075
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:1265
Bitset forward declaration.
Definition bitset_legacy.h:1130
Definition bitset_legacy.h:127
Definition bitset_legacy.h:113
Definition bitset_legacy.h:141
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition exception.h:47
Definition integral_limits.h:516
enable_if
Definition type_traits_generator.h:1254
make_unsigned
Definition type_traits_generator.h:1244
bitset_ext
Definition absolute.h:39
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition byte.h:265
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:273
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1151
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:281
ETL_CONSTEXPR14 size_t strlen(const T *t) ETL_NOEXCEPT
Alternative strlen for all character types.
Definition char_traits.h:287