-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHASHTCounter.cpp
108 lines (89 loc) · 2.68 KB
/
HASHTCounter.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
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
#include "HASHTCounter.hpp"
namespace mkmh{
using namespace std;
using namespace mkmh;
HASHTCounter::HASHTCounter(){
my_size = 1000000;
counts = new int [my_size];
}
HASHTCounter::HASHTCounter(uint64_t sz){
my_size = sz;
counts = new int [my_size];
}
HASHTCounter::~HASHTCounter(){
delete [] counts;
my_size = 0;
}
string HASHTCounter::to_string(){
stringstream sst;
for (int i = 0; i < my_size; ++i){
sst << counts[i] << endl;
}
return sst.str();
}
void HASHTCounter::write_to_binary(string filename){
ofstream ostr;
ostr.open(filename, ios::out | ios::binary);
ostr << this->my_size;
for (int i = 0; i < my_size; ++i){
ostr << counts[i];
}
ostr.close();
}
void HASHTCounter::print(){
for (int i = 0; i < my_size; i++){
cout << counts[i] << endl;
}
}
void HASHTCounter::increment(const hash_t& key){
//cout << (++counts [ key % my_size ]) << endl;
#pragma omp atomic update
++(counts[ key % static_cast<uint64_t>( my_size ) ]);
/** #pragma omp critical
{
uint64_t k = key % (uint64_t) my_size;
int v;
v = *(counts + (int) k);
v += 1;
*(counts + (int) k) = v;
} */
}
void HASHTCounter::bulk_increment(hash_t* h, int num){
for (int i = 0; i < num; ++i){
this->increment( *(h + i) );
}
}
int& HASHTCounter::get(const hash_t& key){
return (counts[ key % static_cast<uint64_t>(my_size) ]);
}
void HASHTCounter::get(const hash_t& key, int& ret){
ret = (counts[ key % static_cast<uint64_t>(my_size) ]);
}
int HASHTCounter::size(void){
return my_size;
}
void HASHTCounter::size(int sz){
delete [] counts;
my_size = sz;
counts = new int [my_size];
}
// TODO: not at all guaranteed safe.
// Division / positioning in new array is uncheck, and wrong.
void HASHTCounter::resize(int sz){
int* n_counts = new int [ sz ];
for (int i = 0; i < my_size; i++){
*(n_counts + (i % sz)) = *(counts + i);
}
my_size = sz;
delete [] counts;
counts = n_counts;
}
int& HASHTCounter::operator[](hash_t key){
//value_t& operator[](std::size_t idx) { return mVector[idx]; }
//const value_t& operator[](std::size_t idx) const { return mVector[idx]; }
return (counts [ key % my_size ]);
}
int* HASHTCounter::begin(void){
return counts;
}
}