/* Builds Huffman code for a string @str. * * @params str Input string. * @return Huffman code. */ bitset encode_to_huffman(std::string str) { bitset code; map<char, uint> freq_table = make_freq_table(str); HuffmanTree* huff_tree = build_tree(freq_table); map<char, bitset> code_table = count_codes(huff_tree); tree_traversal(huff_tree, 0); // encoding each character of @str for (uint i = 0; i < str.length(); i++) { bitset c_code = code_table[str[i]]; code.insert(code.end(), c_code.begin(), c_code.end()); } delete huff_tree; huff_tree = nullptr; return code; }
int main() { int i,k; double temp,aux; FILE *fp; /* read the opacity table (opacity should be read in cgs units !!!) */ /* opacity table should be lambda, abs, scat, increasing lambda */ read_opacity(&opacity); /* make the table of the frequencies you want to reemit the photons */ make_freq_table(&reemit); fp = fopen("ftable.dat", "w"); for (k=0; k<NFREQ ; k++) fprintf(fp,"%.4e\n", reemit.freq[k]); fclose(fp); /* calculate the reemition probability */ /* NOTE: ASSUMES THAT THE PHOTONS WILL BE REEMITTED IN THE CHOSEN F INTERVAL ! */ aux=1.*(MAX_TEMP-1)/(TEMPDIM-1); for(k=1;k<TEMPDIM;k++) { temp=aux*k; reemit.prob[k][0]=0.; for (i=1;i<NFREQ;i++) reemit.prob[k][i]=reemit.prob[k][i-1]+ (simp_kdb(reemit.freq[i-1],reemit.freq[i],temp,4)); for (i=1;i<NFREQ;i++) reemit.prob[k][i]=reemit.prob[k][i]/reemit.prob[k][NFREQ-1]; /* doing some diagnositcs */ printf("\nTEMP=%4.2f\n",temp); for (i=0;i<NFREQ;i=i+100) printf("%.4e\n",reemit.prob[k][i]); } /* write ptable.dat (table of reemission probabilities) */ fp = fopen("ptable.dat", "w"); for (i=1;i<TEMPDIM; i++) { for (k=0; k<NFREQ ; k++) fprintf(fp,"%.2e ", reemit.prob[i][k]); fprintf(fp,"\n"); } fclose(fp); return (0); }
HuffmanTree* build_tree(const string& str) { return build_tree(make_freq_table(str)); }