huffman

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: Zlib Imports: 5 Imported by: 2

README

huffman

Go Reference Go Report Card

Teeworlds huffman compression library.

Installation


// for latest tagged released
go get github.com/teeworlds-go/huffman/v2@latest

// for bleeding edge master branch version
go get github.com/teeworlds-go/huffman@master

Sample usage

package main

import (
	"fmt"

	"github.com/teeworlds-go/huffman/v2"
)

func main() {
	data, err := huffman.Compress([]byte("hello world"))
	if err != nil {
		panic(err)
	}
	// data: [174 149 19 92 9 87 194 22 177 86 220 218 34 56 185 18 156 168 184 1]
	fmt.Printf("data: %v\n", data)

	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)
	}
	// data: hello world
	fmt.Printf("data: %v\n", string(data))
}

Documentation

Index

Examples

Constants

View Source
const (
	EofSymbol  = 256
	MaxSymbols = EofSymbol
)

Variables

View Source
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 */

)
View Source
var (
	ErrHuffmanCompress = errors.New("compression error")
)
View Source
var (
	ErrHuffmanDecompress = errors.New("decompression error")
)

Functions

func Compress

func Compress(data []byte) ([]byte, error)

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

func Decompress(data []byte) ([]byte, error)

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

func (huff *Huffman) Compress(data []byte) ([]byte, error)

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

func (huff *Huffman) Decompress(data []byte) ([]byte, error)

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 NewReader

func NewReader(w io.Reader) *Reader

New creates a new Reader with the default Teeworlds' dictionary.

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

func (r *Reader) Read(decompressed []byte) (read int, err error)

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

func (*Reader) Reset

func (h *Reader) Reset(r io.Reader)

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer) *Writer

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) Reset

func (h *Writer) Reset(w io.Writer)

func (*Writer) Write

func (h *Writer) Write(data []byte) (written int, err error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL