forked from nmeisburger/LSH-Tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservoir.cpp
60 lines (49 loc) · 1.21 KB
/
Reservoir.cpp
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
#include "Reservoir.h"
Reservoir::Reservoir() {
size = RESERVOIR_SIZE;
count = 0;
lock = new omp_lock_t();
omp_init_lock(lock);
reservoir = new unsigned int[size];
for (size_t i = 0; i < size; i++) {
reservoir[i] = EMPTY;
}
}
void Reservoir::add(unsigned int item) {
omp_set_lock(lock);
if (count < size) {
reservoir[count] = item;
count++;
} else {
unsigned int loc = rand() % count;
if (loc < size) {
reservoir[loc] = item;
}
count++;
}
omp_unset_lock(lock);
}
void Reservoir::retrieve(unsigned int *buffer) {
omp_set_lock(lock);
std::copy(reservoir, reservoir + size, buffer);
omp_unset_lock(lock);
}
unsigned int Reservoir::get_size() { return size; }
unsigned int Reservoir::get_count() { return count; }
void Reservoir::reset() {
for (size_t i = 0; i < size; i++) {
reservoir[i] = EMPTY;
}
count = 0;
}
void Reservoir::view() {
printf("Reservoir [%d/%d] ", count, size);
for (size_t i = 0; i < std::min(count, size); i++) {
printf("%u ", reservoir[i]);
}
printf("\n");
}
Reservoir::~Reservoir() {
omp_destroy_lock(lock);
delete[] reservoir;
}