Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( EofSymbol = 256 MaxSymbols = EofSymbol )
Variables ¶
var ( // DefaultDictionary is a huffman dictionary that is used to encode and decode data. // It is defined as a global variable in order to avoid re-creating it every time, as that is expensive. // This global value can be changed to a custom dictionary if needed which will then be reused globally. DefaultDictionary = NewDictionary() // TeeworldsFrequencyTable is the one used in Teeworlds by default. // The C++ implementation has an additional frequency on // the 256th index with the value 1517 which is overwritten // in the huffman constructor anyway, making it obsolete TeeworldsFrequencyTable = [MaxSymbols]uint32{}/* 256 elements not displayed */ )
var (
ErrHuffmanCompress = errors.New("compression error")
)
var (
ErrHuffmanDecompress = errors.New("decompression error")
)
Functions ¶
func Compress ¶
Compress compresses the given data using the default Teeworlds' dictionary.
Example ¶
data, err := huffman.Compress([]byte("hello world"))
if err != nil {
panic(err)
}
fmt.Printf("data: %v\n", data)
Output: data: [174 149 19 92 9 87 194 22 177 86 220 218 34 56 185 18 156 168 184 1]
func CompressDict ¶
func CompressDict(dict *Dictionary, data []byte) ([]byte, error)
CompressDict compresses the given data using the given dictionary.
func Decompress ¶
Decompress decompresses the given data using the default Teeworlds' dictionary.
Example ¶
data, err := huffman.Decompress([]byte{174, 149, 19, 92, 9, 87, 194, 22, 177, 86, 220, 218, 34, 56, 185, 18, 156, 168, 184, 1})
if err != nil {
panic(err)
}
fmt.Printf("data: %v\n", string(data))
Output: data: hello world
func DecompressDict ¶
func DecompressDict(dict *Dictionary, data []byte) ([]byte, error)
DecompressDict decompresses the given data using the given dictionary.
Types ¶
type Dictionary ¶
type Dictionary struct {
// contains filtered or unexported fields
}
Dictionary is a huffman lookup table/tree that is used to lookup symbols and their corresponding huffman codes.
func NewDictionary ¶
func NewDictionary() *Dictionary
NewDictionary returns a initialized lookup table that uses the Teeworlds' default frequency table, which can be found as TeeworldsFrequencyTable global variable.
func NewDictionaryWithFrequencies ¶
func NewDictionaryWithFrequencies(frequencyTable [MaxSymbols]uint32) *Dictionary
type Huffman ¶
type Huffman struct {
*Dictionary
}
func NewHuffman ¶
func NewHuffman() *Huffman
NewHuffman creates a new Huffman instance with the default dictionary.
func NewHuffmanDict ¶
func NewHuffmanDict(d *Dictionary) *Huffman
NewHuffmanDict creates a new Huffman instance with the given dictionary.
func (*Huffman) Compress ¶
Compress compresses the given data.
Example ¶
huff := huffman.NewHuffman()
data, err := huff.Compress([]byte("hello world"))
if err != nil {
panic(err)
}
fmt.Printf("data: %v\n", data)
Output: data: [174 149 19 92 9 87 194 22 177 86 220 218 34 56 185 18 156 168 184 1]
func (*Huffman) Decompress ¶
Decompress decompresses the given data.
Example ¶
huff := huffman.NewHuffman()
data, err := huff.Decompress([]byte{174, 149, 19, 92, 9, 87, 194, 22, 177, 86, 220, 218, 34, 56, 185, 18, 156, 168, 184, 1})
if err != nil {
panic(err)
}
fmt.Printf("data: %v\n", string(data))
Output: data: hello world
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
func NewReaderDict ¶
func NewReaderDict(d *Dictionary, r io.Reader) *Reader
NewReaderDict expects a Dictionary (index -> symbol) You can use the default one if you just want to work with Teeworlds' default compression.
func (*Reader) Read ¶
Decompress decompresses 'data' and writes the result into 'decompressed'. The decompressed slice must be preallocated to fit the decompressed data. Read is the size that was decompressed and written into the 'decompressed' slice.
Example ¶
data, err := huffman.Compress([]byte("hello world"))
if err != nil {
panic(err)
}
r := huffman.NewReader(bytes.NewReader(data))
out := bytes.NewBuffer(nil)
_, err = io.Copy(out, r)
if err != nil {
panic(err)
}
fmt.Println(out.String())
Output: hello world
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
func NewWriter ¶
New creates a new Writer that uses the default Teeworlds dictionary in order to compress data.
func NewWriterDict ¶
func NewWriterDict(d *Dictionary, w io.Writer) *Writer
NewWriterDict expects a Dictionary (index -> symbol) You can use the default one if you just want to work with Teeworlds' default compression.
func (*Writer) Write ¶
Write compresses the pased data and writes it to the underlying writer. The returned returned value is the number of uncompressed bytes that were written.
Example ¶
// buf can be anything you can write to, e.g. a network connection.
buf := bytes.NewBuffer(nil)
w := huffman.NewWriter(buf)
_, err := io.WriteString(w, "hello world")
if err != nil {
panic(err)
}
data, err := huffman.Decompress(buf.Bytes())
if err != nil {
panic(err)
}
fmt.Println(string(data))
Output: hello world