You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
1.5 KiB

  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. package triegen_test
  5. import (
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "golang.org/x/text/internal/triegen"
  10. )
  11. func ExampleCompacter() {
  12. t := triegen.NewTrie("root")
  13. for r := rune(0); r < 10000; r += 64 {
  14. t.Insert(r, 0x9015BADA55^uint64(r))
  15. }
  16. sz, _ := t.Gen(ioutil.Discard)
  17. fmt.Printf("Size normal: %5d\n", sz)
  18. var c myCompacter
  19. sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c))
  20. fmt.Printf("Size compacted: %5d\n", sz)
  21. // Output:
  22. // Size normal: 81344
  23. // Size compacted: 3224
  24. }
  25. // A myCompacter accepts a block if only the first value is given.
  26. type myCompacter []uint64
  27. func (c *myCompacter) Size(values []uint64) (sz int, ok bool) {
  28. for _, v := range values[1:] {
  29. if v != 0 {
  30. return 0, false
  31. }
  32. }
  33. return 8, true // the size of a uint64
  34. }
  35. func (c *myCompacter) Store(v []uint64) uint32 {
  36. x := uint32(len(*c))
  37. *c = append(*c, v[0])
  38. return x
  39. }
  40. func (c *myCompacter) Print(w io.Writer) error {
  41. fmt.Fprintln(w, "var firstValue = []uint64{")
  42. for _, v := range *c {
  43. fmt.Fprintf(w, "\t%#x,\n", v)
  44. }
  45. fmt.Fprintln(w, "}")
  46. return nil
  47. }
  48. func (c *myCompacter) Handler() string {
  49. return "getFirstValue"
  50. // Where getFirstValue is included along with the generated code:
  51. // func getFirstValue(n uint32, b byte) uint64 {
  52. // if b == 0x80 { // the first continuation byte
  53. // return firstValue[n]
  54. // }
  55. // return 0
  56. // }
  57. }