Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / bitfield.h
blob4f6369071ab696af9b3d1f3bb0a85e5e380265d7
1 #ifndef _WDL_BITFIELD_H_
2 #define _WDL_BITFIELD_H_
4 #include "heapbuf.h"
6 class WDL_BitField // ultra simple bit field
8 public:
9 bool SetSize(int sz) // clears state
11 void *b=m_hb.ResizeOK((sz+7)/8);
12 if (b) memset(b,0,m_hb.GetSize());
13 return !!b;
15 int GetApproxSize() const { return m_hb.GetSize()*8; } // may return slightly greater than the size set
17 bool IsSet(unsigned int idx) const
19 const unsigned char mask = 1<<(idx&7);
20 idx>>=3;
21 return idx < m_hb.GetSize() && (((unsigned char *)m_hb.Get())[idx]&mask);
23 void Set(unsigned int idx)
25 const unsigned char mask = 1<<(idx&7);
26 idx>>=3;
27 if (idx < m_hb.GetSize()) ((unsigned char *)m_hb.Get())[idx] |= mask;
30 private:
31 WDL_HeapBuf m_hb;
34 #endif //_WDL_BITFIELD_H_