Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adaptive Huffman: What's Wrong with My Code?

62 views
Skip to first unread message

Harry Potter

unread,
Nov 14, 2020, 5:32:16 PM11/14/20
to
Hi! I am working on file compression and am having a problem with debugging my version of Adaptive Huffman codes. Following is the code that recalculates the Huffman tree:
----------------------
static unsigned __fastcall GetHuffGreatestInBlock (unsigned c)
{
register struct HuffEnv_Entry* h=GetHuffEntry (512);
register unsigned j=GetHuffEntry (c)->occur;
register int c2=GetHuffEntry (c)->c;
for (tmpcptr=512; tmpcptr>=c; --tmpcptr){
if (h->occur==j /*&& c2^h->c>=0*/) return tmpcptr;
--h;
} return c;
}

void UpdateHuffCode (unsigned char c)
{
unsigned n, ch=HuffEnv.entrynum[c], x, y;
static struct HuffEnv_Entry* e1;
HuffEnv.Entry[512].parent=-1;
while (ch<512) {
n=GetHuffGreatestInBlock (ch);
e2=GetHuffEntry (ch);
e1=GetHuffEntry (n);
if (n!=ch &&
e2->parent!=n){
tmpcptr=(unsigned) e1;
if (((int)e2->c)>=0) {
HuffEnv.entrynum[e2->c]=n;
}
if (((int)e1->c)>=0) {
HuffEnv.entrynum[e1->c]=ch;
}
x=e1->c;
e1->c=e2->c;
e2->c=x;
x=e1->parent;
e1->parent=e2->parent;
e2->parent=x;
++e2->occur;
if (n>=512) break;
ch=e2->parent;
} else {
++e2->occur;
if (n>=512) break;
ch=e2->parent;
}
} HuffEnv.Entry[512].occur++;
}
--------------------------------------
GetHuffEntry() returns a pointer to the given Huffman tree entry. e1 and e2 are temp. pointers. In the Huffman tree entry struct, c is the character at the entry, or -1 if none. x is a temp. variable. Everything else should be self-explanatory. If you need more information, just ask.
0 new messages