huffman commenting

This commit is contained in:
Christoph J. Scherr 2022-11-28 08:03:10 +01:00
parent 457a655d01
commit c802cb3b62
1 changed files with 26 additions and 7 deletions

View File

@ -212,7 +212,7 @@ int main(int argc, char *argv[]) {
(float)(100*(addedUpOccurences/(float)filelen))); (float)(100*(addedUpOccurences/(float)filelen)));
} }
// TODO calculate frequenciePririties // calculate frequenciePririties
float addedUpFrequencies = 0; // only needed in debug, but it's efficient to calculate it with the loop. float addedUpFrequencies = 0; // only needed in debug, but it's efficient to calculate it with the loop.
for(int i = 0; i < 256; i++){ // calculate frequencies for(int i = 0; i < 256; i++){ // calculate frequencies
nodes[i].frequencyRaw = 100*nodes[i].occurences / (float)filelen; nodes[i].frequencyRaw = 100*nodes[i].occurences / (float)filelen;
@ -229,8 +229,8 @@ int main(int argc, char *argv[]) {
printf("\nAddedUpFrequencies: %f%%\n", addedUpFrequencies); printf("\nAddedUpFrequencies: %f%%\n", addedUpFrequencies);
} }
// TODO sort by frequencieRaw, then sort references by frequency // sort by frequencieRaw, then sort references by frequency
short* refs = malloc(256*sizeof(short)); short refs[256];
short tmp; short tmp;
for(int i = 0; i < 256; i++){ for(int i = 0; i < 256; i++){
refs[i] = i; refs[i] = i;
@ -245,6 +245,7 @@ int main(int argc, char *argv[]) {
} }
} }
// bubblesort, i don't care. TODO might improve some time later // bubblesort, i don't care. TODO might improve some time later
// FIXME doesnt work for all zeros?
printf("\n"); printf("\n");
for (int i = 0; i < 256 - 1; i++){ for (int i = 0; i < 256 - 1; i++){
for (int j = 0; j < 256 - 1; j++){ for (int j = 0; j < 256 - 1; j++){
@ -261,7 +262,6 @@ int main(int argc, char *argv[]) {
if(i%4==0) if(i%4==0)
printf("\n"); printf("\n");
printf("ref: %d \tfreq: %0.02f\t", refs[i], nodes[refs[i]].frequencyRaw); printf("ref: %d \tfreq: %0.02f\t", refs[i], nodes[refs[i]].frequencyRaw);
// FIXME doesnt work for all zeros?
} }
printf("\n"); printf("\n");
} }
@ -270,14 +270,33 @@ int main(int argc, char *argv[]) {
// frequenciesPrority: lower is more frequent. // frequenciesPrority: lower is more frequent.
nodes[refs[i]].frequencyPriority = i; nodes[refs[i]].frequencyPriority = i;
} }
// TODO rename refs array and keep until actually no longer needed. // TODO build tree
//free(refs); /*
// TODO build tree using Heaps * Start with as many leaves as there are symbols.
* Enqueue all leaf nodes into the first queue (by probability in increasing order so that the
* least likely item is in the head of the queue).
* While there is more than one node in the queues:
* Dequeue the two nodes with the lowest weight by examining the fronts of both queues.
* Create a new internal node, with the two just-removed nodes as children (either node can be
* either child) and the sum of their weights as the new weight.
* Enqueue the new node into the rear of the second queue.
* The remaining node is the root node; the tree has now been generated.
*
* Once the Huffman tree has been generated, it is traversed to generate a dictionary which maps
* the symbols to binary codes as follows:
*
* Start with current node set to the root.
* If node is not a leaf node, label the edge to the left child as 0 and the edge to the right
* child as 1. Repeat the process at both the left child and the right child.
*/
// TODO write Tree and compression to file // TODO write Tree and compression to file
// TODO specify output file using -o flag
} }
fclose(fptrR); fclose(fptrR);
fclose(fptrW);
printf("\n"); printf("\n");
if(debug){ // wait for input to end. if(debug){ // wait for input to end.
printf("Press Enter to finish.\n"); printf("Press Enter to finish.\n");