From c428d4650c4d1d5158e961a4587e7641eaa5befe Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Wed, 27 Dec 2023 23:24:13 +0100 Subject: [PATCH] implementing some new functionality to bitvecotr --- bitvector.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++------- bitvector.h | 24 ++++++++++++++----- la_random.cc | 1 + la_random.h | 8 +++++++ t.cc | 27 ++++++++++++++++++++- 5 files changed, 111 insertions(+), 16 deletions(-) diff --git a/bitvector.cc b/bitvector.cc index 5056773..c650df5 100644 --- a/bitvector.cc +++ b/bitvector.cc @@ -101,9 +101,7 @@ return r; bitvector& bitvector::operator&=(const bitvector &rhs) { -#ifdef DEBUG -if(nn!=rhs.nn || modulo!=rhs.modulo) laerror("operation on incompatible bitvectors"); -#endif +if(size()>=(unsigned int i) +{ +if(i==0) return *this; +copyonwrite(); +unsigned int imod = i%blockbits; +unsigned int ishift = i/blockbits; +for(int dest=0; dest=nn) v[dest]=0; + else + { + v[dest] = v[src]>>imod; + if(imod && (src+1=0; --dest) + { + int src=dest-ishift; + if(src<0) v[dest]=0; + else + { + v[dest] = v[src]<=0)) v[dest] |= (v[src-1]& (((1ULL<>(blockbits-imod); + } + } + +return *this; +} + + +void bitvector::randomize() +{ +copyonwrite(); +for(int i=0; i + namespace LA { - //compressed storage of large bit vectors -//any reasonable compiler changes the dividions and modulos to shift/and instructions +//let's now use 64-bit blocks exclusively -typedef unsigned long bitvector_block; //should be automatically portable and efficiently use wordlength of each machine (32 vs 64) +typedef uint64_t bitvector_block; #define blockbits (8*sizeof(bitvector_block)) @@ -47,19 +48,21 @@ public: //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;}; + void resize(const unsigned int n, bool preserve=false) {NRVec::resize((n+blockbits-1)/blockbits,preserve); modulo=n%blockbits;}; unsigned int size() const {return (nn*blockbits)-blockbits+(modulo?modulo:blockbits);}; //arguments must be unsigned to keep the resulting assembly code simple and efficient const bool operator[](const unsigned int i) const {return (v[i/blockbits] >>(i%blockbits))&1UL;}; + 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;}; int getblocksize() const {return 8*sizeof(bitvector_block);}; void set(const unsigned int i) {v[i/blockbits] |= (1UL<<(i%blockbits));}; void reset(const unsigned int i) {v[i/blockbits] &= ~(1UL<<(i%blockbits));}; - const bool get(const unsigned int i) {return (v[i/blockbits] >>(i%blockbits))&1UL;}; const bool assign(const unsigned int i, const bool r) {if(r) set(i); else reset(i); return r;}; void clear() {copyonwrite(true); memset(v,0,nn*sizeof(bitvector_block));}; void fill() {memset(v,0xff,nn*sizeof(bitvector_block));}; + bool iszero() const {for(int i=0; i(const bitvector &rhs) const; @@ -70,13 +73,22 @@ public: bitvector& operator&=(const bitvector &rhs); bitvector& operator|=(const bitvector &rhs); bitvector& operator^=(const bitvector &rhs); + 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;}; 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 *this ^ rhs;}; //addition modulo 2 + bitvector operator-(const bitvector &rhs) const {return *this ^ rhs;}; //subtraction modulo 2 unsigned int operator%(const bitvector &y) const; //number of differing bits 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 shifts + bitvector& operator>>=(unsigned int i); + bitvector& leftshift(unsigned int i, bool autoresize=false); + bitvector& operator<<=(unsigned int i) {return leftshift(i,true);}; + bitvector operator>>(unsigned int i) {bitvector r(*this); return r>>=i;}; + bitvector operator<<(unsigned int i) {bitvector r(*this); return r<<=i;}; //logical rotations not implemented yet //unformatted file IO void read(int fd, bool dimensions=1, bool transp=0); diff --git a/la_random.cc b/la_random.cc index ef8b15d..7bca304 100644 --- a/la_random.cc +++ b/la_random.cc @@ -6,5 +6,6 @@ namespace LA { WEAK_SYMBOL double randdouble() {return random()/(1.+RAND_MAX);} WEAK_SYMBOL double randdoublesigned() {return 2.*random()/(1.+RAND_MAX)-1.;} WEAK_SYMBOL int randint32() {return random();} +WEAK_SYMBOL uint64_t randint64() {uint64_t r = random(); r<<=32; r|= random(); return r; } }//namespace diff --git a/la_random.h b/la_random.h index d6e544c..ad4bb58 100644 --- a/la_random.h +++ b/la_random.h @@ -1,11 +1,14 @@ #ifndef _LA_RANDOM_H #define _LA_RANDOM_H +#include + namespace LA { extern double randdouble(); extern double randdoublesigned(); extern int randint32(); +extern uint64_t randint64(); //RANDOM numbers defaulting to standard library but switchable to user's functions #ifndef RANDDOUBLE @@ -20,6 +23,11 @@ extern int randint32(); #define RANDINT32 LA::randint32 #endif +#ifndef RANDINT64 +#define RANDINT64 LA::randint64 +#endif + + #ifdef __GNUC__ #define WEAK_SYMBOL __attribute__((weak)) #else diff --git a/t.cc b/t.cc index 6e8ecae..26dc7b7 100644 --- a/t.cc +++ b/t.cc @@ -2784,7 +2784,7 @@ NRSMat adjperm = adj.permuted(p); cout <<"resorted graph = "<>n; +bitvector v(n); +v.randomize(); +//do{ +// cout <>=1; +//}while(!v.iszero()); + + +for(int i=0; i