29 #ifndef __ETL_BITSET__
30 #define __ETL_BITSET__
64 template <const
size_t N>
83 return p_bitset->test(position);
91 p_bitset->set(position, b);
100 p_bitset->set(position,
bool(r));
109 p_bitset->flip(position);
118 return !p_bitset->test(position);
135 bit_reference(
bitset<N>& r_bitset,
size_t position)
136 : p_bitset(&r_bitset),
148 class iterator :
public std::iterator<std::random_access_iterator_tag, bool>
167 : position(other.position)
222 return p_bitset->test(position);
248 position = other.position;
249 p_bitset = other.p_bitset;
278 return lhs.position == rhs.position;
286 return lhs.position != rhs.position;
294 return lhs.position < rhs.position;
302 return lhs.position > rhs.position;
310 return lhs.position <= rhs.position;
318 return lhs.position >-rhs.position;
327 : p_bitset(&r_bitset),
338 class const_iterator :
public std::iterator<std::random_access_iterator_tag, const bool>
356 : position(other.position)
364 : position(other.position)
411 return p_bitset->test(position);
437 position = other.position;
438 p_bitset = other.p_bitset;
467 return lhs.position == rhs.position;
475 return lhs.position != rhs.position;
483 return lhs.position < rhs.position;
491 return lhs.position > rhs.position;
499 return lhs.position <= rhs.position;
507 return lhs.position >- rhs.position;
516 : p_bitset(&r_bitset),
559 while ((value != 0) && (i < ARRAY_SIZE))
561 data[i++] = value & ALL_SET;
562 value = value >> SHIFT;
566 data.
back() &= TOP_MASK;
576 size_t i = std::min(N, strlen(text));
580 set(--i, *text++ ==
'1');
590 data.
back() &= TOP_MASK;
608 bit = element_type(1) << position;
613 bit = element_type(1) << (position & (BITS_PER_ELEMENT - 1));
636 size_t i = std::min(N, strlen(text));
640 set(--i, *text++ ==
'1');
651 data.
fill(ALL_CLEAR);
668 bit = element_type(1) << position;
673 bit = element_type(1) << (position & (BITS_PER_ELEMENT - 1));
687 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
692 data.
back() &= TOP_MASK;
710 bit = element_type(1) << position;
715 bit = element_type(1) << (position & (BITS_PER_ELEMENT - 1));
729 return test(position);
737 return bit_reference(*
this, position);
744 bool test(
size_t position)
const
754 bit = element_type(1) << position;
759 bit = element_type(1) << (position & (BITS_PER_ELEMENT - 1));
762 return (data[index] & bit) != 0;
776 for (
size_t i = 0; i < (ARRAY_SIZE - 1); ++i)
778 if (data[i] != ALL_SET)
785 if (data[ARRAY_SIZE - 1] != (ALL_SET & TOP_MASK))
806 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
824 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
826 element_type value = data[i];
867 size_t bit_index = position & log2<BITS_PER_ELEMENT>::value;;
868 element_type mask = 1 << bit_index;
871 while (element_index < ARRAY_SIZE)
873 const element_type& element = data[element_index];
876 while ((bit_index < BITS_PER_ELEMENT) && (position != N))
879 if (((element & mask) != 0) == state)
905 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
907 data[i] &= other.data[i];
918 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
920 data[i] |= other.data[i];
931 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
933 data[i] ^= other.data[i];
946 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
963 temp.data[0] = data[0] << shift;
967 size_t source = N - shift - 1;
968 size_t destination = N - 1;
970 for (
size_t i = 0; i < (N - shift); ++i)
972 temp.
set(destination--,
test(source--));
990 size_t source = N - shift - 1;
991 size_t destination = N - 1;
993 for (
size_t i = 0; i < (N - shift); ++i)
995 set(destination--,
test(source--));
998 for (
size_t i = 0; i < shift; ++i)
1000 reset(destination--);
1014 if (ARRAY_SIZE == 1)
1016 temp.data[0] = data[0] >> shift;
1020 size_t source = shift;
1021 size_t destination = 0;
1025 temp.
set(destination++,
test(source++));
1037 if (ARRAY_SIZE == 1)
1043 size_t source = shift;
1044 size_t destination = 0;
1046 for (
size_t i = 0; i < (N - shift); ++i)
1048 set(destination++,
test(source++));
1051 for (
size_t i = 0; i < shift; ++i)
1053 reset(destination++);
1065 data.
swap(other.data);
1073 return iterator(*
this, 0);
1081 return const_iterator(*
this, 0);
1089 return const_iterator(*
this, 0);
1097 return iterator(*
this, N - 1);
1105 return const_iterator(*
this, N - 1);
1113 return const_iterator(*
this, N - 1);
1122 for (
size_t i = 0; i < ARRAY_SIZE; ++i)
1124 if (lhs.data[i] != rhs.data[i])
1136 typedef typename smallest_uint_for_bits<N>::type element_type;
1139 static const element_type ALL_CLEAR = 0;
1141 static const size_t ARRAY_SIZE = (N % BITS_PER_ELEMENT == 0) ? N / BITS_PER_ELEMENT : N / BITS_PER_ELEMENT + 1;
1142 static const size_t TOTAL_BITS = ARRAY_SIZE * BITS_PER_ELEMENT;
1143 static const size_t TOP_MASK_SHIFT = ((BITS_PER_ELEMENT - (TOTAL_BITS - N)) % BITS_PER_ELEMENT);
1144 static const element_type TOP_MASK = element_type(TOP_MASK_SHIFT == 0 ? ALL_SET : ~(ALL_SET << TOP_MASK_SHIFT));
1153 template <const
size_t N>
1165 template<const
size_t N>
1177 template<const
size_t N>
1189 template<const
size_t N>
1192 return !(lhs == rhs);
1199 template <const
size_t N>
1206 #define min(a,b) (((a) < (b)) ? (a) : (b))
iterator begin()
begin
Definition: bitset.h:1071
friend bool operator>=(const const_iterator &lhs, const const_iterator &rhs)
>= operator
Definition: bitset.h:505
const_iterator()
Constructor.
Definition: bitset.h:347
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
!= operator
Definition: bitset.h:473
friend const_iterator operator+(const const_iterator &other, int i)
Definition: bitset.h:445
bitset< N > & operator<<=(size_t shift)
operator <<=
Definition: bitset.h:982
bitset< N > operator<<(size_t shift) const
operator <<
Definition: bitset.h:957
bool none() const
Are none of th bits set?
Definition: bitset.h:804
bitset< N > operator&(const bitset< N > &lhs, const bitset< N > &rhs)
Definition: bitset.h:1154
bitset< N > & operator^=(const bitset< N > &other)
operator ^=
Definition: bitset.h:929
const_iterator begin() const
begin
Definition: bitset.h:1079
iterator & operator-=(int i)
-= operator
Definition: bitset.h:237
void fill(parameter_t value)
Definition: array.h:354
bool test(size_t position) const
Definition: bitset.h:744
friend bool operator==(const iterator &lhs, const iterator &rhs)
== operator
Definition: bitset.h:276
void swap(etl::bitset< N > &lhs, etl::bitset< N > &rhs)
swap
Definition: bitset.h:1200
bitset< N > operator>>(size_t shift) const
operator >>
Definition: bitset.h:1010
iterator & operator=(const iterator &other)
= operator
Definition: bitset.h:246
size_t find_next(bool state, size_t position) const
Definition: bitset.h:863
bitset< N > operator^(const bitset< N > &lhs, const bitset< N > &rhs)
Definition: bitset.h:1178
bitset(const bitset< N > &other)
Copy constructor.
Definition: bitset.h:536
const_iterator cbegin()
cbegin
Definition: bitset.h:1087
friend bool operator>=(const iterator &lhs, const iterator &rhs)
>= operator
Definition: bitset.h:316
const_iterator cend()
cend
Definition: bitset.h:1111
bitset< N > & operator|=(const bitset< N > &other)
operator |=
Definition: bitset.h:916
iterator()
Constructor.
Definition: bitset.h:158
bool operator*() const
Definition: bitset.h:409
friend bool operator>(const const_iterator &lhs, const const_iterator &rhs)
operator
Definition: bitset.h:489
friend bool operator>(const iterator &lhs, const iterator &rhs)
operator
Definition: bitset.h:300
bool operator!=(const bitset< N > &lhs, const bitset< N > &rhs)
Definition: bitset.h:1190
iterator & operator+=(int i)
+= operator
Definition: bitset.h:228
size_t find_first(bool state) const
Definition: bitset.h:852
bit_reference operator*()
Definition: bitset.h:212
bitset< N > operator~() const
operator ~
Definition: bitset.h:942
bitset(unsigned long long value)
Construct from a value.
Definition: bitset.h:544
const_iterator & operator+=(int i)
+= operator
Definition: bitset.h:417
bitset< N > & set(const char *text)
Set from a string.
Definition: bitset.h:632
bool operator~() const
Return the logical inverse of the bit.
Definition: bitset.h:116
bitset< N > & operator>>=(size_t shift)
operator >>=
Definition: bitset.h:1035
const_iterator end() const
end
Definition: bitset.h:1103
Definition: algorithm.h:43
Definition: integral_limits.h:54
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
== operator
Definition: bitset.h:465
reference back()
Returns a reference to the last element.
Definition: array.h:189
friend iterator operator-(const iterator &other, int i)
Definition: bitset.h:266
bitset< N > & flip(size_t position)
Flip the bit at the position.
Definition: bitset.h:700
const_iterator & operator-=(int i)
-= operator
Definition: bitset.h:426
bit_reference & operator=(bool b)
Assignment operator.
Definition: bitset.h:89
bitset< N > & set()
Set all of the bits.
Definition: bitset.h:587
bitset()
Default constructor.
Definition: bitset.h:528
iterator & operator++()
++ operator (pre)
Definition: bitset.h:174
const_iterator & operator--()
– operator (pre)
Definition: bitset.h:390
friend bool operator==(const bitset< N > &lhs, const bitset< N > &rhs)
operator ==
Definition: bitset.h:1120
friend bool operator!=(const iterator &lhs, const iterator &rhs)
!= operator
Definition: bitset.h:284
The reference type returned.
Definition: bitset.h:72
size_t size() const
The size of the bitset.
Definition: bitset.h:842
bool any() const
Are any of the bits set?
Definition: bitset.h:796
bitset< N > operator|(const bitset< N > &lhs, const bitset< N > &rhs)
Definition: bitset.h:1166
void swap(bitset< N > &other)
swap
Definition: bitset.h:1063
bitset< N > & flip()
Flip all of the bits.
Definition: bitset.h:685
const_iterator & operator++()
++ operator (pre)
Definition: bitset.h:371
const_iterator & operator=(const const_iterator &other)
= operator
Definition: bitset.h:435
friend bool operator<(const const_iterator &lhs, const const_iterator &rhs)
< operator
Definition: bitset.h:481
bitset< N > & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset.h:598
bitset< N > & reset()
Reset all of the bits.
Definition: bitset.h:649
bit_reference & flip()
Flip the bit.
Definition: bitset.h:107
size_t count() const
Count the number of bits set.
Definition: bitset.h:820
The const_iterator type.
Definition: bitset.h:338
bitset< N > & operator&=(const bitset< N > &other)
operator &=
Definition: bitset.h:903
const_iterator(const typename bitset< N >::iterator &other)
Copy constructor from iterator.
Definition: bitset.h:355
iterator end()
end
Definition: bitset.h:1095
friend bool operator<=(const iterator &lhs, const iterator &rhs)
<= operator
Definition: bitset.h:308
friend bool operator<=(const const_iterator &lhs, const const_iterator &rhs)
<= operator
Definition: bitset.h:497
const_iterator(const const_iterator &other)
Copy constructor.
Definition: bitset.h:363
The iterator type.
Definition: bitset.h:148
bitset(const char *text)
Construct from a string.
Definition: bitset.h:572
iterator & operator--()
– operator (pre)
Definition: bitset.h:193
void swap(array &other)
Definition: array.h:363
bool operator[](size_t position) const
Read [] operator.
Definition: bitset.h:727
bitset< N > & reset(size_t position)
Reset the bit at the position.
Definition: bitset.h:658
friend iterator operator+(const iterator &other, int i)
Definition: bitset.h:256
friend bool operator<(const iterator &lhs, const iterator &rhs)
< operator
Definition: bitset.h:292
friend const_iterator operator-(const const_iterator &other, int i)
Definition: bitset.h:455
iterator(const iterator &other)
Copy constructor.
Definition: bitset.h:166