/* LA: linear algebra C++ interface library Copyright (C) 2008-2023 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "bitvector.h" #include namespace LA { //inefficient I/O operators std::ostream & operator<<(std::ostream &s, const bitvector &x) { for(unsigned int i=0; i>(std::istream &s, bitvector &x) { std::string str; s >> str; x.resize(str.size()); for(unsigned int i=0; i(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=rhs.nn? 0 : rhs.v[i]); return *this; } bitvector& bitvector::operator|=(const bitvector &rhs) { if(size()>1)&0x55555555); x = (x&0x33333333) + ((x>>2)&0x33333333); x=(x + (x>>4))&0x0f0f0f0f; x+= (x>>8); x+= (x>>16); return x&0x3f; } #else //@@@@ use an efficient trick too static unsigned int word_popul(unsigned long x) { unsigned int s=0; for(int i=0; i<64; ++i) { if(x&1) ++s; x>>=1; } return s; } #endif bitvector& bitvector::operator>>=(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>=1; goto L; } static unsigned int ntz64(uint64_t x) { unsigned int n; if(x==0) return 64; n=1; if((x&0xffffffff)==0) {n+=32; x>>=32;} if((x&0xffff)==0) {n+=16; x>>=16;} if((x&0xff)==0) {n+=8; x>>=8;} if((x&0xf)==0) {n+=4; x>>=4;} if((x&0x3)==0) {n+=2; x>>=2;} return n-(x&1); } unsigned int bitvector::nlz() const { int leadblock=nn-1; unsigned int n=0; while(leadblock>0 && v[leadblock] == 0) { --leadblock; n+=blockbits; } n+= nlz64(v[leadblock]); if(modulo) n-= blockbits-modulo; return n; } unsigned int bitvector::ntz() const { int tailblock=0; unsigned int n=0; if(iszero()) return size(); while(tailblock::resize((n+blockbits-1)/blockbits,preserve); modulo=n%blockbits; if(preserve) //clear newly allocated memory { for(int i=old; ireset(i); } else clear(); } bitvector bitvector::division(const bitvector &rhs, bitvector &remainder) const { if(rhs.is_zero()) laerror("division by zero binary polynomial"); if(is_zero() || rhs.is_one()) {remainder.clear(); return *this;} bitvector r(size()); r.clear(); remainder= *this; remainder.copyonwrite(); int rhsd = rhs.degree(); int d; while((d=remainder.degree()) >= rhsd) { unsigned int pos = d-rhsd; r.set(pos); remainder -= (rhs<=rhs.degree()) {big= *this; small=rhs;} else {big=rhs; small= *this;} if(big.is_zero()) { if(small.is_zero()) laerror("two zero arguments in gcd"); return small; } if(small.is_zero()) return big; if(small.is_one()) return small; if(big.is_one()) return big; do { bitvector help=small; small= big%small; big=help; } while(! small.is_zero()); return big; } void bitvector::read(int fd, bool dimensions, bool transp) { if(dimensions) { int r = ::read(fd,&modulo,sizeof(modulo)); if(r!=sizeof(modulo)) laerror("cannot read in bitvector"); } NRVec::get(fd,dimensions,transp); } void bitvector::write(int fd, bool dimensions, bool transp) { if(dimensions) { int r = ::write(fd,&modulo,sizeof(modulo)); if(r!=sizeof(modulo)) laerror("cannot write in bitvector"); } NRVec::put(fd,dimensions,transp); } }//namespace