compact.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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
  5. // This file defines Compacter and its implementations.
  6. import "io"
  7. // A Compacter generates an alternative, more space-efficient way to store a
  8. // trie value block. A trie value block holds all possible values for the last
  9. // byte of a UTF-8 encoded rune. Excluding ASCII characters, a trie value block
  10. // always has 64 values, as a UTF-8 encoding ends with a byte in [0x80, 0xC0).
  11. type Compacter interface {
  12. // Size returns whether the Compacter could encode the given block as well
  13. // as its size in case it can. len(v) is always 64.
  14. Size(v []uint64) (sz int, ok bool)
  15. // Store stores the block using the Compacter's compression method.
  16. // It returns a handle with which the block can be retrieved.
  17. // len(v) is always 64.
  18. Store(v []uint64) uint32
  19. // Print writes the data structures associated to the given store to w.
  20. Print(w io.Writer) error
  21. // Handler returns the name of a function that gets called during trie
  22. // lookup for blocks generated by the Compacter. The function should be of
  23. // the form func (n uint32, b byte) uint64, where n is the index returned by
  24. // the Compacter's Store method and b is the last byte of the UTF-8
  25. // encoding, where 0x80 <= b < 0xC0, for which to do the lookup in the
  26. // block.
  27. Handler() string
  28. }
  29. // simpleCompacter is the default Compacter used by builder. It implements a
  30. // normal trie block.
  31. type simpleCompacter builder
  32. func (b *simpleCompacter) Size([]uint64) (sz int, ok bool) {
  33. return blockSize * b.ValueSize, true
  34. }
  35. func (b *simpleCompacter) Store(v []uint64) uint32 {
  36. h := uint32(len(b.ValueBlocks) - blockOffset)
  37. b.ValueBlocks = append(b.ValueBlocks, v)
  38. return h
  39. }
  40. func (b *simpleCompacter) Print(io.Writer) error {
  41. // Structures are printed in print.go.
  42. return nil
  43. }
  44. func (b *simpleCompacter) Handler() string {
  45. panic("Handler should be special-cased for this Compacter")
  46. }