blob: 95486d1ffb04b14ef7ca1fca16ff37c3b8a1f11b [file] [log] [blame]
Marcel van Lohuizen1d5b4972014-08-07 10:15:28 +02001// 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
5package triegen_test
6
7import (
8 "fmt"
9 "io"
Marcel van Lohuizen1d5b4972014-08-07 10:15:28 +020010
Andrew Gerrandd541d342014-11-10 08:48:25 +110011 "golang.org/x/text/internal/triegen"
Marcel van Lohuizen1d5b4972014-08-07 10:15:28 +020012)
13
14func ExampleCompacter() {
15 t := triegen.NewTrie("root")
16 for r := rune(0); r < 10000; r += 64 {
17 t.Insert(r, 0x9015BADA55^uint64(r))
18 }
cui fliter795e8542022-09-16 02:08:40 +000019 sz, _ := t.Gen(io.Discard)
Marcel van Lohuizen1d5b4972014-08-07 10:15:28 +020020
21 fmt.Printf("Size normal: %5d\n", sz)
22
23 var c myCompacter
cui fliter795e8542022-09-16 02:08:40 +000024 sz, _ = t.Gen(io.Discard, triegen.Compact(&c))
Marcel van Lohuizen1d5b4972014-08-07 10:15:28 +020025
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.
34type myCompacter []uint64
35
36func (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
45func (c *myCompacter) Store(v []uint64) uint32 {
46 x := uint32(len(*c))
47 *c = append(*c, v[0])
48 return x
49}
50
51func (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
60func (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}