*** empty log message ***
This commit is contained in:
		
							parent
							
								
									8f8b5b855f
								
							
						
					
					
						commit
						5ea385fc30
					
				
							
								
								
									
										123
									
								
								bitvector.cc
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								bitvector.cc
									
									
									
									
									
								
							| @ -10,8 +10,131 @@ return s; | |||||||
| istream & operator>>(istream  &s, bitvector &x) | istream & operator>>(istream  &s, bitvector &x) | ||||||
| { | { | ||||||
| bool a; | bool a; | ||||||
|  | x.copyonwrite(); | ||||||
| for(unsigned int i=0; i<x.size(); ++i) {s >>a; x.assign(i,a);} | for(unsigned int i=0; i<x.size(); ++i) {s >>a; x.assign(i,a);} | ||||||
| return s; | 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<b; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | bitvector bitvector::operator~() const | ||||||
|  | { | ||||||
|  | bitvector r((*this).size()); | ||||||
|  | for(int i=0; i<nn; ++i) r.v[i] = ~v[i]; | ||||||
|  | return r; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bitvector& bitvector::operator&=(const bitvector &rhs) | ||||||
|  | { | ||||||
|  | #ifdef DEBUG | ||||||
|  | if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("operation on incompatible bitvectors"); | ||||||
|  | #endif | ||||||
|  | copyonwrite(); | ||||||
|  | for(int i=0; i<nn; ++i) v[i] &= rhs.v[i]; | ||||||
|  | return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bitvector& bitvector::operator|=(const bitvector &rhs) | ||||||
|  | { | ||||||
|  | #ifdef DEBUG | ||||||
|  | if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("operation on incompatible bitvectors"); | ||||||
|  | #endif | ||||||
|  | copyonwrite(); | ||||||
|  | for(int i=0; i<nn; ++i) v[i] |= rhs.v[i]; | ||||||
|  | return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bitvector& bitvector::operator^=(const bitvector &rhs) | ||||||
|  | { | ||||||
|  | #ifdef DEBUG | ||||||
|  | if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("operation on incompatible bitvectors"); | ||||||
|  | #endif | ||||||
|  | copyonwrite(); | ||||||
|  | for(int i=0; i<nn; ++i) v[i] ^= rhs.v[i]; | ||||||
|  | return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /*number of ones in a binary number, from "Hacker's delight" book*/ | ||||||
|  | #ifdef LONG_IS_32  | ||||||
|  | static unsigned long 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; | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | unsigned int bitvector::population(const unsigned int before) const | ||||||
|  | { | ||||||
|  | //@@@before
 | ||||||
|  | int i; | ||||||
|  | unsigned int s=0; | ||||||
|  | for(i=0; i<nn-1; ++i) s+=word_popul(v[i]); | ||||||
|  | bitvector_block a=v[nn-1]; | ||||||
|  | if(modulo) | ||||||
|  | 	{ | ||||||
|  | 	bitvector_block mask= ~((bitvector_block)0); | ||||||
|  | 	mask <<=modulo; | ||||||
|  | 	a &= ~mask; | ||||||
|  | 	} | ||||||
|  | return s+word_popul(a); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								bitvector.h
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								bitvector.h
									
									
									
									
									
								
							| @ -23,7 +23,8 @@ public: | |||||||
| 	bitvector() : NRVec<bitvector_block>() {}; | 	bitvector() : NRVec<bitvector_block>() {}; | ||||||
| 	explicit bitvector (const unsigned int n):NRVec<bitvector_block>((n+blockbits-1)/blockbits) {modulo=n%blockbits;}; | 	explicit bitvector (const unsigned int n):NRVec<bitvector_block>((n+blockbits-1)/blockbits) {modulo=n%blockbits;}; | ||||||
| 	bitvector (const bitvector_block a, const unsigned int n):NRVec<bitvector_block>(a,(n+blockbits-1)/blockbits) {modulo=n%blockbits;}; | 	bitvector (const bitvector_block a, const unsigned int n):NRVec<bitvector_block>(a,(n+blockbits-1)/blockbits) {modulo=n%blockbits;}; | ||||||
| 	bitvector(const bitvector &rhs) : NRVec<bitvector_block>(rhs) {}; | 	bitvector(const bitvector &rhs) : NRVec<bitvector_block>(rhs) {modulo=rhs.modulo;}; | ||||||
|  | 	//operator= seems to be correctly synthetized by the compiler
 | ||||||
| 	//override dereferencing to address single bits, is however possible
 | 	//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)
 | 	//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<bitvector_block>::resize((n+blockbits-1)/blockbits); modulo=n%blockbits;}; | 	void resize(const unsigned int n) {NRVec<bitvector_block>::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;}; | 	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 clear() {memset(v,0,nn*sizeof(bitvector_block));}; | ||||||
| 	void fill() {memset(v,0xff,nn*sizeof(bitvector_block));}; | 	void fill() {memset(v,0xff,nn*sizeof(bitvector_block));}; | ||||||
| 	const bool operator!=(const bitvector &rhs) const {}; | 	bool operator!=(const bitvector &rhs) const; | ||||||
| 	const bool operator==(const bitvector &rhs) const {return !(*this != rhs);}; | 	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
 | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user