31#ifndef ETL_BITSET_LEGACY_INCLUDED
32#define ETL_BITSET_LEGACY_INCLUDED
45#include "../static_assert.h"
54#if defined(ETL_COMPILER_KEIL)
55 #pragma diag_suppress 1300
60 #define ETL_STRL(x) L##x
61 #define ETL_STRu(x) u##x
62 #define ETL_STRU(x) U##x
86 bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
87 :
exception(reason_, file_name_, line_number_)
96 class bitset_nullptr :
public bitset_exception
100 bitset_nullptr(string_type file_name_, numeric_type line_number_)
101 : bitset_exception(ETL_ERROR_TEXT(
"bitset:null pointer", ETL_BITSET_FILE_ID
"A"), file_name_, line_number_)
110 class bitset_type_too_small :
public bitset_exception
114 bitset_type_too_small(string_type file_name_, numeric_type line_number_)
115 : bitset_exception(ETL_ERROR_TEXT(
"bitset:type_too_small", ETL_BITSET_FILE_ID
"B"), file_name_, line_number_)
124 class bitset_overflow :
public bitset_exception
128 bitset_overflow(string_type file_name_, numeric_type line_number_)
129 : bitset_exception(ETL_ERROR_TEXT(
"bitset:overflow", ETL_BITSET_FILE_ID
"C"), file_name_, line_number_)
143#if !defined(ETL_BITSET_ELEMENT_TYPE)
144 #define ETL_BITSET_ELEMENT_TYPE uint_least8_t
149 typedef size_t size_type;
151 typedef typename etl::make_unsigned<ETL_BITSET_ELEMENT_TYPE>::type element_type;
152 typedef element_type element_t;
155 static ETL_CONSTANT element_type ALL_CLEAR = 0;
176 friend class ibitset;
181 operator bool()
const
183 return p_bitset->test(position);
190 : p_bitset(other.p_bitset)
191 , position(other.position)
200 p_bitset->set(position, b);
209 p_bitset->set(position,
bool(r));
218 p_bitset->
flip(position);
227 return !p_bitset->test(position);
236 : p_bitset(ETL_NULLPTR)
245 : p_bitset(&r_bitset)
246 , position(position_)
269 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
282 bool test(
size_t position)
const
284 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow),
false);
288 if (position >= Active_Bits)
292 if (Number_Of_Elements == 0)
296 else if (Number_Of_Elements == 1)
299 mask = element_type(1) << position;
303 index = position >> etl::log2<Bits_Per_Element>::value;
304 mask = element_type(1) << (position & (Bits_Per_Element - 1));
307 return (pdata[index] & mask) != 0;
315 etl::fill_n(pdata, Number_Of_Elements - 1U, ALL_SET);
316 pdata[Number_Of_Elements - 1U] = Top_Mask;
326 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
330 if (position < Active_Bits)
332 if (Number_Of_Elements == 0)
336 else if (Number_Of_Elements == 1)
339 bit = element_type(1) << position;
343 index = position >> etl::log2<Bits_Per_Element>::value;
344 bit = element_type(1) << (position & (Bits_Per_Element - 1));
353 pdata[index] &=
~bit;
367 size_t i = etl::min(Active_Bits,
etl::strlen(text));
371 set(--i, *text++ == ETL_STRL(
'1'));
384 size_t i = etl::min(Active_Bits,
etl::strlen(text));
388 set(--i, *text++ == ETL_STRL(
'1'));
401 size_t i = etl::min(Active_Bits,
etl::strlen(text));
405 set(--i, *text++ == ETL_STRu(
'1'));
418 size_t i = etl::min(Active_Bits,
etl::strlen(text));
422 set(--i, *text++ == ETL_STRU(
'1'));
433 if (text == ETL_NULLPTR)
450 if (text == ETL_NULLPTR)
467 if (text == ETL_NULLPTR)
484 if (text == ETL_NULLPTR)
499 template <
typename T>
500 typename etl::enable_if<etl::is_integral<T>::value, T>
::type value()
const
504 const bool OK = (
sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
510 uint_least8_t shift = 0U;
512 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
514 v |= T(
typename etl::make_unsigned<T>::type(pdata[i]) << shift);
515 shift += uint_least8_t(Bits_Per_Element);
543 etl::fill_n(pdata, Number_Of_Elements, ALL_CLEAR);
553 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
557 if (position < Active_Bits)
559 if (Number_Of_Elements == 0)
563 else if (Number_Of_Elements == 1)
566 bit = element_type(1) << position;
570 index = position >> etl::log2<Bits_Per_Element>::value;
571 bit = element_type(1) << (position & (Bits_Per_Element - 1));
574 pdata[index] &=
~bit;
587 clear_unused_bits_in_msb();
597 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
601 if (Number_Of_Elements == 0)
605 else if (Number_Of_Elements == 1)
608 bit = element_type(1) << position;
612 index = position >> log2<Bits_Per_Element>::value;
613 bit = element_type(1) << (position & (Bits_Per_Element - 1));
626 if (Number_Of_Elements == 0UL)
632 for (
size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
634 if (pdata[i] != ALL_SET)
641 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
662 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
696 if (Number_Of_Elements == 0)
698 return ibitset::npos;
700 else if (Number_Of_Elements == 1)
707 index = position >> log2<Bits_Per_Element>::value;
708 bit = position & (Bits_Per_Element - 1);
711 element_type mask = 1 <<
bit;
714 while (index < Number_Of_Elements)
716 element_type
value = pdata[index];
719 if ((state && (
value != ALL_CLEAR)) || (!state && (
value != ALL_SET)))
722 while ((
bit < Bits_Per_Element) && (position < Active_Bits))
725 if (((
value & mask) != 0) == state)
738 position += (Bits_Per_Element -
bit);
748 return ibitset::npos;
756 return test(position);
772 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
774 pdata[i] &= other.pdata[i];
785 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
787 pdata[i] |= other.pdata[i];
798 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
800 pdata[i] ^= other.pdata[i];
811 if (shift >= Active_Bits)
815 else if (Number_Of_Elements != 0UL)
818 if (Number_Of_Elements == 1UL)
822 else if (shift == Bits_Per_Element)
824 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
830 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
833 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
836 int dst_index = int(Number_Of_Elements - 1U);
839 const size_t lsb_shift = Bits_Per_Element - split_position;
840 const size_t msb_shift = split_position;
844 const element_type lsb_shifted_mask = element_type(
lsb_mask << lsb_shift);
847 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
848 pdata[dst_index] = lsb;
852 while (src_index >= 0)
855 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
856 pdata[dst_index] = pdata[dst_index] | msb;
860 lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
861 pdata[dst_index] = lsb;
867 pdata[dst_index] &= lsb_shifted_mask;
871 for (
int i = 0; i <= dst_index; ++i)
878 clear_unused_bits_in_msb();
889 if (shift >= Active_Bits)
893 else if (Number_Of_Elements != 0UL)
896 if (Number_Of_Elements == 1UL)
901 else if (shift == Bits_Per_Element)
903 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
904 pdata[Number_Of_Elements - 1U] = 0;
909 const size_t split_position = shift % Bits_Per_Element;
912 int src_index = int(shift / Bits_Per_Element);
918 const size_t lsb_shift = Bits_Per_Element - split_position;
919 const size_t msb_shift = split_position;
923 const element_type msb_shifted_mask = element_type(
msb_mask >> msb_shift);
926 while (src_index <
int(Number_Of_Elements - 1))
929 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
933 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
936 pdata[dst_index] = lsb | msb;
941 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
942 pdata[dst_index] = msb;
946 pdata[dst_index] &= msb_shifted_mask;
950 for (
int i = dst_index; i < int(Number_Of_Elements); ++i)
967 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
978 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
988 return span_type(pdata, pdata + Number_Of_Elements);
995 const_span_type span()
const
997 return const_span_type(pdata, pdata + Number_Of_Elements);
1015 pdata[0] = element_type(
value);
1021 while ((
value != 0) && (i < Number_Of_Elements))
1023 pdata[i++] =
value & ALL_SET;
1028 clear_unused_bits_in_msb();
1038 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
1040 pdata[i] = ~pdata[i];
1043 clear_unused_bits_in_msb();
1057 ibitset(
size_t nbits_,
size_t size_, element_type* pdata_)
1058 : Active_Bits(nbits_)
1059 , Number_Of_Elements(size_)
1062 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1063 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1064 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1072 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1075 element_type Top_Mask;
1082 void clear_unused_bits_in_msb()
1084 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1090 const size_t Active_Bits;
1091 const size_t Number_Of_Elements;
1092 element_type* pdata;
1097#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1110 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1112 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1114 ETL_CONSTANT
size_t ibitset::Bits_Per_Element;
1122 template <
size_t MaxN>
1125 static ETL_CONSTANT
size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1129 static ETL_CONSTANT
size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1130 static ETL_CONSTANT
size_t Allocated_Bits = ALLOCATED_BITS;
1149 etl::copy_n(other.data, Array_Size, data);
1298 template <
typename T>
1299 typename etl::enable_if<etl::is_integral<T>::value, T>
::type value()
const
1301 ETL_STATIC_ASSERT(etl::is_integral<T>::value,
"Only integral types are supported");
1302 ETL_STATIC_ASSERT((
sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element),
"Type too small");
1347 template <
typename TString = etl::
string<MaxN>>
1349 template <
typename TString>
1351 TString
to_string(
typename TString::value_type zero =
typename TString::value_type(
'0'),
1352 typename TString::value_type one =
typename TString::value_type(
'1'))
const
1356 result.resize(MaxN,
'\0');
1360 for (
size_t i = MaxN; i > 0; --i)
1362 result[MaxN - i] =
test(i - 1) ? one : zero;
1375 etl::copy_n(other.data, Array_Size, data);
1472 element_type
data[Array_Size > 0U ? Array_Size : 1U];
1475 template <
size_t MaxN>
1476 ETL_CONSTANT
size_t bitset<MaxN>::ALLOCATED_BITS;
1478 template <
size_t MaxN>
1479 ETL_CONSTANT
size_t bitset<MaxN>::Allocated_Bits;
1485 template <
size_t MaxN>
1497 template <
size_t MaxN>
1509 template <
size_t MaxN>
1521 template <
size_t MaxN>
1524 return !(lhs == rhs);
1531template <
size_t MaxN>
void swap(etl::bitset< MaxN > &lhs, etl::bitset< MaxN > &rhs)
swap
Definition bitset_legacy.h:1532
The reference type returned.
Definition bitset_legacy.h:173
bit_reference & operator=(const bit_reference &r)
Assignment operator.
Definition bitset_legacy.h:207
bool operator~() const
Return the logical inverse of the bit.
Definition bitset_legacy.h:225
bit_reference(const bit_reference &other)
Copy constructor.
Definition bitset_legacy.h:189
bit_reference & operator=(bool b)
Assignment operator.
Definition bitset_legacy.h:198
bit_reference & flip()
Flip the bit.
Definition bitset_legacy.h:216
Span - Fixed Extent.
Definition span.h:208
ETL_CONSTEXPR14 void transform_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function)
Definition algorithm.h:2960
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:918
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:500
ibitset & set()
Set all bits.
Definition bitset_legacy.h:313
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:1209
ibitset & reset()
Resets the bitset.
Definition bitset_legacy.h:541
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition bitset_legacy.h:1384
bitset(const char16_t *text)
Construct from a string.
Definition bitset_legacy.h:1182
size_t find_first(bool state) const
Definition bitset_legacy.h:679
size_t find_next(bool state, size_t position) const
Definition bitset_legacy.h:690
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition bitset_legacy.h:1006
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:397
bool any() const
Are any of the bits set?
Definition bitset_legacy.h:652
~ibitset()
Destructor.
Definition bitset_legacy.h:1106
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition bitset_legacy.h:1465
bitset< MaxN > & flip()
Flip all of the bits.
Definition bitset_legacy.h:1328
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition bitset_legacy.h:1049
ibitset & operator|=(const ibitset &other)
operator |=
Definition bitset_legacy.h:783
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition bitset_legacy.h:1146
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition bitset_legacy.h:1057
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition bitset_legacy.h:1248
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition bitset_legacy.h:1228
bitset(const char *text)
Construct from a string.
Definition bitset_legacy.h:1164
void swap(ibitset &other)
swap
Definition bitset_legacy.h:976
ibitset & operator=(const ibitset &other)
operator =
Definition bitset_legacy.h:963
bitset< MaxN > & reset()
Reset all of the bits.
Definition bitset_legacy.h:1310
bitset(const wchar_t *text)
Construct from a string.
Definition bitset_legacy.h:1173
bit_reference operator[](size_t position)
Write [] operator.
Definition bitset_legacy.h:762
ibitset & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:887
ibitset & operator^=(const ibitset &other)
operator ^=
Definition bitset_legacy.h:796
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:1299
bitset(unsigned long long value)
Construct from a value.
Definition bitset_legacy.h:1155
ETL_CONSTEXPR14 void swap(etl::bitset< Active_Bits, TElement > &other) ETL_NOEXCEPT
swap
Definition bitset_new.h:2454
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:1319
size_t count() const
Count the number of bits set.
Definition bitset_legacy.h:265
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:380
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:1351
ibitset & set(const char32_t *text)
Set from a u32string.
Definition bitset_legacy.h:482
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:414
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:324
ibitset & flip()
Flip all of the bits.
Definition bitset_legacy.h:583
bitset(const char32_t *text)
Construct from a string.
Definition bitset_legacy.h:1191
ibitset & operator&=(const ibitset &other)
operator &=
Definition bitset_legacy.h:770
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:1268
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:1435
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition bitset_legacy.h:1423
unsigned long to_ulong() const
Put to a unsigned long.
Definition bitset_legacy.h:525
bool operator[](size_t position) const
Read [] operator.
Definition bitset_legacy.h:754
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition bitset_legacy.h:533
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition bitset_legacy.h:1393
void invert()
Invert.
Definition bitset_legacy.h:1036
bitset()
Default constructor.
Definition bitset_legacy.h:1137
bitset< MaxN > operator~() const
operator ~
Definition bitset_legacy.h:1411
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition bitset_legacy.h:1444
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:1278
ibitset & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:551
bitset< MaxN > & set(const char *text)
Set from a string.
Definition bitset_legacy.h:1218
ibitset & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:363
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition bitset_legacy.h:1371
ibitset & set(const char16_t *text)
Set from a u16string.
Definition bitset_legacy.h:465
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition bitset_legacy.h:448
ibitset & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:809
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition bitset_legacy.h:1402
ETL_CONSTEXPR14 bool test() const
Definition bitset_new.h:2091
bool none() const
Are none of the bits set?
Definition bitset_legacy.h:660
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:1288
bool test(size_t position) const
Definition bitset_legacy.h:282
size_t size() const
The number of bits in the bitset.
Definition bitset_legacy.h:257
bitset< MaxN > & set(const char16_t *text)
Set from a string.
Definition bitset_legacy.h:1238
ibitset & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:595
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:1337
ibitset & set(const char *text)
Set from a string.
Definition bitset_legacy.h:431
bitset< MaxN > & set()
Set all of the bits.
Definition bitset_legacy.h:1200
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:1456
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition bitset_legacy.h:1070
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:1258
Bitset forward declaration.
Definition bitset_legacy.h:1124
Definition bitset_legacy.h:125
Definition bitset_legacy.h:111
Definition bitset_legacy.h:139
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition byte.h:250
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:258
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:266
ETL_CONSTEXPR14 size_t strlen(const T *t) ETL_NOEXCEPT
Alternative strlen for all character types.
Definition char_traits.h:293