added some convenience functions for bitvector

This commit is contained in:
Jiri Pittner 2022-06-22 19:41:25 +02:00
parent 2621f444e1
commit 631ec298f5
1 changed files with 23 additions and 0 deletions

View File

@ -51,6 +51,9 @@ public:
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;};
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;};
@ -80,6 +83,26 @@ public:
void write(int fd, bool dimensions=1, bool transp=0);
};
//expand to separate bytes or ints
template <typename T>
void bitvector_expand(const bitvector &v, NRVec<T> &r)
{
int n=v.size();
r.resize(n);
r.clear();
for(int i=0; i<n; ++i) if(v[i]) r[i]=1;
}
template <typename T>
void bitvector_compress(bitvector &r, const NRVec<T> &v)
{
int n=v.size();
r.resize(n);
r.clear();
for(int i=0; i<n; ++i) if(v[i]) r.set(i);
}
extern std::ostream & operator<<(std::ostream &s, const bitvector &x);
extern std::istream & operator>>(std::istream &s, bitvector &x);