huffman sort frequencies, makefile for testfiles

This commit is contained in:
Christoph J. Scherr 2022-11-27 01:55:25 +01:00
parent 364d085c7c
commit 8f1237b257
2 changed files with 56 additions and 20 deletions

View File

@ -25,27 +25,30 @@ huffman/bin/huffman: huffman/huffman.c
$(CC) $(CFLAGS) -o $@ $< -lm $(CC) $(CFLAGS) -o $@ $< -lm
testfiles: testfiles:
dd if=/dev/urandom of=huffman/testfiles/1K-random.img count=1KiB @dd if=/dev/urandom of=huffman/testfiles/1K-random.img count=1KiB
dd if=/dev/urandom of=huffman/testfiles/10K-random.img count=10KiB @dd if=/dev/urandom of=huffman/testfiles/10K-random.img count=10KiB
dd if=/dev/urandom of=huffman/testfiles/100K-random.img count=100KiB @dd if=/dev/urandom of=huffman/testfiles/100K-random.img count=100KiB
dd if=/dev/zero of=huffman/testfiles/1K-zero.img count=1KiB @dd if=/dev/urandom of=huffman/testfiles/1M-random.img count=1MiB
dd if=/dev/zero of=huffman/testfiles/10K-zero.img count=10KiB @dd if=/dev/urandom of=huffman/testfiles/10M-random.img count=10MiB
dd if=/dev/zero of=huffman/testfiles/100K-zero.img count=100KiB @dd if=/dev/zero of=huffman/testfiles/1K-zero.img count=1KiB
@dd if=/dev/zero of=huffman/testfiles/10K-zero.img count=10KiB
@dd if=/dev/zero of=huffman/testfiles/100K-zero.img count=100KiB
@dd if=/dev/zero of=huffman/testfiles/1M-zero.img count=1MiB
@dd if=/dev/zero of=huffman/testfiles/10M-zero.img count=10MiB
@echo -e "Wer\ndas\nliest\nist\ndoof\n" > huffman/testfiles/tiny.txt
@yes 'SAFJALJ AF OIAIFOsdp' | head -c 100KB > huffman/testfiles/small.txt
@yes 'lslfkpoipop iipfiasp' | head -c 1MB > huffman/testfiles/mid.txt
bigfiles: bigfiles:
dd if=/dev/urandom of=huffman/testfiles/1M-random.img count=1MiB @dd if=/dev/urandom of=huffman/testfiles/100M-random.img count=100MiB
dd if=/dev/urandom of=huffman/testfiles/10M-random.img count=10MiB @dd if=/dev/zero of=huffman/testfiles/100M-zero.img count=100MiB
dd if=/dev/urandom of=huffman/testfiles/100M-random.img count=100MiB @dd if=/dev/urandom of=huffman/testfiles/1G-random.img count=1GiB
dd if=/dev/zero of=huffman/testfiles/1M-zero.img count=1MiB @dd if=/dev/urandom of=huffman/testfiles/10G-random.img count=10GiB
dd if=/dev/zero of=huffman/testfiles/10M-zero.img count=10MiB @dd if=/dev/zero of=huffman/testfiles/1G-zero.img count=1GiB
dd if=/dev/zero of=huffman/testfiles/100M-zero.img count=100MiB @dd if=/dev/zero of=huffman/testfiles/10G-zero.img count=10GiB
dd if=/dev/urandom of=huffman/testfiles/1G-random.img count=1GiB @yes 'a sla d JK J Kkl' | head -c 100MB > huffman/testfiles/big.txt
dd if=/dev/urandom of=huffman/testfiles/10G-random.img count=10GiB @yes ' POP OPO )()( as' | head -c 1G > huffman/testfiles/huge.txt
dd if=/dev/zero of=huffman/testfiles/1G-zero.img count=1GiB
dd if=/dev/zero of=huffman/testfiles/10G-zero.img count=10GiB
@echo -e "Wer\ndas\nliest\nist\ndoof\n" > huffman/testfiles/small.txt
@man gcc > huffman/testfiles/mid.txt
$(EXECUTABLES): $(SOURCES) $(EXECUTABLES): $(SOURCES)
$(CC) $(CFLAGS) -o $@ $< -lm $(CC) $(CFLAGS) -o $@ $< -lm

View File

@ -129,12 +129,10 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (extract_mode) { if (extract_mode) {
printf("extracting is not yet implemented.\n"); printf("extracting is not yet implemented.\n");
// decompress the file // decompress the file
} }
else { else {
// compress the file // compress the file
@ -232,7 +230,42 @@ int main(int argc, char *argv[]) {
} }
// TODO sort by frequencieRaw, then sort references by frequency // TODO sort by frequencieRaw, then sort references by frequency
short* refs = malloc(256*sizeof(short));
short tmp;
for(int i = 0; i < 256; i++){
refs[i] = i;
}
if(debug){ // print refs in debug
printf("unsorted reference values:\n");
for (int i = 0; i < 256 - 1; i++){
if(i%4==0)
printf("\n");
printf("ref: %d freq: %0.02f\t", refs[i], nodes[refs[i]].frequencyRaw);
}
}
// bubblesort, i don't care. TODO might improve some time later
printf("\n");
for (int i = 0; i < 256 - 1; i++){
for (int j = 0; j < 256 - 1; j++){
if (nodes[refs[j]].frequencyRaw > nodes[refs[j + 1]].frequencyRaw){
tmp = refs[j];
refs[j] = refs[j + 1];
refs[j + 1] = tmp;
}
}
}
if(debug){ // print refs in debug
printf("sorted reference values:\n");
for (int i = 0; i < 256 - 1; i++){
if(i%4==0)
printf("\n");
printf("ref: %d \tfreq: %0.02f\t", refs[i], nodes[refs[i]].frequencyRaw);
// FIXME doesnt work for all zeros?
}
printf("\n");
}
free(refs);
// TODO build tree using Heaps // TODO build tree using Heaps
// TODO write Tree and compression to file // TODO write Tree and compression to file