31#ifndef ETL_BITSET_LEGACY_INCLUDED
32#define ETL_BITSET_LEGACY_INCLUDED
34#include "../platform.h"
35#include "../algorithm.h"
36#include "../iterator.h"
37#include "../integral_limits.h"
38#include "../algorithm.h"
39#include "../nullptr.h"
41#include "../exception.h"
42#include "../integral_limits.h"
44#include "../char_traits.h"
45#include "../static_assert.h"
46#include "../error_handler.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_)
103 :
bitset_exception(ETL_ERROR_TEXT(
"bitset:null pointer", ETL_BITSET_FILE_ID
"A"), file_name_, line_number_)
117 :
bitset_exception(ETL_ERROR_TEXT(
"bitset:type_too_small", ETL_BITSET_FILE_ID
"B"), file_name_, 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 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;
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)
244 bit_reference(
ibitset& r_bitset,
size_t position_)
245 : p_bitset(&r_bitset)
246 , position(position_)
269 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
281 bool test(
size_t position)
const
283 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow),
false);
287 if (position >= Active_Bits)
291 if (Number_Of_Elements == 0)
295 else if (Number_Of_Elements == 1)
298 mask = element_type(1) << position;
303 mask = element_type(1) << (position & (Bits_Per_Element - 1));
306 return (pdata[index] & mask) != 0;
314 ::memset(pdata, 0xFF, Number_Of_Elements);
315 pdata[Number_Of_Elements - 1U] = Top_Mask;
325 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
329 if (position < Active_Bits)
331 if (Number_Of_Elements == 0)
335 else if (Number_Of_Elements == 1)
338 bit = element_type(1) << position;
343 bit = element_type(1) << (position & (Bits_Per_Element - 1));
352 pdata[index] &=
~bit;
366 size_t i = etl::min(Active_Bits,
etl::strlen(text));
370 set(--i, *text++ == ETL_STRL(
'1'));
383 size_t i = etl::min(Active_Bits,
etl::strlen(text));
387 set(--i, *text++ == ETL_STRL(
'1'));
400 size_t i = etl::min(Active_Bits,
etl::strlen(text));
404 set(--i, *text++ == ETL_STRu(
'1'));
417 size_t i = etl::min(Active_Bits,
etl::strlen(text));
421 set(--i, *text++ == ETL_STRU(
'1'));
470 template <
typename T>
476 const bool OK = (
sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
482 uint_least8_t shift = 0U;
484 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
486 v |= T(
typename etl::make_unsigned<T>::type(pdata[i]) << shift);
487 shift += uint_least8_t(Bits_Per_Element);
499 return value<unsigned long>();
507 return value<unsigned long long>();
515 ::memset(pdata, 0x00, Number_Of_Elements);
525 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
529 if (position < Active_Bits)
531 if (Number_Of_Elements == 0)
535 else if (Number_Of_Elements == 1)
538 bit = element_type(1) << position;
543 bit = element_type(1) << (position & (Bits_Per_Element - 1));
546 pdata[index] &=
~bit;
557 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
559 pdata[i] = ~pdata[i];
562 clear_unused_bits_in_msb();
572 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(
bitset_overflow), *
this);
576 if (Number_Of_Elements == 0)
580 else if (Number_Of_Elements == 1)
583 bit = element_type(1) << position;
588 bit = element_type(1) << (position & (Bits_Per_Element - 1));
601 if (Number_Of_Elements == 0UL)
607 for (
size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
609 if (pdata[i] != ALL_SET)
616 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
637 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
670 if (Number_Of_Elements == 0)
672 return ibitset::npos;
674 else if (Number_Of_Elements == 1)
682 bit = position & (Bits_Per_Element - 1);
685 element_type mask = 1 <<
bit;
688 while (index < Number_Of_Elements)
690 element_type
value = pdata[index];
693 if ((state && (
value != ALL_CLEAR)) ||
694 (!state && (
value != ALL_SET)))
697 while ((
bit < Bits_Per_Element) && (position < Active_Bits))
700 if (((
value & mask) != 0) == state)
713 position += (Bits_Per_Element -
bit);
723 return ibitset::npos;
731 return test(position);
747 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
749 pdata[i] &= other.pdata[i];
760 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
762 pdata[i] |= other.pdata[i];
773 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
775 pdata[i] ^= other.pdata[i];
786 if (shift >= Active_Bits)
790 else if (Number_Of_Elements != 0UL)
793 if (Number_Of_Elements == 1UL)
797 else if (shift == Bits_Per_Element)
799 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
805 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
808 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
811 int dst_index = int(Number_Of_Elements - 1U);
814 const size_t lsb_shift = Bits_Per_Element - split_position;
815 const size_t msb_shift = split_position;
819 const element_type lsb_shifted_mask = element_type(
lsb_mask << lsb_shift);
822 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
823 pdata[dst_index] = lsb;
827 while (src_index >= 0)
830 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
831 pdata[dst_index] = pdata[dst_index] | msb;
835 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
836 pdata[dst_index] = lsb;
842 pdata[dst_index] &= lsb_shifted_mask;
846 while (dst_index >= 0)
848 pdata[dst_index] = 0;
854 clear_unused_bits_in_msb();
865 if (shift >= Active_Bits)
869 else if (Number_Of_Elements != 0UL)
872 if (Number_Of_Elements == 1UL)
877 else if (shift == Bits_Per_Element)
879 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
880 pdata[Number_Of_Elements - 1U] = 0;
885 const size_t split_position = shift % Bits_Per_Element;
888 int src_index = int(shift / Bits_Per_Element);
894 const size_t lsb_shift = Bits_Per_Element - split_position;
895 const size_t msb_shift = split_position;
899 const element_type msb_shifted_mask = element_type(
msb_mask >> msb_shift);
902 while (src_index <
int(Number_Of_Elements - 1))
905 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
909 element_type lsb = element_type((pdata[src_index] &
lsb_mask) << lsb_shift);
912 pdata[dst_index] = lsb | msb;
917 element_type msb = element_type((pdata[src_index] &
msb_mask) >> msb_shift);
918 pdata[dst_index] = msb;
922 pdata[dst_index] &= msb_shifted_mask;
926 while (dst_index <
int(Number_Of_Elements))
928 pdata[dst_index] = 0;
944 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
955 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
965 return span_type(pdata, pdata + Number_Of_Elements);
972 const_span_type span()
const
974 return const_span_type(pdata, pdata + Number_Of_Elements);
992 pdata[0] = element_type(
value);
998 while ((
value != 0) && (i < Number_Of_Elements))
1000 pdata[i++] =
value & ALL_SET;
1005 clear_unused_bits_in_msb();
1015 for (
size_t i = 0UL; i < Number_Of_Elements; ++i)
1017 pdata[i] = ~pdata[i];
1020 clear_unused_bits_in_msb();
1034 ibitset(
size_t nbits_,
size_t size_, element_type* pdata_)
1035 : Active_Bits(nbits_)
1036 , Number_Of_Elements(size_)
1039 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1040 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1041 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1049 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1052 element_type Top_Mask;
1059 void clear_unused_bits_in_msb()
1061 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1067 const size_t Active_Bits;
1068 const size_t Number_Of_Elements;
1069 element_type* pdata;
1074#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1087 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1089 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1091 ETL_CONSTANT
size_t ibitset::Bits_Per_Element;
1100 template <
size_t MaxN>
1104 static ETL_CONSTANT
size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1108 static ETL_CONSTANT
size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1109 static ETL_CONSTANT
size_t Allocated_Bits = ALLOCATED_BITS;
1128 etl::copy_n(other.data, Array_Size, data);
1199 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(
bitset_nullptr), *
this);
1210 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(
bitset_nullptr), *
this);
1221 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(
bitset_nullptr), *
this);
1232 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(
bitset_nullptr), *
this);
1281 template <
typename T>
1286 ETL_STATIC_ASSERT((
sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element),
"Type too small");
1288 return ibitset::value<T>();
1331 template <
typename TString = etl::
string<MaxN>>
1333 template <
typename TString>
1335 TString
to_string(
typename TString::value_type zero =
typename TString::value_type(
'0'),
typename TString::value_type one =
typename TString::value_type(
'1'))
const
1339 result.resize(MaxN,
'\0');
1343 for (
size_t i = MaxN; i > 0; --i)
1345 result[MaxN - i] =
test(i - 1) ? one : zero;
1358 etl::copy_n(other.data, Array_Size, data);
1455 element_type data[Array_Size > 0U ? Array_Size : 1U];
1458 template <
size_t MaxN>
1459 ETL_CONSTANT
size_t bitset<MaxN>::ALLOCATED_BITS;
1461 template <
size_t MaxN>
1462 ETL_CONSTANT
size_t bitset<MaxN>::Allocated_Bits;
1468 template <
size_t MaxN>
1480 template<
size_t MaxN>
1492 template<
size_t MaxN>
1504 template<
size_t MaxN>
1507 return !(lhs == rhs);
1514template <
size_t MaxN>
void swap(etl::bitset< MaxN > &lhs, etl::bitset< MaxN > &rhs)
swap
Definition: bitset_legacy.h:1515
The reference type returned.
Definition: bitset_legacy.h:173
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
Definition: binary.h:2211
Definition: binary.h:2233
Span - Fixed Extent.
Definition: span.h:62
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:956
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset_legacy.h:472
ibitset & set()
Set all bits.
Definition: bitset_legacy.h:312
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset_legacy.h:1188
ibitset & reset()
Resets the bitset.
Definition: bitset_legacy.h:513
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition: bitset_legacy.h:1367
bitset(const char16_t *text)
Construct from a string.
Definition: bitset_legacy.h:1161
size_t find_first(bool state) const
Definition: bitset_legacy.h:653
size_t find_next(bool state, size_t position) const
Definition: bitset_legacy.h:664
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition: bitset_legacy.h:983
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset_legacy.h:396
bool any() const
Are any of the bits set?
Definition: bitset_legacy.h:627
~ibitset()
Destructor.
Definition: bitset_legacy.h:1081
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition: bitset_legacy.h:1448
bitset< MaxN > & flip()
Flip all of the bits.
Definition: bitset_legacy.h:1312
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition: bitset_legacy.h:1026
ibitset & operator|=(const ibitset &other)
operator |=
Definition: bitset_legacy.h:758
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition: bitset_legacy.h:1125
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition: bitset_legacy.h:1034
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition: bitset_legacy.h:1230
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition: bitset_legacy.h:1208
bitset(const char *text)
Construct from a string.
Definition: bitset_legacy.h:1143
void swap(ibitset &other)
swap
Definition: bitset_legacy.h:953
ibitset & operator=(const ibitset &other)
operator =
Definition: bitset_legacy.h:940
bitset< MaxN > & reset()
Reset all of the bits.
Definition: bitset_legacy.h:1294
bitset(const wchar_t *text)
Construct from a string.
Definition: bitset_legacy.h:1152
ibitset & operator>>=(size_t shift)
operator >>=
Definition: bitset_legacy.h:863
ibitset & operator^=(const ibitset &other)
operator ^=
Definition: bitset_legacy.h:771
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset_legacy.h:1283
bitset(unsigned long long value)
Construct from a value.
Definition: bitset_legacy.h:1134
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition: bitset_legacy.h:1303
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:379
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:1335
ibitset & set(const char32_t *text)
Set from a u32string.
Definition: bitset_legacy.h:460
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset_legacy.h:413
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset_legacy.h:323
ibitset & flip()
Flip all of the bits.
Definition: bitset_legacy.h:555
bitset(const char32_t *text)
Construct from a string.
Definition: bitset_legacy.h:1170
ibitset & operator&=(const ibitset &other)
operator &=
Definition: bitset_legacy.h:745
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset_legacy.h:1251
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition: bitset_legacy.h:1418
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition: bitset_legacy.h:1406
unsigned long to_ulong() const
Put to a unsigned long.
Definition: bitset_legacy.h:497
bool operator[](size_t position) const
Read [] operator.
Definition: bitset_legacy.h:729
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition: bitset_legacy.h:505
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition: bitset_legacy.h:1376
void invert()
Invert.
Definition: bitset_legacy.h:1013
bitset()
Default constructor.
Definition: bitset_legacy.h:1116
bitset< MaxN > operator~() const
operator ~
Definition: bitset_legacy.h:1394
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition: bitset_legacy.h:1427
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset_legacy.h:1261
ibitset & reset(size_t position)
Reset the bit at the position.
Definition: bitset_legacy.h:523
bitset< MaxN > & set(const char *text)
Set from a string.
Definition: bitset_legacy.h:1197
ibitset & from_string(const char *text)
Set from a string.
Definition: bitset_legacy.h:362
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition: bitset_legacy.h:1354
ibitset & set(const char16_t *text)
Set from a u16string.
Definition: bitset_legacy.h:450
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition: bitset_legacy.h:440
ibitset & operator<<=(size_t shift)
operator <<=
Definition: bitset_legacy.h:784
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition: bitset_legacy.h:1385
bool none() const
Are none of the bits set?
Definition: bitset_legacy.h:635
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset_legacy.h:1271
bool test(size_t position) const
Definition: bitset_legacy.h:281
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:1219
ibitset & flip(size_t position)
Flip the bit at the position.
Definition: bitset_legacy.h:570
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition: bitset_legacy.h:1321
ibitset & set(const char *text)
Set from a string.
Definition: bitset_legacy.h:430
bitset< MaxN > & set()
Set all of the bits.
Definition: bitset_legacy.h:1179
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition: bitset_legacy.h:1439
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition: bitset_legacy.h:1047
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition: bitset_legacy.h:1241
Definition: bitset_legacy.h:1102
Definition: bitset_legacy.h:85
Definition: bitset_legacy.h:99
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:468
enable_if
Definition: type_traits_generator.h:1191
is_integral
Definition: type_traits_generator.h:1001
bitset_ext
Definition: absolute.h:38
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:645
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition: byte.h:281
ETL_CONSTEXPR size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:267