-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNewHash.h
executable file
·286 lines (231 loc) · 6.23 KB
/
NewHash.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef NEWHASH
#define NEWHASH
#include "util.h"
const int INIT_CAP = 128;//Must be a multiple of 2
const Float UPPER_UTIL_RATE = 0.75;
const Float LOWER_UTIL_RATE = 0.5;
//Note this Hash Map does not preserve entries with value=0.0
//(it could be deleted when resizing)
class NewHash{
public:
NewHash(HashFunc* _hashfunc){
hashfunc = _hashfunc;
list_cap = INIT_CAP;
list_size = 0;
list = new pair<int, Float>[list_cap];
for(int i=0;i<list_cap;i++){
list[i].first = -1;
list[i].second = 0.0;
}
}
~NewHash(){
delete[] list;
}
int size(){
return list_size;
}
pair<int,Float>* get_addr(int index){
int list_index;
int size0 = list_cap-1;
find_index( list, list_index, index, size0, hashfunc->hashindices );
return &(list[ list_index ]);
}
Float get(int index){
pair<int,Float>* p = get_addr(index);
return (p->first==-1)?0.0:(p->second);
}
void set(int index, Float value){
pair<int,Float>* p = get_addr(index);
set( index, p, value );
}
void set(int index, pair<int,Float>* p, Float value){
int prev_index = p->first;
p->first = index;
p->second = value;
if( prev_index == -1 ){
list_size+=1;
pair<int,Float>* list2 = list;
int list_cap2 = list_cap;
int size0 = list_cap-1;
if( list_size > list_cap * UPPER_UTIL_RATE ){
resize(list2, list, list_cap, list_cap2, size0, list_size, hashfunc->hashindices);
}
}
}
void clear(){
for(int i=0;i<list_cap;i++){
list[i].first = -1;
list[i].second = 0.0;
}
list_size=0;
}
class iterator{
public:
iterator(pair<int,Float>* _list, int _cap, int _pos){
arr = _list;
cap = _cap;
pos = _pos;
while( pos<cap && arr[pos].first==-1 )
pos++;
}
iterator& operator++(){
pos++;
while( pos<cap && arr[pos].first==-1 )
pos++;
return *this;
}
bool operator==(const iterator& it2){
return pos==it2.pos;
}
bool operator!=(const iterator& it2){
return pos!=it2.pos;
}
pair<int,Float>* operator->() const {
return &(arr[pos]);
}
private:
pair<int,Float>* arr;
int pos;
int cap;
};
iterator begin(){
iterator it(list, list_cap, 0);
return it;
}
iterator end(){
iterator it(list, list_cap, list_cap);
return it;
}
//for debug
void printList(){
for(int i=0;i<list_cap;i++)
cerr << "(" << list[i].first << "," << list[i].second << "),";
cerr << endl;
}
private:
HashFunc* hashfunc;
pair<int, Float>* list;
int list_cap;
int list_size;
inline void find_index(pair<int, Float>*& l, int& st, const int& index, int& size0, int*& hashindices){
st = hashindices[index] & size0;
if (l[st].first != index){
while (l[st].first != index && l[st].first != -1){
st++;
st &= size0;
}
}
}
inline void resize(pair<int, Float>*& l, pair<int, Float>*& L, int& size, int& new_size, int& new_size0, int& util, int*& hashindices){
while (util > UPPER_UTIL_RATE * new_size){
new_size *= 2;
}
if (new_size == size)
return;
pair<int, Float>* new_l = new pair<int, Float>[new_size];
new_size0 = new_size - 1;
int index_l = 0;
for (int tt = 0; tt < new_size; tt++){
new_l[tt] = make_pair(-1, 0.0);
}
int new_util=0;
for (int tt = 0; tt < size; tt++){
//insert old elements into new array
pair<int, Float> p = l[tt];
if (p.first != -1 && p.second != 0.0){
find_index(new_l, index_l, p.first, new_size0, hashindices);
new_l[index_l] = p;
new_util++;
}
}
delete[] l;
l = new_l;
size = new_size;
L = new_l;
util = new_util;
}
inline void trim(pair<int, Float>*& l, int& size, int& util, int*& hashindices){
util = 0;
for (int tt = 0; tt < size; tt++){
pair<int, Float> p = l[tt];
if (p.first != -1 && p.second != 0.0)
util++;
}
int new_size = size;
while (util > UPPER_UTIL_RATE * new_size){
new_size *= 2;
}
while (new_size > INIT_CAP && util < LOWER_UTIL_RATE * new_size){
new_size /= 2;
}
pair<int, Float>* new_l = new pair<int, Float>[new_size];
int new_size0 = new_size - 1;
int index_l = 0;
for (int tt = 0; tt < new_size; tt++){
new_l[tt] = make_pair(-1, 0.0);
}
for (int tt = 0; tt < size; tt++){
//insert old elements into new array
pair<int, Float> p = l[tt];
if (p.first != -1 && p.second != 0.0){
find_index(new_l, index_l, p.first, new_size0, hashindices);
new_l[index_l] = p;
}
}
delete[] l;
l = new_l;
size = new_size;
}
};
void importance_samples( NewHash& sv, int num_sample, SparseVec& samples ){
samples.clear();
if( sv.size() == 0 )
return;
double* cum = new double[sv.size()];
NewHash::iterator it = sv.begin();
if( it->first != 0 )//not bias
cum[0] = fabs(it->second);
else
cum[0] = 0.0;
++it;
for(int i=1;it!=sv.end();++it,++i){
if( it->first != 0 )//not bias
cum[i] = cum[i-1] + fabs(it->second);
else//bias
cum[i] = cum[i-1];
}
double weight_sum = cum[sv.size()-1];
vector<double> rands;
for(int i=0;i<num_sample;i++)
rands.push_back( weight_sum * ((double)rand()/RAND_MAX) );
sort( rands.begin(), rands.end() );
int i=0,j=0;
it = sv.begin();
while( i < rands.size() ){
while( rands[i] > cum[j] ){
j++;
++it;
}
double count = 0.0;
while( i<rands.size() && rands[i] < cum[j] ){
count += 1.0;
i++;
}
if( it->second > 0.0 )
samples.push_back(make_pair(it->first,count));
else
samples.push_back(make_pair(it->first,-count));
}
double tmp = (weight_sum/num_sample);
for(SparseVec::iterator it2=samples.begin(); it2!=samples.end(); it2++)
it2->second *= tmp;
delete[] cum;
}
void hash_to_sv( NewHash& hash, SparseVec& sv ){
sv.clear();
for(NewHash::iterator it=hash.begin(); it!=hash.end(); ++it){
if( it->second != 0.0 )
sv.push_back(make_pair(it->first, it->second));
}
}
#endif