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

ListCoders.h « CompactPT « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b41e183ce394e53fc5571b4430d4e43e79a9700a (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// $Id$
// vim:tabstop=2
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#ifndef moses_ListCoders_h
#define moses_ListCoders_h

#include <cmath>
#include <cassert>

namespace Moses
{

template <typename T = unsigned int>
class VarIntType
{
private:
  template <typename IntType, typename OutIt>
  static void EncodeSymbol(IntType input, OutIt output) {
    if(input == 0) {
      *output = 0;
      output++;
      return;
    }

    T msb = 1 << (sizeof(T)*8-1);
    IntType mask  = ~msb;
    IntType shift = (sizeof(T)*8-1);

    while(input) {
      T res = input & mask;
      input >>= shift;
      if(input)
        res |= msb;
      *output = res;
      output++;
    }
  };

  template <typename InIt, typename IntType>
  static void DecodeSymbol(InIt &it, InIt end, IntType &output) {
    T msb = 1 << (sizeof(T)*8-1);
    IntType shift = (sizeof(T)*8-1);

    output = 0;
    size_t i = 0;
    while(it != end && *it & msb) {
      IntType temp = *it & ~msb;
      temp <<= shift*i;
      output |= temp;
      it++;
      i++;
    }
    assert(it != end);

    IntType temp = *it;
    temp <<= shift*i;
    output |= temp;
    it++;
  }

public:

  template <typename InIt, typename OutIt>
  static void Encode(InIt it, InIt end, OutIt outIt) {
    while(it != end) {
      EncodeSymbol(*it, outIt);
      it++;
    }
  }

  template <typename InIt, typename OutIt>
  static void Decode(InIt &it, InIt end, OutIt outIt) {
    while(it != end) {
      size_t output;
      DecodeSymbol(it, end, output);
      *outIt = output;
      outIt++;
    }
  }

  template <typename InIt>
  static size_t DecodeAndSum(InIt &it, InIt end, size_t num) {
    size_t sum = 0;
    size_t curr = 0;

    while(it != end && curr < num) {
      size_t output;
      DecodeSymbol(it, end, output);
      sum += output;
      curr++;
    }

    return sum;
  }

};

typedef VarIntType<unsigned char> VarByte;

typedef VarByte VarInt8;
typedef VarIntType<unsigned short> VarInt16;
typedef VarIntType<unsigned int>   VarInt32;

class Simple9
{
private:
  typedef unsigned int uint;

  template <typename InIt>
  inline static void EncodeSymbol(uint &output, InIt it, InIt end) {
    uint length = end - it;

    uint type = 0;
    uint bitlength = 0;

    switch(length) {
    case 1:
      type = 1;
      bitlength = 28;
      break;
    case 2:
      type = 2;
      bitlength = 14;
      break;
    case 3:
      type = 3;
      bitlength = 9;
      break;
    case 4:
      type = 4;
      bitlength = 7;
      break;
    case 5:
      type = 5;
      bitlength = 5;
      break;
    case 7:
      type = 6;
      bitlength = 4;
      break;
    case 9:
      type = 7;
      bitlength = 3;
      break;
    case 14:
      type = 8;
      bitlength = 2;
      break;
    case 28:
      type = 9;
      bitlength = 1;
      break;
    }

    output = 0;
    output |= (type << 28);

    uint i = 0;
    while(it != end) {
      uint l = bitlength * (length-i-1);
      output |= *it << l;
      it++;
      i++;
    }
  }

  template <typename OutIt>
  static inline void DecodeSymbol(uint input, OutIt outIt) {
    uint type = (input >> 28);

    uint bitlen = 0;
    uint shift = 0;
    uint mask = 0;

    switch(type) {
    case 1:
      bitlen = 28;
      shift = 0;
      mask = 268435455;
      break;
    case 2:
      bitlen = 14;
      shift = 14;
      mask = 16383;
      break;
    case 3:
      bitlen = 9;
      shift = 18;
      mask = 511;
      break;
    case 4:
      bitlen = 7;
      shift = 21;
      mask = 127;
      break;
    case 5:
      bitlen = 5;
      shift = 20;
      mask = 31;
      break;
    case 6:
      bitlen = 4;
      shift = 24;
      mask = 15;
      break;
    case 7:
      bitlen = 3;
      shift = 24;
      mask = 7;
      break;
    case 8:
      bitlen = 2;
      shift = 26;
      mask = 3;
      break;
    case 9:
      bitlen = 1;
      shift = 27;
      mask = 1;
      break;
    }

    while(shift > 0) {
      *outIt = (input >> shift) & mask;
      shift -= bitlen;
      outIt++;
    }
    *outIt = input & mask;
    outIt++;
  }

  static inline size_t DecodeAndSumSymbol(uint input, size_t num, size_t &curr) {
    uint type = (input >> 28);

    uint bitlen = 0;
    uint shift = 0;
    uint mask = 0;

    switch(type) {
    case 1:
      bitlen = 28;
      shift = 0;
      mask = 268435455;
      break;
    case 2:
      bitlen = 14;
      shift = 14;
      mask = 16383;
      break;
    case 3:
      bitlen = 9;
      shift = 18;
      mask = 511;
      break;
    case 4:
      bitlen = 7;
      shift = 21;
      mask = 127;
      break;
    case 5:
      bitlen = 5;
      shift = 20;
      mask = 31;
      break;
    case 6:
      bitlen = 4;
      shift = 24;
      mask = 15;
      break;
    case 7:
      bitlen = 3;
      shift = 24;
      mask = 7;
      break;
    case 8:
      bitlen = 2;
      shift = 26;
      mask = 3;
      break;
    case 9:
      bitlen = 1;
      shift = 27;
      mask = 1;
      break;
    }

    size_t sum = 0;
    while(shift > 0) {
      sum += (input >> shift) & mask;
      shift -= bitlen;
      if(++curr == num)
        return sum;
    }
    sum += input & mask;
    curr++;
    return sum;
  }

public:
  template <typename InIt, typename OutIt>
  static void Encode(InIt it, InIt end, OutIt outIt) {
    uint parts[] = { 1, 2, 3, 4, 5, 7, 9, 14, 28 };

    uint buffer[28];
    for(InIt i = it; i < end; i++) {
      uint lastbit = 1;
      uint lastpos = 0;
      uint lastyes = 0;
      uint j = 0;

      double log2 = log(2);
      while(j < 9 && lastpos < 28 && (i+lastpos) < end) {
        if(lastpos >= parts[j])
          j++;

        buffer[lastpos] = *(i + lastpos);

        uint reqbit = ceil(log(buffer[lastpos]+1)/log2);
        assert(reqbit <= 28);

        uint bit = 28/floor(28/reqbit);
        if(lastbit < bit)
          lastbit = bit;

        if(parts[j] > 28/lastbit)
          break;
        else if(lastpos == parts[j]-1)
          lastyes = lastpos;

        lastpos++;
      }
      i += lastyes;

      uint length = lastyes + 1;
      uint output;
      EncodeSymbol(output, buffer, buffer + length);

      *outIt = output;
      outIt++;
    }
  }

  template <typename InIt, typename OutIt>
  static void Decode(InIt &it, InIt end, OutIt outIt) {
    while(it != end) {
      DecodeSymbol(*it, outIt);
      it++;
    }
  }

  template <typename InIt>
  static size_t DecodeAndSum(InIt &it, InIt end, size_t num) {
    size_t sum = 0;
    size_t curr = 0;
    while(it != end && curr < num) {
      sum += DecodeAndSumSymbol(*it, num, curr);
      it++;
    }
    assert(curr == num);
    return sum;
  }
};

}

#endif