]> code.bitgloo.com Git - clyne/consteval-huffman.git/summary
 
descriptionCompile-time Huffman coding compression in modern C++
ownerClyne Sullivan
last changeTue, 12 Oct 2021 18:31:51 +0000 (14:31 -0400)

consteval-huffman

A C++20 utility for compressing string literals at compile-time to save program space. The compressed data can be decompressed at run-time through the use of a decoder that follows std::forward_iterator.

Compression is achieved using Huffman coding, which works by creating short codes (measured in bits) for frequently-occuring characters. This works best on larger pieces of data that are limited in their range of values (e.g. written text).

Requirements

A C++20 compliant compiler. consteval-huffman is confirmed to work on gcc 10.1 and later, doesn't work yet on clang.

Use cases

1. Text configurations

A ~3.5kB string of JSON can be compressed down ~2.5kB (see it on Godbolt).

2. Scripts

A ~40 line comment-including sample of Lisp can be reduced from 1,662 bytes to 1,251 (412 bytes saved) (Godbolt).

3. Numbers?

A string of numbers 1 to 100 separated with spaces can be compressed to 64% of its original size (Godbolt).

How to Use

```cpp // 1. Include

include

// 2. Use huffman suffix (data now stores compressed string) auto data = "This is my string of data"huffman; // "\0\x1 Non-text data works too!"

// 3. Use iterator to decompress at run-time for (char c : data) std::cout << c; ```

Use data.begin() or data.cbegin() to get an iterator for the data which decompresses the next byte with every increment.
These of course come with end() and cend().

Use data() to get a pointer to the compressed data.

Use size() to get the size of the compressed data.

Should compression not decrease the size of the given data, the data will be stored uncompressed. The above functions will still behave as they should.

shortlog
2021-10-12 clyneadd huffman_compress_array helper master
2021-10-11 clyneadd helper for compressing arrays; support unsigned...
2020-12-31 clyneMerge pull request #4 from friendlyanon/master
2020-12-31 friendlyanonInclude CPack only if top level project 4/head
2020-12-31 clyneFixed decoder::end() for uncompressed scenario
2020-12-31 clyneFix godbolt links for new include location
2020-12-31 clyneMerge pull request #1 from friendlyanon/master
2020-12-30 friendlyanonFix include path in readme 1/head
2020-12-30 friendlyanonAdd root lists file
2020-12-30 friendlyanonMove the library header to the include folder
2020-12-30 clyneadding missing returns for cbegin and cend
2020-12-30 clyneUpdate README.md
2020-12-30 clyneUpdate README.md
2020-12-30 clyneClean up readme, add guide
2020-12-29 clyneRename decoder
2020-12-29 clyneAdd data(); optimize decoder
...
heads
3 years ago master
4 years ago memoize