1 // Copyright 2011 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.
5 // Trie table generator.
6 // Used by make*tables tools to generate a go file with trie data structures
7 // for mapping UTF-8 to a 16-bit value. All but the last byte in a UTF-8 byte
8 // sequence are used to lookup offsets in the index table to be used for the
9 // next byte. The last byte is used to index into a table with 16-bit values.
21 const maxSparseEntries = 16
23 // Intermediate trie structure
24 type trieNode struct {
31 func newNode() *trieNode {
35 func (n trieNode) String() string {
36 s := fmt.Sprint("trieNode{table: { non-nil at index: ")
37 for i, v := range n.table {
39 s += fmt.Sprintf("%d, ", i)
42 s += fmt.Sprintf("}, value:%#x, b:%#x leaf:%v}", n.value, n.b, n.leaf)
46 func (n trieNode) isInternal() bool {
48 for i := 0; i < 256; i++ {
49 if nn := n.table[i]; nn != nil {
50 if !internal && !nn.leaf {
51 log.Fatalf("triegen: isInternal: node contains both leaf and non-leaf children (%v)", n)
53 internal = internal && !nn.leaf
59 func (n trieNode) mostFrequentStride() int {
60 counts := make(map[int]int)
62 for _, t := range n.table[0x80 : 0x80+blockSize] {
64 if stride := t.value - v; v != 0 && stride >= 0 {
71 for stride, cnt := range counts {
73 maxs, maxc = stride, cnt
79 func (n trieNode) countSparseEntries() int {
80 stride := n.mostFrequentStride()
82 for _, t := range n.table[0x80 : 0x80+blockSize] {
97 func (n *trieNode) insert(rune int, value uint16) {
98 var p [utf8.UTFMax]byte
99 sz := utf8.EncodeRune(p[:], rune)
101 for i := 0; i < sz; i++ {
103 log.Fatalf("triegen: insert: node (%#v) should not be a leaf", n)
117 type nodeIndex struct {
118 lookupBlocks []*trieNode
119 valueBlocks []*trieNode
120 sparseBlocks []*trieNode
121 sparseOffset []uint16
124 lookupBlockIdx map[uint32]int
125 valueBlockIdx map[uint32]int
128 func newIndex() *nodeIndex {
129 index := &nodeIndex{}
130 index.lookupBlocks = make([]*trieNode, 0)
131 index.valueBlocks = make([]*trieNode, 0)
132 index.sparseBlocks = make([]*trieNode, 0)
133 index.sparseOffset = make([]uint16, 1)
134 index.lookupBlockIdx = make(map[uint32]int)
135 index.valueBlockIdx = make(map[uint32]int)
139 func computeOffsets(index *nodeIndex, n *trieNode) int {
143 hasher := crc32.New(crc32.MakeTable(crc32.IEEE))
144 // We only index continuation bytes.
145 for i := 0; i < blockSize; i++ {
147 if nn := n.table[0x80+i]; nn != nil {
148 v = computeOffsets(index, nn)
150 hasher.Write([]byte{uint8(v >> 8), uint8(v)})
154 v, ok := index.lookupBlockIdx[h]
156 v = len(index.lookupBlocks)
157 index.lookupBlocks = append(index.lookupBlocks, n)
158 index.lookupBlockIdx[h] = v
162 v, ok := index.valueBlockIdx[h]
164 if c := n.countSparseEntries(); c > maxSparseEntries {
165 v = len(index.valueBlocks)
166 index.valueBlocks = append(index.valueBlocks, n)
167 index.valueBlockIdx[h] = v
169 v = -len(index.sparseOffset)
170 index.sparseBlocks = append(index.sparseBlocks, n)
171 index.sparseOffset = append(index.sparseOffset, uint16(index.sparseCount))
172 index.sparseCount += c + 1
173 index.valueBlockIdx[h] = v
181 func printValueBlock(nr int, n *trieNode, offset int) {
182 boff := nr * blockSize
183 fmt.Printf("\n// Block %#x, offset %#x", nr, boff)
184 var printnewline bool
185 for i := 0; i < blockSize; i++ {
190 if nn := n.table[i+offset]; nn != nil {
198 fmt.Printf("%#04x:%#04x, ", boff+i, v)
203 func printSparseBlock(nr int, n *trieNode) {
205 fmt.Printf("\n// Block %#x, offset %#x", nr, boff)
208 stride := n.mostFrequentStride()
209 c := n.countSparseEntries()
210 fmt.Printf("\n{value:%#04x,lo:%#02x},", stride, uint8(c))
211 for i, nn := range n.table[0x80 : 0x80+blockSize] {
218 fmt.Printf(",hi:%#02x},", 0x80+i-1)
221 fmt.Printf("\n{value:%#04x,lo:%#02x", nv, nn.b)
227 fmt.Printf(",hi:%#02x},", 0x80+blockSize-1)
231 func printLookupBlock(nr int, n *trieNode, offset, cutoff int) {
232 boff := nr * blockSize
233 fmt.Printf("\n// Block %#x, offset %#x", nr, boff)
234 var printnewline bool
235 for i := 0; i < blockSize; i++ {
240 if nn := n.table[i+offset]; nn != nil {
251 fmt.Printf("%#03x:%#02x, ", boff+i, v)
256 // printTables returns the size in bytes of the generated tables.
257 func (t *trieNode) printTables(name string) int {
259 // Values for 7-bit ASCII are stored in first two block, followed by nil block.
260 index.valueBlocks = append(index.valueBlocks, nil, nil, nil)
261 // First byte of multi-byte UTF-8 codepoints are indexed in 4th block.
262 index.lookupBlocks = append(index.lookupBlocks, nil, nil, nil, nil)
263 // Index starter bytes of multi-byte UTF-8.
264 for i := 0xC0; i < 0x100; i++ {
265 if t.table[i] != nil {
266 computeOffsets(index, t.table[i])
270 nv := len(index.valueBlocks) * blockSize
271 fmt.Printf("// %sValues: %d entries, %d bytes\n", name, nv, nv*2)
272 fmt.Printf("// Block 2 is the null block.\n")
273 fmt.Printf("var %sValues = [%d]uint16 {", name, nv)
274 printValueBlock(0, t, 0)
275 printValueBlock(1, t, 64)
276 printValueBlock(2, newNode(), 0)
277 for i := 3; i < len(index.valueBlocks); i++ {
278 printValueBlock(i, index.valueBlocks[i], 0x80)
282 ls := len(index.sparseBlocks)
283 fmt.Printf("// %sSparseOffset: %d entries, %d bytes\n", name, ls, ls*2)
284 fmt.Printf("var %sSparseOffset = %#v\n\n", name, index.sparseOffset[1:])
286 ns := index.sparseCount
287 fmt.Printf("// %sSparseValues: %d entries, %d bytes\n", name, ns, ns*4)
288 fmt.Printf("var %sSparseValues = [%d]valueRange {", name, ns)
289 for i, n := range index.sparseBlocks {
290 printSparseBlock(i, n)
294 cutoff := len(index.valueBlocks)
295 ni := len(index.lookupBlocks) * blockSize
296 fmt.Printf("// %sLookup: %d bytes\n", name, ni)
297 fmt.Printf("// Block 0 is the null block.\n")
298 fmt.Printf("var %sLookup = [%d]uint8 {", name, ni)
299 printLookupBlock(0, newNode(), 0, cutoff)
300 printLookupBlock(1, newNode(), 0, cutoff)
301 printLookupBlock(2, newNode(), 0, cutoff)
302 printLookupBlock(3, t, 0xC0, cutoff)
303 for i := 4; i < len(index.lookupBlocks); i++ {
304 printLookupBlock(i, index.lookupBlocks[i], 0x80, cutoff)
307 fmt.Printf("var %sTrie = trie{ %sLookup[:], %sValues[:], %sSparseValues[:], %sSparseOffset[:], %d}\n\n",
308 name, name, name, name, name, cutoff)
309 return nv*2 + ns*4 + ni + ls*2