| 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 |
|
| 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 |
// Output: |
| 29 |
// Size normal: 81344 |
| 30 |
// Size compacted: 3224 |
| 31 |
} |
| 32 |
|
| 33 |
// A myCompacter accepts a block if only the first value is given. |
| 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 // the size of a uint64 |
| 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 |
// Where getFirstValue is included along with the generated code: |
| 64 |
// func getFirstValue(n uint32, b byte) uint64 { |
| 65 |
// if b == 0x80 { // the first continuation byte |
| 66 |
// return firstValue[n] |
| 67 |
// } |
| 68 |
// return 0 |
| 69 |
// } |
| 70 |
} |