huffman

package
v0.0.0-...-de997bf Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const EndOfHeaderLength = ":"

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code byte
const (
	CodeZero      Code = 0
	CodeOne       Code = 1
	NodeSeparator      = " "
)

type Compressor

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

func NewCompressor

func NewCompressor(inputFile, outputFile string) *Compressor

func (*Compressor) Compress

func (c *Compressor) Compress() error

type Decompressor

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

func NewDecompressor

func NewDecompressor(inputFile, outputFile string) *Decompressor

func (*Decompressor) Decompress

func (d *Decompressor) Decompress() error

type Node

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

func FromHeader

func FromHeader(header string) (*Node, error)

FromHeader deserializes the header string in a Huffman tree.

func NewHuffmanTree

func NewHuffmanTree(pq heap.Interface) *Node

NewHuffmanTree builds a HuffmanTree based on the order of weights from the priority queue (ref: https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/Huffman.html).

func (*Node) ToHeader

func (h *Node) ToHeader() string

ToHeader serializes the tree in a string format using pre-order traversal. The Node's weight is not necessary for either compression and decompression.

func (*Node) ToLookup

func (h *Node) ToLookup() map[rune]string

ToLookup assigns the Huffman Code path on the tree to each rune to generate a lookup map, ex: The tree below (using chars instead of runes to simplify the example):

	   3
	  0  1
	 A    8
          0  1
	    C    D

Becomes: {A: 0, C: 10, B: 11}

func (*Node) ToReverseLookup

func (h *Node) ToReverseLookup() map[string]rune

ToReverseLookup assigns runes to Huffman Code paths to generate a reverse lookup map.

func (*Node) Weight

func (h *Node) Weight() int

Implements the Weighter interface required on the PriorityQueue.

type PriorityQueue

type PriorityQueue []Weighter

func NewPriorityQueue

func NewPriorityQueue(runeFrequency map[rune]int) *PriorityQueue

NewPriorityQueue returns a PriorityQueue of Nodes with min heap property based on runeFrequency map.

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() any

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(item any)

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

type Weighter

type Weighter interface {
	Weight() int
}

Jump to

Keyboard shortcuts

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