/* Count how many symbols have each possible codeword length. *Notethatalengthof0indicatesthecorrespondingsymbolisnot *usedinthecodeandthereforedoesnothaveacodeword.
*/ for (len = 0; len <= max_codeword_len; len++)
len_counts[len] = 0; for (sym = 0; sym < num_syms; sym++)
len_counts[lens[sym]]++;
/* We can assume all lengths are <= max_codeword_len, but we *cannotassumetheyformavalidprefixcode.Acodewordof *lengthnshouldrequireaproportionofthecodespaceequaling *(1/2)^n.Thecodeisvalidifandonlyifthecodespaceis *exactlyfilledbythelengths,bythismeasure.
*/
left = 1; for (len = 1; len <= max_codeword_len; len++) {
left <<= 1;
left -= len_counts[len]; if (left < 0) { /* The lengths overflow the codespace; that is, the code *isover-subscribed.
*/ return -1;
}
}
if (left) { /* The lengths do not fill the codespace; that is, they form an *incompleteset.
*/ if (left == (1 << max_codeword_len)) { /* The code is completely empty. This is arguably *invalid,butinfactitisvalidinLZXandXPRESS, *sowemustallowit.Bydefinition,nosymbolscan *bedecodedwithanemptycode.Consequently,we *technicallydon'tevenneedtofillinthedecode *table.However,toavoidaccessinguninitialized *memoryifthealgorithmneverthelessattemptsto *decodesymbolsusingsuchacode,wezerooutthe *decodetable.
*/
memset(decode_table, 0,
table_num_entries * sizeof(decode_table[0])); return0;
} return -1;
}
/* Sort the symbols primarily by length and secondarily by symbol order.
*/
/* Initialize 'offsets' so that offsets[len] for 1 <= len <= *max_codeword_lenisthenumberofcodewordsshorterthan'len'bits.
*/
offsets[1] = 0; for (len = 1; len < max_codeword_len; len++)
offsets[len + 1] = offsets[len] + len_counts[len];
/* Use the 'offsets' array to sort the symbols. Note that we do not *includesymbolsthatarenotusedinthecode.Consequently,fewer *than'num_syms'entriesin'sorted_syms'maybefilled.
*/ for (sym = 0; sym < num_syms; sym++) if (lens[sym])
sorted_syms[offsets[lens[sym]]++] = sym;
entry = ((u32)codeword_len << 11) | sorted_syms[sym_idx];
p = (u16 *)decode_table_ptr;
n = stores_per_loop;
do {
*p++ = entry;
} while (--n);
decode_table_ptr = p;
}
}
/* If we've filled in the entire table, we are done. Otherwise, *therearecodewordslongerthantable_bitsforwhichwemust *generatebinarytrees.
*/
decode_table_pos = (u16 *)decode_table_ptr - decode_table; if (decode_table_pos != table_num_entries) {
u32 j;
u32 next_free_tree_slot;
u32 cur_codeword;
/* First, zero out the remaining entries. This is *necessarysothattheseentriesappearas *"unallocated"inthenextpart.Eachoftheseentries *willeventuallybefilledwiththerepresentationof *therootnodeofabinarytree.
*/
j = decode_table_pos; do {
decode_table[j] = 0;
} while (++j != table_num_entries);
/* We allocate child nodes starting at the end of the *directlookuptable.Notethatthereshouldbe *2*num_symsextraentriesforthispurpose,although *fewerthanthismayactuallybeneeded.
*/
next_free_tree_slot = table_num_entries;
/* Iterate through each codeword with length greater than *'table_bits',primarilyinorderofcodewordlength *andsecondarilyinorderofsymbol.
*/ for (cur_codeword = decode_table_pos << 1;
codeword_len <= max_codeword_len;
codeword_len++, cur_codeword <<= 1) {
u32 end_sym_idx = sym_idx + len_counts[codeword_len];
for (; sym_idx < end_sym_idx; sym_idx++, cur_codeword++) { /* 'sorted_sym' is the symbol represented by the *codeword.
*/
u32 sorted_sym = sorted_syms[sym_idx];
u32 extra_bits = codeword_len - table_bits;
u32 node_idx = cur_codeword >> extra_bits;
/* Go through each bit of the current codeword *beyondtheprefixoflength@table_bitsand *walktheappropriatebinarytree,allocating *anyslotsthathavenotyetbeenallocated. * *Notethatthe'pointer'entrytothebinary *tree,whichisstoredinthedirectlookup *portionofthetable,isrepresented *identicallytootherinternal(non-leaf) *nodesofthebinarytree;itcanbethought *ofassimplytherootofthetree.The *representationoftheseinternalnodesis *simplytheindexoftheleftchildcombined *withthespecialbits0xC000todistinguish *theentryfromdirectmappingandleafnode *entries.
*/ do { /* At least one bit remains in the *codeword,butthecurrentnodeisan *unallocatedleaf.Changeittoan *internalnode.
*/ if (decode_table[node_idx] == 0) {
decode_table[node_idx] =
next_free_tree_slot | 0xC000;
decode_table[next_free_tree_slot++] = 0;
decode_table[next_free_tree_slot++] = 0;
}
/* Go to the left child if the next bit *inthecodewordis0;otherwisegoto *therightchild.
*/
node_idx = decode_table[node_idx] & 0x3FFF;
--extra_bits;
node_idx += (cur_codeword >> extra_bits) & 1;
} while (extra_bits != 0);
/* We've traversed the tree using the entire *codeword,andwe'renowattheentrywhere *theactualsymbolwillbestored.Thisis *distinguishedfrominternalnodesbynot *havingitshightwobitsset.
*/
decode_table[node_idx] = sorted_sym;
}
}
} return0;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.