Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.5 KiB

9 years ago
//========= Copyright <EFBFBD> 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#ifndef BITVEC_H
#define BITVEC_H
#ifdef _WIN32
#pragma once
#endif
#include <assert.h>
#include <string.h>
class CBitVecAccessor
{
public:
8 years ago
CBitVecAccessor( unsigned long *pDWords, int iBit );
9 years ago
8 years ago
void operator=( int val );
operator unsigned long();
9 years ago
private:
8 years ago
unsigned long *m_pDWords;
int m_iBit;
9 years ago
};
// CBitVec allows you to store a list of bits and do operations on them like they were
// an atomic type.
template<int NUM_BITS>
class CBitVec
{
public:
8 years ago
CBitVec();
9 years ago
// Set all values to the specified value (0 or 1..)
8 years ago
void Init( int val = 0 );
9 years ago
// Access the bits like an array.
8 years ago
CBitVecAccessor operator[]( int i );
9 years ago
// Operations on other bit vectors.
8 years ago
CBitVec& operator=( CBitVec<NUM_BITS> const &other );
bool operator==( CBitVec<NUM_BITS> const &other );
bool operator!=( CBitVec<NUM_BITS> const &other );
9 years ago
// Get underlying dword representations of the bits.
8 years ago
int GetNumDWords();
unsigned long GetDWord( int i );
void SetDWord( int i, unsigned long val );
9 years ago
8 years ago
int GetNumBits();
9 years ago
private:
8 years ago
enum
{
NUM_DWORDS = NUM_BITS / 32 + !!( NUM_BITS & 31 )
};
unsigned long m_DWords[NUM_DWORDS];
9 years ago
};
// ------------------------------------------------------------------------ //
// CBitVecAccessor inlines.
// ------------------------------------------------------------------------ //
inline CBitVecAccessor::CBitVecAccessor(unsigned long *pDWords, int iBit)
{
m_pDWords = pDWords;
m_iBit = iBit;
}
8 years ago
inline void CBitVecAccessor::operator=( int val )
9 years ago
{
8 years ago
if( val )
m_pDWords[m_iBit >> 5] |= ( 1 << ( m_iBit & 31 ) );
9 years ago
else
8 years ago
m_pDWords[m_iBit >> 5] &= ~(unsigned long)( 1 << ( m_iBit & 31 ) );
9 years ago
}
inline CBitVecAccessor::operator unsigned long()
{
8 years ago
return m_pDWords[m_iBit >> 5] & ( 1 << ( m_iBit & 31 ) );
9 years ago
}
// ------------------------------------------------------------------------ //
// CBitVec inlines.
// ------------------------------------------------------------------------ //
template<int NUM_BITS>
inline int CBitVec<NUM_BITS>::GetNumBits()
{
return NUM_BITS;
}
template<int NUM_BITS>
inline CBitVec<NUM_BITS>::CBitVec()
{
8 years ago
for( int i = 0; i < NUM_DWORDS; i++ )
9 years ago
m_DWords[i] = 0;
}
template<int NUM_BITS>
8 years ago
inline void CBitVec<NUM_BITS>::Init( int val )
9 years ago
{
8 years ago
for( int i = 0; i < GetNumBits(); i++ )
9 years ago
{
8 years ago
( *this )[i] = val;
9 years ago
}
}
template<int NUM_BITS>
8 years ago
inline CBitVec<NUM_BITS>& CBitVec<NUM_BITS>::operator=( CBitVec<NUM_BITS> const &other )
9 years ago
{
8 years ago
memcpy( m_DWords, other.m_DWords, sizeof(m_DWords) );
9 years ago
return *this;
}
template<int NUM_BITS>
8 years ago
inline CBitVecAccessor CBitVec<NUM_BITS>::operator[]( int i )
9 years ago
{
8 years ago
assert( i >= 0 && i < GetNumBits() );
return CBitVecAccessor( m_DWords, i );
9 years ago
}
template<int NUM_BITS>
8 years ago
inline bool CBitVec<NUM_BITS>::operator==( CBitVec<NUM_BITS> const &other )
9 years ago
{
8 years ago
for( int i = 0; i < NUM_DWORDS; i++ )
if( m_DWords[i] != other.m_DWords[i] )
9 years ago
return false;
return true;
}
template<int NUM_BITS>
8 years ago
inline bool CBitVec<NUM_BITS>::operator!=( CBitVec<NUM_BITS> const &other )
9 years ago
{
8 years ago
return !( *this == other );
9 years ago
}
template<int NUM_BITS>
inline int CBitVec<NUM_BITS>::GetNumDWords()
{
return NUM_DWORDS;
}
template<int NUM_BITS>
8 years ago
inline unsigned long CBitVec<NUM_BITS>::GetDWord( int i )
9 years ago
{
8 years ago
assert( i >= 0 && i < NUM_DWORDS );
9 years ago
return m_DWords[i];
}
template<int NUM_BITS>
8 years ago
inline void CBitVec<NUM_BITS>::SetDWord( int i, unsigned long val )
9 years ago
{
8 years ago
assert( i >= 0 && i < NUM_DWORDS );
9 years ago
m_DWords[i] = val;
}
#endif // BITVEC_H