diff --git a/Makefile b/Makefile index 1c0b9a1..1ef6101 100644 --- a/Makefile +++ b/Makefile @@ -25,27 +25,30 @@ huffman/bin/huffman: huffman/huffman.c $(CC) $(CFLAGS) -o $@ $< -lm testfiles: - 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/100K-random.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/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/100K-random.img count=100KiB + @dd if=/dev/urandom of=huffman/testfiles/1M-random.img count=1MiB + @dd if=/dev/urandom of=huffman/testfiles/10M-random.img count=10MiB + @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: - dd if=/dev/urandom of=huffman/testfiles/1M-random.img count=1MiB - dd if=/dev/urandom of=huffman/testfiles/10M-random.img count=10MiB - dd if=/dev/urandom of=huffman/testfiles/100M-random.img count=100MiB - dd if=/dev/zero of=huffman/testfiles/1M-zero.img count=1MiB - dd if=/dev/zero of=huffman/testfiles/10M-zero.img count=10MiB - dd if=/dev/zero of=huffman/testfiles/100M-zero.img count=100MiB - dd if=/dev/urandom of=huffman/testfiles/1G-random.img count=1GiB - dd if=/dev/urandom of=huffman/testfiles/10G-random.img count=10GiB - 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 + @dd if=/dev/urandom of=huffman/testfiles/100M-random.img count=100MiB + @dd if=/dev/zero of=huffman/testfiles/100M-zero.img count=100MiB + @dd if=/dev/urandom of=huffman/testfiles/1G-random.img count=1GiB + @dd if=/dev/urandom of=huffman/testfiles/10G-random.img count=10GiB + @dd if=/dev/zero of=huffman/testfiles/1G-zero.img count=1GiB + @dd if=/dev/zero of=huffman/testfiles/10G-zero.img count=10GiB + @yes 'a sla d JK J Kkl' | head -c 100MB > huffman/testfiles/big.txt + @yes ' POP OPO )()( as' | head -c 1G > huffman/testfiles/huge.txt $(EXECUTABLES): $(SOURCES) $(CC) $(CFLAGS) -o $@ $< -lm diff --git a/huffman/huffman.c b/huffman/huffman.c index 1f6c230..ee3bc2a 100644 --- a/huffman/huffman.c +++ b/huffman/huffman.c @@ -129,12 +129,10 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); exit(EXIT_FAILURE); } - if (extract_mode) { printf("extracting is not yet implemented.\n"); // decompress the file } - else { // compress the file @@ -232,7 +230,42 @@ int main(int argc, char *argv[]) { } // 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 write Tree and compression to file