blob: 397b975c1b713cfc119e5d300bf912edaed4ebba [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001// 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
6
7// This file defines Compacter and its implementations.
8
9import "io"
10
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).
15type 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)
19
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
24
25 // Print writes the data structures associated to the given store to w.
26 Print(w io.Writer) error
27
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
33 // block.
34 Handler() string
35}
36
37// simpleCompacter is the default Compacter used by builder. It implements a
38// normal trie block.
39type simpleCompacter builder
40
41func (b *simpleCompacter) Size([]uint64) (sz int, ok bool) {
42 return blockSize * b.ValueSize, true
43}
44
45func (b *simpleCompacter) Store(v []uint64) uint32 {
46 h := uint32(len(b.ValueBlocks) - blockOffset)
47 b.ValueBlocks = append(b.ValueBlocks, v)
48 return h
49}
50
51func (b *simpleCompacter) Print(io.Writer) error {
52 // Structures are printed in print.go.
53 return nil
54}
55
56func (b *simpleCompacter) Handler() string {
57 panic("Handler should be special-cased for this Compacter")
58}