diff --git a/bitvector.cc b/bitvector.cc index ad3b12e..ad5180b 100644 --- a/bitvector.cc +++ b/bitvector.cc @@ -113,34 +113,38 @@ 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; +//numeric order: bit i has weight 2^i, compare from the most significant block 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; +if(modulo) //zero out the irrelevant bits + { + bitvector_block mask= ~((bitvector_block)0); + mask <<=modulo; + mask = ~mask; + a&=mask; b&=mask; + } +if(a!=b) return a>b; +for(int i=nn-2; i>=0; --i) if(v[i]!=rhs.v[i]) return v[i]>rhs.v[i]; +return false; } 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; +//numeric order: bit i has weight 2^i, compare from the most significant block 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=0; --i) if(v[i]!=rhs.v[i]) return v[i]>=(unsigned int i) { if(i==0) return *this; copyonwrite(); +zero_padding(); //garbage in the padding would be shifted into the valid range unsigned int imod = i%blockbits; unsigned int ishift = i/blockbits; for(int dest=0; dest>(i%blockbits))&1ULL;}; const bool get(const unsigned int i) const {return (*this)[i];}; bitvector_block getblock(const unsigned int i) const {return v[i];}; //integer interpretation - void setblock(const unsigned int i, const bitvector_block b) {v[i]=b;}; + void setblock(const unsigned int i, const bitvector_block b) { +#ifdef DEBUG + if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first"); +#endif + v[i]=b;}; //element-level, caller calls copyonwrite() if shared int getblocksize() const {return 8*sizeof(bitvector_block);}; void set(const unsigned int i) { @@ -84,6 +88,7 @@ public: std::cout<<"bitvector index 0 < "< 1, call copyonwrite() first"); #endif v[i/blockbits] |= (1UL<<(i%blockbits)); }; @@ -95,6 +100,7 @@ if(i>=size()) std::cout<<"bitvector index 0 < "< 1, call copyonwrite() first"); #endif v[i/blockbits] &= ~(1UL<<(i%blockbits)); }; @@ -106,6 +112,7 @@ if(i>=size()) std::cout<<"bitvector index 0 < "< 1, call copyonwrite() first"); #endif v[i/blockbits] ^= (1UL<<(i%blockbits)); }; @@ -129,9 +136,21 @@ if(i>=size()) bitvector& operator&=(const bitvector &rhs); bitvector& operator|=(const bitvector &rhs); bitvector& operator^=(const bitvector &rhs); - bitvector& operator&=(const bitvector_block rhs) {v[0]&=rhs; return *this;}; - bitvector& operator|=(const bitvector_block rhs) {v[0]|=rhs; return *this;}; - bitvector& operator^=(const bitvector_block rhs) {v[0]^=rhs; return *this;}; + bitvector& operator&=(const bitvector_block rhs) { //element-level (first block only), caller calls copyonwrite() if shared +#ifdef DEBUG + if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first"); +#endif + v[0]&=rhs; return *this;}; + bitvector& operator|=(const bitvector_block rhs) { +#ifdef DEBUG + if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first"); +#endif + v[0]|=rhs; return *this;}; + bitvector& operator^=(const bitvector_block rhs) { +#ifdef DEBUG + if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first"); +#endif + v[0]^=rhs; return *this;}; bitvector& operator+=(const bitvector &rhs) {return (*this)^=rhs;}; //addition modulo 2 bitvector& operator-=(const bitvector &rhs) {return (*this)^=rhs;}; //subtraction modulo 2 bitvector operator&(const bitvector &rhs) const {return bitvector(*this) &= rhs;};