Check-in [6bd6e6d5c7]
Overview
SHA1:6bd6e6d5c710d341e813e6d5425391980ff91823
Date: 2015-01-04 12:52:20
User: spaskalev
Comment:commands/mtf now uses fibonacci representation when encoding
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2015-01-04
16:45
[9ded78a659] Use a lookup table for fibonacci's encoding of bytes. (user: spaskalev, tags: trunk)
12:52
[6bd6e6d5c7] commands/mtf now uses fibonacci representation when encoding (user: spaskalev, tags: trunk)
12:24
[470d7e947b] encoding/mtf.Encoder now returns an io.Reader. This allows to encoding in place without allocating buffers. (user: spaskalev, tags: trunk)
Changes

Modified src/0dev.org/commands/mtf/main.go from [a9b931eec8] to [a0aa5f4298].

     1      1   package main
     2      2   
     3      3   import (
            4  +	fib "0dev.org/encoding/fibonacci"
     4      5   	mtf "0dev.org/encoding/mtf"
     5      6   	iou "0dev.org/ioutil"
     6      7   	"flag"
     7         -	"fmt"
     8      8   	"io"
     9      9   	"os"
    10     10   )
    11     11   
    12     12   func main() {
    13         -	d := flag.Bool("d", false, "Use to toggle decode mode")
           13  +	d := flag.Bool("d", false, "Toggle decode mode.")
    14     14   	flag.Parse()
    15     15   
    16         -	var code int
    17         -	if *d {
    18         -		code = decode(os.Stdout, os.Stdin)
    19         -	} else {
    20         -		code = encode(os.Stdout, os.Stdin)
    21         -	}
    22         -
    23         -	os.Exit(code)
    24         -}
    25         -
    26         -// Transforms the data according to the move-to-front algorithm
    27         -// I/O is buffered for better performance
    28         -func encode(output io.Writer, input io.Reader) int {
    29     16   	var (
    30         -		err     error
    31         -		encoder io.Reader = mtf.Encoder(iou.SizedReader(input, 4096))
           17  +		input  io.Reader = iou.SizedReader(os.Stdin, 4096)
           18  +		output io.Writer = iou.SizedWriter(os.Stdout, 4096)
           19  +		code   int
    32     20   	)
    33     21   
    34         -	_, err = io.Copy(output, encoder)
    35         -	if err != nil {
    36         -		fmt.Fprintln(os.Stderr, "Error while encoding.\n", err)
    37         -		return 1
           22  +	// Exit handler
           23  +	defer func() {
           24  +		os.Exit(code)
           25  +	}()
           26  +
           27  +	// Flush the output buffer
           28  +	defer output.Write(nil)
           29  +
           30  +	if *d {
           31  +		input = mtf.Decoder(fib.Decoder(input))
           32  +	} else {
           33  +		input = mtf.Encoder(input)
           34  +
           35  +		// Encode output as fibonacci integers
           36  +		output = fib.Encoder(output)
           37  +		defer output.Write(nil)
    38     38   	}
    39     39   
    40         -	return 0
    41         -}
    42         -
    43         -// Reverses MTF`ed data and writes back the original bytes
    44         -// I/O is buffered for better performance
    45         -func decode(output io.Writer, input io.Reader) int {
    46         -	var (
    47         -		err     error
    48         -		decoder io.Reader = mtf.Decoder(iou.SizedReader(input, 4096))
    49         -	)
    50         -
    51         -	_, err = io.Copy(output, decoder)
    52         -	if err != nil {
    53         -		fmt.Fprintln(os.Stderr, "Error while decoding.\n", err)
    54         -		return 1
           40  +	if _, err := io.Copy(output, input); err != nil {
           41  +		os.Stderr.WriteString("Error while transforming data.\n" + err.Error())
           42  +		code = 1
    55     43   	}
    56         -
    57         -	return 0
    58     44   }

Modified src/0dev.org/encoding/mtf/mtf_test.go from [8e70ad85db] to [74a3c46eb8].

    23     23   
    24     24   	delta := diff.Diff(diff.D{Len1: len(data), Len2: len(processed),
    25     25   		EqualFunc: func(i, j int) bool { return data[i] == processed[j] }})
    26     26   	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
    27     27   		t.Error("Differences detected ", delta)
    28     28   	}
    29     29   }
    30         -
    31         -// func TestEncoder(t *testing.T) {
    32         -// 	var (
    33         -// 		input    []byte = []byte{1, 1, 0, 0}
    34         -// 		expected []byte = []byte{1, 0, 1, 0}
    35         -
    36         -// 		buffer  bytes.Buffer
    37         -// 		encoder io.Writer = Encoder(&buffer)
    38         -// 	)
    39         -
    40         -// 	count, err := encoder.Write(input)
    41         -// 	if count != len(input) {
    42         -// 		t.Error("Unexpected write count from encoder", count)
    43         -// 	}
    44         -// 	if err != nil {
    45         -// 		t.Error("Unexpected write error from encoder", err)
    46         -// 	}
    47         -
    48         -// 	output := buffer.Bytes()
    49         -
    50         -// 	// Diff the output against the expected result
    51         -// 	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
    52         -// 		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
    53         -// 	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
    54         -// 		t.Error("Differences detected ", delta)
    55         -// 	}
    56         -// }
    57         -
    58         -// func TestDecoder(t *testing.T) {
    59         -// 	var (
    60         -// 		input    []byte = []byte{1, 0, 1, 0}
    61         -// 		expected []byte = []byte{1, 1, 0, 0}
    62         -// 		output   []byte = make([]byte, 4)
    63         -
    64         -// 		reader  *bytes.Reader = bytes.NewReader(input)
    65         -// 		decoder io.Reader     = Decoder(reader)
    66         -// 	)
    67         -
    68         -// 	count, err := decoder.Read(output)
    69         -// 	if count != len(output) {
    70         -// 		t.Error("Unexpected read count from decoder", count)
    71         -// 	}
    72         -// 	if err != nil {
    73         -// 		t.Error("Unexpected read error from decoder", err)
    74         -// 	}
    75         -
    76         -// 	// Diff the output against the expected result
    77         -// 	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
    78         -// 		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
    79         -// 	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
    80         -// 		t.Error("Differences detected ", delta)
    81         -// 	}
    82         -// }