Loading...
Searching...
No Matches
TfBits Class Reference

Fast bit array that keeps track of the number of bits set and can find the next set in a timely manner. More...

#include <bits.h>

Classes

struct  FastHash
 A hash functor for TfBits that is faster than Hash. More...
 
struct  Hash
 Hash for TfBits. More...
 
class  View
 Iterator support. More...
 

Public Types

enum  Mode { All , AllSet , AllUnset }
 
using AllView = View< All >
 
using AllSetView = View< AllSet >
 
using AllUnsetView = View< AllUnset >
 

Public Member Functions

 TfBits (size_t num=0)
 Constructs a fixed size bit array, clears all bits.
 
 TfBits (size_t num, size_t first, size_t last)
 Constructs a fixed size bit array, with a range of bits set.
 
 TfBits (const TfBits &rhs)
 Copy-constructs a fixed size bit array.
 
 TfBits (TfBits &&rhs)
 Move constructor.
 
 ~TfBits ()
 Destructor.
 
TfBitsoperator= (const TfBits &rhs)
 Assignment operator.
 
TfBitsoperator= (TfBits &&rhs)
 Move assignment operator.
 
void Resize (size_t num)
 Resizes the bit array, however, the bits are left uninitialized.
 
void ResizeKeepContent (size_t num)
 Resizes the size of the bit array while keeping the content.
 
TF_API void OrSubset (const TfBits &rhs)
 Combines two differently sized TfBits using an or operator.
 
void Swap (TfBits &rhs)
 Provides a fast swap.
 
void ClearAll ()
 Clears all bits to zero.
 
void SetAll ()
 Sets all bits to one.
 
void Clear (size_t index)
 Clears bit # index to zero.
 
void Set (size_t index)
 Sets bit # index to one.
 
void Assign (size_t index, bool val)
 Assigns val to bit # index.
 
bool IsSet (size_t index) const
 Returns true, if bit # index is set.
 
size_t FindNextSet (size_t index) const
 Finds the next set bit that has a higher or equal index than index.
 
size_t FindPrevSet (size_t index) const
 Finds the prev set bit that has a lower or equal index than index.
 
size_t FindNextUnset (size_t index) const
 Finds the next unset bit that has a higher or equal index than index.
 
size_t GetSize () const
 Returns the size of the bit array, ie.
 
bool IsEmpty () const
 Returns true if this bit array is empty, i.e.
 
size_t GetFirstSet () const
 Returns the index of the first bit set in the bit array.
 
size_t GetLastSet () const
 Returns the index of the last bit set in the bit array.
 
size_t GetNumSet () const
 Returns the number of bits currently set in this array.
 
bool AreAllSet () const
 Returns true, if all the bits in this bit array are set.
 
bool AreAllUnset () const
 Returns true, if all the bits in this bit array are unset.
 
bool IsAnySet () const
 Returns true, if there is at least a single set bit.
 
bool IsAnyUnset () const
 Returns true, if there is at least a single unset bit.
 
bool AreContiguouslySet () const
 Returns true if the set bits in this bit array are contiguous.
 
size_t GetAllocatedSize () const
 Returns the amount of memory this object holds on to.
 
TF_API size_t GetHash () const
 Returns a hash for this instance.
 
TF_API std::string GetAsStringLeftToRight () const
 Returns a string representing the bits for debugging with bits ordered from left to right with increasing indices.
 
TF_API std::string GetAsStringRightToLeft () const
 Returns a string representing the bits for debugging with bits ordered from right to left with increasing indices.
 
bool HasNonEmptyIntersection (const TfBits &rhs) const
 Returns true if the result of the intersection with rhs would be non-zero.
 
bool HasNonEmptyDifference (const TfBits &rhs) const
 Returns true if the result of an asymmetric set different is non-zero.
 
bool Contains (const TfBits &rhs) const
 Returns true if this bit array contains rhs by computing: (rhs - this).GetNumSet() == 0.
 
AllView GetAllView () const
 Returns an iteratable view for the bits that steps over all bits.
 
AllSetView GetAllSetView () const
 Returns an iteratable view for the bits that steps over all set bits.
 
AllUnsetView GetAllUnsetView () const
 Returns an iteratable view for the bits that steps over all unset bits.
 
Operators
TF_API bool operator== (const TfBits &rhs) const
 Returns true if this == rhs.
 
bool operator!= (const TfBits &rhs) const
 Returns true if this != rhs.
 
TF_API TfBitsoperator&= (const TfBits &rhs)
 Ands these bits with the rhs bits.
 
TfBits operator& (const TfBits &rhs) const
 Returns these bits and'ed with rhs.
 
TF_API TfBitsoperator|= (const TfBits &rhs)
 Ors these bits with the rhs bits.
 
TfBits operator| (const TfBits &rhs) const
 Returns these bits or'ed with rhs.
 
TF_API TfBitsoperator^= (const TfBits &rhs)
 Xors these bits with the rhs bits.
 
TfBits operator^ (const TfBits &rhs) const
 Returns these bits xor'ed with rhs.
 
TF_API TfBitsoperator-= (const TfBits &rhs)
 Removes all bits in the rhs bits from these bits.
 
TF_API TfBitsComplement ()
 Flips all bits.
 
bool operator[] (size_t index) const
 Returns bit at index.
 

Friends

void swap (TfBits &lhs, TfBits &rhs)
 

Detailed Description

Fast bit array that keeps track of the number of bits set and can find the next set in a timely manner.

Note about thread safety in this class:

TfBits supports only the most basic thread safety guarantee: multiple threads may safely call const methods concurrently. A thread must not invoke any non-const method on a TfBits object while any other thread is accessing it.

There are certain members in this class that are mutable and modified in const methods. However, since they are updated without being read and all threads would update them with the same values in the case of a race condition, the worst thing that can happen is redundant computation.

Successive access to these members will result in read only access patterns.

All occurances are marked.

Definition at line 48 of file bits.h.

Member Typedef Documentation

◆ AllSetView

using AllSetView = View<AllSet>

Definition at line 903 of file bits.h.

◆ AllUnsetView

using AllUnsetView = View<AllUnset>

Definition at line 904 of file bits.h.

◆ AllView

using AllView = View<All>

Definition at line 902 of file bits.h.

Member Enumeration Documentation

◆ Mode

enum Mode

Definition at line 53 of file bits.h.

Constructor & Destructor Documentation

◆ TfBits() [1/4]

TfBits ( size_t  num = 0)
inlineexplicit

Constructs a fixed size bit array, clears all bits.

Definition at line 85 of file bits.h.

◆ TfBits() [2/4]

TfBits ( size_t  num,
size_t  first,
size_t  last 
)
inline

Constructs a fixed size bit array, with a range of bits set.

Definition at line 95 of file bits.h.

◆ TfBits() [3/4]

TfBits ( const TfBits rhs)
inline

Copy-constructs a fixed size bit array.

Definition at line 114 of file bits.h.

◆ TfBits() [4/4]

TfBits ( TfBits &&  rhs)
inline

Move constructor.

Definition at line 130 of file bits.h.

◆ ~TfBits()

~TfBits ( )
inline

Destructor.

Definition at line 137 of file bits.h.

Member Function Documentation

◆ AreAllSet()

bool AreAllSet ( ) const
inline

Returns true, if all the bits in this bit array are set.

Definition at line 534 of file bits.h.

◆ AreAllUnset()

bool AreAllUnset ( ) const
inline

Returns true, if all the bits in this bit array are unset.

Definition at line 543 of file bits.h.

◆ AreContiguouslySet()

bool AreContiguouslySet ( ) const
inline

Returns true if the set bits in this bit array are contiguous.

Note: This returns false if there are no set bits.

Definition at line 566 of file bits.h.

◆ Assign()

void Assign ( size_t  index,
bool  val 
)
inline

Assigns val to bit # index.

Definition at line 402 of file bits.h.

◆ Clear()

void Clear ( size_t  index)
inline

Clears bit # index to zero.

Definition at line 353 of file bits.h.

◆ ClearAll()

void ClearAll ( )
inline

Clears all bits to zero.

Definition at line 330 of file bits.h.

◆ Complement()

TF_API TfBits & Complement ( )

Flips all bits.

The resulting bit set is the complement of this bit set.

◆ Contains()

bool Contains ( const TfBits rhs) const
inline

Returns true if this bit array contains rhs by computing: (rhs - this).GetNumSet() == 0.

Ie. it will return true if all bits of rhs are also set in this.

Definition at line 787 of file bits.h.

◆ FindNextSet()

size_t FindNextSet ( size_t  index) const
inline

Finds the next set bit that has a higher or equal index than index.

If no more set bits are found, index returns 'GetSize()'.

Definition at line 422 of file bits.h.

◆ FindNextUnset()

size_t FindNextUnset ( size_t  index) const
inline

Finds the next unset bit that has a higher or equal index than index.

If no more set bits are found, index returns 'GetSize()'.

Definition at line 458 of file bits.h.

◆ FindPrevSet()

size_t FindPrevSet ( size_t  index) const
inline

Finds the prev set bit that has a lower or equal index than index.

If no more set bits are found, index returns 'GetSize()'.

Definition at line 440 of file bits.h.

◆ GetAllocatedSize()

size_t GetAllocatedSize ( ) const
inline

Returns the amount of memory this object holds on to.

Definition at line 573 of file bits.h.

◆ GetAllSetView()

AllSetView GetAllSetView ( ) const
inline

Returns an iteratable view for the bits that steps over all set bits.

Definition at line 914 of file bits.h.

◆ GetAllUnsetView()

AllUnsetView GetAllUnsetView ( ) const
inline

Returns an iteratable view for the bits that steps over all unset bits.

Definition at line 920 of file bits.h.

◆ GetAllView()

AllView GetAllView ( ) const
inline

Returns an iteratable view for the bits that steps over all bits.

Definition at line 908 of file bits.h.

◆ GetAsStringLeftToRight()

TF_API std::string GetAsStringLeftToRight ( ) const

Returns a string representing the bits for debugging with bits ordered from left to right with increasing indices.

◆ GetAsStringRightToLeft()

TF_API std::string GetAsStringRightToLeft ( ) const

Returns a string representing the bits for debugging with bits ordered from right to left with increasing indices.

◆ GetFirstSet()

size_t GetFirstSet ( ) const
inline

Returns the index of the first bit set in the bit array.

If no bits are set, the return value is 'GetSize()'.

Definition at line 490 of file bits.h.

◆ GetHash()

TF_API size_t GetHash ( ) const

Returns a hash for this instance.

◆ GetLastSet()

size_t GetLastSet ( ) const
inline

Returns the index of the last bit set in the bit array.

If no bits are set, the return value is 'GetSize()'.

Definition at line 505 of file bits.h.

◆ GetNumSet()

size_t GetNumSet ( ) const
inline

Returns the number of bits currently set in this array.

Definition at line 520 of file bits.h.

◆ GetSize()

size_t GetSize ( ) const
inline

Returns the size of the bit array, ie.

the # of bits it can hold.

Definition at line 475 of file bits.h.

◆ HasNonEmptyDifference()

bool HasNonEmptyDifference ( const TfBits rhs) const
inline

Returns true if the result of an asymmetric set different is non-zero.

This is the equivalent to computing: return (this - rhs).GetNumSet() != 0 but avoids creating temporary copies.

Definition at line 740 of file bits.h.

◆ HasNonEmptyIntersection()

bool HasNonEmptyIntersection ( const TfBits rhs) const
inline

Returns true if the result of the intersection with rhs would be non-zero.

This method can be used for efficiency because it doesn't perform the full AND operation on a copy, and it can return early.

Definition at line 696 of file bits.h.

◆ IsAnySet()

bool IsAnySet ( ) const
inline

Returns true, if there is at least a single set bit.

Definition at line 550 of file bits.h.

◆ IsAnyUnset()

bool IsAnyUnset ( ) const
inline

Returns true, if there is at least a single unset bit.

Definition at line 557 of file bits.h.

◆ IsEmpty()

bool IsEmpty ( ) const
inline

Returns true if this bit array is empty, i.e.

it is of size zero.

Definition at line 482 of file bits.h.

◆ IsSet()

bool IsSet ( size_t  index) const
inline

Returns true, if bit # index is set.

Definition at line 412 of file bits.h.

◆ operator!=()

bool operator!= ( const TfBits rhs) const
inline

Returns true if this != rhs.

Definition at line 611 of file bits.h.

◆ operator&()

TfBits operator& ( const TfBits rhs) const
inline

Returns these bits and'ed with rhs.

Definition at line 625 of file bits.h.

◆ operator&=()

TF_API TfBits & operator&= ( const TfBits rhs)

Ands these bits with the rhs bits.

The resulting bit set is the intersection of the two bit sets.

◆ operator-=()

TF_API TfBits & operator-= ( const TfBits rhs)

Removes all bits in the rhs bits from these bits.

The resulting bit set is the asymmetric set difference of the two bit sets.

◆ operator=() [1/2]

TfBits & operator= ( const TfBits rhs)
inline

Assignment operator.

Definition at line 144 of file bits.h.

◆ operator=() [2/2]

TfBits & operator= ( TfBits &&  rhs)
inline

Move assignment operator.

Definition at line 172 of file bits.h.

◆ operator==()

TF_API bool operator== ( const TfBits rhs) const

Returns true if this == rhs.

◆ operator[]()

bool operator[] ( size_t  index) const
inline

Returns bit at index.

Definition at line 682 of file bits.h.

◆ operator^()

TfBits operator^ ( const TfBits rhs) const
inline

Returns these bits xor'ed with rhs.

Definition at line 658 of file bits.h.

◆ operator^=()

TF_API TfBits & operator^= ( const TfBits rhs)

Xors these bits with the rhs bits.

The resulting bit set is the union of the two bit sets minus the intersection of the two bit sets.

◆ operator|()

TfBits operator| ( const TfBits rhs) const
inline

Returns these bits or'ed with rhs.

Definition at line 641 of file bits.h.

◆ operator|=()

TF_API TfBits & operator|= ( const TfBits rhs)

Ors these bits with the rhs bits.

The resulting bit set is the union of the two bit sets.

◆ OrSubset()

TF_API void OrSubset ( const TfBits rhs)

Combines two differently sized TfBits using an or operator.

This can be used if GetSize() >= rhs.GetSize(). This is more efficient than padding rhs to the correct size beforehand.

◆ Resize()

void Resize ( size_t  num)
inline

Resizes the bit array, however, the bits are left uninitialized.

So you most likely want to call ClearAll(); or SetAll();.

Definition at line 185 of file bits.h.

◆ ResizeKeepContent()

void ResizeKeepContent ( size_t  num)
inline

Resizes the size of the bit array while keeping the content.

Definition at line 208 of file bits.h.

◆ Set()

void Set ( size_t  index)
inline

Sets bit # index to one.

Definition at line 377 of file bits.h.

◆ SetAll()

void SetAll ( )
inline

Sets all bits to one.

Definition at line 340 of file bits.h.

◆ Swap()

void Swap ( TfBits rhs)
inline

Provides a fast swap.

Definition at line 261 of file bits.h.

Friends And Related Function Documentation

◆ swap

void swap ( TfBits lhs,
TfBits rhs 
)
friend

Definition at line 324 of file bits.h.


The documentation for this class was generated from the following file: