static void Select_on (int k, heap_comp hc) { int i, num, mem; WordData **wd; Alloc_keep_discard (); num = Num[0] + Num[1]; wd = Xmalloc (num * sizeof (WordData *)); for (i = 0; i < Num[0]; i++) wd[i] = Words[0] + i; for (i = 0; i < Num[1]; i++) wd[i + Num[0]] = Words[1] + i; heap_build (wd, sizeof (*wd), num, hc); mem = 0; while (k > mem && num) { int idx; WordData *word = wd[0]; #ifdef DEBUG fprintf (stderr, "%4d:%4d:%8d :%8d :%8d : \"%s\"\n", keep[0].num_wds, keep[1].num_wds, mem, word->freq, word->occur_num, word2str (word->word)); #endif mem += sizeof (u_char *) + word->word[0]; heap_deletehead (wd, sizeof (*wd), &num, hc); idx = KIND (word); keep[idx].wd[keep[idx].num_wds++] = word; } for (i = 0; i < num; i++) { WordData *word = wd[i]; int idx = KIND (word); discard[idx].wd[discard[idx].num_wds++] = word; } SortAndCount_DictData (&keep[0]); SortAndCount_DictData (&keep[1]); SortAndCount_DictData (&discard[0]); SortAndCount_DictData (&discard[1]); assert (keep[0].num_wds + discard[0].num_wds == Num[0]); assert (keep[1].num_wds + discard[1].num_wds == Num[1]); }
static void game_findmove(char*s) { char buf[MAXLINE]; Rack rack; Play play; char c; sscanf(s, "%s\n", buf); printf("findmove [%s]\n", buf); str2rack(buf,&rack); rackprint(&rack); if(findplay(&play, &rack)) { word2str(&play.word, buf); printf("m %d %d %c %s\n", play.pos.x, play.pos.y, ori2c(play.o), buf); } else { printf("no word can be found\n"); } }
int main(int argc, char *argv[]) { int ia[5] = {1,2,3,4,5}; char hello[] = "Hello world"; char seq[] = "ACGT"; int word; char *str; checkInt("reverse_int(1)", 1, ia[0]); reverse_int(ia,5); checkInt("reverse_int(2)", 5, ia[0]); checkStr("reverse_seq(1)", "Hello world", hello); reverse_seq(hello); checkStr("reverse_seq(2)", "dlrow olleH", hello); word = str2word(seq, 4); checkInt("str2word(1)", 27, word); str = word2str(228, 4); checkStr("word2str(1)", "TGCA", str); return 0; }
static void Method3 (int k) { int i, keep_num, discard_num, mem, num_trans, recalc_reqd; int keep_to_discard = 0; int discard_to_keep = 0; int recalcs = 0; double freqs_trans[2], total; u_long escape[2]; WordData **keep_heap, **discard_heap; freqs_trans[0] = freqs_trans[1] = 0; num_trans = 0; discard_num = Num[0] + Num[1]; discard_heap = Xmalloc (discard_num * sizeof (WordData *)); keep_num = 0; keep_heap = Xmalloc (discard_num * sizeof (WordData *)); for (i = 0; i < Num[0]; i++) discard_heap[i] = Words[0] + i; for (i = 0; i < Num[1]; i++) discard_heap[i + Num[0]] = Words[1] + i; heap_build (discard_heap, sizeof (*discard_heap), discard_num, DecFreqIncWL); mem = 0; while (k > mem && discard_num) { WordData *word = discard_heap[0]; mem += sizeof (u_char *) + word->word[0]; heap_deletehead (discard_heap, sizeof (word), &discard_num, DecFreqIncWL); keep_heap[keep_num++] = word; freqs_trans[KIND (word)] += word->freq; num_trans++; } CalcBitCost (discard_heap, discard_num, keep_heap, keep_num, freqs_trans, escape, num_trans); heap_build (discard_heap, sizeof (*discard_heap), discard_num, BigSaving); heap_build (keep_heap, sizeof (*keep_heap), keep_num, SmallSaving); total = 0; recalc_reqd = 0; while (keep_num && discard_num) { if ((keep_num && keep_heap[0]->num_trans < num_trans) || (discard_num && discard_heap[0]->num_trans < num_trans)) { if (keep_num && keep_heap[0]->num_trans < num_trans) { WordData *word = keep_heap[0]; int idx = KIND (word); float wbc; #ifdef DEBUG1 fprintf (stderr, "KEEP \"%s\" %.2f ->", word2str (word->word), word->saving); #endif wbc = -log2 (word->freq / (freqs_trans[idx] + escape[idx])) * word->freq; word->saving = (word->char_bit_cost - wbc) / (sizeof (char *) + word->word[0]); #ifdef DEBUG1 fprintf (stderr, " %.2f\n", word->saving); #endif word->num_trans = num_trans; heap_changedhead (keep_heap, sizeof (word), keep_num, SmallSaving); } if (discard_num && discard_heap[0]->num_trans < num_trans) { WordData *word = discard_heap[0]; int idx = KIND (word); float wbc; #ifdef DEBUG1 fprintf (stderr, "DISCARD \"%s\" %.2f ->", word2str (word->word), word->saving); #endif wbc = -log2 (word->freq / (freqs_trans[idx] + escape[idx])) * word->freq; word->saving = (word->char_bit_cost - wbc) / (sizeof (char *) + word->word[0]); #ifdef DEBUG1 fprintf (stderr, " %.2f\n", word->saving); #endif word->num_trans = num_trans; heap_changedhead (discard_heap, sizeof (word), discard_num, BigSaving); } } else if (keep_heap[0]->saving < discard_heap[0]->saving) { assert (keep_num && discard_num); if (keep_num && mem + sizeof (char *) + discard_heap[0]->word[0] > k) { /* Transfer the word at the top of the keep heap to the discard heap. */ WordData *word = keep_heap[0]; int idx = KIND (word); heap_deletehead (keep_heap, sizeof (word), &keep_num, SmallSaving); discard_heap[discard_num] = word; heap_additem (discard_heap, sizeof (word), &discard_num, BigSaving); freqs_trans[idx] -= word->freq; mem -= sizeof (u_char *) + word->word[0]; num_trans++; total += word->saving; keep_to_discard++; #ifdef DEBUG fprintf (stderr, "KEEP -> DISCARD %8d :%8d :%8.0f :%8.0f : \"%s\"\n", mem, word->freq, word->char_bit_cost, word->saving, word2str (word->word)); #endif } else { /* Transfer the word at the top of the discard heap to the keep heap. */ WordData *word = discard_heap[0]; int idx = KIND (word); heap_deletehead (discard_heap, sizeof (word), &discard_num, BigSaving); keep_heap[keep_num] = word; heap_additem (keep_heap, sizeof (word), &keep_num, SmallSaving); freqs_trans[idx] += word->freq; mem += sizeof (u_char *) + word->word[0]; num_trans++; total += word->saving; discard_to_keep++; #ifdef DEBUG fprintf (stderr, "DISCARD -> KEEP %8d :%8d :%8.0f :%8.0f : \"%s\"\n", mem, word->freq, word->char_bit_cost, word->saving, word2str (word->word)); #endif } recalc_reqd = 1; } else { if (recalc_reqd == 0) break; #ifdef DEBUG1 fprintf (stderr, "--------------\n"); #endif if (recalcs == MAX_RECALCULATIONS) break; CalcBitCost (discard_heap, discard_num, keep_heap, keep_num, freqs_trans, escape, num_trans); heap_build (discard_heap, sizeof (*discard_heap), discard_num, BigSaving); heap_build (keep_heap, sizeof (keep_heap), keep_num, SmallSaving); recalc_reqd = 0; recalcs++; } } Alloc_keep_discard (); for (i = 0; i < discard_num; i++) { WordData *word = discard_heap[i]; int idx = KIND (word); assert (IsWord (word) || IsNonWord (word)); discard[idx].wd[discard[idx].num_wds++] = word; } for (i = 0; i < keep_num; i++) { WordData *word = keep_heap[i]; int idx = KIND (word); assert (IsWord (word) || IsNonWord (word)); keep[idx].wd[keep[idx].num_wds++] = word; } SortAndCount_DictData (&keep[0]); SortAndCount_DictData (&keep[1]); SortAndCount_DictData (&discard[0]); SortAndCount_DictData (&discard[1]); assert (keep[0].num_wds + discard[0].num_wds == Num[0]); assert (keep[1].num_wds + discard[1].num_wds == Num[1]); Message ("Keep -> Discard : %8d", keep_to_discard); Message ("Discard -> Keep : %8d", discard_to_keep); Message ("Huffman Recalculations : %8d", recalcs); if (recalcs == MAX_RECALCULATIONS) Message ("WARNING: The number of recalculations == %d", MAX_RECALCULATIONS); }