【发布时间】:2014-10-28 04:50:38
【问题描述】:
在 RFC-1951 中有一个简单的算法可以从代码长度列表中恢复霍夫曼树,描述如下:
1) Count the number of codes for each code length. Let
bl_count[N] be the number of codes of length N, N >= 1.
2) Find the numerical value of the smallest code for each
code length:
code = 0;
bl_count[0] = 0;
for (bits = 1; bits <= MAX_BITS; bits++) {
code = (code + bl_count[bits-1]) << 1;
next_code[bits] = code;
}
3) Assign numerical values to all codes, using consecutive
values for all codes of the same length with the base
values determined at step 2. Codes that are never used
(which have a bit length of zero) must not be assigned a
value.
for (n = 0; n <= max_code; n++) {
len = tree[n].Len;
if (len != 0) {
tree[n].Code = next_code[len];
next_code[len]++;
}
但是算法中没有任何数据一致性检查。另一方面,很明显长度列表可能是无效的。长度值,因为 4 位编码不能是无效的,但是,例如,可以有更多的代码比可以编码的代码长度。
提供数据验证的最少检查集是什么?或者由于某些我错过的原因不需要此类检查?
【问题讨论】:
标签: algorithm language-agnostic deflate validation