-
Notifications
You must be signed in to change notification settings - Fork 0
/
limitedkraftheap.c
342 lines (275 loc) · 9.67 KB
/
limitedkraftheap.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
337
338
339
340
341
342
// //////////////////////////////////////////////////////////
// limitedkraftheap.c
// written by Stephan Brumme, 2021
// see https://create.stephan-brumme.com/length-limited-prefix-codes/
//
#include "limitedkraftheap.h"
#include <stdlib.h> // malloc/free
#include <stdint.h> // int32_t
// related websites:
// https://cbloomrants.blogspot.com/2018/04/engel-coding-and-length-limited-huffman.html
// https://github.com/JoernEngel/joernblog/blob/master/engel_coding.md
// ----- a basic max-heap storing key/value pairs -----
typedef float Key;
typedef unsigned int Value;
/// the heap is stored as two flat arrays (one for keys, one for values)
typedef struct
{
Key* keys;
Value* values;
size_t size;
} Heap;
/// comparison function (akin std::less)
static int heap_isLess(const Heap* heap, size_t posA, size_t posB)
{
// max-heap: <
// min-heap: >
return (heap->keys[posA] < heap->keys[posB]) ? 1 : 0;
}
/// swap two heap items
static void heap_swap(Heap* heap, size_t posA, size_t posB)
{
Key tmpKey = heap->keys[posA];
heap->keys[posA] = heap->keys[posB];
heap->keys[posB] = tmpKey;
Value tmpValue = heap->values[posA];
heap->values[posA] = heap->values[posB];
heap->values[posB] = tmpValue;
}
/// move last item towards the beginning
static void heap_bubbleUp(Heap* heap)
{
size_t current = heap->size;
size_t parent = (current - 1) / 2;
while (current > 0 && heap_isLess(heap, parent, current))
{
heap_swap(heap, current, parent);
current = parent;
parent = (parent - 1) / 2;
}
}
/// move smaller items towards the end of the underlying array
static void heap_sinkDown(Heap* heap, size_t current)
{
// iterate until current item is bigger than its children
for (;;)
{
// assume the current item is already the biggest
size_t biggest = current;
// compare to left child
size_t leftPos = 2*current + 1;
if (leftPos < heap->size && heap_isLess(heap, current, leftPos))
biggest = leftPos;
// compare to right child
size_t rightPos = 2*current + 2; // same as leftPos + 1
if (rightPos < heap->size && heap_isLess(heap, biggest, rightPos))
biggest = rightPos;
// positioned at correct location ?
if (biggest == current)
return;
// restore order
heap_swap(heap, biggest, current);
// continue with next item
current = biggest;
}
}
/// erase largest key (top of the heap) and its values
static void heap_removeTop(Heap* heap)
{
// shrink by one
heap->size--;
// restore heap order
if (heap->size > 0)
{
// move smallest item to the top (where usually the largest is found)
heap->keys [0] = heap->keys [heap->size];
heap->values[0] = heap->values[heap->size];
// and let it sink down to its correct spot
heap_sinkDown(heap, 0);
}
}
/// add an item to the heap
static void heap_insert(Heap* heap, Key key, Value value)
{
// append at the end
heap->keys [heap->size] = key;
heap->values[heap->size] = value;
// move it closer to the root if big enough
heap_bubbleUp(heap);
// and of course: heap grew by one
heap->size++;
}
// ----- end of max-heap code -----
// ----- local helper function -----
// compute log(x), about 7x faster than C's log2 and no need for math.h / link -lm
static float fastlog2(float x)
{
// see https://www.flipcode.com/archives/Fast_log_Function.shtml
// posted by Laurent de Soras
// error is 4.26 * 10^-3 according to http://www1.icsi.berkeley.edu/~fractor/papers/friedland_84.pdf
// invalid input such as infinite or NaN leads to undefined results
// float format: SIGN | EXPONENT | MANTISSA
// where SIGN has 1 bit
// EXPONENT 8 bits
// MANTISSA 23 bits
#define FASTLOG2_SHIFT 23
#define FASTLOG2_MASK ((1 << 8) - 1)
// constants if x is double instead of float:
// shift = 52 and mask = 0x7FF
// (need to change all data types from float to double and int32_t to int64_t, too)
#define FASTLOG2_BIAS (FASTLOG2_MASK >> 1)
// map float and int to the same 32 bit memory location
union
{
float f;
int32_t i;
} alias = { x };
// get exponent
int32_t exponent = alias.i >> FASTLOG2_SHIFT;
exponent &= FASTLOG2_MASK; // exclude sign bit
exponent -= FASTLOG2_BIAS; // exponent's bias is 127
// now we already have the integer part
float result = exponent;
// let's set to exponent to 0 so that we get a number between 1 and 2
// clear exponent's bits (due to bias that represents -127 now / that's the bias)
alias.i &= ~(FASTLOG2_MASK << FASTLOG2_SHIFT);
// add 127 to the exponent so that it's zero now
alias.i += FASTLOG2_BIAS << FASTLOG2_SHIFT;
// there are three options for the final step:
// version A
// fast approximation of log2(m) for m between 1 and 2
// log2(x) ~ (x^2)/-3 + 2x - 5/3
//result += (alias.f * -0.333333333f + 2) * alias.f - 1.666666667f;
// for comparison:
// https://www.wolframalpha.com/input/?i=%28x%2F-3%2B2%29x+-+5%2F3+between+1+and+2
// https://www.wolframalpha.com/input/?i=log2%28x%29+between+1+and+2
// version B
// the average error of the following function is higher
// but there's zero error at the important threshold f(1.5) = log2(1.5)
//result += (alias.f * -0.33985f + 2.01955f) * alias.f - 1.6797f;
// => it's the only quadratic curve passing through (1,0), (1.5,log2(1.5)) and (2,1)
// version C
// for our use case, even this simplified approximation works (zero error at f(1.5), too):
result += 0.5849625f * alias.f; // log2(1.5) = 0.5849625
//result += 0.389975f * alias.f;
#undef FASTLOG2_SHIFT
#undef FASTLOG2_MASK
#undef FASTLOG2_BIAS
return result;
// the code at https://github.com/romeric/fastapprox/blob/master/fastapprox/src/fastlog.h
// seems to be marginally faster but I can't fully understand its magic
}
// ----- and now externally visible code -----
/// create prefix code lengths solely by optimizing the Kraft inequality
/**
* @param maxLength maximum code length, e.g. 15 for DEFLATE or JPEG
* @param numCodes number of codes, equals the array size of histogram and codeLength
* @param histogram how often each code/symbol was found
* @param codeLength [out] computed code lengths
* @result actual maximum code length, 0 if error
*/
unsigned char limitedKraftHeap(unsigned char maxLength, unsigned int numCodes, const unsigned int histogram[], unsigned char codeLengths[])
{
// my allround variable for various loops
unsigned int i;
// total number of symbols
unsigned long long sumHistogram = 0;
for (i = 0; i < numCodes; i++)
sumHistogram += histogram[i];
// 1/sumHistogram is needed multiple times lateron, let's replace division by multiplication
float invSumHistogram = 1.0f / sumHistogram;
// Kraft sum must not exceed 1
// I try avoiding floating-point due to numerical instabilities
// therefore instead of coping with 2^(-codeLength[i]) I switched to 2^(maxLength - codeLength[i])
// which is always an integer >= 1
unsigned long long one = 1 << maxLength;
// portion of "one" already consumed
unsigned long long spent = 0;
Heap heap;
heap.size = 0;
heap.keys = (float*) malloc(numCodes * sizeof(float));
heap.values = (unsigned int*) malloc(numCodes * sizeof(unsigned int));
// start with rounded optimal code length
for (i = 0; i < numCodes; i++)
{
// ignore unused
if (histogram[i] == 0)
{
codeLengths[i] = 0;
continue;
}
// compute theoretical number of bits
float entropy = -fastlog2(histogram[i] * invSumHistogram);
// and round to next integer
unsigned char rounded = (unsigned char)(entropy + 0.5f);
// at least one bit
if (rounded == 0)
rounded = 1;
// and never more than the length limit
if (rounded > maxLength)
rounded = maxLength;
// assign code length
codeLengths[i] = rounded;
// accumulate Kraft sum
spent += one >> rounded;
if (rounded < maxLength)
{
float gain = entropy - rounded;
heap_insert(&heap, gain, i);
}
}
// Kraft sum is most likely above 1 now, so we need to make a few codes one bit longer
// to shrink the Kraft below 1
// initially pick those codes that were "lucky" and rounded down
// i.e. they got a shorter code
// iterate until Kraft inequality is satisfied
while (spent > one)
{
// extract code with largest gain (theoretical entropy minus code length)
float gain = heap.keys [0];
unsigned int code = heap.values[0];
heap_removeTop(&heap);
// all valid codes except those already at maximum length
if (codeLengths[code] == 0 || codeLengths[code] >= maxLength)
continue;
// extend code by one more bit
codeLengths[code]++;
// reduce Kraft sum accordingly
spent -= one >> codeLengths[code];
// exit early if done
if (spent <= one)
break;
// re-insert into the heap
gain--;
heap_insert(&heap, gain, code);
}
// optional: Kraft sum is below one, therefore a few codes might become shorter
// this step can be skipped, we already have created a (suboptimal) prefix code
while (spent < one && heap.size > 0)
{
// extract code with largest gain (theoretical entropy minus code length)
unsigned int code = heap.values[0];
heap_removeTop(&heap);
// check if removing one bit still preserves Kraft inequality
unsigned long long have = one >> codeLengths[code];
if (one - spent >= have)
{
// yes, adjust this code
codeLengths[code]--;
spent += have;
}
}
// don't be leaking ...
free(heap.keys);
free(heap.values);
// find longest code
unsigned char result = 0;
for (i = 0; i < numCodes; i++)
if (result < codeLengths[i])
{
result = codeLengths[i];
if (result == maxLength)
break;
}
return result;
}