HOG2
utils
BitVector.cpp
Go to the documentation of this file.
1
/*
2
* $Id: BitVector.cpp
3
* hog2
4
*
5
* Created by Nathan Sturtevant on 09/18/06.
6
* Modified by Nathan Sturtevant on 02/29/20.
7
*
8
* This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9
*
10
*/
11
12
#include <cstdlib>
13
#include <cstdio>
14
#include "
BitVector.h
"
15
#include <cinttypes>
16
#include <cassert>
17
18
BitVector::BitVector
(uint64_t _size)
19
{
20
true_size
= _size;
21
size
= (_size>>
storageBitsPower
)+1;
22
storage
=
new
storageElement
[
size
];
23
for
(
size_t
x = 0; x <
size
; x++)
24
storage
[x] = 0;
25
}
26
27
BitVector::BitVector
(uint64_t size,
const
char
*fname)
28
{
29
Load
(fname);
30
assert(
size
==
true_size
);
31
}
32
33
BitVector::~BitVector
()
34
{
35
delete
[]
storage
;
36
}
37
38
void
BitVector::Save
(
const
char
*file)
39
{
40
FILE *f = fopen(file,
"w+"
);
41
if
(f == 0)
42
{
43
printf(
"File write error (%s)\n"
, file);
44
exit(0);
45
}
46
fwrite(&
true_size
,
sizeof
(
true_size
), 1, f);
47
fwrite(
storage
,
sizeof
(
storageElement
),
size
, f);
48
fclose(f);
49
}
50
51
void
BitVector::Load
(
const
char
*file)
52
{
53
delete
[]
storage
;
54
FILE *f = fopen(file,
"r"
);
55
if
(f == 0)
56
{
57
printf(
"File read error (%s)\n"
, file);
58
exit(0);
59
}
60
//fread(&size, sizeof(size), 1, f);
61
fread(&
true_size
,
sizeof
(
true_size
), 1, f);
62
printf(
"Loading %"
PRId64
" entries\n"
,
true_size
);
63
// allocate storage
64
size
= (
true_size
>>
storageBitsPower
)+1;
65
66
storage
=
new
storageElement
[
size
];
67
// for (int x = 0; x < size; x++)
68
// storage[x] = 0;
69
70
// TODO:
71
fread(
storage
,
sizeof
(
storageElement
),
size
, f);
72
fclose(f);
73
}
74
75
void
BitVector::clear
()
76
{
77
for
(
int
x = 0; x <
size
; x++)
78
storage
[x] = 0;
79
}
80
81
//BitVector *BitVector::Clone()
82
//{
83
// BitVector *bv = new BitVector(true_size);
84
// bv->Merge(this);
85
// return bv;
86
//}
87
88
void
BitVector::Set
(uint64_t index,
bool
value)
89
{
90
if
((index>>
storageBitsPower
) >
size
) {
91
printf(
"SET %"
PRId64
" OUT OF RANGE\n"
, index);
92
exit(0);
93
}
94
if
(value)
95
storage
[index>>
storageBitsPower
] =
storage
[index>>
storageBitsPower
]|(1<<(index&
storageMask
));
96
else
97
storage
[index>>
storageBitsPower
] =
storage
[index>>
storageBitsPower
]&(~(1<<(index&
storageMask
)));
98
}
99
100
101
//void BitVector::Merge(BitVector *bv)
102
//{
103
// if (bv == 0) return;
104
// if (bv->size != size) {
105
// printf("Error; can't merge vectors of different sizes (%d/%d)\n", bv->true_size, true_size);
106
// return;
107
// }
108
// for (int x = 0; x < size; x++) storage[x] |= bv->storage[x];
109
//}
110
111
bool
BitVector::Equals
(
BitVector
*bv)
112
{
113
if
(bv->
size
!=
size
)
return
false
;
114
for
(
size_t
x = 0; x <
size
; x++)
115
if
(
storage
[x] != bv->
storage
[x])
116
return
false
;
117
return
true
;
118
}
119
120
uint64_t
BitVector::GetNumSetBits
()
121
{
122
uint64_t sum = 0;
123
for
(
size_t
x = 0; x <
size
; x++)
124
{
125
storageElement
iter =
storage
[x];
126
while
(iter) {
127
sum++;
128
iter &= (iter-1);
129
}
130
}
131
return
sum;
132
}
storageMask
const int storageMask
Definition:
BitVector.h:31
storageElement
uint8_t storageElement
An efficient bit-wise vector implementation.
Definition:
BitVector.h:28
BitVector::GetNumSetBits
uint64_t GetNumSetBits()
Definition:
BitVector.cpp:120
BitVector::storage
storageElement * storage
Definition:
BitVector.h:50
BitVector::~BitVector
~BitVector()
Definition:
BitVector.cpp:33
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
BitVector::Save
void Save(const char *)
Definition:
BitVector.cpp:38
BitVector::BitVector
BitVector(uint64_t size)
Definition:
BitVector.cpp:18
BitVector.h
storageBitsPower
const int storageBitsPower
Definition:
BitVector.h:30
Generated by
1.8.17