/* LA: linear algebra C++ interface library Copyright (C) 2008 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>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 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::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