...
1
2
3
4
5
6
7
8
9
10
11 package colltab
12
13 const blockSize = 64
14
15 type Trie struct {
16 Index0 []uint16
17 Values0 []uint32
18 Index []uint16
19 Values []uint32
20 }
21
22 const (
23 t1 = 0x00
24 tx = 0x80
25 t2 = 0xC0
26 t3 = 0xE0
27 t4 = 0xF0
28 t5 = 0xF8
29 t6 = 0xFC
30 te = 0xFE
31 )
32
33 func (t *Trie) lookupValue(n uint16, b byte) Elem {
34 return Elem(t.Values[int(n)<<6+int(b)])
35 }
36
37
38
39
40 func (t *Trie) lookup(s []byte) (v Elem, sz int) {
41 c0 := s[0]
42 switch {
43 case c0 < tx:
44 return Elem(t.Values0[c0]), 1
45 case c0 < t2:
46 return 0, 1
47 case c0 < t3:
48 if len(s) < 2 {
49 return 0, 0
50 }
51 i := t.Index0[c0]
52 c1 := s[1]
53 if c1 < tx || t2 <= c1 {
54 return 0, 1
55 }
56 return t.lookupValue(i, c1), 2
57 case c0 < t4:
58 if len(s) < 3 {
59 return 0, 0
60 }
61 i := t.Index0[c0]
62 c1 := s[1]
63 if c1 < tx || t2 <= c1 {
64 return 0, 1
65 }
66 o := int(i)<<6 + int(c1)
67 i = t.Index[o]
68 c2 := s[2]
69 if c2 < tx || t2 <= c2 {
70 return 0, 2
71 }
72 return t.lookupValue(i, c2), 3
73 case c0 < t5:
74 if len(s) < 4 {
75 return 0, 0
76 }
77 i := t.Index0[c0]
78 c1 := s[1]
79 if c1 < tx || t2 <= c1 {
80 return 0, 1
81 }
82 o := int(i)<<6 + int(c1)
83 i = t.Index[o]
84 c2 := s[2]
85 if c2 < tx || t2 <= c2 {
86 return 0, 2
87 }
88 o = int(i)<<6 + int(c2)
89 i = t.Index[o]
90 c3 := s[3]
91 if c3 < tx || t2 <= c3 {
92 return 0, 3
93 }
94 return t.lookupValue(i, c3), 4
95 }
96
97 return 0, 1
98 }
99
100
101 func (t *Trie) lookupString(s string) (v Elem, sz int) {
102 c0 := s[0]
103 switch {
104 case c0 < tx:
105 return Elem(t.Values0[c0]), 1
106 case c0 < t2:
107 return 0, 1
108 case c0 < t3:
109 if len(s) < 2 {
110 return 0, 0
111 }
112 i := t.Index0[c0]
113 c1 := s[1]
114 if c1 < tx || t2 <= c1 {
115 return 0, 1
116 }
117 return t.lookupValue(i, c1), 2
118 case c0 < t4:
119 if len(s) < 3 {
120 return 0, 0
121 }
122 i := t.Index0[c0]
123 c1 := s[1]
124 if c1 < tx || t2 <= c1 {
125 return 0, 1
126 }
127 o := int(i)<<6 + int(c1)
128 i = t.Index[o]
129 c2 := s[2]
130 if c2 < tx || t2 <= c2 {
131 return 0, 2
132 }
133 return t.lookupValue(i, c2), 3
134 case c0 < t5:
135 if len(s) < 4 {
136 return 0, 0
137 }
138 i := t.Index0[c0]
139 c1 := s[1]
140 if c1 < tx || t2 <= c1 {
141 return 0, 1
142 }
143 o := int(i)<<6 + int(c1)
144 i = t.Index[o]
145 c2 := s[2]
146 if c2 < tx || t2 <= c2 {
147 return 0, 2
148 }
149 o = int(i)<<6 + int(c2)
150 i = t.Index[o]
151 c3 := s[3]
152 if c3 < tx || t2 <= c3 {
153 return 0, 3
154 }
155 return t.lookupValue(i, c3), 4
156 }
157
158 return 0, 1
159 }
160
View as plain text