...
1
2
3
4
5 package triegen_test
6
7 import (
8 "fmt"
9 "io"
10
11 "golang.org/x/text/internal/triegen"
12 )
13
14 func ExampleCompacter() {
15 t := triegen.NewTrie("root")
16 for r := rune(0); r < 10000; r += 64 {
17 t.Insert(r, 0x9015BADA55^uint64(r))
18 }
19 sz, _ := t.Gen(io.Discard)
20
21 fmt.Printf("Size normal: %5d\n", sz)
22
23 var c myCompacter
24 sz, _ = t.Gen(io.Discard, triegen.Compact(&c))
25
26 fmt.Printf("Size compacted: %5d\n", sz)
27
28
29
30
31 }
32
33
34 type myCompacter []uint64
35
36 func (c *myCompacter) Size(values []uint64) (sz int, ok bool) {
37 for _, v := range values[1:] {
38 if v != 0 {
39 return 0, false
40 }
41 }
42 return 8, true
43 }
44
45 func (c *myCompacter) Store(v []uint64) uint32 {
46 x := uint32(len(*c))
47 *c = append(*c, v[0])
48 return x
49 }
50
51 func (c *myCompacter) Print(w io.Writer) error {
52 fmt.Fprintln(w, "var firstValue = []uint64{")
53 for _, v := range *c {
54 fmt.Fprintf(w, "\t%#x,\n", v)
55 }
56 fmt.Fprintln(w, "}")
57 return nil
58 }
59
60 func (c *myCompacter) Handler() string {
61 return "getFirstValue"
62
63
64
65
66
67
68
69
70 }
71
View as plain text