Clean up readme, add guide

pull/1/head
Clyne 4 years ago committed by GitHub
parent 9eea49e312
commit 9b019e37aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,8 +1,12 @@
# consteval-huffman
Allows for long strings of text or data to be compressed at compile-time, with an efficient decoder routine for decompression at run-time. The decoder conforms to `std::forward_iterator`, allowing for use in ranged-for or standard algorithms.
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](https://en.wikipedia.org/wiki/Huffman_coding), which works by creating short codes (measured in bits) for frequently-occuring characters. This works best on larger pieces of data, or more so data that is limited in its range of values (e.g. written text).
Compression is achieved using [Huffman coding](https://en.wikipedia.org/wiki/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
@ -12,21 +16,31 @@ A ~3.5kB string of JSON can be compressed down ~2.5kB ([see it on Godbolt](https
**2. Scripts (e.g. Lisp)**
A ~40 line comment-including sample of Lisp can be reduced from 1,662 bytes to 1,244 (418 bytes saved) ([Godbolt](https://godbolt.org/z/Kbenbh)).
A ~40 line comment-including sample of Lisp can be reduced from 1,662 bytes to 1,251 (412 bytes saved) ([Godbolt](https://godbolt.org/z/Kbenbh)).
**3. Numbers?**
A string of numbers 1 to 100 separated with spaces can be compressed to 64% of its original size ([Godbolt](https://godbolt.org/z/Te17aM)).
## How to Use
```cpp
// 1.
#include "consteval_huffman.hpp"
// Using the _huffman suffix creates an object with the compressed data.
// If data is not smaller after compression, the object will keep the data uncompressed.
constinit auto some_data = "This is my string of data"_huffman;
// 2.
auto data = "This is my string of data"_huffman; // "\0\x1 Non-text data works too!"
int main()
{
// Decompress and print out the data
for (auto c : some_data)
putchar(c);
}
// 3.
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.

Loading…
Cancel
Save