diff --git a/bitvector.cc b/bitvector.cc index b1bcf83..9855b22 100644 --- a/bitvector.cc +++ b/bitvector.cc @@ -182,31 +182,7 @@ return *this; } -/*number of ones in a binary number, from "Hacker's delight" book*/ -#ifdef LONG_IS_32 -static unsigned int word_popul(unsigned long x) -{ -x -= ((x>>1)&0x55555555); -x = (x&0x33333333) + ((x>>2)&0x33333333); -x=(x + (x>>4))&0x0f0f0f0f; -x+= (x>>8); -x+= (x>>16); -return x&0x3f; -} -#else -//@@@@ use an efficient trick too -static unsigned int word_popul(unsigned long x) -{ -unsigned int s=0; -for(int i=0; i<64; ++i) - { - if(x&1) ++s; - x>>=1; - } -return s; -} - -#endif +//word_popul is inline in bitvector.h bitvector& bitvector::operator>>=(unsigned int i) @@ -260,30 +236,7 @@ zero_padding(); -//with before>0 only the ones at positions 0..before-1 are counted -unsigned int bitvector::population(const unsigned int before) const -{ -int i; -unsigned int s=0; -if(before) - { - if(before>size()) laerror("before parameter beyond the size in population()"); - int full=before/blockbits; - for(i=0; i>1) & 0x5555555555555555ULL; +x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL); +x = (x + (x>>4)) & 0x0f0f0f0f0f0f0f0fULL; +return (x*0x0101010101010101ULL)>>56; +#endif +} + class bitvector : public NRVec { private: @@ -176,7 +190,25 @@ if(i>=size()) bitvector lcm(const bitvector &rhs) const {return (*this)*rhs/this->gcd(rhs);}; bitvector composition(const bitvector &rhs) const; unsigned int bitdiff(const bitvector &y) const; //number of differing bits (Hamming distance) - unsigned int population(const unsigned int before=0) const; //number of 1's (Hamming weight) + unsigned int population(const unsigned int before=0) const //number of 1's (Hamming weight), with before>0 only at positions 0..before-1 + { + unsigned int s=0; + if(before) + { +#ifdef DEBUG + if(before>size()) laerror("before parameter beyond the size in population()"); +#endif + int full=before/blockbits; + for(int i=0; is) s=t; resize(s,true);}; diff --git a/doc/bitvector.tex b/doc/bitvector.tex index 7037fbd..3bcb8aa 100644 --- a/doc/bitvector.tex +++ b/doc/bitvector.tex @@ -61,7 +61,9 @@ when the data are shared (count check as in \code{NRVec::operator[]}). \lafunc{bitvector &operator&=(const bitvector_block rhs); operator|=, operator^=}{Bitwise operation with the \emph{first block} only.} \lafunc{bitvector &operator>>=(unsigned int i); bitvector operator>>(unsigned int i) const}{Logical right shift (towards bit 0), length kept.} \lafunc{bitvector &leftshift(unsigned int i, bool autoresize=false); bitvector &operator<<=(unsigned int i); bitvector operator<<(unsigned int i) const}{Logical left shift; \code{<<} and \code{<<=} enlarge the vector by $i$ bits (autoresize), \code{leftshift} by default keeps the length and drops the top bits.} -\lafunc{unsigned int population(const unsigned int before=0) const}{Hamming weight (number of ones); \code{before}$\neq0$ is not implemented (laerror).} +\lafunc{unsigned int population(const unsigned int before=0) const}{Hamming weight (number of ones); with \code{before}$\neq0$ only the ones at the positions $0,\ldots,$\code{before}$-1$ are +counted (\code{laerror} if \code{before} exceeds the size, checked only with \code{DEBUG}). Inline, using the hardware population +count (\code{__builtin_popcountll}) with gcc.} \lafunc{unsigned int bitdiff(const bitvector &y) const}{Hamming distance (same number of blocks required).} \lafunc{unsigned int nlz() const; unsigned int ntz() const}{Number of leading (from the top bit) and trailing zeros.} \end{lareference}