-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeneralizedStirlingNumber.hpp
200 lines (198 loc) · 3.92 KB
/
GeneralizedStirlingNumber.hpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef GeneralizedStirlingNumber_HPP_
#define GeneralizedStirlingNumber_HPP_
#include<cmath>
#include<climits>
#include<cfloat>
#include<cassert>
#include<unordered_map>
#include"util.hpp"
namespace AliasLDA {
class NMPair {
public:
int n;
int m;
NMPair() :
n(0), m(0) {
}
NMPair(int _n, int _m) :
n(_n), m(_m) {
}
};
class GeneralizedStirlingNumber {
GeneralizedStirlingNumber() :
lock(0) {
}
GeneralizedStirlingNumber(GeneralizedStirlingNumber const&);
void operator=(GeneralizedStirlingNumber const&);
public:
static GeneralizedStirlingNumber& getInstance() {
static GeneralizedStirlingNumber instance;
return instance;
}
std::unordered_map<int, double*> cache;
std::unordered_map<int, NMPair> limits;
std::unordered_map<int, int> reading;
std::unordered_map<int, bool> writing;
bool lock;
inline int nm(int n, int m, int mLimit) {
return n * mLimit + m;
}
double* getCache(double _a) {
while (lock) {
}
lock = true;
double* ret = NULL;
int a = _a * 100;
if (writing.count(a) == 0) {
reading[a] = 1;
ret = cache[a];
} else {
if (!writing[a]) {
reading[a]++;
ret = cache[a];
}
}
lock = false;
return ret;
}
void releaseCache(double _a) {
while (lock) {
}
lock = true;
int a = _a * 100;
reading[a]--;
lock = false;
}
NMPair getLimit(double _a) {
while (lock) {
}
lock = true;
NMPair ret;
int a = _a * 100;
if (writing.count(a) == 0) {
//reading[a]=true;
ret = limits[a];
} else {
if (!writing[a]) {
//reading[a]=true;
ret = limits[a];
} else {
ret.m = -1;
ret.n = -1;
}
}
lock = false;
return ret;
}
bool initialize(double __a, int n, int m) {
while (lock) {
}
lock = true;
int a = __a * 100;
if (reading.count(a) > 0) {
if (reading[a]) {
lock = false;
return false;
}
}
if (writing.count(a) > 0) {
if (writing[a]) {
lock = false;
return false;
}
}
writing[a] = true;
if (cache.count(a) > 0) {
NMPair pair = limits[a];
int newn = std::max(n, pair.n);
int newm = std::max(m, pair.m);
if (newn != n && newm != m) {
freeRecord(a);
} else {
writing[a] = false;
lock = false;
return true;
}
}
//printf("test1\n");
lock = false;
NMPair p;
p.n = n;
p.m = m;
limits[a] = p;
double* localCache = new double[nm(n, 0, m)];
//printf("test2\n");
cache[a] = localCache;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int ind = nm(i, j, m);
localCache[ind] = -DBL_MAX;
}
}
//printf("test3\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int ind = nm(i, j, m);
localCache[ind] = computelog(__a, i, j, m, localCache);
//if(!isnan(localCache[ind]))printf("%f %d %d %d\n",localCache[ind],i,j,m);
}
}
//printf("test4\n");
writing[a] = false;
return true;
}
~GeneralizedStirlingNumber() {
//freeAll();
}
inline void freeRecord(int _a) {
delete[] cache[_a];
cache.erase(_a);
limits.erase(_a);
}
inline void freeAll() {
for (std::unordered_map<int, double*>::iterator it = cache.begin();
it != cache.end(); it++) {
delete[] it->second;
}
}
inline double fact(double a, int n) {
assert(n >= 0);
if (n == 1) {
return 0;
}
return (double) log((n - 1) - a) + fact(a, n - 1);
}
inline double computelog(double a, int n, int m, int mLimit,
double* localCache) {
if (m > n)
return NAN;
else if (m == n)
return 0;
else if (m <= 0)
return n == 0 ? 0 : NAN;
else {
int ind = nm(n, m, mLimit);
if (localCache[ind] != -DBL_MAX) {
return localCache[ind];
} else {
double r;
if (m == 1) {
r = fact(a, n);
} else {
r = log(
exp(
computelog(a, n - 1, m - 1, mLimit,
localCache)
- computelog(a, n - 1, m, mLimit,
localCache)) + (n - 1)
- m * a)
+ computelog(a, n - 1, m, mLimit, localCache);
}
localCache[ind] = r;
return r;
}
}
}
};
}
#endif