From 5ea385fc3004414e3ae166a6c0cd0458004f46cd Mon Sep 17 00:00:00 2001 From: jiri Date: Fri, 31 Mar 2006 19:01:14 +0000 Subject: [PATCH] *** empty log message *** --- bitvector.cc | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ bitvector.h | 22 +++++++-- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/bitvector.cc b/bitvector.cc index 43dba55..aac2cfd 100644 --- a/bitvector.cc +++ b/bitvector.cc @@ -10,8 +10,131 @@ return s; istream & operator>>(istream &s, bitvector &x) { bool a; +x.copyonwrite(); for(unsigned int i=0; i>a; x.assign(i,a);} return s; } +//implemented so that vectors of different length are considered different automatically +bool bitvector::operator!=(const bitvector &rhs) const +{ +if(nn!=rhs.nn || modulo!=rhs.modulo) return 1; +if(v==rhs.v) return 0; +if(!modulo) return memcmp(v,rhs.v,nn*sizeof(bitvector_block)); +if(memcmp(v,rhs.v,(nn-1)*sizeof(bitvector_block))) return 1; +bitvector_block a=v[nn-1]; +bitvector_block b=rhs.v[nn-1]; +//zero out the irrelevant bits +bitvector_block mask= ~((bitvector_block)0); +mask <<=modulo; +mask = ~mask; +a&=mask; b&=mask; +return a!=b; +} + + +bool bitvector::operator>(const bitvector &rhs) const +{ +if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("at the moment only bitvectors of the same length comparable"); +if(v==rhs.v) return 0; +if(!modulo) return memcmp(v,rhs.v,nn*sizeof(bitvector_block)>0); +int r; +if((r=memcmp(v,rhs.v,(nn-1)*sizeof(bitvector_block)))) return r>0; +bitvector_block a=v[nn-1]; +bitvector_block b=rhs.v[nn-1]; +//zero out the irrelevant bits +bitvector_block mask= ~((bitvector_block)0); +mask <<=modulo; +mask = ~mask; +a&=mask; b&=mask; +return a>b; +} + +bool bitvector::operator<(const bitvector &rhs) const +{ +if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("at the moment only bitvectors of the same length comparable"); +if(v==rhs.v) return 0; +if(!modulo) return memcmp(v,rhs.v,nn*sizeof(bitvector_block)<0); +int r; +if((r=memcmp(v,rhs.v,(nn-1)*sizeof(bitvector_block)))) return r<0; +bitvector_block a=v[nn-1]; +bitvector_block b=rhs.v[nn-1]; +//zero out the irrelevant bits +bitvector_block mask= ~((bitvector_block)0); +mask <<=modulo; +mask = ~mask; +a&=mask; b&=mask; +return a>1)&0x55555555); +x = (x&0x33333333) + ((x>>2)&0x33333333); +x=(x + (x>>4))&0x0f0f0f0f; +x+= (x>>8); +x+= (x>>16); +return x&0x3f; +} +#endif + + +unsigned int bitvector::population(const unsigned int before) const +{ +//@@@before +int i; +unsigned int s=0; +for(i=0; i() {}; explicit bitvector (const unsigned int n):NRVec((n+blockbits-1)/blockbits) {modulo=n%blockbits;}; bitvector (const bitvector_block a, const unsigned int n):NRVec(a,(n+blockbits-1)/blockbits) {modulo=n%blockbits;}; - bitvector(const bitvector &rhs) : NRVec(rhs) {}; + bitvector(const bitvector &rhs) : NRVec(rhs) {modulo=rhs.modulo;}; + //operator= seems to be correctly synthetized by the compiler //override dereferencing to address single bits, is however possible //only in the const context (otherwise we would have to define a type which, when assigned to, changes a single bit - possible but probably inefficient) void resize(const unsigned int n) {NRVec::resize((n+blockbits-1)/blockbits); modulo=n%blockbits;}; @@ -36,8 +37,23 @@ public: const bool assign(const unsigned int i, const bool r) {if(r) set(i); else reset(i); return r;}; void clear() {memset(v,0,nn*sizeof(bitvector_block));}; void fill() {memset(v,0xff,nn*sizeof(bitvector_block));}; - const bool operator!=(const bitvector &rhs) const {}; - const bool operator==(const bitvector &rhs) const {return !(*this != rhs);}; + bool operator!=(const bitvector &rhs) const; + bool operator==(const bitvector &rhs) const {return !(*this != rhs);}; + bool operator>(const bitvector &rhs) const; + bool operator<(const bitvector &rhs) const; + bool operator>=(const bitvector &rhs) const {return !(*this < rhs);}; + bool operator<=(const bitvector &rhs) const {return !(*this > rhs);}; + bitvector operator~() const; + bitvector& operator&=(const bitvector &rhs); + bitvector& operator|=(const bitvector &rhs); + bitvector& operator^=(const bitvector &rhs); + bitvector operator&(const bitvector &rhs) const {return bitvector(*this) &= rhs;}; + bitvector operator|(const bitvector &rhs) const {return bitvector(*this) |= rhs;}; + bitvector operator^(const bitvector &rhs) const {return bitvector(*this) ^= rhs;}; + unsigned int population(const unsigned int before=0) const; //@@@number of 1's + //extended, truncated const i.e. not on *this but return new entity, take care of modulo's bits + //logical shifts <<= >>= << >> not implemented yet + //logical rotations not implemented yet };