HOG2
BitVector.h
Go to the documentation of this file.
1 // HOG File
2 /*
3  * $Id: BitVector.h
4  * hog2
5  *
6  * Created by Nathan Sturtevant on 09/18/06.
7  * Modified by Nathan Sturtevant on 02/29/20.
8  *
9  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
10  *
11  */
12 
13 #ifndef _BITVECTOR_
14 #define _BITVECTOR_
15 
16 #include <stdint.h>
17 #include "MMapUtil.h"
18 
23 //typedef uint32_t storageElement;
24 //const int storageBits = 32;
25 //const int storageBitsPower = 5;
26 //const int storageMask = 0x1F;
27 
28 typedef uint8_t storageElement;
29 const int storageBits = 8;
30 const int storageBitsPower = 3;
31 const int storageMask = 0x7;
32 
33 
34 class BitVector {
35 public:
36  BitVector(uint64_t size);
37  BitVector(uint64_t size, const char *);
38  ~BitVector();
39  void clear();
40  uint64_t GetSize() { return true_size; }
41  bool Get(uint64_t index) const;
42  void Set(uint64_t index, bool value);
43  void SetTrue(uint64_t index);
44  void Save(const char *);
45  void Load(const char *);
46  bool Equals(BitVector *);
47  uint64_t GetNumSetBits();
48 private:
49  uint64_t size, true_size;
51  int fd;
52 };
53 
54 inline bool BitVector::Get(uint64_t index) const
55 {
56  return (((storage[index>>storageBitsPower])>>(index&storageMask))&0x1);
57 }
58 
59 inline void BitVector::SetTrue(uint64_t index)
60 {
61  storage[index>>storageBitsPower] = storage[index>>storageBitsPower]|(1<<(index&storageMask));
62 }
63 
64 #endif
storageMask
const int storageMask
Definition: BitVector.h:31
storageElement
uint8_t storageElement
An efficient bit-wise vector implementation.
Definition: BitVector.h:28
BitVector::GetSize
uint64_t GetSize()
Definition: BitVector.h:40
BitVector::GetNumSetBits
uint64_t GetNumSetBits()
Definition: BitVector.cpp:120
BitVector::fd
int fd
Definition: BitVector.h:51
BitVector::storage
storageElement * storage
Definition: BitVector.h:50
BitVector::~BitVector
~BitVector()
Definition: BitVector.cpp:33
BitVector::SetTrue
void SetTrue(uint64_t index)
Definition: BitVector.h:59
BitVector::true_size
uint64_t true_size
Definition: BitVector.h:49
BitVector::Equals
bool Equals(BitVector *)
Definition: BitVector.cpp:111
BitVector::size
uint64_t size
Definition: BitVector.h:49
BitVector
Definition: BitVector.h:34
BitVector::Set
void Set(uint64_t index, bool value)
Definition: BitVector.cpp:88
BitVector::Load
void Load(const char *)
Definition: BitVector.cpp:51
BitVector::clear
void clear()
Definition: BitVector.cpp:75
MMapUtil.h
BitVector::Save
void Save(const char *)
Definition: BitVector.cpp:38
BitVector::BitVector
BitVector(uint64_t size)
Definition: BitVector.cpp:18
storageBits
const int storageBits
Definition: BitVector.h:29
BitVector::Get
bool Get(uint64_t index) const
Definition: BitVector.h:54
storageBitsPower
const int storageBitsPower
Definition: BitVector.h:30