github.com/go-xe2/third@v1.0.3/golang.org/x/text/internal/triegen/example_compact_test.go (about) 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. 4 5 package triegen_test 6 7 import ( 8 "fmt" 9 "io" 10 "io/ioutil" 11 12 "github.com/go-xe2/third/golang.org/x/text/internal/triegen" 13 ) 14 15 func ExampleCompacter() { 16 t := triegen.NewTrie("root") 17 for r := rune(0); r < 10000; r += 64 { 18 t.Insert(r, 0x9015BADA55^uint64(r)) 19 } 20 sz, _ := t.Gen(ioutil.Discard) 21 22 fmt.Printf("Size normal: %5d\n", sz) 23 24 var c myCompacter 25 sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c)) 26 27 fmt.Printf("Size compacted: %5d\n", sz) 28 29 // Output: 30 // Size normal: 81344 31 // Size compacted: 3224 32 } 33 34 // A myCompacter accepts a block if only the first value is given. 35 type myCompacter []uint64 36 37 func (c *myCompacter) Size(values []uint64) (sz int, ok bool) { 38 for _, v := range values[1:] { 39 if v != 0 { 40 return 0, false 41 } 42 } 43 return 8, true // the size of a uint64 44 } 45 46 func (c *myCompacter) Store(v []uint64) uint32 { 47 x := uint32(len(*c)) 48 *c = append(*c, v[0]) 49 return x 50 } 51 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) 56 } 57 fmt.Fprintln(w, "}") 58 return nil 59 } 60 61 func (c *myCompacter) Handler() string { 62 return "getFirstValue" 63 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] 68 // } 69 // return 0 70 // } 71 }