-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_code.cpp
35 lines (30 loc) · 968 Bytes
/
fast_code.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
#include <unordered_map>
#include <cmath>
#include <iostream>
class MyHash
{
public:
std::size_t operator()(long s) const
{
return 1664525*s + 1013904223;
}
};
extern "C" void pairwise_entropies_window(
double *output_sum, double *output_sum_log, long output_size,
const long *words, long words_size,
const double *weights) {
auto temp = std::unordered_map<long, double, MyHash>();
temp.reserve(words_size);
const long center = words[words_size / 2];
for (int i = 0; i < words_size; ++i) {
//std::cerr << words[i] << ' ' << weights[i] << '\n';
temp[words[i]] += weights[i];
}
//return;
for (auto it = temp.begin(); it != temp.end(); ++it) {
//std::cerr << it->first << ' ' << it->second << '\n';
const long index = (center*1664525 + it->first) % output_size;
output_sum[index] += it->second;
output_sum_log[index] += it->second * std::log(it->second);
}
}