summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/triegen/example_compact_test.go
blob: 7cf604ca474cce71119c7843fac3e53fd1d0a566 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package triegen_test

import (
	"fmt"
	"io"
	"io/ioutil"

	"golang.org/x/text/internal/triegen"
)

func ExampleCompacter() {
	t := triegen.NewTrie("root")
	for r := rune(0); r < 10000; r += 64 {
		t.Insert(r, 0x9015BADA55^uint64(r))
	}
	sz, _ := t.Gen(ioutil.Discard)

	fmt.Printf("Size normal:    %5d\n", sz)

	var c myCompacter
	sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c))

	fmt.Printf("Size compacted: %5d\n", sz)

	// Output:
	// Size normal:    81344
	// Size compacted:  3224
}

// A myCompacter accepts a block if only the first value is given.
type myCompacter []uint64

func (c *myCompacter) Size(values []uint64) (sz int, ok bool) {
	for _, v := range values[1:] {
		if v != 0 {
			return 0, false
		}
	}
	return 8, true // the size of a uint64
}

func (c *myCompacter) Store(v []uint64) uint32 {
	x := uint32(len(*c))
	*c = append(*c, v[0])
	return x
}

func (c *myCompacter) Print(w io.Writer) error {
	fmt.Fprintln(w, "var firstValue = []uint64{")
	for _, v := range *c {
		fmt.Fprintf(w, "\t%#x,\n", v)
	}
	fmt.Fprintln(w, "}")
	return nil
}

func (c *myCompacter) Handler() string {
	return "getFirstValue"

	// Where getFirstValue is included along with the generated code:
	// func getFirstValue(n uint32, b byte) uint64 {
	//     if b == 0x80 { // the first continuation byte
	//         return firstValue[n]
	//     }
	//     return 0
	//  }
}