Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hash.h « DynSAInclude « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 233d0be5d4825415951433e71a9056a399c2e5e1 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef INC_ALLHASHFUNCS_H
#define INC_ALLHASHFUNCS_H

#include "util/check.hh"
#include <cmath>
#include "types.h"
#include "utils.h"
#include "file.h"
using namespace Moses;
typedef uint64_t P;   // largest input range is 2^64

template <typename T>
class HashBase {
  protected:
    T m_;       // range of hash output
    count_t H_; // number of hash functions to instantiate
    virtual void initSeeds()=0;
    virtual void freeSeeds()=0;
  public:
    HashBase(float m, count_t H=1):m_((T)m), H_(H) {
      //cerr << "range = (0..." << m_ << "]" << endl;
    }
    HashBase(FileHandler* fin) {
      load(fin);
    }
    virtual ~HashBase(){}
    virtual T hash(const char*s, count_t h)=0;  // string hashing
    virtual T hash(const wordID_t* id, const int len, count_t h)=0;  // vocab mapped hashing
    count_t size() { return H_;}
    virtual void save(FileHandler* fout) {
      CHECK(fout != 0);
      fout->write((char*)&m_, sizeof(m_));
      fout->write((char*)&H_, sizeof(H_));
    }
    virtual void load(FileHandler* fin) {
      CHECK(fin != 0);
      fin->read((char*)&m_, sizeof(m_));
      fin->read((char*)&H_, sizeof(H_));
    }
};
template <typename T>
class UnivHash_linear: public HashBase<T> {
  public:
    UnivHash_linear(float m, count_t H, P pr):
      HashBase<T>(m, H), pr_(pr) {
      //CHECK(isPrime(pr_));
      initSeeds();
    }
    UnivHash_linear(FileHandler* fin):
      HashBase<T>(fin) {
      load(fin);
    }
    ~UnivHash_linear() {freeSeeds();}
    T hash(const char* s, count_t h){return 0;}  //not implemented
    T hash(const wordID_t* id, const int len, count_t h);
    T hash(const wordID_t id, const count_t pos, 
           const T prevValue, count_t h);
    void save(FileHandler* fout);
    void load(FileHandler* fin);
  private:
    T** a_, **b_;
    P pr_;
    void initSeeds();
    void freeSeeds(); 
};

/* UnivHash_noPrimes:
 * From Dietzfelbinger 2008
 * p = input domain range = 2^l
 * m = output range = 2^k
 * # of hash function = 2^(l-1)
*/
template <typename T>
class UnivHash_noPrimes: public HashBase<T> {
  public:
    UnivHash_noPrimes(float k, float l): 
      HashBase<T>(k, 100), d_(count_t((l-k))) {
      if(((int)l >> 3) == sizeof(P)) p_ = (P) pow(2,l) - 1;
      else p_ = (P) pow(2,l);
      initSeeds();
    }
    UnivHash_noPrimes(FileHandler* fin):
     HashBase<T>(fin) {
     load(fin);
    }
    ~UnivHash_noPrimes() {freeSeeds();}
    T hash(const char* s, count_t h);
    T hash(const wordID_t* id, const int len, count_t h);
    T hash(const P x, count_t h);
    void save(FileHandler* fout);
    void load(FileHandler* fin);
  private:
    count_t d_;  // l-k
    P p_, *a_;   // real-valued input range, storage
    void initSeeds();
    void freeSeeds() {delete[] a_;}
};

template <typename T>
class Hash_shiftAddXOR: public HashBase<T> {
  public:
    Hash_shiftAddXOR(float m, count_t H=5): HashBase<T>(m,H),
      l_(5), r_(2) {
      initSeeds();
    }
    ~Hash_shiftAddXOR() {freeSeeds();}
    T hash(const char* s, count_t h);
    T hash(const wordID_t* id, const int len, count_t h) {} // empty
  private:
    T* v_;      // random seed storage
    const unsigned short l_, r_; // left-shift bits, right-shift bits
    void initSeeds();
    void freeSeeds() {delete[] v_;}
};

template <typename T>
class UnivHash_tableXOR: public HashBase<T> {
  public:
    UnivHash_tableXOR(float m, count_t H=5): HashBase<T>(m, H),
      table_(NULL), tblLen_(255*MAX_STR_LEN) {
      initSeeds();
    }
    ~UnivHash_tableXOR() {freeSeeds();}
    T hash(const char* s, count_t h);
    T hash(const wordID_t* id, const int len, count_t h) {}
  private:
    T** table_; // storage for random numbers
    count_t tblLen_;     // length of table
    void initSeeds();
    void freeSeeds(); 
};

// ShiftAddXor
template <typename T>
void Hash_shiftAddXOR<T>::initSeeds() {
  v_ = new T[this->H_];
  for(count_t i=0; i < this->H_; i++)
    v_[i] = Utils::rand<T>() + 1; 
}
template <typename T>
T Hash_shiftAddXOR<T>::hash(const char* s, count_t h=0) {
  T value = v_[h];
  int pos(0);
  unsigned char c;
  while((c = *s++) && (++pos < MAX_STR_LEN)) {
    value ^= ((value << l_) + (value >> r_) + c);
  }
  return (value % this->m_);
}

// UnivHash_tableXOR
template <typename T>
void UnivHash_tableXOR<T>::initSeeds() {
  // delete any values in table
  if(table_) freeSeeds(); 
  // instance of new table
  table_ = new T* [this->H_];
  // fill with random values
  for(count_t j=0; j < this->H_; j++) {
    table_[j] = new T[tblLen_];
    for(count_t i=0; i < tblLen_; i++) { 
      table_[j][i] = Utils::rand<T>(this->m_-1); 
    }
  }
}
template <typename T>
void UnivHash_tableXOR<T>::freeSeeds() {
  for(count_t j = 0; j < this->H_; j++)
    delete[] table_[j];
  delete[] table_;
  table_ = NULL;
}
template <typename T>
T UnivHash_tableXOR<T>::hash(const char* s, count_t h = 0) {  
  T value = 0;
  count_t pos = 0, idx = 0;
  unsigned char c;
  while((c = *s++) && (++pos < MAX_STR_LEN))
    value ^= table_[h][idx += c];
  CHECK(value < this->m_); 
  return value;
}

// UnivHash_noPrimes
template <typename T>
void UnivHash_noPrimes<T>::initSeeds() {
  a_ = new P[this->H_];
  for(T i=0; i < this->H_; i++) {
    a_[i] = Utils::rand<P>();
    if(a_[i] % 2 == 0) a_[i]++;  // a must be odd
  }
}
template <typename T>
T UnivHash_noPrimes<T>::hash(const P x, count_t h=0) {
  // h_a(x) = (ax mod 2^l) div 2^(l-k)
  T value = ((a_[h] * x) % p_) >> d_;
  return value % this->m_;
}
template <typename T>
T UnivHash_noPrimes<T>::hash(const wordID_t* id, const int len, 
  count_t h=0) {
  T value = 0;
  int pos(0);
  while(pos < len) {
    value ^= hash((P)id[pos], h++);
    pos++;
  }
  return value % this->m_;
}
template <typename T>
T UnivHash_noPrimes<T>::hash(const char* s, count_t h=0) {
  T value = 0;
  int pos(0);
  unsigned char c;
  while((c = *s++) && (++pos < MAX_STR_LEN)) {
    value ^= hash((P)c, h); 
  }
  return value % this->m_;
}
template <typename T>
void UnivHash_noPrimes<T>::save(FileHandler* fout) {
  HashBase<T>::save(fout);
  fout->write((char*)&p_, sizeof(p_));
  fout->write((char*)&d_, sizeof(d_));
  for(T i=0; i < this->H_; i++) { 
    fout->write((char*)&a_[i], sizeof(a_[i]));
  }
}
template <typename T>
void UnivHash_noPrimes<T>::load(FileHandler* fin) {
  a_ = new P[this->H_];
  // HashBase<T>::load(fin) already done in constructor
  fin->read((char*)&p_, sizeof(p_));
  fin->read((char*)&d_, sizeof(d_));
  for(T i=0; i < this->H_; i++) 
  { 
    fin->read((char*)&a_[i], sizeof(a_[i]));
  }
}

//UnivHash_linear
template <typename T>
void UnivHash_linear<T>::initSeeds() {
  a_ = new T*[this->H_];
  b_ = new T*[this->H_];
  for(count_t i=0; i < this->H_; i++) {
    a_[i] = new T[MAX_NGRAM_ORDER];
    b_[i] = new T[MAX_NGRAM_ORDER];
    for(count_t j=0; j < MAX_NGRAM_ORDER; j++) {
      a_[i][j] = 1 + Utils::rand<T>();
      b_[i][j] = Utils::rand<T>();
    }
  }
}
template <typename T>
void UnivHash_linear<T>::freeSeeds() {
  for(count_t i=0; i < this->H_; i++) {
    delete[] a_[i];
    delete[] b_[i];
  }
  delete[] a_;
  delete[] b_;
  a_ = b_ = NULL;
}
template <typename T>
inline T UnivHash_linear<T>::hash(const wordID_t* id, const int len, 
                           count_t h=0) {
  CHECK(h < this->H_);
  T value = 0;
  int pos(0);
  while(pos < len) {
    value += ((a_[h][pos] * id[pos]) + b_[h][pos]);// % pr_;
    ++pos;
  }
  return value % this->m_;
}
template <typename T>
inline T UnivHash_linear<T>::hash(const wordID_t id, const count_t pos,
                           const T prevValue, count_t h=0) {
  CHECK(h < this->H_);
  T value = prevValue + ((a_[h][pos] * id) + b_[h][pos]); // % pr_;
  return value % this->m_;
}
template <typename T>
void UnivHash_linear<T>::save(FileHandler* fout) {
  // int bytes = sizeof(a_[0][0]);
  HashBase<T>::save(fout);
  fout->write((char*)&pr_, sizeof(pr_));
  for(count_t i=0; i < this->H_; i++) {
    for(count_t j=0; j < MAX_NGRAM_ORDER; j++) {
      fout->write((char*)&a_[i][j], sizeof(a_[i][j])); 
      fout->write((char*)&b_[i][j], sizeof(b_[i][j]));
      //cout << "a[" << i << "][" << j << "]=" << a_[i][j] << endl;
      //cout << "b[" << i << "][" << j << "]=" << b_[i][j] << endl;
    }
  }
}
template <typename T>
void UnivHash_linear<T>::load(FileHandler* fin) {
  // HashBase<T>::load(fin) already done in constructor
  fin->read((char*)&pr_, sizeof(pr_));
  a_ = new T*[this->H_];
  b_ = new T*[this->H_];
  for(count_t i=0; i < this->H_; i++) {
    a_[i] = new T[MAX_NGRAM_ORDER];
    b_[i] = new T[MAX_NGRAM_ORDER];
    for(count_t j=0; j < MAX_NGRAM_ORDER; j++) {
      fin->read((char*)&a_[i][j], sizeof(a_[i][j])); 
      fin->read((char*)&b_[i][j], sizeof(b_[i][j])); 
      //cout << "a[" << i << "][" << j << "]=" << a_[i][j] << endl;
      //cout << "b[" << i << "][" << j << "]=" << b_[i][j] << endl;
    }
  }
}
/*
template <typename T>
T UnivHash_linear<T>::hash(const char* s, count_t h=0) {
  CHECK(h < this->H_);
  T value = 0;
  int pos(0);
  unsigned char c;
  while((c = *s++) && (++pos < MAX_STR_LEN)) {
    value += ((a_[h][pos] * c) + b_[h][pos]);// % pr_;
  }
  return value % this->m_;
}*/
#endif