bugfixes in bitvector
This commit is contained in:
1 parent
4bc7be4265
commit
a95c7bfefe
2 files changed
+38
-14
No files matched your search
+15
-10
@@ -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(nn!=rhs.nn || modulo!=rhs.modulo) laerror("at the moment only bitvectors of the same length comparable");
|
||||||
if(v==rhs.v) return 0;
|
if(v==rhs.v) return 0;
|
||||||
if(!modulo) return memcmp(v,rhs.v,nn*sizeof(bitvector_block)>0);
|
//numeric order: bit i has weight 2^i, compare from the most significant block
|
||||||
int r;
|
|
||||||
if((r=memcmp(v,rhs.v,(nn-1)*sizeof(bitvector_block)))) return r>0;
|
|
||||||
bitvector_block a=v[nn-1];
|
bitvector_block a=v[nn-1];
|
||||||
bitvector_block b=rhs.v[nn-1];
|
bitvector_block b=rhs.v[nn-1];
|
||||||
//zero out the irrelevant bits
|
if(modulo) //zero out the irrelevant bits
|
||||||
|
{
|
||||||
bitvector_block mask= ~((bitvector_block)0);
|
bitvector_block mask= ~((bitvector_block)0);
|
||||||
mask <<=modulo;
|
mask <<=modulo;
|
||||||
mask = ~mask;
|
mask = ~mask;
|
||||||
a&=mask; b&=mask;
|
a&=mask; b&=mask;
|
||||||
return a>b;
|
}
|
||||||
|
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
|
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(nn!=rhs.nn || modulo!=rhs.modulo) laerror("at the moment only bitvectors of the same length comparable");
|
||||||
if(v==rhs.v) return 0;
|
if(v==rhs.v) return 0;
|
||||||
if(!modulo) return memcmp(v,rhs.v,nn*sizeof(bitvector_block)<0);
|
//numeric order: bit i has weight 2^i, compare from the most significant block
|
||||||
int r;
|
|
||||||
if((r=memcmp(v,rhs.v,(nn-1)*sizeof(bitvector_block)))) return r<0;
|
|
||||||
bitvector_block a=v[nn-1];
|
bitvector_block a=v[nn-1];
|
||||||
bitvector_block b=rhs.v[nn-1];
|
bitvector_block b=rhs.v[nn-1];
|
||||||
//zero out the irrelevant bits
|
if(modulo) //zero out the irrelevant bits
|
||||||
|
{
|
||||||
bitvector_block mask= ~((bitvector_block)0);
|
bitvector_block mask= ~((bitvector_block)0);
|
||||||
mask <<=modulo;
|
mask <<=modulo;
|
||||||
mask = ~mask;
|
mask = ~mask;
|
||||||
a&=mask; b&=mask;
|
a&=mask; b&=mask;
|
||||||
return a<b;
|
}
|
||||||
|
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;
|
if(i==0) return *this;
|
||||||
copyonwrite();
|
copyonwrite();
|
||||||
|
zero_padding(); //garbage in the padding would be shifted into the valid range
|
||||||
unsigned int imod = i%blockbits;
|
unsigned int imod = i%blockbits;
|
||||||
unsigned int ishift = i/blockbits;
|
unsigned int ishift = i/blockbits;
|
||||||
for(int dest=0; dest<nn; ++dest)
|
for(int dest=0; dest<nn; ++dest)
|
||||||
|
|||||||
+23
-4
@@ -74,7 +74,11 @@ public:
|
|||||||
const bool operator[](const unsigned int i) const {return (v[i/blockbits] >>(i%blockbits))&1ULL;};
|
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];};
|
const bool get(const unsigned int i) const {return (*this)[i];};
|
||||||
bitvector_block getblock(const unsigned int i) const {return v[i];}; //integer interpretation
|
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);};
|
int getblocksize() const {return 8*sizeof(bitvector_block);};
|
||||||
void set(const unsigned int i)
|
void set(const unsigned int i)
|
||||||
{
|
{
|
||||||
@@ -84,6 +88,7 @@ public:
|
|||||||
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
||||||
laerror("bitvector index out of range in");
|
laerror("bitvector index out of range in");
|
||||||
}
|
}
|
||||||
|
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first");
|
||||||
#endif
|
#endif
|
||||||
v[i/blockbits] |= (1UL<<(i%blockbits));
|
v[i/blockbits] |= (1UL<<(i%blockbits));
|
||||||
};
|
};
|
||||||
@@ -95,6 +100,7 @@ if(i>=size())
|
|||||||
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
||||||
laerror("bitvector index out of range in");
|
laerror("bitvector index out of range in");
|
||||||
}
|
}
|
||||||
|
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first");
|
||||||
#endif
|
#endif
|
||||||
v[i/blockbits] &= ~(1UL<<(i%blockbits));
|
v[i/blockbits] &= ~(1UL<<(i%blockbits));
|
||||||
};
|
};
|
||||||
@@ -106,6 +112,7 @@ if(i>=size())
|
|||||||
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
std::cout<<"bitvector index 0 < "<<i<<" "<<size()<<std::endl;
|
||||||
laerror("bitvector index out of range in");
|
laerror("bitvector index out of range in");
|
||||||
}
|
}
|
||||||
|
if(_LA_count_check && *count != 1) laerror("bitvector element write with count > 1, call copyonwrite() first");
|
||||||
#endif
|
#endif
|
||||||
v[i/blockbits] ^= (1UL<<(i%blockbits));
|
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 &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) { //element-level (first block only), caller calls copyonwrite() if shared
|
||||||
bitvector& operator|=(const bitvector_block rhs) {v[0]|=rhs; return *this;};
|
#ifdef DEBUG
|
||||||
bitvector& operator^=(const bitvector_block rhs) {v[0]^=rhs; return *this;};
|
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;}; //addition modulo 2
|
||||||
bitvector& operator-=(const bitvector &rhs) {return (*this)^=rhs;}; //subtraction modulo 2
|
bitvector& operator-=(const bitvector &rhs) {return (*this)^=rhs;}; //subtraction modulo 2
|
||||||
bitvector operator&(const bitvector &rhs) const {return bitvector(*this) &= rhs;};
|
bitvector operator&(const bitvector &rhs) const {return bitvector(*this) &= rhs;};
|
||||||
|
|||||||
Reference in new issue
Block a user