forked from thomasmueller/xorfilter_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnbit_array.h
More file actions
174 lines (166 loc) · 5.19 KB
/
Copy pathnbit_array.h
File metadata and controls
174 lines (166 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef NBIT_ARRAY_H_
#define NBIT_ARRAY_H_
//namespace nbit_array {
template <typename ItemType>
class UIntArray {
size_t byteCount;
ItemType* data;
public:
UIntArray(size_t size) {
byteCount = sizeof(ItemType[size]);
data = new ItemType[size]();
}
~UIntArray() {
delete[] data;
}
inline ItemType get(size_t index) {
return data[index];
}
inline void set(size_t index, ItemType value) {
data[index] = value;
}
void bulkSet(uint16_t* source, size_t length) {
for(int i = 0; i < length; i++) {
data[i] = source[i];
}
}
inline ItemType mask(ItemType fingerprint) {
return fingerprint;
}
size_t getByteCount() {
return byteCount;
}
};
class UInt12Array {
size_t byteCount;
uint8_t* data;
public:
UInt12Array(size_t size) {
byteCount = size * 3 / 2 + 32;
data = new uint8_t[byteCount]();
}
~UInt12Array() {
delete[] data;
}
// the returned value may contain other high-order bits;
// call mask() to clear them
inline uint32_t get(size_t index) {
size_t firstBytePos = (index * 3) / 2;
uint16_t word;
memcpy(&word, data + firstBytePos, sizeof(uint16_t));
return word >> ((index & 1) << 2);
}
void bulkSet(uint16_t* source, size_t length) {
assert((length / 2)*3 + (length % 1) * 2 <= byteCount);
size_t i = 0, j = 0;
for(; i + 1 < length;) {
uint32_t a = source[i++];
uint32_t b = source[i++];
data[j++] = (uint8_t) (a);
data[j++] = (uint8_t) ((a >> 8) | (b << 4));
data[j++] = (uint8_t) (b >> 4);
}
if(i < length) {
uint32_t a = source[i++];
uint32_t b = 0; // duh
data[j++] = (uint8_t) (a);
data[j++] = (uint8_t) ((a >> 8) | (b << 4));
}
}
inline void set(size_t index, uint32_t value) {
size_t wordpos = (index >> 1) * 3;
unsigned int wp = (index & 1) * 12;
size_t offval = (value & 0xfff) << wp;
size_t offmask = 0xfff << wp;
uint32_t word;
memcpy(&word, data + wordpos, sizeof(uint32_t));
// no need for word & ~(offmask) if it's always 0 at the beginning
// word = (word) | offval;
word = (word & ~(offmask)) | offval;
memcpy(data + wordpos, &word, sizeof(uint32_t));
}
inline uint32_t mask(uint32_t fingerprint) {
return fingerprint & 0xfff;
}
size_t getByteCount() {
return byteCount;
}
};
class UInt10Array {
size_t byteCount;
uint64_t* data;
public:
UInt10Array(size_t size) {
byteCount = size / 6 * 8;
data = new uint64_t[(byteCount + 7) / 8]();
}
~UInt10Array() {
delete[] data;
}
// the returned value may contain other high-order bits;
// call mask() to clear them
inline uint32_t get(size_t index) {
uint64_t x = data[index / 6];
// return (x >> (10 * (index % 6)));
int m = 3 * (index % 2) + (index % 3);
return x >> (10 * m);
}
void bulkSet(uint16_t* source, size_t length) {
for(size_t index = 0; index < length; index++) {
set(index, source[index]);
}
}
inline void set(size_t index, uint32_t value) {
// data[index / 6] |= ((uint64_t) value & 0x3ff) << (10 * (index % 6));
int m = 3 * (index % 2) + (index % 3);
data[index / 6] |= ((uint64_t) value & 0x3ff) << (10 * m);
}
inline uint32_t mask(uint32_t fingerprint) {
return fingerprint & 0x3ff;
}
size_t getByteCount() {
return byteCount;
}
};
template <typename ItemType, size_t bitsPerEntry, uint32_t bitMask = (1 << bitsPerEntry) - 1>
class NBitArray {
size_t byteCount;
uint8_t* data;
public:
NBitArray(size_t size) {
byteCount = (size * bitsPerEntry + 63 + 128) / 64 * 64 / 8;
data = new uint8_t[byteCount]();
}
~NBitArray() {
delete[] data;
}
inline ItemType get(size_t index) {
size_t bitPos = index * bitsPerEntry;
size_t firstBytePos = (size_t) (bitPos >> 3);
uint32_t word = __builtin_bswap32(*((uint32_t*) (data + firstBytePos))) >> 8;
return (ItemType) ((word >> (24 - bitsPerEntry - (bitPos & 7))) & bitMask);
}
void bulkSet(uint16_t* source, size_t length) {
for(size_t i = 0; i < length; i++) {
set(i, source[i]);
}
}
inline void set(size_t index, ItemType value) {
size_t bitPos = index * bitsPerEntry;
size_t firstBytePos = (size_t) (bitPos >> 3);
uint32_t word = __builtin_bswap32(*((uint32_t*) (data + firstBytePos))) >> 8;
word &= ~(bitMask << (24 - bitsPerEntry - (bitPos & 7)));
word |= ((value & bitMask) << (24 - bitsPerEntry - (bitPos & 7)));
data[firstBytePos] = (uint8_t) (word >> 16);
data[firstBytePos + 1] = (uint8_t) (word >> 8);
data[firstBytePos + 2] = (uint8_t) word;
}
inline ItemType mask(ItemType fingerprint) {
return fingerprint & bitMask;
}
size_t getByteCount() {
return byteCount;
}
};
// } // namespace n_bit_array
#endif // NBIT_ARRAY_H_