void resetPQ(RDS *rds, uint p_num) { PAIR **p_que = rds->p_que; PAIR *pair = p_que[p_num]; PAIR *q; p_que[p_num] = NULL; while (pair != NULL) { q = pair->p_next; destructPair(rds, pair); pair = q; } }
static void deletePQ(CRDS *crds, uint p_pos, PCODE pcode) { PQUE *p_que = crds->p_que[pcode]; PAIR *pair = p_que->p_head[p_pos]; PAIR *q; p_que->p_head[p_pos] = NULL; while (pair != NULL) { q = pair->p_next; destructPair(crds, pair); pair = q; } }
void decrementPair(RDS *rds, PAIR *target) { uint h; if (target->freq > rds->p_max) { target->freq--; return; } if (target->freq == 1) { destructPair(rds, target); } else { removePair_PQ(rds, target, target->freq); target->freq--; insertPair_PQ(rds, target, target->freq); } }
uint replacePairs(RDS *rds, PAIR *max_pair, CODE new_code) { uint i, j; uint num_replaced = 0; SEQ *seq = rds->seq; i = max_pair->f_pos; while (i != DUMMY_POS) { j = seq[i].next; if (j == rightPos_SQ(rds, i)) { j = seq[j].next; } updateBlock_SQ(rds, new_code, i); i = j; num_replaced++; } if (max_pair->freq != 1) { destructPair(rds, max_pair); } resetPQ(rds, 1); return num_replaced; }
DICT *RunCodeRepair(FILE *input, uint code_len, uint cont_len, uint mchar_size) { uint i, j; CRDS *crds; DICT *dict; CODE new_code; PAIR **mp_ary; uint limit = (uint)pow(2, code_len); uint num_loop, num_replaced; uint t_num_rules, c_seq_len; double comp_ratio; //initialization #ifdef DISPLAY printf("\n"); printf("Initializing ...\n"); #endif crds = createCRDS(input, cont_len, mchar_size); dict = createDict(crds, code_len); #ifdef DISPLAY printf("///////////////////////////////////////\n"); printf(" Input text size = %d (bytes).\n", crds->txt_len); printf(" Alphabet size = %d.\n", crds->char_size); printf(" # of contexts = %d.\n", crds->num_contexts); printf(" Code length = %d (bits).\n", code_len); printf(" # of new_code = %d.\n", limit - crds->char_size); printf("///////////////////////////////////////\n"); printf("\n"); printf("Compressing text ...\n"); #endif mp_ary = (PAIR **)malloc(sizeof(PAIR *) * (crds->num_contexts + 1)); num_loop = 0; num_replaced = 0; new_code = crds->char_size; t_num_rules = 0; c_seq_len = crds->txt_len; //select replaced pairs while (new_code < limit) { for (i = 0; i <= crds->num_contexts; i++) { mp_ary[i] = NULL; } for (i = 0; i < crds->num_contexts; i++) { mp_ary[i] = getMaxPair(crds, i); } //sort mp_ary by frequencies. qsort(mp_ary, crds->num_contexts + 1, sizeof(PAIR *), (int(*)(const void *, const void *))comparePair); //if mp_ary is empty, then break. if (mp_ary[0] == NULL) break; //replace pairs for (i = 0; mp_ary[i] != NULL; i++) { addNewPair(dict, new_code, mp_ary[i]); c_seq_len -= replacePairs(crds, mp_ary[i], new_code); t_num_rules++; } #ifdef DISPLAY comp_ratio = calCompRatio(crds->txt_len, crds->char_size, crds->cont_len, crds->num_contexts, c_seq_len, t_num_rules, code_len, false); printf("\r"); printf("new_code = [%5d], Comp.ratio = %0.3f %%.",new_code, comp_ratio); fflush(stdout); #endif //free replaced pairs for (i = 0; mp_ary[i] != NULL; i++) { destructPair(crds, mp_ary[i]); } //free unused pairs for (i = 0; i < crds->num_contexts; i++) { for (j = 1; j < THRESHOLD; j++) { deletePQ(crds, j, i); } } new_code++; } #ifdef DISPLAY printf("\n"); calCompRatio(crds->txt_len, crds->char_size, crds->cont_len, crds->num_contexts, c_seq_len, t_num_rules, code_len, true); #endif //post processing copyCompSeq(crds, dict); free(mp_ary); destructCRDS(crds); return dict; }