netgen/libsrc/general/ngbitarray.hpp

148 lines
2.5 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
#ifndef FILE_BitArray
#define FILE_BitArray
/**************************************************************************/
/* File: bitarray.hpp */
/* Author: Joachim Schoeberl */
/* Date: 01. Jun. 95 */
/**************************************************************************/
#include <limits.h>
2009-07-20 14:36:36 +06:00
namespace netgen
{
2009-01-13 04:40:13 +05:00
/**
2019-08-28 17:00:49 +05:00
data type NgBitArray
2009-01-13 04:40:13 +05:00
2019-08-28 17:00:49 +05:00
NgBitArray is a compressed array of Boolean information. By Set and Clear
2009-01-13 04:40:13 +05:00
the whole array or one bit can be set or reset, respectively.
2017-12-05 02:32:47 +05:00
Test returns the state of the occurring bit.
2009-01-13 04:40:13 +05:00
No range checking is done.
index ranges from 0 to size-1
*/
2019-08-28 17:00:49 +05:00
class NgBitArray
2009-01-13 04:40:13 +05:00
{
INDEX size;
unsigned char * data;
public:
DLL_HEADER NgBitArray ();
2009-01-13 04:40:13 +05:00
///
DLL_HEADER NgBitArray (INDEX asize);
2009-01-13 04:40:13 +05:00
///
DLL_HEADER ~NgBitArray ();
2009-01-13 04:40:13 +05:00
///
DLL_HEADER void SetSize (INDEX asize);
2009-01-13 04:40:13 +05:00
///
INDEX Size () const
{
return size;
}
///
DLL_HEADER void Set ();
2009-01-13 04:40:13 +05:00
///
void Set (INDEX i)
{
data[Addr(i)] |= Mask(i);
}
DLL_HEADER void Clear ();
2009-01-13 04:40:13 +05:00
void Clear (INDEX i)
{
data[Addr(i)] &= ~Mask(i);
}
bool Test (INDEX i) const
{
return (data[i / CHAR_BIT] & (char(1) << (i % CHAR_BIT) ) ) ? true : false;
}
///
void Invert ();
///
2019-08-28 17:00:49 +05:00
void And (const NgBitArray & ba2);
2009-01-13 04:40:13 +05:00
///
2019-08-28 17:00:49 +05:00
void Or (const NgBitArray & ba2);
2009-01-13 04:40:13 +05:00
private:
///
inline unsigned char Mask (INDEX i) const
{
return char(1) << (i % CHAR_BIT);
}
///
inline INDEX Addr (INDEX i) const
{
return (i / CHAR_BIT);
}
///
2019-08-28 17:00:49 +05:00
NgBitArray & operator= (NgBitArray &);
2009-01-13 04:40:13 +05:00
///
2019-08-28 17:00:49 +05:00
NgBitArray (const NgBitArray &);
2009-01-13 04:40:13 +05:00
};
// print bitarray
2019-08-28 17:00:49 +05:00
inline ostream & operator<< (ostream & s, const NgBitArray & a)
2009-01-13 04:40:13 +05:00
{
for (int i = 1; i <= a.Size(); i++)
{
s << int (a.Test(i));
if (i % 40 == 0) s << "\n";
}
if (a.Size() % 40 != 0) s << "\n";
return s;
}
/*
inline
2019-08-28 17:00:49 +05:00
INDEX NgBitArray :: Size () const
2009-01-13 04:40:13 +05:00
{
return size;
}
inline
2019-08-28 17:00:49 +05:00
unsigned char NgBitArray :: Mask (INDEX i) const
2009-01-13 04:40:13 +05:00
{
return char(1) << (i % CHAR_BIT);
}
inline
2019-08-28 17:00:49 +05:00
INDEX NgBitArray :: Addr (INDEX i) const
2009-01-13 04:40:13 +05:00
{
return (i / CHAR_BIT);
}
inline
2019-08-28 17:00:49 +05:00
void NgBitArray :: Set (INDEX i)
2009-01-13 04:40:13 +05:00
{
data[Addr(i)] |= Mask(i);
}
inline
2019-08-28 17:00:49 +05:00
void NgBitArray :: Clear (INDEX i)
2009-01-13 04:40:13 +05:00
{
data[Addr(i)] &= ~Mask(i);
}
inline
2019-08-28 17:00:49 +05:00
int NgBitArray :: Test (INDEX i) const
2009-01-13 04:40:13 +05:00
{
return (data[i / CHAR_BIT] & (char(1) << (i % CHAR_BIT) ) ) ? 1 : 0;
}
*/
2009-07-20 14:36:36 +06:00
}
2009-01-13 04:40:13 +05:00
#endif