/** * @brief Do the increments */ static void increment(huff_t *huff, node_t *node) { if (!node) { return; } if (node->next != NULL && node->next->weight == node->weight) { node_t *lnode = *node->head; if (lnode != node->parent) { swap(huff, lnode, node); } swaplist(lnode, node); } if (node->prev && node->prev->weight == node->weight) { *node->head = node->prev; } else { *node->head = NULL; free_ppnode(huff, node->head); } node->weight++; if (node->next && node->next->weight == node->weight) { node->head = node->next->head; } else { node->head = get_ppnode(huff); *node->head = node; } if (node->parent) { increment(huff, node->parent); if (node->prev == node->parent) { swaplist(node, node->parent); if (*node->head == node) { *node->head = node->parent; } } } }
/* Do the increments */ static void increment(Huff* huff, Node *n) { Node *ln; if(n == nil) return; if(n->next != nil && n->next->weight == n->weight){ ln = *n->head; if(ln != n->parent) swap(huff, ln, n); swaplist(ln, n); } if(n->prev && n->prev->weight == n->weight) *n->head = n->prev; else{ *n->head = nil; free_ppnode(huff, n->head); } n->weight++; if(n->next && n->next->weight == n->weight) n->head = n->next->head; else{ n->head = get_ppnode(huff); *n->head = n; } if(n->parent){ increment(huff, n->parent); if(n->prev == n->parent){ swaplist(n, n->parent); if(*n->head == n) *n->head = n->parent; } } }
void Huff_addRef(huff_t *huff, byte ch) { if (huff->loc[ch] == NULL) // if this is the first transmission of this node { node_t *tnode = &(huff->nodeList[huff->blocNode++]); node_t *tnode2 = &(huff->nodeList[huff->blocNode++]); tnode2->symbol = INTERNAL_NODE; tnode2->weight = 1; tnode2->next = huff->lhead->next; if (huff->lhead->next) { huff->lhead->next->prev = tnode2; if (huff->lhead->next->weight == 1) { tnode2->head = huff->lhead->next->head; } else { tnode2->head = get_ppnode(huff); *tnode2->head = tnode2; } } else { tnode2->head = get_ppnode(huff); *tnode2->head = tnode2; } huff->lhead->next = tnode2; tnode2->prev = huff->lhead; tnode->symbol = ch; tnode->weight = 1; tnode->next = huff->lhead->next; if (huff->lhead->next) { huff->lhead->next->prev = tnode; if (huff->lhead->next->weight == 1) { tnode->head = huff->lhead->next->head; } else { // this should never happen tnode->head = get_ppnode(huff); *tnode->head = tnode2; } } else { // this should never happen tnode->head = get_ppnode(huff); *tnode->head = tnode; } huff->lhead->next = tnode; tnode->prev = huff->lhead; tnode->left = tnode->right = NULL; if (huff->lhead->parent) { if (huff->lhead->parent->left == huff->lhead) // lhead is guaranteed to by the NYT { huff->lhead->parent->left = tnode2; } else { huff->lhead->parent->right = tnode2; } } else { huff->tree = tnode2; } tnode2->right = tnode; tnode2->left = huff->lhead; tnode2->parent = huff->lhead->parent; huff->lhead->parent = tnode->parent = tnode2; huff->loc[ch] = tnode; increment(huff, tnode2->parent); } else { increment(huff, huff->loc[ch]); } }
void huffaddref(Huff* h, byte ch) { Node *tn, *tn2; if(h->loc[ch] != nil){ increment(h, h->loc[ch]); return; } /* else, this is the first transmission of this node */ tn = &(h->nodeList[h->blocNode++]); tn2 = &(h->nodeList[h->blocNode++]); tn2->symbol = INTERNAL_NODE; tn2->weight = 1; tn2->next = h->lhead->next; if(h->lhead->next){ h->lhead->next->prev = tn2; if(h->lhead->next->weight == 1) tn2->head = h->lhead->next->head; else{ tn2->head = get_ppnode(h); *tn2->head = tn2; } }else{ tn2->head = get_ppnode(h); *tn2->head = tn2; } h->lhead->next = tn2; tn2->prev = h->lhead; tn->symbol = ch; tn->weight = 1; tn->next = h->lhead->next; if(h->lhead->next){ h->lhead->next->prev = tn; if(h->lhead->next->weight == 1) tn->head = h->lhead->next->head; else{ /* this should never happen */ tn->head = get_ppnode(h); *tn->head = tn2; } }else{ /* this should never happen */ tn->head = get_ppnode(h); *tn->head = tn; } h->lhead->next = tn; tn->prev = h->lhead; tn->left = tn->right = nil; if(h->lhead->parent){ if(h->lhead->parent->left == h->lhead) /* lhead is guaranteed to be the NYT */ h->lhead->parent->left = tn2; else h->lhead->parent->right = tn2; }else h->tree = tn2; tn2->right = tn; tn2->left = h->lhead; tn2->parent = h->lhead->parent; h->lhead->parent = tn->parent = tn2; h->loc[ch] = tn; increment(h, tn2->parent); }