forked from Stream-AD/MIDAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodehash.cpp
More file actions
63 lines (55 loc) · 1.35 KB
/
Copy pathnodehash.cpp
File metadata and controls
63 lines (55 loc) · 1.35 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
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#include <iostream>
#include "nodehash.hpp"
Nodehash::Nodehash(int r, int b)
{
num_rows = r;
num_buckets = b;
hash_a.resize(num_rows);
hash_b.resize(num_rows);
for (int i = 0; i < r; i++) {
// a is in [1, p-1]; b is in [0, p-1]
hash_a[i] = rand() % (num_buckets - 1) + 1;
hash_b[i] = rand() % num_buckets;
}
this->clear();
}
Nodehash::~Nodehash()
{
}
int Nodehash::hash(int a, int i)
{
int resid = (a * hash_a[i] + hash_b[i]) % num_buckets;
return resid + (resid < 0 ? num_buckets : 0);
}
void Nodehash::insert(int a, double weight)
{
int bucket;
for (int i = 0; i < num_rows; i++) {
bucket = hash(a, i);
count[i][bucket] += weight;
}
}
double Nodehash::get_count(int a)
{
double min_count = numeric_limits<double>::max();
int bucket;
for (int i = 0; i < num_rows; i++) {
bucket = hash(a, i);
min_count = MIN(min_count, count[i][bucket]);
}
return min_count;
}
void Nodehash::clear()
{
count = vector<vector<double> >(num_rows, vector<double>(num_buckets, 0.0));
}
void Nodehash::lower(double factor)
{
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_buckets; j++) {
count[i][j] = count[i][j] * factor;
}
}
}