huffman calculate frequencies

This commit is contained in:
Christoph J. Scherr 2022-11-26 22:48:15 +01:00
parent 4c688c1742
commit 364d085c7c
2 changed files with 22 additions and 3 deletions

View File

@ -19,7 +19,7 @@ pre:
@mkdir -p bin huffman/bin huffman/testfiles @mkdir -p bin huffman/bin huffman/testfiles
clean: clean:
rm -rvf bin huffman/bin rm -rvf bin huffman/testfiles huffman/bin
huffman/bin/huffman: huffman/huffman.c huffman/bin/huffman: huffman/huffman.c
$(CC) $(CFLAGS) -o $@ $< -lm $(CC) $(CFLAGS) -o $@ $< -lm

View File

@ -16,6 +16,7 @@ struct Node {
uint8_t byte; uint8_t byte;
size_t occurences; size_t occurences;
unsigned frequencyPriority; unsigned frequencyPriority;
float frequencyRaw;
}; };
// a combination of nodes. // a combination of nodes.
@ -38,9 +39,9 @@ void initNodes(){
nodes[i].byte = i; nodes[i].byte = i;
nodes[i].occurences = 0; nodes[i].occurences = 0;
nodes[i].frequencyPriority = 0; nodes[i].frequencyPriority = 0;
nodes[i].frequencyRaw = -1;
heaps[i].isRoot = false; heaps[i].isRoot = false;
} }
} }
// stolen from stackoverflow // stolen from stackoverflow
@ -77,6 +78,7 @@ int main(int argc, char *argv[]) {
FILE *fptrR = NULL; // file pointer for reading FILE *fptrR = NULL; // file pointer for reading
FILE *fptrW = NULL; // file pointer for writing FILE *fptrW = NULL; // file pointer for writing
// process command line options
while ((opt = getopt(argc, argv, "dvxhf:")) != -1) { while ((opt = getopt(argc, argv, "dvxhf:")) != -1) {
if (debug) if (debug)
printf("optarg is: %s\n", optarg); printf("optarg is: %s\n", optarg);
@ -103,7 +105,7 @@ int main(int argc, char *argv[]) {
} }
} }
// Now optind (declared extern int by <unistd.h>) is the index of the first // Now optint (declared extern int by <unistd.h>) is the index of the first
// non-option argument. If it is >= argc, there were no non-option arguments. // non-option argument. If it is >= argc, there were no non-option arguments.
if (verbose) if (verbose)
@ -213,6 +215,23 @@ int main(int argc, char *argv[]) {
} }
// TODO calculate frequenciePririties // TODO calculate frequenciePririties
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
nodes[i].frequencyRaw = 100*nodes[i].occurences / (float)filelen;
addedUpFrequencies += nodes[i].frequencyRaw;
}
if(debug){ // print frequencies in debug mode
printf("Raw Frequencies of bytes:\n");
for(int i = 0; i < 256; i++){
printf("0x%02x: %lf\t", i, nodes[i].frequencyRaw);
if(i%5==0)
printf("\n");
}
printf("\nAddedUpFrequencies: %f%%\n", addedUpFrequencies);
}
// TODO sort by frequencieRaw, then sort references by frequency
// TODO build tree using Heaps // TODO build tree using Heaps