-
Notifications
You must be signed in to change notification settings - Fork 2
/
ppm_model.h
323 lines (261 loc) · 6.77 KB
/
ppm_model.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
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
#ifndef PPM_MODEL_H
#define PPM_MODEL_H
#include <map>
#include <list>
#include <deque>
#include <string>
using namespace std;
#include "common.h"
#include "model.h"
#include "arithmetic_encoder.h"
#include "arithmetic_decoder.h"
#include "index.h"
const char escape = '\0';
class context_entry
{
public:
char cLetter;
DWORD dwCount;
DWORD dwCumCount;
};
class context_entry_list
{
private:
list<context_entry> m_List;
context_entry m_ceEscape;
public:
typedef list<context_entry>::iterator iterator;
context_entry_list()
{
m_ceEscape.cLetter = escape;
m_ceEscape.dwCount = 1;
m_ceEscape.dwCumCount = 1;
}
inline DWORD total_count()
{
return(m_ceEscape.dwCumCount);
}
void insert(char cNewLetter)
{
context_entry ceNew;
ceNew.cLetter = cNewLetter;
ceNew.dwCount = 1;
ceNew.dwCumCount = m_ceEscape.dwCumCount - m_ceEscape.dwCount + 1;
m_List.push_back(ceNew);
m_ceEscape.dwCount += 1;
m_ceEscape.dwCumCount += 2;
}
void update(iterator i)
{
i->dwCount += 1;
for (; i != end(); ++i)
{
i->dwCumCount += 1;
}
m_ceEscape.dwCumCount += 1;
}
iterator begin()
{
return(m_List.begin());
}
iterator end()
{
return(m_List.end());
}
inline DWORD escape_lower_bound()
{
return(m_ceEscape.dwCumCount - m_ceEscape.dwCount);
}
inline DWORD escape_upper_bound()
{
return(m_ceEscape.dwCumCount);
}
inline context_entry& escape_entry()
{
return(m_ceEscape);
}
};
template<size_t S = 5>
class ppm_model : public model<char>
{
protected:
typedef map<string, context_entry_list> context_order;
context_order aContextOrders[S + 1];
string sHistory;
DWORD m_dwNegativeContextCumCount;
arithmetic_encoder* m_encoder;
arithmetic_decoder* m_decoder;
ppm_model(const ppm_model&);
ppm_model& operator=(const ppm_model&);
context_entry search_symbol(DWORD dwTarget, context_entry_list& ContextEntryList);
char rec_decode(string sContext, int iContext);
public:
ppm_model(arithmetic_encoder* encoder): m_encoder(encoder),
m_decoder(NULL)
{
}
ppm_model(arithmetic_decoder* decoder): m_encoder(NULL),
m_decoder(decoder)
{
}
virtual void encode_symbol(char cNewSymbol);
virtual void encode_eof();
virtual char decode_symbol();
virtual ~ppm_model()
{
}
};
///////////////////////////////////////////////////////////
template<size_t S>
context_entry ppm_model<S>::search_symbol(DWORD dwTarget, context_entry_list& ContextEntryList)
{
context_entry ceRet;
if ((ContextEntryList.escape_lower_bound <= dwTarget) &&
(dwTarget < ContextEntryList.escape_upper_bound))
return ContextEntryList.escape_entry();
DWORD dwPrevCumCount = 0;
for(context_entry_list::iterator i = ContextEntryList.begin();
i != ContextEntryList.end();
++i)
{
if ((dwPrevCumCount <= dwTarget) && (dwTarget < i->dwCumCount))
return (*i);
dwPrevCumCount = i->dwCumCount;
}
return ceRet;
}
template<size_t S>
void ppm_model<S>::encode_symbol(char cNewSymbol)
{
if (m_encoder)
{
bool bContextFound = false;
int j = 0;
for ( int iContext = sHistory.length();
(iContext >= 0) && !bContextFound;
--iContext)
{
string sContext(sHistory, j, iContext);
++j;
context_entry_list& ContextEntryList = aContextOrders[iContext][sContext];
for(context_entry_list::iterator i = ContextEntryList.begin();
(i != ContextEntryList.end()) && !bContextFound;
++i)
{
if(i->cLetter == cNewSymbol)
{
//codifica il simbolo
m_encoder->encode(i->dwCumCount - i->dwCount,
i->dwCumCount,
ContextEntryList.total_count());
//update (existing)
ContextEntryList.update(i);
bContextFound = true;
}
}
if(!bContextFound)
{
//codifica un escape
m_encoder->encode(ContextEntryList.escape_lower_bound(),
ContextEntryList.escape_upper_bound(),
ContextEntryList.total_count());
//update_new
ContextEntryList.insert(cNewSymbol);
}
}
if(!bContextFound)
{
//-1 context order
m_encoder->encode((BYTE)cNewSymbol - 1,
(BYTE)cNewSymbol,
0xFF);
}
if (sHistory.size() < S)
sHistory += cNewSymbol;
else
sHistory = sHistory.substr(1) + cNewSymbol;
}
}
template<size_t S>
void ppm_model<S>::encode_eof()
{
m_encoder->encode_eof();
}
template<size_t S>
char ppm_model<S>::rec_decode(string sContext, int iContext)
{
context_entry ceSymbol;
context_entry_list::iterator i;
if (iContext >= 0)
{
context_entry_list& ContextEntryList = aContextOrders[iContext][sContext];
DWORD dwTarget = m_decoder->decode_target(ContextEntryList.total_count());
//search symbol
if ((ContextEntryList.escape_lower_bound() <= dwTarget) &&
(dwTarget < ContextEntryList.escape_upper_bound()))
{
ceSymbol= ContextEntryList.escape_entry();
}
else
{
DWORD dwPrevCumCount = 0;
for(i = ContextEntryList.begin();
i != ContextEntryList.end();
++i)
{
if ((dwPrevCumCount <= dwTarget) && (dwTarget < i->dwCumCount))
{
ceSymbol = *i;
break;
}
dwPrevCumCount = i->dwCumCount;
}
}
//fine ricerca simbolo
m_decoder->decode(ceSymbol.dwCumCount - ceSymbol.dwCount,
ceSymbol.dwCumCount,
ContextEntryList.total_count());
if (ceSymbol.cLetter != escape)
{
ContextEntryList.update(i);
return (ceSymbol.cLetter);
}
else
{
string sNewContext;
if (sContext.size() == 0)
sNewContext = sContext;
else
sNewContext = string(sContext, 1, iContext - 1);
char cSymbol = rec_decode(sNewContext, iContext - 1);
ASSERT(cSymbol != escape);
ContextEntryList.insert(cSymbol);
return (cSymbol);
}
}
else
{
//-1 context order
DWORD dwTarget = m_decoder->decode_target((DWORD)0xFF);
char cSymbol = (char)(dwTarget + 1);
m_decoder->decode((BYTE)cSymbol - 1,
(BYTE)cSymbol,
0xFF);
return (cSymbol);
}
}
template<size_t S>
char ppm_model<S>::decode_symbol()
{
char cSymbol;
if (m_decoder)
{
cSymbol = rec_decode(sHistory, sHistory.length());
if (sHistory.size() < S)
sHistory += cSymbol;
else
sHistory = sHistory.substr(1) + cSymbol;
}
return cSymbol;
}
#endif //PPM_MODEL_H