CMS 3D CMS Logo

Classes | Public Member Functions | Static Public Member Functions | Private Attributes | Friends

BitArray< N > Class Template Reference

#include <BitArray.h>

List of all members.

Classes

class  refToBit

Public Member Functions

int any ()
void assign (const int p, const int n, const int val)
void assign (const int p, const int n, const BitArray< N > &val)
void assign (const int p, const int n, const char *str)
 BitArray ()
 BitArray (const BitArray< N > &br)
 BitArray (const char *str, const int p, const int n)
 BitArray (const unsigned i)
 BitArray (const char *str)
BitArray< 8 > byte (const int i) const
void cleanUnused ()
int count () const
unsigned dataWord (const int i) const
unsigned & dataWord (const int i)
int element (const int pos) const
BitArray< N > & flip ()
unsigned & getWord (const int pos)
unsigned getWord (const int pos) const
unsigned lastWordMask () const
int nBits () const
int none ()
int nWords () const
void one ()
bool operator!= (const BitArray< N > &a) const
BitArray< N > operator& (const BitArray< N > &a)
BitArray< N > & operator&= (const BitArray< N > &a)
BitArray< N > operator+ (const BitArray< N > &a)
BitArray< N > & operator++ (int)
BitArray< N > & operator+= (const BitArray< N > &a)
BitArray< N > operator- (const BitArray< N > &a)
BitArray< N > & operator-= (const BitArray< N > &a)
bool operator< (const BitArray< N > &a) const
BitArray< N > operator<< (const int n)
BitArray< N > & operator<<= (const int n)
bool operator<= (const BitArray< N > &a) const
BitArray< N > & operator= (const BitArray< N > &a)
BitArray< N > & operator= (const unsigned i)
BitArray< N > & operator= (const char *str)
bool operator== (const BitArray< N > &a) const
bool operator> (const BitArray< N > &a) const
bool operator>= (const BitArray< N > &a) const
BitArray< N > operator>> (const int n)
BitArray< N > & operator>>= (const int n)
refToBit operator[] (const int pos)
int operator[] (const int pos) const
BitArray< N > operator^ (const BitArray< N > &a)
BitArray< N > & operator^= (const BitArray< N > &a)
BitArray< N > operator| (const BitArray< N > &a)
BitArray< N > & operator|= (const BitArray< N > &a)
BitArray< N > operator~ () const
std::ostream & print (std::ostream &o=std::cout) const
unsigned read (const int p, const int n) const
void reset (const int i)
void reset ()
void set (const int i)
void set (const int i, const int val)
void set (const int i, const char *str)
int size () const
int test (const int i) const
BitArray< N > & twoComplement ()
BitArray< N > twoComplement () const
void unset (const int i)
int unusedBits () const
void zero ()

Static Public Member Functions

static int getPosInWord (const int pos)
static unsigned getPosMask (const int pos)

Private Attributes

unsigned _data [N/32+1]

Friends

class refToBit

Detailed Description

template<int N>
class BitArray< N >

Definition at line 30 of file BitArray.h.


Constructor & Destructor Documentation

template<int N>
BitArray< N >::BitArray ( ) [inline]

Definition at line 83 of file BitArray.h.

{this->zero();}
template<int N>
BitArray< N >::BitArray ( const BitArray< N > &  br) [inline]

Definition at line 85 of file BitArray.h.

                                  {
    for (int i=0;i<this->nWords();i++) {
      _data[i] = br._data[i];         
    }
    this->cleanUnused();
  }
template<int N>
BitArray< N >::BitArray ( const char *  str) [inline]

Definition at line 91 of file BitArray.h.

                            { 
    this->zero(); 
    this->assign(0,this->nBits(),str); 
    this->cleanUnused();
  }
template<int N>
BitArray< N >::BitArray ( const char *  str,
const int  p,
const int  n 
) [inline]

Definition at line 96 of file BitArray.h.

                                                      {
    this->zero(); 
    this->assign(p, n, str);
  }
template<int N>
BitArray< N >::BitArray ( const unsigned  i) [inline]

Definition at line 100 of file BitArray.h.

                             { 
    this->zero();
    _data[0] = i;                 // the nBit least sign. bits are considered
    this->cleanUnused();
  }

Member Function Documentation

template<int N>
int BitArray< N >::any ( ) [inline]

Definition at line 184 of file BitArray.h.

Referenced by DTTSTheta::runDTTSTheta().

            {
    int nw = this->nWords();
    int ub = unusedBits();
    if(this->dataWord(nw-1)<<ub!=0)return 1;
    if(nw>1){
      for (int iw=0;iw<nw-1;iw++){
        if(this->dataWord(iw)!=0) return 1;
      }
    }
    return 0;
  }    
template<int N>
void BitArray< N >::assign ( const int  p,
const int  n,
const int  val 
) [inline]

Definition at line 239 of file BitArray.h.

Referenced by BitArray< 9 >::BitArray(), DTSectCollPhCand::clearBits(), DTTSCand::clearBits(), DTTSCand::clearBitsBkmod(), DTSectCollPhCand::clearBitsSectColl(), DTTracoChip::insideAngWindow(), BitArray< 9 >::set(), DTSectCollPhCand::setBitsSectColl(), DTTSCand::setBitsTsm(), and DTTSCand::setBitsTss().

                                                       {
    assert(p>=0 && p+n<=this->nBits());  
    // only the n least significant bits of val are considered
    for(int i=0; i<n;i++){
      if(val>>i&1) {
        this->set(p+i);
      } else {
        this->unset(p+i);
      }
    }
  }
template<int N>
void BitArray< N >::assign ( const int  p,
const int  n,
const BitArray< N > &  val 
) [inline]

Definition at line 250 of file BitArray.h.

                                                                {
    assert(p>=0 && p+n<=this->nBits());  
    // only the n least significant bits of val are considered
    for(int i=0; i<n;i++){
      if(val.element(i)) {
        this->set(p+i);
      } else {
        this->unset(p+i);
      }
    }
  }
template<int N>
void BitArray< N >::assign ( const int  p,
const int  n,
const char *  str 
) [inline]

Definition at line 261 of file BitArray.h.

                                                         {
    assert(p>=0 && p+n<=this->nBits());  
    // only the n least significant bits of val are considered
    for(int i=0; i<n;i++){
      assert(str[i]=='1'||str[i]=='0');  
      if(str[i]=='1') {
        this->set(p+n-i-1);   // reading a string from left to right 
      } else {                // --> most significative bit is the one 
        this->unset(p+n-i-1); // with lower string index
      }
    }
  }
template<int N>
BitArray<8> BitArray< N >::byte ( const int  i) const [inline]

Definition at line 287 of file BitArray.h.

Referenced by DTTSTheta::runDTTSTheta().

                                      {
    BitArray<8> out;
    if(i>=0&&i<4*this->nWords()){
      unsigned k=(_data[i/4]>>8*(i%4))&0xff;
      out=k;
    }
    return out;
  }
template<int N>
void BitArray< N >::cleanUnused ( ) [inline]

Definition at line 170 of file BitArray.h.

Referenced by BitArray< 9 >::BitArray(), and BitArray< 9 >::operator=().

                     {
    _data[this->nWords()-1] &= (this->lastWordMask());
  }
template<int N>
int BitArray< N >::count ( void  ) const [inline]

Definition at line 175 of file BitArray.h.

                    {
    int n=0;
    for(int i=0;i<this->nBits();i++) {
      if(this->element(i))n++;
    }
    return n;
  }
template<int N>
unsigned& BitArray< N >::dataWord ( const int  i) [inline]

Definition at line 132 of file BitArray.h.

                                   {
    assert(i>=0 && i<this->nWords());
    return _data[i];
  }
template<int N>
unsigned BitArray< N >::dataWord ( const int  i) const [inline]
template<int N>
int BitArray< N >::element ( const int  pos) const [inline]
template<int N>
BitArray<N>& BitArray< N >::flip ( ) [inline]

Definition at line 406 of file BitArray.h.

Referenced by BitArray< 9 >::operator~().

                       {
    for(int i=0;i<this->nWords();i++) {
      _data[i] = ~_data[i];
    }
    return *this;
  }
template<int N>
static int BitArray< N >::getPosInWord ( const int  pos) [inline, static]

Definition at line 148 of file BitArray.h.

Referenced by BitArray< 9 >::getPosMask(), and BitArray< N >::refToBit::refToBit().

                                         {
    // assert(pos>=0&& pos<this->nBits());
    return pos%32;
  }
template<int N>
static unsigned BitArray< N >::getPosMask ( const int  pos) [inline, static]
template<int N>
unsigned& BitArray< N >::getWord ( const int  pos) [inline]

Definition at line 138 of file BitArray.h.

Referenced by BitArray< 9 >::element(), BitArray< N >::refToBit::refToBit(), BitArray< 9 >::set(), and BitArray< 9 >::unset().

                                    {
    assert(pos>=0&& pos<this->nBits());
    return _data[pos/32];
  }
template<int N>
unsigned BitArray< N >::getWord ( const int  pos) const [inline]

Definition at line 142 of file BitArray.h.

                                        {
    assert(pos>=0&& pos<this->nBits());
    return _data[pos/32];
  }
template<int N>
unsigned BitArray< N >::lastWordMask ( ) const [inline]

Definition at line 165 of file BitArray.h.

Referenced by BitArray< 9 >::cleanUnused().

                                {
    return static_cast<unsigned>(0xffffffff)>>(this->unusedBits());
  }
template<int N>
int BitArray< N >::nBits ( ) const [inline]
template<int N>
int BitArray< N >::none ( ) [inline]

Definition at line 197 of file BitArray.h.

             {
    int nw = this->nWords();
    int ub = unusedBits();
    if(this->dataWord(nw-1)<<ub!=0xffffffff)return 1;
    if(nw>1){
      for (int iw=0;iw<nw-1;iw++){
        if(this->dataWord(iw)!=0xffffffff) return 1;
      }
    }
    return 0;
  }    
template<int N>
int BitArray< N >::nWords ( ) const [inline]
template<int N>
void BitArray< N >::one ( ) [inline]

Definition at line 223 of file BitArray.h.

Referenced by DTTSCand::clear(), DTSectCollPhCand::clear(), DTSectCollPhCand::DTSectCollPhCand(), DTTSCand::DTTSCand(), and DTConfigTSPhi::setDefaults().

             {
    for (int i=0;i<this->nWords();i++) {
      _data[i] = 0xffffffff;       // set to 1
    }
  }
template<int N>
bool BitArray< N >::operator!= ( const BitArray< N > &  a) const [inline]

Definition at line 394 of file BitArray.h.

{ return !(a==*this); }
template<int N>
BitArray<N> BitArray< N >::operator& ( const BitArray< N > &  a) [inline]

Definition at line 425 of file BitArray.h.

{return BitArray<N> (*this)&=a; }
template<int N>
BitArray<N>& BitArray< N >::operator&= ( const BitArray< N > &  a) [inline]

Definition at line 417 of file BitArray.h.

                                                 {
    for(int i = 0;i<this->nWords();i++) {
      this->dataWord(i) &= a.dataWord(i);
    }
    return *this;
  }    
template<int N>
BitArray<N> BitArray< N >::operator+ ( const BitArray< N > &  a) [inline]

Definition at line 489 of file BitArray.h.

{return BitArray<N> (*this)+=a; }
template<int N>
BitArray<N>& BitArray< N >::operator++ ( int  ) [inline]

Definition at line 492 of file BitArray.h.

                               {
    int i = 0;
    while(i<this->nBits()&&this->element(i)==1) { this->unset(i); i++; }
    if(i<this->nBits())this->set(i);
    return *this;
  }
template<int N>
BitArray<N>& BitArray< N >::operator+= ( const BitArray< N > &  a) [inline]

Definition at line 476 of file BitArray.h.

                                                {
    int rep=0;
    int sum=0;
    for(int i=0;i<this->nBits();i++) {
      sum=this->element(i)^rep;
      rep=this->element(i)&rep;
      this->set(i,sum^a.element(i));
      rep|=(sum&a.element(i));
    }
    return *this;
  }
template<int N>
BitArray<N> BitArray< N >::operator- ( const BitArray< N > &  a) [inline]

Definition at line 515 of file BitArray.h.

{return BitArray<N> (*this)-=a; }
template<int N>
BitArray<N>& BitArray< N >::operator-= ( const BitArray< N > &  a) [inline]

Definition at line 510 of file BitArray.h.

                                                {
    return *this+=a.twoComplement();
  }
template<int N>
bool BitArray< N >::operator< ( const BitArray< N > &  a) const [inline]

Definition at line 371 of file BitArray.h.

                                             {
    int nw = this->nWords();
    int ub = this->unusedBits();
    unsigned aaa = this->dataWord(nw-1)<<ub; // ignore unused bits
    unsigned bbb =     a.dataWord(nw-1)<<ub; // in both operands
    if        (aaa<bbb) {
      return 1;
    } else if (aaa>bbb) {
      return 0;
    }
    if(nw>1){
      for (int iw=nw-2;iw>=0;iw--){
        if        (this->dataWord(iw)<a.dataWord(iw)) {
          return 1;
        } else if (this->dataWord(iw)>a.dataWord(iw)) {
          return 0;
        }
      }
    }
    return 0;
  }
template<int N>
BitArray<N> BitArray< N >::operator<< ( const int  n) [inline]

Definition at line 460 of file BitArray.h.

{ return BitArray<N> (*this)<<=n; }
template<int N>
BitArray<N>& BitArray< N >::operator<<= ( const int  n) [inline]

Definition at line 450 of file BitArray.h.

                                        {
    assert(n>=0&&n<this->nBits());
    if(n==0)return *this;
    int i=0;
    for(i=this->nBits()-1;i>=n;i--) this->set(i,this->element(i-n));
    for(i=n-1;i>=0;i--) this->unset(i);
    return *this;
  }
template<int N>
bool BitArray< N >::operator<= ( const BitArray< N > &  a) const [inline]

Definition at line 403 of file BitArray.h.

{ return !(*this>a); }
template<int N>
BitArray<N>& BitArray< N >::operator= ( const unsigned  i) [inline]

Definition at line 307 of file BitArray.h.

                                           {
    this->zero();
    _data[0] = i;                 // the nBit least sign. bits are considered
    this->cleanUnused();
    return *this;
  }
template<int N>
BitArray<N>& BitArray< N >::operator= ( const char *  str) [inline]

Definition at line 328 of file BitArray.h.

                                          {
    this->zero();
    for(int i=0; i<this->nBits();i++){
      assert(str[i]=='1'||str[i]=='0');  
      if(str[i]=='1') {
        this->set(this->nBits()-i-1);   // reading a string from left to right 
      } else if(str[i]=='0') {    // --> most significative bit is the one 
        this->unset(this->nBits()-i-1); // with lower string index
      } else {
        break;                    // exit when find a char which is not 0 or 1
      }
    }
    this->cleanUnused();
    return *this;
  }
template<int N>
BitArray<N>& BitArray< N >::operator= ( const BitArray< N > &  a) [inline]

Definition at line 296 of file BitArray.h.

                                               {
    if(this != &a) {
      for (int i=0;i<this->nWords();i++) {
        _data[i] = a._data[i];
      }
    }
    this->cleanUnused();
    return *this;
  }
template<int N>
bool BitArray< N >::operator== ( const BitArray< N > &  a) const [inline]

Definition at line 357 of file BitArray.h.

                                              {
   int nw = this->nWords();
    int ub = this->unusedBits();
    if(this->dataWord(nw-1)<<ub!=         // check last word
           a.dataWord(nw-1)<<ub)return 0;
    if(nw>1){
      for (int iw=0;iw<nw-1;iw++){
        if(this->dataWord(iw)!=a.dataWord(iw)) return 0;
      }
    }
    return 1;
  }
template<int N>
bool BitArray< N >::operator> ( const BitArray< N > &  a) const [inline]

Definition at line 400 of file BitArray.h.

{ return !(*this<a||*this==a); }
template<int N>
bool BitArray< N >::operator>= ( const BitArray< N > &  a) const [inline]

Definition at line 397 of file BitArray.h.

{ return !(*this<a); }
template<int N>
BitArray<N> BitArray< N >::operator>> ( const int  n) [inline]

Definition at line 473 of file BitArray.h.

{ return BitArray<N> (*this)>>=n; }
template<int N>
BitArray<N>& BitArray< N >::operator>>= ( const int  n) [inline]

Definition at line 463 of file BitArray.h.

                                        {
    assert(n>=0&&n<this->nBits());
    if(n==0)return *this;
    int i=0;
    for(i=0;i<this->nBits()-n;i++) this->set(i,this->element(i+n));
    for(i=this->nBits()-n;i<this->nBits();i++) this->unset(i);
    return *this;
  }
template<int N>
refToBit BitArray< N >::operator[] ( const int  pos) [inline]

Definition at line 353 of file BitArray.h.

{ return refToBit(*this,pos); }
template<int N>
int BitArray< N >::operator[] ( const int  pos) const [inline]

Definition at line 354 of file BitArray.h.

{ return element(pos); }
template<int N>
BitArray<N> BitArray< N >::operator^ ( const BitArray< N > &  a) [inline]

Definition at line 447 of file BitArray.h.

{return BitArray<N> (*this)^=a; }
template<int N>
BitArray<N>& BitArray< N >::operator^= ( const BitArray< N > &  a) [inline]

Definition at line 439 of file BitArray.h.

                                                {
    for(int i = 0;i<this->nWords();i++) {
      this->dataWord(i) ^= a.dataWord(i);
    }
    return *this;
  }    
template<int N>
BitArray<N> BitArray< N >::operator| ( const BitArray< N > &  a) [inline]

Definition at line 436 of file BitArray.h.

{return BitArray<N> (*this)|=a;}
template<int N>
BitArray<N>& BitArray< N >::operator|= ( const BitArray< N > &  a) [inline]

Definition at line 428 of file BitArray.h.

                                                {
    for(int i = 0;i<this->nWords();i++) {
      this->dataWord(i) |= a.dataWord(i);
    }
    return *this;
  }    
template<int N>
BitArray<N> BitArray< N >::operator~ ( ) const [inline]

Definition at line 414 of file BitArray.h.

{ return BitArray<N> (*this).flip(); }
template<int N>
std::ostream& BitArray< N >::print ( std::ostream &  o = std::cout) const [inline]

Definition at line 345 of file BitArray.h.

Referenced by DTBtiChip::keepTrig(), DTBtiChip::keepTrigPatt(), DTTSCand::print(), DTConfigTSPhi::print(), and DTSectCollPhCand::print().

                                                  {
    for(int i = this->nBits()-1; i>=0; i--){
      o << this->element(i);
    }
    return o;
  }
template<int N>
unsigned BitArray< N >::read ( const int  p,
const int  n 
) const [inline]

Definition at line 275 of file BitArray.h.

Referenced by DTTracoChip::insideAngWindow().

                                                {
    assert(p>=0 && p+n<=this->nBits());  
    assert(n<=32); // the output must fit in a 32 bit word
    // only the n least significant bits of val are considered
    unsigned out=0x0;
    for(int i=0; i<n;i++){
      if(this->test(p+i)) out |= 1<<i;
    }
    return out;
  }
template<int N>
void BitArray< N >::reset ( const int  i) [inline]

Definition at line 232 of file BitArray.h.

{ this->unset(i); }
template<int N>
void BitArray< N >::reset ( void  ) [inline]

Definition at line 221 of file BitArray.h.

Referenced by DTBtiChip::keepTrigPatt().

{ zero(); }
template<int N>
void BitArray< N >::set ( const int  i,
const char *  str 
) [inline]

Definition at line 236 of file BitArray.h.

{ this->assign(i,1,str); }
template<int N>
void BitArray< N >::set ( const int  i,
const int  val 
) [inline]

Definition at line 235 of file BitArray.h.

{ this->assign(i,1,val); }
template<int N>
void BitArray< N >::set ( const int  i) [inline]
template<int N>
int BitArray< N >::size ( void  ) const [inline]

Definition at line 122 of file BitArray.h.

{ return this->nBits(); }
template<int N>
int BitArray< N >::test ( const int  i) const [inline]

Definition at line 213 of file BitArray.h.

Referenced by BitArray< 9 >::read().

{ return element(i); }
template<int N>
BitArray<N>& BitArray< N >::twoComplement ( ) [inline]

Definition at line 503 of file BitArray.h.

                               { 
    (*this).flip();
    (*this)++;
    return *this;
  }
template<int N>
BitArray<N> BitArray< N >::twoComplement ( ) const [inline]

Definition at line 500 of file BitArray.h.

Referenced by DTTracoChip::insideAngWindow(), and BitArray< 9 >::operator-=().

{ return BitArray<N> (~*this)++; }
template<int N>
void BitArray< N >::unset ( const int  i) [inline]
template<int N>
int BitArray< N >::unusedBits ( ) const [inline]

Definition at line 159 of file BitArray.h.

Referenced by BitArray< 9 >::any(), BitArray< 9 >::lastWordMask(), BitArray< 9 >::none(), BitArray< 9 >::operator<(), and BitArray< 9 >::operator==().

                         {
    if(this->nBits()==0)return 32;
    return 31-((this->nBits()-1)%32);
  }
template<int N>
void BitArray< N >::zero ( ) [inline]

Friends And Related Function Documentation

template<int N>
friend class refToBit [friend]

Definition at line 33 of file BitArray.h.

Referenced by BitArray< 9 >::operator[]().


Member Data Documentation

template<int N>
unsigned BitArray< N >::_data[N/32+1] [private]