bugfixes in bitvector

This commit is contained in:
jiri committed 2026-10-28 23:05:50 +01:00
1 parent 4bc7be4265
commit a95c7bfefe
2 files changed
+46 -22

No files matched your search

+23 -18
View File
@@ -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<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;
}
@@ -209,6 +213,7 @@ bitvector& bitvector::operator>>=(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<nn; ++dest)
+23 -4
View File
@@ -74,7 +74,11 @@ public:
const bool operator[](const unsigned int i) const {return (v[i/blockbits] >>(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 < "<<i<<" "<<size()<<std::endl;
laerror("bitvector index out of range in");
}
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first");
#endif
v[i/blockbits] |= (1UL<<(i%blockbits));
};
@@ -95,6 +100,7 @@ if(i>=size())
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
laerror("bitvector index out of range in");
}
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first");
#endif
v[i/blockbits] &= ~(1UL<<(i%blockbits));
};
@@ -106,6 +112,7 @@ if(i>=size())
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
laerror("bitvector index out of range in");
}
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 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;};