-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAliasTable.hpp
More file actions
160 lines (158 loc) · 3.46 KB
/
AliasTable.hpp
File metadata and controls
160 lines (158 loc) · 3.46 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
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
/*
* AliasTable.hpp
*
* Created on: 05/01/2014
* Author: polymorpher
*/
#pragma once
#include"util.hpp"
#include<climits>
namespace AliasLDA{
class AliasTable{
public:
struct LabeledElement{
int label;
double prob;
LabeledElement(int _label,double _prob):label(_label),prob(_prob){}
};
struct AliasElement{
int label;
int alias;
double prob;
AliasElement(int _label,int _alias, double _prob):label(_label),alias(_alias),prob(_prob){}
};
protected:
VecDouble proportions;
std::vector<AliasElement> table;
VecInt labels; //main labels
bool useLabels;
std::mt19937_64 rgen;
std::uniform_real_distribution<double> u01;
int tableSize;
int maxTableSize;
MapIntInt labelPosLookup;
bool locked;
public:
const inline VecDouble& getProportions() const {
return proportions;
}
AliasTable(size_t n):useLabels(false),u01(0,1),maxTableSize(n),locked(false){
init();
}
AliasTable(size_t n, VecInt& _labels):labels(_labels),useLabels(true),u01(0,1),maxTableSize(n),locked(false){
assert(_labels.size()==n);
init();
for(int i=0;i<labels.size();i++){
labelPosLookup[labels[i]]=i;
}
}
void init(){
AliasElement ae(-1,-1,0);
table.clear();
table.resize(maxTableSize, ae);
proportions.resize(maxTableSize);
rgen.seed(time(NULL));
labelPosLookup.set_empty_key(INT_MAX);
}
inline double rand01(){
return u01(rgen);
}
inline int randInt(int limit){
return (int)(rand01()*limit);
}
inline int getLabel(int pos){
return useLabels?labels[pos]:pos;
}
void build(VecDouble& prop){
int n=prop.size();
tableSize=n;
double sum=0;
for(int i=0;i<prop.size();i++){
sum+=prop[i];
}
std::vector<LabeledElement> small,large;
proportions = prop;
small.reserve(n);
large.reserve(n);
double mfactor=n/sum;
for(int i=0;i<prop.size();i++){
double val=prop[i]*mfactor;
if(val>=1){
large.push_back(LabeledElement(i,val));
}else{
small.push_back(LabeledElement(i,val));
}
}
int largeCursor=0;
int smallCursor=0;
while(smallCursor<small.size() && largeCursor<large.size()){
LabeledElement& sle=small[smallCursor];
LabeledElement& lle=large[largeCursor];
AliasElement& ae=table[sle.label];
ae.label=getLabel(sle.label);
ae.alias=getLabel(lle.label);
ae.prob=sle.prob;
lle.prob=lle.prob+sle.prob-1;
if(lle.prob<1){
small.push_back(lle);
smallCursor++;
largeCursor++;
}else{
smallCursor++;
}
}
while(largeCursor<large.size()){
LabeledElement& lle=large[largeCursor];
AliasElement& ae=table[lle.label];
ae.label=getLabel(lle.label);
ae.alias=-1;
ae.prob=1;
largeCursor++;
}
while(smallCursor<small.size()){
// printf("Warning: numerical instability. %d/%d\n", smallCursor,small.size());
LabeledElement& sle=small[smallCursor];
AliasElement& ae=table[sle.label];
ae.label=getLabel(sle.label);
ae.alias=-1;
ae.prob=1;
smallCursor++;
}
}
int sample(){
int roll=randInt(tableSize);
AliasElement& ae=table[roll];
double roll01=rand01();
if(roll01<ae.prob){
return ae.label;
}else{
return ae.alias;
}
}
inline void lock(){
while(locked){}
locked=true;
}
inline bool try_lock(){
if(locked){
return false;
}else{
lock();
return true;
}
}
inline void unlock(){
locked=false;
}
inline void force_lock(){
locked=true;
}
inline bool isLocked(){
return locked;
}
~AliasTable(){
while(locked){printf("warning: delaying destruction of alias table\n");}
}
};
typedef std::unique_ptr<AliasTable> ATableUPtr;
}