1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/text/internal/triegen"
15 func ExampleCompacter() {
16 t := triegen.NewTrie("root")
17 for r := rune(0); r < 10000; r += 64 {
18 t.Insert(r, 0x9015BADA55^uint64(r))
20 sz, _ := t.Gen(ioutil.Discard)
22 fmt.Printf("Size normal: %5d\n", sz)
25 sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c))
27 fmt.Printf("Size compacted: %5d\n", sz)
31 // Size compacted: 3224
34 // A myCompacter accepts a block if only the first value is given.
35 type myCompacter []uint64
37 func (c *myCompacter) Size(values []uint64) (sz int, ok bool) {
38 for _, v := range values[1:] {
43 return 8, true // the size of a uint64
46 func (c *myCompacter) Store(v []uint64) uint32 {
52 func (c *myCompacter) Print(w io.Writer) error {
53 fmt.Fprintln(w, "var firstValue = []uint64{")
54 for _, v := range *c {
55 fmt.Fprintf(w, "\t%#x,\n", v)
61 func (c *myCompacter) Handler() string {
62 return "getFirstValue"
64 // Where getFirstValue is included along with the generated code:
65 // func getFirstValue(n uint32, b byte) uint64 {
66 // if b == 0x80 { // the first continuation byte
67 // return firstValue[n]