Class bitset

Synopsis

#include <include/EASTL/bitset.h>

template <size_t N, typename WordType = EASTL_BITSET_WORD_TYPE_DEFAULT>
class bitset : private BitsetBase<BITSET_WORD_COUNT(N, WordType), WordType>

Description

bitset

Implements a bitset much like the C++ std::bitset.

As of this writing we don't implement a specialization of bitset<0>, as it is deemed an academic exercise that nobody would actually use and it would increase code space and provide little practical benefit. Note that this doesn't mean bitset<0> isn't supported; it means that our version of it isn't as efficient as it would be if a specialization was made for it.

  • N can be any unsigned (non-zero) value, though memory usage is linear with respect to N, so large values of N use large amounts of memory.
  • WordType must be one of [uint16_t, uint32_t, uint64_t, uint128_t] and the compiler must support the type. By default the WordType is the largest native register type that the target platform supports.

Mentioned in

Inheritance

Ancestors: BitsetBase

Classes

referencereference

Methods

bitset overloadbitset
alltemplate <size_t N, typename WordType> inline bool bitset<N, WordType>::any() const { return base_type::any(); }
any
count
data overload
DoFindFirst
DoFindLast
DoFindNext
DoFindPrev
DoGetWord overload
find_firstFinds the index of the first "on" bit, returns kSize if none are set.
find_lastFinds the index of the last "on" bit, returns kSize if none are set.
find_nextFinds the index of the next "on" bit after last_find, returns kSize if none are set.
find_prevFinds the index of the last "on" bit before last_find, returns kSize if none are set.
flip overload
from_uint32
from_uint64
none
operator!=
operator&=We don't define copy constructor and operator= because the compiler-generated versions will suffice.
operator<<
operator<<=
operator==
operator>>
operator>>=
operator[] overload
operator^=
operator|=
operator~
reset overload
set overload
sizetemplate <size_t N, typename WordType> inline unsigned long bitset<N, WordType>::to_ulong() const { return base_type::to_ulong(); } template <size_t N, typename WordType> inline uint32_t bitset<N, WordType>::to_uint32() const { return base_type::to_uint32(); } template <size_t N, typename WordType> inline uint64_t bitset<N, WordType>::to_uint64() const { return base_type::to_uint64(); } template <size_t N, typename WordType> inline typename bitset<N, WordType>::size_type bitset<N, WordType>::count() const { return base_type::count(); }
test
to_uint32
to_uint64
to_ulong

Source

Lines 302-430 in include/EASTL/bitset.h.

template <size_t N, typename WordType = EASTL_BITSET_WORD_TYPE_DEFAULT>
class bitset : private BitsetBase<BITSET_WORD_COUNT(N, WordType), WordType>
{
public:
    typedef BitsetBase<BITSET_WORD_COUNT(N, WordType), WordType>  base_type;
    typedef bitset<N, WordType>                                   this_type;
    typedef WordType                                              word_type;
    typedef typename base_type::size_type                         size_type;
    enum
    {
        kBitsPerWord      = (8 * sizeof(word_type)),
        kBitsPerWordMask  = (kBitsPerWord - 1),
        kBitsPerWordShift = ((kBitsPerWord == 8) ? 3 : ((kBitsPerWord == 16) ? 4 : ((kBitsPerWord == 32) ? 5 : (((kBitsPerWord == 64) ? 6 : 7))))),
        kSize             = N,                               // The number of bits the bitset holds
        kWordSize         = sizeof(word_type),               // The size of individual words the bitset uses to hold the bits.
        kWordCount        = BITSET_WORD_COUNT(N, WordType)   // The number of words the bitset uses to hold the bits. sizeof(bitset<N, WordType>) == kWordSize * kWordCount.
    };
    using base_type::mWord;
    using base_type::DoGetWord;
    using base_type::DoFindFirst;
    using base_type::DoFindNext;
    using base_type::DoFindLast;
    using base_type::DoFindPrev;
    using base_type::to_ulong;
    using base_type::to_uint32;
    using base_type::to_uint64;
    using base_type::count;
    using base_type::any;
public:
    /// reference
    ///
    /// A reference is a reference to a specific bit in the bitset.
    /// The C++ standard specifies that this be a nested class, 
    /// though it is not clear if a non-nested reference implementation
    /// would be non-conforming.
    ///
    class reference
    {
    protected:
        friend class bitset<N, WordType>;
        word_type* mpBitWord;
        size_type  mnBitIndex;

        reference(){} // The C++ standard specifies that this is private.

    public:
        reference(const bitset& x, size_type i);
        reference& operator=(bool value);
        reference& operator=(const reference& x);
        bool operator~() const;
        operator bool() const // Defined inline because CodeWarrior fails to be able to compile it outside.
           { return (*mpBitWord & (static_cast<word_type>(1) << (mnBitIndex & kBitsPerWordMask))) != 0; }
        reference& flip();
    };
public:
    friend class reference;
    bitset();
    bitset(uint32_t value);
  //bitset(uint64_t value); // Disabled because it causes conflicts with the 32 bit version with existing user code. Use from_uint64 instead.
    // We don't define copy constructor and operator= because 
    // the compiler-generated versions will suffice.
    this_type& operator&=(const this_type& x);
    this_type& operator|=(const this_type& x);
    this_type& operator^=(const this_type& x);
    this_type& operator<<=(size_type n);
    this_type& operator>>=(size_type n);
    this_type& set();
    this_type& set(size_type i, bool value = true);
    this_type& reset();
    this_type& reset(size_type i);

    this_type& flip();
    this_type& flip(size_type i);
    this_type  operator~() const;
    reference operator[](size_type i);
    bool      operator[](size_type i) const;
    const word_type* data() const;
    word_type*       data();
    void          from_uint32(uint32_t value);
    void          from_uint64(uint64_t value);
  //unsigned long to_ulong()  const;    // We inherit this from the base class.
  //uint32_t      to_uint32() const;
  //uint64_t      to_uint64() const;
  //size_type count() const;            // We inherit this from the base class.
    size_type size() const;
    bool operator==(const this_type& x) const;
    bool operator!=(const this_type& x) const;
    bool test(size_type i) const;
  //bool any() const;                   // We inherit this from the base class.
    bool all() const;
    bool none() const;
    this_type operator<<(size_type n) const;
    this_type operator>>(size_type n) const;
    // Finds the index of the first "on" bit, returns kSize if none are set.
    size_type find_first() const;
    // Finds the index of the next "on" bit after last_find, returns kSize if none are set.
    size_type find_next(size_type last_find) const;
    // Finds the index of the last "on" bit, returns kSize if none are set.
    size_type find_last() const;
    // Finds the index of the last "on" bit before last_find, returns kSize if none are set.
    size_type find_prev(size_type last_find) const;
}; // bitset





Add Discussion as Guest

Log in