forked from antirez/smaz
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gsinterpolative.c
336 lines (264 loc) · 7.99 KB
/
gsinterpolative.c
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
/*
Copyright (C) 2005,2012 Paul Gardner-Stephen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
Interpolative Coder/Decoder.
Paul Gardner-Stephen, sometime around 2005.
Implements the traditional recursive algorithm, and the recursive turned stack base version.
In addition, it implements my new fast interpolative coder which all but does away with recursion.
In all cases, the assignment of binary codes follows the same rules as indicated in the
2000 paper on interpolative codes.
Similarly, the divisions are placed on powers of two where possible to avoid unbalanced
traversals.
The ic_encode_*() routines encode a simple list which has list_length entries,
and a maximum possible value of max_value. Word frequencies and positions
within d document are handled by passing an optional pointer to an array of
cumulative frequencies and the position of each hit within its host document.
*/
/* Parse the file twice, once for encoding and common functions, and once more
for decoding functions. This allows us to keep the code base very small, and less
prone to cut-and-paste errors */
#ifndef UNDER_CONTROL
#define UNDER_CONTROL
#define COMMON
#undef ENCODE
#undef ENCODING
#define ENCODE(X,Y) X ## decode ## Y
#include "gsinterpolative.c"
#undef COMMON
#undef ENCODE
#define ENCODING
#define ENCODE(X,Y) X ## encode ## Y
#endif
#ifdef COMMON
#undef DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include "arithmetic.h"
// inline void encode_bits(int code_bits,unsigned int value,range_coder *c);
// inline void decode_bits(int code_bits,unsigned int *value,range_coder *c);
// inline void decode_few_bits(int code_bits,unsigned int *value,range_coder *c);
int biggest_power_of_2(int v)
{
int b;
for(b=30;b>=0;b--)
if ((1<<b)<v) return 1<<b;
return 0;
}
int log2_ceil(int v)
{
int b;
for(b=0;b<31;b++)
if ((1<<b)>=v) return b;
return 31;
}
int ic_encode_recursive(int *list,
int list_length,
int max_value,
range_coder *c);
void binary_encode(int low,int *pp,int high,range_coder *c)
{
/* Work out the range of values we can encode/decode */
int p=*pp;
int range_minus_1=high-low;
int range=range_minus_1+1;
int value=p-low;
#ifdef DEBUG
if (low>high||(p>high)||(p<low))
{
printf("menc Illegal triple encountered: [%d,%d,%d]\n",
low,p,high);
sleep(60);
}
#endif
range_encode_equiprobable(c,range,value);
return;
}
void binary_decode(int low,int *pp,int high,range_coder *c)
{
/* Work out the range of values we can encode/decode */
int range_minus_1=high-low;
int range=range_minus_1+1;
int value;
value=range_decode_equiprobable(c,range);
*pp=value+low;
return;
}
#endif
/* -------------------------------------------------
Recursive Implementation
------------------------------------------------- */
int ENCODE(ic_,_recursive_r)(int *list,
int list_length,
int max_value,
range_coder *c,
int lo,
int p,
int hi,
int step)
{
// int doc_number;
int doc_low;
int doc_high;
if (lo>=list_length) return 0;
/* Skip coding of entries outside the valid range, since the pseudo entries do not require
coding as their values are known */
if (p<list_length)
{
// doc_number=list[p];
/* Work out initial constraint on this value */
if (lo>-1) doc_low=list[lo]+1; else doc_low=0;
if (hi<list_length) doc_high=list[hi]-1;
else doc_high=max_value+(hi-list_length);
/* Now narrow range to take into account the number of postings which occur between us and lo and hi */
doc_low+=(p-lo-1);
doc_high-=(hi-p-1);
/* Encode the document number */
if (p<list_length)
{
ENCODE(binary_,)(doc_low,&list[p],doc_high,c);
// printf("%d [%d,%d,%d] step=%d\n",p,doc_low,list[p],doc_high,step);
}
}
if (step>1)
{
/* Now recurse left and right children */
ENCODE(ic_,_recursive_r)(list,list_length,
max_value,
c,
lo,p-(step>>1),p,
step>>1);
ENCODE(ic_,_recursive_r)(list,list_length,
max_value,
c,
p,p+(step>>1),hi,
step>>1);
}
return 0;
}
int ENCODE(ic_,_recursive)(int *list,
int list_length,
int max_value,
range_coder *c)
{
/* Start at largest power of two, and round list size up to next power of two
to keep recursion simple, and keep bit stream compatible with fast
interpolative coder. */
int powerof2=biggest_power_of_2(list_length+1);
ENCODE(ic_,_recursive_r)(list,list_length,
max_value,
c,
-1,powerof2-1,list_length+1,powerof2);
return 0;
}
/* -------------------------------------------------
Test Bed
------------------------------------------------- */
#ifdef TESTMODE
#ifdef COMMON
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
#ifndef __sun__
#include <sys/time.h>
typedef long long hrtime_t;
hrtime_t gethrvtime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000000000+tv.tv_usec*1000;
}
#endif
/* Size of demo list for compression / decompression */
#define DEMO_SIZE (40*1024)
#define LIST_DENSITY 0.05
#define REPEAT_COUNT 1
int main(int argc,char **argv)
{
int list2[DEMO_SIZE];
int list2_out[DEMO_SIZE];
hrtime_t last_time,this_time;
long long mean;
int i,b;
int fraction=0xffff*LIST_DENSITY;
/* Generate a series of gaps corresponding to a geometric series */
srandom(0);
b=0;
for(i=0;i<DEMO_SIZE;i++)
{
b++;
while((random()&0xffff)>fraction) b++;
list2[i]=b;
}
printf("Using %d integers ranging from %d to %d inclusive,"
" requiring %d bits\n",
DEMO_SIZE,list2[0],list2[DEMO_SIZE-1],
log2_ceil(list2[DEMO_SIZE-1]-list2[0]));
if (DEMO_SIZE<20)
{
printf(" ");
for(i=0;i<DEMO_SIZE;i++) printf("%d ",list2[i]);
printf("\n");
}
if (1)
{
printf("\n\nRecursive implementation:\n");
printf("-------------------------\n\n");
range_coder *c=range_new_coder(65536);
bzero(list2_out,sizeof(int)*DEMO_SIZE);
/* Encode */
printf("Encoding ...\n"); fflush(stdout);
last_time=gethrvtime();
ic_encode_recursive(list2,DEMO_SIZE,b,c);
this_time=gethrvtime();
range_conclude(c);
printf("%d bits written in %lldns per pointer,"
" %d pointers in range [%d,%d]\n",
c->bits_used,(this_time-last_time)/DEMO_SIZE,
DEMO_SIZE,list2[0],b);
c->bit_stream_length=c->bits_used;
c->bits_used=0;
c->value=0x80000000;
c->low=0; c->high=0xffffffff;
range_decode_prefetch(c);
mean=0;
printf("Decoding ...\n"); fflush(stdout);
last_time=gethrvtime();
ic_decode_recursive(list2_out,DEMO_SIZE,b,c);
this_time=gethrvtime();
mean+=(this_time-last_time)/DEMO_SIZE;
printf(" Used %d of %d bits during decoding at %lldns per pointer.\n",
c->bits_used,c->bit_stream_length,mean/REPEAT_COUNT);
/* Verify */
printf("Verifying ...\n");
for(i=0;i<DEMO_SIZE;i++)
{
if (list2[i]!=list2_out[i])
{
printf("Verify error at entry %d (found %d instead of %d)\n",
i,list2_out[i],list2[i]);
exit(-1);
}
}
}
printf(" -- passed\n");
return 0;
}
#endif
#endif