Loading...
Searching...
No Matches
bitset_legacy.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BITSET_LEGACY_INCLUDED
#define ETL_BITSET_LEGACY_INCLUDED
#include "../platform.h"
#include "../algorithm.h"
#include "../binary.h"
#include "../char_traits.h"
#include "../error_handler.h"
#include "../exception.h"
#include "../integral_limits.h"
#include "../iterator.h"
#include "../log.h"
#include "../nullptr.h"
#include "../span.h"
#include "../static_assert.h"
#include "../string.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "minmax_push.h"
#if defined(ETL_COMPILER_KEIL)
#pragma diag_suppress 1300
#endif
#if ETL_USING_CPP11
#define ETL_STR(x) x
#define ETL_STRL(x) L##x
#define ETL_STRu(x) u##x
#define ETL_STRU(x) U##x
#else
#define ETL_STR(x) x
#define ETL_STRL(x) x
#define ETL_STRu(x) x
#define ETL_STRU(x) x
#endif
//*****************************************************************************
//*****************************************************************************
namespace etl
{
//***************************************************************************
//***************************************************************************
class bitset_exception : public etl::exception
{
public:
bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class bitset_nullptr : public bitset_exception
{
public:
bitset_nullptr(string_type file_name_, numeric_type line_number_)
: bitset_exception(ETL_ERROR_TEXT("bitset:null pointer", ETL_BITSET_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class bitset_type_too_small : public bitset_exception
{
public:
bitset_type_too_small(string_type file_name_, numeric_type line_number_)
: bitset_exception(ETL_ERROR_TEXT("bitset:type_too_small", ETL_BITSET_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class bitset_overflow : public bitset_exception
{
public:
bitset_overflow(string_type file_name_, numeric_type line_number_)
: bitset_exception(ETL_ERROR_TEXT("bitset:overflow", ETL_BITSET_FILE_ID"C"), file_name_, line_number_)
{
}
};
//*************************************************************************
//*************************************************************************
class ibitset
{
protected:
// The type used for each element in the array.
#if !defined(ETL_BITSET_ELEMENT_TYPE)
#define ETL_BITSET_ELEMENT_TYPE uint_least8_t
#endif
public:
typedef size_t size_type;
typedef typename etl::make_unsigned<ETL_BITSET_ELEMENT_TYPE>::type element_type;
typedef element_type element_t; // Backward compatibility
static ETL_CONSTANT element_type ALL_SET = etl::integral_limits<element_type>::max;
static ETL_CONSTANT element_type ALL_CLEAR = 0;
static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
#if ETL_USING_CPP11
typedef etl::span<element_type> span_type;
typedef etl::span<const element_type> const_span_type;
#endif
enum
{
npos = etl::integral_limits<size_t>::max
};
//*************************************************************************
//*************************************************************************
class bit_reference
{
public:
friend class ibitset;
//*******************************
//*******************************
operator bool() const
{
return p_bitset->test(position);
}
//*******************************
//*******************************
bit_reference(const bit_reference& other)
: p_bitset(other.p_bitset)
, position(other.position)
{
}
//*******************************
//*******************************
bit_reference& operator=(bool b)
{
p_bitset->set(position, b);
return *this;
}
//*******************************
//*******************************
bit_reference& operator=(const bit_reference& r)
{
p_bitset->set(position, bool(r));
return *this;
}
//*******************************
//*******************************
bit_reference& flip()
{
p_bitset->flip(position);
return *this;
}
//*******************************
//*******************************
bool operator~() const
{
return !p_bitset->test(position);
}
private:
//*******************************
//*******************************
bit_reference()
: p_bitset(ETL_NULLPTR)
, position(0)
{
}
//*******************************
//*******************************
bit_reference(ibitset& r_bitset, size_t position_)
: p_bitset(&r_bitset)
, position(position_)
{
}
ibitset* p_bitset;
size_t position;
};
//*************************************************************************
//*************************************************************************
size_t size() const
{
return Active_Bits;
}
//*************************************************************************
//*************************************************************************
size_t count() const
{
size_t n = 0UL;
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
n += etl::count_bits(pdata[i]);
}
return n;
}
//*************************************************************************
//*************************************************************************
bool test(size_t position) const
{
ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
size_t index;
element_type mask;
if (position >= Active_Bits)
{
return false;
}
if (Number_Of_Elements == 0)
{
return false;
}
else if (Number_Of_Elements == 1)
{
index = 0;
mask = element_type(1) << position;
}
else
{
index = position >> etl::log2<Bits_Per_Element>::value;
mask = element_type(1) << (position & (Bits_Per_Element - 1));
}
return (pdata[index] & mask) != 0;
}
//*************************************************************************
//*************************************************************************
ibitset& set()
{
etl::fill_n(pdata, Number_Of_Elements - 1U, ALL_SET);
pdata[Number_Of_Elements - 1U] = Top_Mask;
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& set(size_t position, bool value = true)
{
ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
size_t index;
element_type bit;
if (position < Active_Bits)
{
if (Number_Of_Elements == 0)
{
return *this;
}
else if (Number_Of_Elements == 1)
{
index = 0;
bit = element_type(1) << position;
}
else
{
index = position >> etl::log2<Bits_Per_Element>::value;
bit = element_type(1) << (position & (Bits_Per_Element - 1));
}
if (value)
{
pdata[index] |= bit;
}
else
{
pdata[index] &= ~bit;
}
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& from_string(const char* text)
{
reset();
size_t i = etl::min(Active_Bits, etl::strlen(text));
while (i > 0)
{
set(--i, *text++ == ETL_STRL('1'));
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& from_string(const wchar_t* text)
{
reset();
size_t i = etl::min(Active_Bits, etl::strlen(text));
while (i > 0)
{
set(--i, *text++ == ETL_STRL('1'));
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& from_string(const char16_t* text)
{
reset();
size_t i = etl::min(Active_Bits, etl::strlen(text));
while (i > 0)
{
set(--i, *text++ == ETL_STRu('1'));
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& from_string(const char32_t* text)
{
reset();
size_t i = etl::min(Active_Bits, etl::strlen(text));
while (i > 0)
{
set(--i, *text++ == ETL_STRU('1'));
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& set(const char* text)
{
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& set(const wchar_t* text)
{
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& set(const char16_t* text)
{
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& set(const char32_t* text)
{
if (text == ETL_NULLPTR)
{
reset();
}
else
{
from_string(text);
}
return *this;
}
//*************************************************************************
//*************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, T>::type value() const
{
T v = T(0);
const bool OK = (sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
ETL_ASSERT_OR_RETURN_VALUE(OK, ETL_ERROR(etl::bitset_type_too_small), T(0));
if (OK)
{
uint_least8_t shift = 0U;
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
v |= T(typename etl::make_unsigned<T>::type(pdata[i]) << shift);
shift += uint_least8_t(Bits_Per_Element);
}
}
return v;
}
//*************************************************************************
//*************************************************************************
unsigned long to_ulong() const
{
return value<unsigned long>();
}
//*************************************************************************
//*************************************************************************
unsigned long long to_ullong() const
{
return value<unsigned long long>();
}
//*************************************************************************
//*************************************************************************
ibitset& reset()
{
etl::fill_n(pdata, Number_Of_Elements, ALL_CLEAR);
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& reset(size_t position)
{
ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
size_t index;
element_type bit;
if (position < Active_Bits)
{
if (Number_Of_Elements == 0)
{
return *this;
}
else if (Number_Of_Elements == 1)
{
index = 0;
bit = element_type(1) << position;
}
else
{
index = position >> etl::log2<Bits_Per_Element>::value;
bit = element_type(1) << (position & (Bits_Per_Element - 1));
}
pdata[index] &= ~bit;
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& flip()
{
etl::transform_n(pdata, Number_Of_Elements, pdata, etl::binary_not<element_type>());
clear_unused_bits_in_msb();
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& flip(size_t position)
{
ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
size_t index;
element_type bit;
if (Number_Of_Elements == 0)
{
return *this;
}
else if (Number_Of_Elements == 1)
{
index = 0;
bit = element_type(1) << position;
}
else
{
index = position >> log2<Bits_Per_Element>::value;
bit = element_type(1) << (position & (Bits_Per_Element - 1));
}
pdata[index] ^= bit;
return *this;
}
//*************************************************************************
// Are all the bits sets?
//*************************************************************************
bool all() const
{
if (Number_Of_Elements == 0UL)
{
return true;
}
// All but the last.
for (size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
{
if (pdata[i] != ALL_SET)
{
return false;
}
}
// The last.
if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
{
return false;
}
return true;
}
//*************************************************************************
//*************************************************************************
bool any() const
{
return !none();
}
//*************************************************************************
//*************************************************************************
bool none() const
{
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
if (pdata[i] != 0)
{
return false;
}
}
return true;
}
//*************************************************************************
//*************************************************************************
size_t find_first(bool state) const
{
return find_next(state, 0);
}
//*************************************************************************
//*************************************************************************
size_t find_next(bool state, size_t position) const
{
// Where to start.
size_t index;
size_t bit;
if (Number_Of_Elements == 0)
{
return ibitset::npos;
}
else if (Number_Of_Elements == 1)
{
index = 0;
bit = position;
}
else
{
index = position >> log2<Bits_Per_Element>::value;
bit = position & (Bits_Per_Element - 1);
}
element_type mask = 1 << bit;
// For each element in the bitset...
while (index < Number_Of_Elements)
{
element_type value = pdata[index];
// Needs checking?
if ((state && (value != ALL_CLEAR)) || (!state && (value != ALL_SET)))
{
// For each bit in the element...
while ((bit < Bits_Per_Element) && (position < Active_Bits))
{
// Equal to the required state?
if (((value & mask) != 0) == state)
{
return position;
}
// Move on to the next bit.
mask <<= 1;
++position;
++bit;
}
}
else
{
position += (Bits_Per_Element - bit);
}
// Start at the beginning for all other elements.
bit = 0;
mask = 1;
++index;
}
return ibitset::npos;
}
//*************************************************************************
//*************************************************************************
bool operator[](size_t position) const
{
return test(position);
}
//*************************************************************************
//*************************************************************************
bit_reference operator[](size_t position)
{
return bit_reference(*this, position);
}
//*************************************************************************
//*************************************************************************
ibitset& operator&=(const ibitset& other)
{
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
pdata[i] &= other.pdata[i];
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& operator|=(const ibitset& other)
{
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
pdata[i] |= other.pdata[i];
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& operator^=(const ibitset& other)
{
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
pdata[i] ^= other.pdata[i];
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& operator<<=(size_t shift)
{
if (shift >= Active_Bits)
{
reset();
}
else if (Number_Of_Elements != 0UL)
{
// Just one element?
if (Number_Of_Elements == 1UL)
{
pdata[0] <<= shift;
}
else if (shift == Bits_Per_Element)
{
etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
pdata[0] = 0;
}
else
{
// The place where the elements are split when shifting.
const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
// Where we are shifting from.
int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
// Where we are shifting to.
int dst_index = int(Number_Of_Elements - 1U);
// Shift control constants.
const size_t lsb_shift = Bits_Per_Element - split_position;
const size_t msb_shift = split_position;
const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
const element_type msb_mask = etl::integral_limits<element_type>::max - lsb_mask;
const element_type lsb_shifted_mask = element_type(lsb_mask << lsb_shift);
// First lsb.
element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
pdata[dst_index] = lsb;
--src_index;
// Now do the shifting.
while (src_index >= 0)
{
// Shift msb.
element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
pdata[dst_index] = pdata[dst_index] | msb;
--dst_index;
// Shift lsb.
lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
pdata[dst_index] = lsb;
--src_index;
}
// Clear the remaining bits.
// First lsb.
pdata[dst_index] &= lsb_shifted_mask;
--dst_index;
// The other remaining elements.
for (int i = 0; i <= dst_index; ++i)
{
pdata[i] = 0;
}
}
// Truncate any bits shifted to the left.
clear_unused_bits_in_msb();
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& operator>>=(size_t shift)
{
if (shift >= Active_Bits)
{
reset();
}
else if (Number_Of_Elements != 0UL)
{
// Just one element?
if (Number_Of_Elements == 1UL)
{
pdata[0] >>= shift;
}
// Shift is the size of an element?
else if (shift == Bits_Per_Element)
{
etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
pdata[Number_Of_Elements - 1U] = 0;
}
else
{
// The place where the elements are split when shifting.
const size_t split_position = shift % Bits_Per_Element;
// Where we are shifting from.
int src_index = int(shift / Bits_Per_Element);
// Where we are shifting to.
int dst_index = 0;
// Shift control constants.
const size_t lsb_shift = Bits_Per_Element - split_position;
const size_t msb_shift = split_position;
const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
const element_type msb_mask = etl::integral_limits<element_type>::max - lsb_mask;
const element_type msb_shifted_mask = element_type(msb_mask >> msb_shift);
// Now do the shifting.
while (src_index < int(Number_Of_Elements - 1))
{
// Shift msb.
element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
++src_index;
// Shift lsb.
element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
// Combine them.
pdata[dst_index] = lsb | msb;
++dst_index;
}
// Final msb.
element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
pdata[dst_index] = msb;
// Clear the remaining bits.
// First msb.
pdata[dst_index] &= msb_shifted_mask;
++dst_index;
// The other remaining elements.
for (int i = dst_index; i < int(Number_Of_Elements); ++i)
{
pdata[i] = 0;
}
}
}
return *this;
}
//*************************************************************************
//*************************************************************************
ibitset& operator=(const ibitset& other)
{
if (this != &other)
{
etl::copy_n(other.pdata, Number_Of_Elements, pdata);
}
return *this;
}
//*************************************************************************
//*************************************************************************
void swap(ibitset& other)
{
etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
span_type span()
{
return span_type(pdata, pdata + Number_Of_Elements);
}
//*************************************************************************
//*************************************************************************
const_span_type span() const
{
return const_span_type(pdata, pdata + Number_Of_Elements);
}
#endif
protected:
//*************************************************************************
//*************************************************************************
ibitset& initialise(unsigned long long value)
{
reset();
const size_t Shift = (integral_limits<unsigned long long>::bits <= (int)Bits_Per_Element) ? 0 : Bits_Per_Element;
// Can we do it in one hit?
if (Shift == 0)
{
pdata[0] = element_type(value);
}
else
{
size_t i = 0UL;
while ((value != 0) && (i < Number_Of_Elements))
{
pdata[i++] = value & ALL_SET;
value = value >> Shift;
}
}
clear_unused_bits_in_msb();
return *this;
}
//*************************************************************************
//*************************************************************************
void invert()
{
for (size_t i = 0UL; i < Number_Of_Elements; ++i)
{
pdata[i] = ~pdata[i];
}
clear_unused_bits_in_msb();
}
//*************************************************************************
//*************************************************************************
bit_reference get_bit_reference(size_t position)
{
return bit_reference(*this, position);
}
//*************************************************************************
//*************************************************************************
ibitset(size_t nbits_, size_t size_, element_type* pdata_)
: Active_Bits(nbits_)
, Number_Of_Elements(size_)
, pdata(pdata_)
{
const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
}
//*************************************************************************
//*************************************************************************
static bool is_equal(const ibitset& lhs, const ibitset& rhs)
{
return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
}
element_type Top_Mask;
private:
//*************************************************************************
//*************************************************************************
void clear_unused_bits_in_msb()
{
pdata[Number_Of_Elements - 1U] &= Top_Mask;
}
// Disable copy construction.
ibitset(const ibitset&);
const size_t Active_Bits;
const size_t Number_Of_Elements;
element_type* pdata;
//*************************************************************************
//*************************************************************************
#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
public:
virtual ~ibitset() {}
#else
protected:
~ibitset() {}
#endif
};
ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
ETL_CONSTANT size_t ibitset::Bits_Per_Element;
//*************************************************************************
//*************************************************************************
template <size_t MaxN>
class bitset : public etl::ibitset
{
static ETL_CONSTANT size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
public:
static ETL_CONSTANT size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
static ETL_CONSTANT size_t Allocated_Bits = ALLOCATED_BITS;
public:
//*************************************************************************
//*************************************************************************
bitset()
: etl::ibitset(MaxN, Array_Size, data)
{
reset();
}
//*************************************************************************
//*************************************************************************
bitset(const bitset<MaxN>& other)
: etl::ibitset(MaxN, Array_Size, data)
{
etl::copy_n(other.data, Array_Size, data);
}
//*************************************************************************
//*************************************************************************
bitset(unsigned long long value)
: etl::ibitset(MaxN, Array_Size, data)
{
initialise(value);
}
//*************************************************************************
//*************************************************************************
bitset(const char* text)
: etl::ibitset(MaxN, Array_Size, data)
{
set(text);
}
//*************************************************************************
//*************************************************************************
bitset(const wchar_t* text)
: etl::ibitset(MaxN, Array_Size, data)
{
set(text);
}
//*************************************************************************
//*************************************************************************
bitset(const char16_t* text)
: etl::ibitset(MaxN, Array_Size, data)
{
set(text);
}
//*************************************************************************
//*************************************************************************
bitset(const char32_t* text)
: etl::ibitset(MaxN, Array_Size, data)
{
set(text);
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set()
{
etl::ibitset::set();
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set(size_t position, bool value = true)
{
etl::ibitset::set(position, value);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set(const char* text)
{
etl::ibitset::set(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set(const wchar_t* text)
{
etl::ibitset::set(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set(const char16_t* text)
{
etl::ibitset::set(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& set(const char32_t* text)
{
etl::ibitset::set(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& from_string(const char* text)
{
ibitset::from_string(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& from_string(const wchar_t* text)
{
ibitset::from_string(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& from_string(const char16_t* text)
{
ibitset::from_string(text);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& from_string(const char32_t* text)
{
ibitset::from_string(text);
return *this;
}
//*************************************************************************
//*************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value, T>::type value() const
{
ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element), "Type too small");
return ibitset::value<T>();
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& reset()
{
ibitset::reset();
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& reset(size_t position)
{
etl::ibitset::reset(position);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& flip()
{
ibitset::flip();
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& flip(size_t position)
{
etl::ibitset::flip(position);
return *this;
}
//*************************************************************************
//*************************************************************************
#if ETL_USING_CPP11
template <typename TString = etl::string<MaxN>>
#else
template <typename TString>
#endif
TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
typename TString::value_type one = typename TString::value_type('1')) const
{
TString result;
result.resize(MaxN, '\0');
ETL_ASSERT_OR_RETURN_VALUE((result.size() == MaxN), ETL_ERROR(etl::bitset_overflow), result);
for (size_t i = MaxN; i > 0; --i)
{
result[MaxN - i] = test(i - 1) ? one : zero;
}
return result;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator=(const bitset<MaxN>& other)
{
if (this != &other)
{
etl::copy_n(other.data, Array_Size, data);
}
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator&=(const bitset<MaxN>& other)
{
etl::ibitset::operator&=(other);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator|=(const bitset<MaxN>& other)
{
etl::ibitset::operator|=(other);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator^=(const bitset<MaxN>& other)
{
ibitset::operator^=(other);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN> operator~() const
{
etl::bitset<MaxN> temp(*this);
temp.invert();
return temp;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN> operator<<(size_t shift) const
{
etl::bitset<MaxN> temp(*this);
temp <<= shift;
return temp;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator<<=(size_t shift)
{
etl::ibitset::operator<<=(shift);
return *this;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN> operator>>(size_t shift) const
{
bitset<MaxN> temp(*this);
temp >>= shift;
return temp;
}
//*************************************************************************
//*************************************************************************
bitset<MaxN>& operator>>=(size_t shift)
{
etl::ibitset::operator>>=(shift);
return *this;
}
//*************************************************************************
//*************************************************************************
friend bool operator==(const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
{
return etl::ibitset::is_equal(lhs, rhs);
}
private:
element_type data[Array_Size > 0U ? Array_Size : 1U];
};
template <size_t MaxN>
ETL_CONSTANT size_t bitset<MaxN>::ALLOCATED_BITS;
template <size_t MaxN>
ETL_CONSTANT size_t bitset<MaxN>::Allocated_Bits;
//***************************************************************************
//***************************************************************************
template <size_t MaxN>
bitset<MaxN> operator&(const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
{
bitset<MaxN> temp(lhs);
temp &= rhs;
return temp;
}
//***************************************************************************
//***************************************************************************
template <size_t MaxN>
bitset<MaxN> operator|(const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
{
bitset<MaxN> temp(lhs);
temp |= rhs;
return temp;
}
//***************************************************************************
//***************************************************************************
template <size_t MaxN>
bitset<MaxN> operator^(const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
{
bitset<MaxN> temp(lhs);
temp ^= rhs;
return temp;
}
//***************************************************************************
//***************************************************************************
template <size_t MaxN>
bool operator!=(const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
{
return !(lhs == rhs);
}
//*************************************************************************
//*************************************************************************
template <size_t MaxN>
void swap(etl::bitset<MaxN>& lhs, etl::bitset<MaxN>& rhs)
{
lhs.swap(rhs);
}
} // namespace etl
#include "minmax_pop.h"
#endif
algorithm.h
binary.h
char_traits.h
etl::ibitset::bit_reference
The reference type returned.
Definition bitset_legacy.h:173
etl::ibitset::bit_reference::operator=
bit_reference & operator=(const bit_reference &r)
Assignment operator.
Definition bitset_legacy.h:207
etl::ibitset::bit_reference::operator~
bool operator~() const
Return the logical inverse of the bit.
Definition bitset_legacy.h:225
etl::ibitset::bit_reference::bit_reference
bit_reference(const bit_reference &other)
Copy constructor.
Definition bitset_legacy.h:189
etl::ibitset::bit_reference::operator=
bit_reference & operator=(bool b)
Assignment operator.
Definition bitset_legacy.h:198
etl::ibitset::bit_reference::flip
bit_reference & flip()
Flip the bit.
Definition bitset_legacy.h:216
etl::lsb_mask
Definition binary.h:2196
etl::msb_mask
Definition binary.h:2228
etl::span
Span - Fixed Extent.
Definition span.h:208
error_handler.h
exception.h
etl::transform_n
ETL_CONSTEXPR14 void transform_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function)
Definition algorithm.h:2999
etl::count_bits
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::binary_not
Definition binary.h:2263
etl::bit
Definition binary.h:356
etl::ibitset::value
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:500
etl::ibitset::set
ibitset & set()
Set all bits.
Definition bitset_legacy.h:313
etl::bitset::set
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:1209
etl::ibitset::reset
ibitset & reset()
Resets the bitset.
Definition bitset_legacy.h:541
etl::bitset::operator&=
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition bitset_legacy.h:1384
etl::bitset::bitset
bitset(const char16_t *text)
Construct from a string.
Definition bitset_legacy.h:1182
etl::ibitset::find_first
size_t find_first(bool state) const
Definition bitset_legacy.h:679
etl::ibitset::find_next
size_t find_next(bool state, size_t position) const
Definition bitset_legacy.h:690
etl::ibitset::initialise
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition bitset_legacy.h:1006
etl::ibitset::from_string
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:397
etl::ibitset::any
bool any() const
Are any of the bits set?
Definition bitset_legacy.h:652
etl::ibitset::~ibitset
~ibitset()
Destructor.
Definition bitset_legacy.h:1106
etl::bitset::operator==
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition bitset_legacy.h:1465
etl::bitset::flip
bitset< MaxN > & flip()
Flip all of the bits.
Definition bitset_legacy.h:1328
etl::ibitset::get_bit_reference
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition bitset_legacy.h:1049
etl::ibitset::operator|=
ibitset & operator|=(const ibitset &other)
operator |=
Definition bitset_legacy.h:783
etl::bitset::bitset
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition bitset_legacy.h:1146
etl::ibitset::ibitset
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition bitset_legacy.h:1057
etl::bitset::set
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition bitset_legacy.h:1248
etl::bitset::set
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition bitset_legacy.h:1228
etl::bitset::bitset
bitset(const char *text)
Construct from a string.
Definition bitset_legacy.h:1164
etl::ibitset::swap
void swap(ibitset &other)
swap
Definition bitset_legacy.h:976
etl::ibitset::operator=
ibitset & operator=(const ibitset &other)
operator =
Definition bitset_legacy.h:963
etl::bitset::reset
bitset< MaxN > & reset()
Reset all of the bits.
Definition bitset_legacy.h:1310
etl::bitset::bitset
bitset(const wchar_t *text)
Construct from a string.
Definition bitset_legacy.h:1173
etl::ibitset::operator[]
bit_reference operator[](size_t position)
Write [] operator.
Definition bitset_legacy.h:762
etl::ibitset::operator>>=
ibitset & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:887
etl::ibitset::operator^=
ibitset & operator^=(const ibitset &other)
operator ^=
Definition bitset_legacy.h:796
etl::bitset::value
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:1299
etl::bitset::bitset
bitset(unsigned long long value)
Construct from a value.
Definition bitset_legacy.h:1155
etl::bitset::swap
ETL_CONSTEXPR14 void swap(etl::bitset< Active_Bits, TElement > &other) ETL_NOEXCEPT
swap
Definition bitset_new.h:2474
etl::bitset::reset
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:1319
etl::ibitset::count
size_t count() const
Count the number of bits set.
Definition bitset_legacy.h:265
etl::ibitset::from_string
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:380
etl::bitset::to_string
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
etl::ibitset::set
ibitset & set(const char32_t *text)
Set from a u32string.
Definition bitset_legacy.h:482
etl::ibitset::from_string
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:414
etl::ibitset::set
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:324
etl::ibitset::flip
ibitset & flip()
Flip all of the bits.
Definition bitset_legacy.h:583
etl::bitset::bitset
bitset(const char32_t *text)
Construct from a string.
Definition bitset_legacy.h:1191
etl::ibitset::operator&=
ibitset & operator&=(const ibitset &other)
operator &=
Definition bitset_legacy.h:770
etl::bitset::from_string
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:1268
etl::bitset::operator<<=
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:1435
etl::bitset::operator<<
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition bitset_legacy.h:1423
etl::ibitset::to_ulong
unsigned long to_ulong() const
Put to a unsigned long.
Definition bitset_legacy.h:525
etl::ibitset::operator[]
bool operator[](size_t position) const
Read [] operator.
Definition bitset_legacy.h:754
etl::ibitset::to_ullong
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition bitset_legacy.h:533
etl::bitset::operator|=
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition bitset_legacy.h:1393
etl::ibitset::invert
void invert()
Invert.
Definition bitset_legacy.h:1036
etl::bitset::bitset
bitset()
Default constructor.
Definition bitset_legacy.h:1137
etl::bitset::operator~
bitset< MaxN > operator~() const
operator ~
Definition bitset_legacy.h:1411
etl::bitset::operator>>
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition bitset_legacy.h:1444
etl::bitset::from_string
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:1278
etl::ibitset::reset
ibitset & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:551
etl::bitset::set
bitset< MaxN > & set(const char *text)
Set from a string.
Definition bitset_legacy.h:1218
etl::ibitset::from_string
ibitset & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:363
etl::bitset::operator=
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition bitset_legacy.h:1371
etl::ibitset::set
ibitset & set(const char16_t *text)
Set from a u16string.
Definition bitset_legacy.h:465
etl::ibitset::set
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition bitset_legacy.h:448
etl::ibitset::operator<<=
ibitset & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:809
etl::bitset::operator^=
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition bitset_legacy.h:1402
etl::bitset::test
ETL_CONSTEXPR14 bool test() const
Definition bitset_new.h:2111
etl::ibitset::none
bool none() const
Are none of the bits set?
Definition bitset_legacy.h:660
etl::bitset::from_string
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:1288
etl::ibitset::test
bool test(size_t position) const
Definition bitset_legacy.h:282
etl::ibitset::size
size_t size() const
The number of bits in the bitset.
Definition bitset_legacy.h:257
etl::bitset::set
bitset< MaxN > & set(const char16_t *text)
Set from a string.
Definition bitset_legacy.h:1238
etl::ibitset::flip
ibitset & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:595
etl::bitset::flip
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:1337
etl::ibitset::set
ibitset & set(const char *text)
Set from a string.
Definition bitset_legacy.h:431
etl::bitset::set
bitset< MaxN > & set()
Set all of the bits.
Definition bitset_legacy.h:1200
etl::bitset::operator>>=
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:1456
etl::ibitset::is_equal
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition bitset_legacy.h:1070
etl::bitset::from_string
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:1258
etl::bitset
Bitset forward declaration.
Definition bitset_legacy.h:1124
etl::bitset_overflow
Definition bitset_legacy.h:125
etl::bitset_type_too_small
Definition bitset_legacy.h:111
etl::ibitset
Definition bitset_legacy.h:139
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::exception
Definition exception.h:59
etl::integral_limits
Definition integral_limits.h:517
integral_limits.h
iterator.h
log.h
minmax_pop.h
minmax_push.h
etl
Definition absolute.h:40
etl::swap
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
etl::operator|
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition byte.h:250
etl::operator&
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:258
etl::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
etl::operator^
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:266
etl::strlen
ETL_CONSTEXPR14 size_t strlen(const T *t) ETL_NOEXCEPT
Alternative strlen for all character types.
Definition char_traits.h:293
nullptr.h
platform.h
span.h
string.h
etl::type