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

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

1
2
3

4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19




20
21
22
23
24






25
26
27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
42










43
44
45
46
47
48
49

50
51

52
53
54


55
56
57
58
1
2
3
4
5
6
7

8
9
10
11
12

13
14
15




16
17
18
19





20
21
22
23
24
25
26









27
28








29
30
31
32
33
34
35
36
37
38







39


40



41
42
43


44



+



-





-
+


-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
-
-
+
-
-
-
+
+

-
-

package main

import (
	fib "0dev.org/encoding/fibonacci"
	mtf "0dev.org/encoding/mtf"
	iou "0dev.org/ioutil"
	"flag"
	"fmt"
	"io"
	"os"
)

func main() {
	d := flag.Bool("d", false, "Use to toggle decode mode")
	d := flag.Bool("d", false, "Toggle decode mode.")
	flag.Parse()

	var code int
	if *d {
		code = decode(os.Stdout, os.Stdin)
	} else {
	var (
		input  io.Reader = iou.SizedReader(os.Stdin, 4096)
		output io.Writer = iou.SizedWriter(os.Stdout, 4096)
		code   int
		code = encode(os.Stdout, os.Stdin)
	}

	os.Exit(code)
}
	)

	// Exit handler
	defer func() {
		os.Exit(code)
	}()

// Transforms the data according to the move-to-front algorithm
// I/O is buffered for better performance
func encode(output io.Writer, input io.Reader) int {
	var (
		err     error
		encoder io.Reader = mtf.Encoder(iou.SizedReader(input, 4096))
	)

	_, err = io.Copy(output, encoder)
	// Flush the output buffer
	defer output.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while encoding.\n", err)
		return 1
	}

	return 0
}


	if *d {
		input = mtf.Decoder(fib.Decoder(input))
	} else {
		input = mtf.Encoder(input)

		// Encode output as fibonacci integers
		output = fib.Encoder(output)
		defer output.Write(nil)
	}
// Reverses MTF`ed data and writes back the original bytes
// I/O is buffered for better performance
func decode(output io.Writer, input io.Reader) int {
	var (
		err     error
		decoder io.Reader = mtf.Decoder(iou.SizedReader(input, 4096))
	)


	_, err = io.Copy(output, decoder)
	if _, err := io.Copy(output, input); err != nil {
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decoding.\n", err)
		return 1
		os.Stderr.WriteString("Error while transforming data.\n" + err.Error())
		code = 1
	}

	return 0
}

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

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
23
24
25
26
27
28
29




























































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

	delta := diff.Diff(diff.D{Len1: len(data), Len2: len(processed),
		EqualFunc: func(i, j int) bool { return data[i] == processed[j] }})
	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
		t.Error("Differences detected ", delta)
	}
}

// func TestEncoder(t *testing.T) {
// 	var (
// 		input    []byte = []byte{1, 1, 0, 0}
// 		expected []byte = []byte{1, 0, 1, 0}

// 		buffer  bytes.Buffer
// 		encoder io.Writer = Encoder(&buffer)
// 	)

// 	count, err := encoder.Write(input)
// 	if count != len(input) {
// 		t.Error("Unexpected write count from encoder", count)
// 	}
// 	if err != nil {
// 		t.Error("Unexpected write error from encoder", err)
// 	}

// 	output := buffer.Bytes()

// 	// Diff the output against the expected result
// 	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
// 		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
// 	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
// 		t.Error("Differences detected ", delta)
// 	}
// }

// func TestDecoder(t *testing.T) {
// 	var (
// 		input    []byte = []byte{1, 0, 1, 0}
// 		expected []byte = []byte{1, 1, 0, 0}
// 		output   []byte = make([]byte, 4)

// 		reader  *bytes.Reader = bytes.NewReader(input)
// 		decoder io.Reader     = Decoder(reader)
// 	)

// 	count, err := decoder.Read(output)
// 	if count != len(output) {
// 		t.Error("Unexpected read count from decoder", count)
// 	}
// 	if err != nil {
// 		t.Error("Unexpected read error from decoder", err)
// 	}

// 	// Diff the output against the expected result
// 	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
// 		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
// 	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
// 		t.Error("Differences detected ", delta)
// 	}
// }