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