1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2019-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#pragma once
namespace iSTD
{
/******************************************************************************\
CStaticBitSet provides standard bitset operations when the bitset size is
known at compile time. The number of bits supported is up to the template
parameter MaxBits. This gets rounded up to the next DWORD aligned value.
The API is the similar to iSTD::CBitSet, but this class will not grow if you
access an index higher than what is allocated. Instead, this class will
provided a nice blue screen to alert you of the error. It is the client's
responsibility to do any bounds checking.
Bits indices start at 0. For a CStaticBitSet< 32 >, the size is 32 and the
valid range of indicies is 0 through 31.
Constructor - Initializes all bits to off.
Clear - Turns all bits to off.
SetAll - Turns all bits to on.
Set - Turns one or more bits on.
Unset - Turns one bit off.
IsSet - Returns true if a bit is on, false otherwise.
\******************************************************************************/
template< DWORD MaxBits >
class CStaticBitSet
{
public:
CStaticBitSet( void );
void Clear( void );
void SetAll( void );
void Set( const DWORD index );
void Set( const DWORD index, const DWORD count );
void UnSet( const DWORD index );
bool IsSet( const DWORD index ) const;
DWORD BitCount() const;
DWORD BitCount( DWORD limit ) const;
protected:
enum { ArraySize = (MaxBits + sizeof(DWORD)*8 - 1 ) >> 5 };
DWORD GetArrayIndex( const DWORD bitNum ) const { return bitNum >> 5; }
DWORD GetBitIndex( const DWORD bitNum ) const { return bitNum & 0x1F; }
DWORD BitNumber( const DWORD number ) const { return 1 << number; }
DWORD m_bits[ ArraySize ];
#ifdef _DEBUG
bool m_debugBits[ MaxBits ];
#endif
};
/******************************************************************************\
CStaticBitSet
\******************************************************************************/
template< DWORD MaxBits >
CStaticBitSet< MaxBits >::CStaticBitSet( void )
{
C_ASSERT( MaxBits >= 1 );
Clear();
}
/******************************************************************************\
CStaticBitSet::Clear
\******************************************************************************/
template< DWORD MaxBits >
void CStaticBitSet< MaxBits >::Clear( void )
{
SafeMemSet( &m_bits, 0, sizeof( m_bits[ 0 ] ) * ArraySize );
#ifdef _DEBUG
for( DWORD ndx = 0; ndx < MaxBits; ndx++ )
{
m_debugBits[ ndx ] = false;
}
#endif
}
/******************************************************************************\
CStaticBitSet::SetAll
\******************************************************************************/
template< DWORD MaxBits >
void CStaticBitSet< MaxBits >::SetAll( void )
{
SafeMemSet( &m_bits, 0xFF, sizeof( m_bits[ 0 ] ) * ArraySize );
#ifdef _DEBUG
for( DWORD ndx = 0; ndx < MaxBits; ndx++ )
{
m_debugBits[ ndx ] = true;
}
#endif
}
/******************************************************************************\
CStaticBitSet::Set
\******************************************************************************/
template< DWORD MaxBits >
void CStaticBitSet< MaxBits >::Set( const DWORD index )
{
#ifdef _DEBUG
ASSERT( IsSet( index ) == m_debugBits[ index ] );
#endif
ASSERT( GetArrayIndex( index ) <= ArraySize );
DWORD arrayIndex = GetArrayIndex( index );
DWORD bitIndex = GetBitIndex( index );
m_bits[ arrayIndex ] |= BitNumber( bitIndex );
#ifdef _DEBUG
m_debugBits[ index ] = true;
#endif
}
/******************************************************************************\
CStaticBitSet::Set (contiguous version) - Sets a contiguous number of bits
that could span multiple DWORDS. Optimized towards setting a contiguous
amount of bits that span many DWORDS.
This algorithm takes advantage of the property that if you set a contiguous
set of bits that span multiple DWORDs, all the DWORDs between the first
and last bits can be set to 0xFFFFFFFF. There is not need to calculate
which bits need to be set. Only the bits in the first and last DWORDs
need to be calculated.
Notes: This function is specifically coded for m_bits to be DWORDs.
If you change the m_bits type, you will need to change this function.
\******************************************************************************/
template< DWORD MaxBits >
void CStaticBitSet< MaxBits >::Set( const DWORD index, DWORD count )
{
ASSERT( GetArrayIndex( index ) <= ArraySize );
#ifdef _DEBUG
for( DWORD ndx = 0; ndx < count; ndx++)
{
m_debugBits[ index + ndx ] = true;
}
#endif
DWORD arrayIndex = GetArrayIndex( index );
DWORD bitIndex = GetBitIndex( index );
DWORD mask;
const DWORD BITS_PER_ELEMENT = 32;
if( ( bitIndex + count ) <= BITS_PER_ELEMENT ) // Spans only a single DWORD
{
// Promote to QWORD due to bug when shifting 0x1 by 32 becomes 0x1
// instead of our desired 0x0. Seems like it is a rotate shift.
mask = (((QWORD)1 << count ) - 1 ) << bitIndex;
m_bits[ arrayIndex ] |= mask;
}
else
{
// Set the bits in the first DWORD
mask = (DWORD) (QWORD) ( ( (DWORD) -1 ) << bitIndex ) ;
m_bits[ arrayIndex ] |= mask ;
arrayIndex++;
count = count - ( BITS_PER_ELEMENT - bitIndex );
// Set the bits in the middle DWORDs
while( count >= BITS_PER_ELEMENT )
{
m_bits[ arrayIndex ] = 0xFFFFFFFF;
arrayIndex++;
count -= BITS_PER_ELEMENT;
}
// Set the bits in the last DWORD
mask = ( (QWORD)1 << count ) - 1;
m_bits[ arrayIndex ] |= mask;
}
#ifdef _DEBUG
for( DWORD ndx = 0; ndx < MaxBits; ndx++)
{
ASSERT( m_debugBits[ ndx ] == IsSet( ndx ) );
}
#endif
}
/******************************************************************************\
CStaticBitSet::UnSet
\******************************************************************************/
template< DWORD MaxBits >
void CStaticBitSet< MaxBits >::UnSet( const DWORD index )
{
#ifdef _DEBUG
ASSERT( IsSet( index ) == m_debugBits[ index ] );
#endif
ASSERT( GetArrayIndex( index ) <= ArraySize );
DWORD arrayIndex = GetArrayIndex( index );
DWORD bitIndex = GetBitIndex( index );
m_bits[ arrayIndex ] &= ~BitNumber( bitIndex );
#ifdef _DEBUG
m_debugBits[ index ] = false;
#endif
}
/******************************************************************************\
CStaticBitSet::IsSet
\******************************************************************************/
template< DWORD MaxBits >
bool CStaticBitSet< MaxBits >::IsSet( const DWORD index ) const
{
ASSERT( GetArrayIndex( index ) <= ArraySize );
DWORD arrayIndex = GetArrayIndex( index );
DWORD bitIndex = GetBitIndex( index );
bool isSet = ( m_bits[ arrayIndex ] & BitNumber( bitIndex ) ) ? true : false;
#ifdef _DEBUG
ASSERT( isSet == m_debugBits[ index ] );
#endif
return isSet;
}
/******************************************************************************\
CStaticBitSet::BitCount
\******************************************************************************/
template< DWORD MaxBits >
DWORD CStaticBitSet< MaxBits >::BitCount() const
{
DWORD bitCount = 0;
const DWORD cBitsPerArrayElement = sizeof(m_bits[0]) * 8;
DWORD index = ArraySize;
while( index-- )
{
if( m_bits[ index ] != 0 )
{
for( DWORD i = 0; i < cBitsPerArrayElement; i++ )
{
if( m_bits[index] & (1<<i) )
{
bitCount++;
}
}
}
}
return bitCount;
}
/******************************************************************************\
CStaticBitSet::BitCount
\******************************************************************************/
template< DWORD MaxBits >
DWORD CStaticBitSet< MaxBits >::BitCount(DWORD limit) const
{
DWORD bitCount = 0;
limit = iSTD::Min<DWORD>( limit, ArraySize-1 );
for( DWORD i=0; i <= limit; i++ )
{
if( IsSet( i ) )
{
bitCount++;
}
}
return bitCount;
}
}
|