Check-in [ad5c25d9ab]
Overview
SHA1:ad5c25d9ab72d5dfa3bcf23b32719c5179081e4b
Date: 2014-12-26 15:50:38
User: spaskalev
Comment:Initial implementation of package mtf - a move-to-front transform.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-12-26
16:02
[e72b637df4] Initial implementation of commands/mtf (based on commands/pdc) (user: spaskalev, tags: trunk)
15:50
[ad5c25d9ab] Initial implementation of package mtf - a move-to-front transform. (user: spaskalev, tags: trunk)
2014-12-25
08:48
[f06f8cd058] Allocate the predictor's decompression buffer per Read call. (user: spaskalev, tags: trunk)
Changes

Added src/0dev.org/mtf/mtf.go version [4488b5a476].

            1  +// Package mtf provides a move-to-front encoder and decoder implementations
            2  +package mtf
            3  +
            4  +import (
            5  +	"io"
            6  +)
            7  +
            8  +// A static table with the initial condition for the mtf algorithm
            9  +var initial [256]byte = [...]byte{
           10  +	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
           11  +	16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
           12  +	32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
           13  +	48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
           14  +	64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
           15  +	80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
           16  +	96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
           17  +	112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
           18  +	128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
           19  +	144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
           20  +	160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
           21  +	176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
           22  +	192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
           23  +	208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
           24  +	224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
           25  +	240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
           26  +}
           27  +
           28  +type context struct {
           29  +	table [256]byte
           30  +}
           31  +
           32  +// Returns an MTF encoder over the provided io.Writer
           33  +func Encoder(writer io.Writer) io.Writer {
           34  +	var enc encoder
           35  +	enc.table = initial
           36  +	enc.target = writer
           37  +	return &enc
           38  +}
           39  +
           40  +type encoder struct {
           41  +	context
           42  +	target io.Writer
           43  +}
           44  +
           45  +func (c *encoder) Write(data []byte) (int, error) {
           46  +	var (
           47  +		dataLength int    = len(data)
           48  +		buffer     []byte = make([]byte, dataLength)
           49  +	)
           50  +
           51  +	// io.Write must not modify the passed data in any way
           52  +	// TODO - check sync.Pool or a local free-list for amortizing buffers
           53  +	// TODO - use a buffer with a fixed max size to avoid OOM conditions
           54  +
           55  +	// Loop over the input data
           56  +	for index, value := range data {
           57  +
           58  +		// Shortcut for sequential, equal values
           59  +		if c.table[0] == value {
           60  +			buffer[index] = 0
           61  +			continue
           62  +		}
           63  +
           64  +		// Loop over the MTF table
           65  +		for j := byte(1); j != 0; j++ {
           66  +			if c.table[j] == value {
           67  +				// Output the value
           68  +				buffer[index] = j
           69  +
           70  +				// Shift the table
           71  +				copy(c.table[1:j+1], c.table[0:j])
           72  +
           73  +				// Restore the value in front and break
           74  +				c.table[0] = value
           75  +				break
           76  +			}
           77  +		}
           78  +	}
           79  +
           80  +	return c.target.Write(buffer)
           81  +}
           82  +
           83  +// Returns an MTF decoder over the provided io.Writer
           84  +func Decoder(reader io.Reader) io.Reader {
           85  +	var dec decoder
           86  +	dec.table = initial
           87  +	dec.source = reader
           88  +	return &dec
           89  +}
           90  +
           91  +type decoder struct {
           92  +	context
           93  +	source io.Reader
           94  +}
           95  +
           96  +func (c *decoder) Read(output []byte) (int, error) {
           97  +	var (
           98  +		count    int
           99  +		err      error
          100  +		position byte
          101  +	)
          102  +
          103  +	// Read from the source and decode in place
          104  +	count, err = c.source.Read(output)
          105  +	for i := 0; i < count; i++ {
          106  +		position = output[i]
          107  +
          108  +		// Shortcut for sequential, equal values
          109  +		if position == 0 {
          110  +			output[i] = c.table[0]
          111  +			continue
          112  +		}
          113  +
          114  +		// Output the value
          115  +		output[i] = c.table[position]
          116  +
          117  +		// Shift the table and restore the value in front
          118  +		copy(c.table[1:position+1], c.table[0:position])
          119  +		c.table[0] = output[i]
          120  +	}
          121  +
          122  +	return count, err
          123  +}

Added src/0dev.org/mtf/mtf_test.go version [efb3f6ccdb].

            1  +package mtf
            2  +
            3  +import (
            4  +	diff "0dev.org/diff"
            5  +	"bytes"
            6  +	"io"
            7  +	"testing"
            8  +)
            9  +
           10  +func TestEncoder(t *testing.T) {
           11  +	var (
           12  +		input    []byte = []byte{1, 1, 0, 0}
           13  +		expected []byte = []byte{1, 0, 1, 0}
           14  +
           15  +		buffer  bytes.Buffer
           16  +		encoder io.Writer = Encoder(&buffer)
           17  +	)
           18  +
           19  +	count, err := encoder.Write(input)
           20  +	if count != len(input) {
           21  +		t.Error("Unexpected write count from encoder", count)
           22  +	}
           23  +	if err != nil {
           24  +		t.Error("Unexpected write error from encoder", err)
           25  +	}
           26  +
           27  +	output := buffer.Bytes()
           28  +
           29  +	// Diff the output against the expected result
           30  +	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
           31  +		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
           32  +	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
           33  +		t.Error("Differences detected ", delta)
           34  +	}
           35  +}
           36  +
           37  +func TestDecoder(t *testing.T) {
           38  +	var (
           39  +		input    []byte = []byte{1, 0, 1, 0}
           40  +		expected []byte = []byte{1, 1, 0, 0}
           41  +		output   []byte = make([]byte, 4)
           42  +
           43  +		reader  *bytes.Reader = bytes.NewReader(input)
           44  +		decoder io.Reader     = Decoder(reader)
           45  +	)
           46  +
           47  +	count, err := decoder.Read(output)
           48  +	if count != len(output) {
           49  +		t.Error("Unexpected read count from decoder", count)
           50  +	}
           51  +	if err != nil {
           52  +		t.Error("Unexpected read error from decoder", err)
           53  +	}
           54  +
           55  +	// Diff the output against the expected result
           56  +	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
           57  +		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
           58  +	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
           59  +		t.Error("Differences detected ", delta)
           60  +	}
           61  +}