MTF + Zero-Run RLE

medium · compression, transform, rle

MTF + Zero-Run RLE Pipeline

Implement Move-to-Front (MTF) encoding/decoding and a zero-run RLE stage.

Functions

func MTFEncode(data []byte) []int
func MTFDecode(indices []int) []byte

func RLEZeroEncode(values []int) []int
func RLEZeroDecode(values []int) []int

func PipelineEncode(data []byte) []int
func PipelineDecode(values []int) []byte

MTF rules

  • Maintain an ordered list of symbols 0..255.
  • For each byte, output its index and move that symbol to the front.
  • Decoding mirrors this process.

Zero-run RLE rules

  • Input values are non-negative integers (MTF output).
  • Replace any run of k >= 1 zeros with a single negative value -k.
  • Non-zero values are emitted unchanged.
  • Decoding expands negative values back into that many zeros.

Pipeline

  • PipelineEncode = MTFEncode then RLEZeroEncode.
  • PipelineDecode = RLEZeroDecode then MTFDecode.
Run tests to see results
No issues detected