@@ -17,29 +17,29 @@ def __init__(self):
17
17
"Root index"
18
18
19
19
def has_value (self , index ):
20
- " Checks if a given index is related to the end of a key."
20
+ # Checks if a given index is related to the end of a key.
21
21
return units .has_leaf (self ._units [index ])
22
22
23
23
def value (self , index ):
24
- " Gets a value from a given index."
24
+ # Gets a value from a given index.
25
25
offset = units .offset (self ._units [index ])
26
26
value_index = (index ^ offset ) & units .PRECISION_MASK
27
27
return units .value (self ._units [value_index ])
28
28
29
29
def read (self , fp ):
30
- " Reads a dictionary from an input stream."
30
+ # Reads a dictionary from an input stream.
31
31
base_size = struct .unpack (str ("=I" ), fp .read (4 ))[0 ]
32
32
self ._units .fromfile (fp , base_size )
33
33
34
34
def contains (self , key ):
35
- " Exact matching."
35
+ # Exact matching.
36
36
index = self .follow_bytes (key , self .ROOT )
37
37
if index is None :
38
38
return False
39
39
return self .has_value (index )
40
40
41
41
def find (self , key ):
42
- " Exact matching (returns value)"
42
+ # Exact matching (returns value)
43
43
index = self .follow_bytes (key , self .ROOT )
44
44
if index is None :
45
45
return - 1
@@ -48,7 +48,7 @@ def find(self, key):
48
48
return self .value (index )
49
49
50
50
def follow_char (self , label , index ):
51
- " Follows a transition"
51
+ # Follows a transition
52
52
offset = units .offset (self ._units [index ])
53
53
next_index = (index ^ offset ^ label ) & units .PRECISION_MASK
54
54
@@ -58,7 +58,7 @@ def follow_char(self, label, index):
58
58
return next_index
59
59
60
60
def follow_bytes (self , s , index ):
61
- " Follows transitions."
61
+ # Follows transitions.
62
62
for ch in s :
63
63
index = self .follow_char (int_from_byte (ch ), index )
64
64
if index is None :
@@ -95,27 +95,17 @@ def size(self):
95
95
return len (self ._units )
96
96
97
97
98
- class Completer (object ):
99
-
98
+ class EdgeFollower (object ):
100
99
def __init__ (self , dic = None , guide = None ):
101
100
self ._dic = dic
102
101
self ._guide = guide
103
102
104
103
def value (self ):
105
- return self ._dic .value (self ._last_index )
104
+ if self ._dic .has_value (self ._cur_index ):
105
+ return self ._dic .value (self ._cur_index )
106
+ return False
106
107
107
108
def start (self , index , prefix = b"" ):
108
- "initial setup for a completer next() action on some prefix"
109
-
110
- self .key = bytearray (prefix )
111
-
112
- if self ._guide .size ():
113
- self ._index_stack = [index ]
114
- self ._last_index = self ._dic .ROOT
115
- else :
116
- self ._index_stack = []
117
-
118
- def start_edges (self , index , prefix = b"" ):
119
109
"""initial setup for a completer next_edge() action on some prefix. If
120
110
there's a child for this prefix, we add that as the one item on the
121
111
index_stack. Otherwise, leave the stack empty, so next_edge() fails"""
@@ -124,6 +114,7 @@ def start_edges(self, index, prefix=b""):
124
114
self .base_key_len = len (self .key )
125
115
self ._parent_index = index
126
116
self ._sib_index = None
117
+ self ._cur_index = None
127
118
if self ._guide .size ():
128
119
child_label = self ._guide .child (index ) # UCharType
129
120
@@ -132,19 +123,21 @@ def start_edges(self, index, prefix=b""):
132
123
next_index = self ._dic .follow_char (child_label , index )
133
124
if index is not None :
134
125
self ._sib_index = next_index
126
+ self ._cur_index = self ._sib_index
135
127
self .key .append (child_label )
136
128
self .decoded_key = self .key .decode ('utf-8' )
137
129
return True
138
130
139
- def next_edge (self ):
140
- " Gets the next edge (not necessarily a terminal)"
131
+ def next (self ):
132
+ # Gets the next edge (not necessarily a terminal)
141
133
142
134
if not self ._sib_index :
143
135
return False
144
136
145
137
sibling_label = self ._guide .sibling (self ._sib_index )
146
138
self ._sib_index = self ._dic .follow_char (sibling_label ,
147
139
self ._parent_index )
140
+ self ._cur_index = self ._sib_index
148
141
if not self ._sib_index :
149
142
return False
150
143
@@ -153,13 +146,13 @@ def next_edge(self):
153
146
try :
154
147
self .decoded_key = self .key .decode ('utf-8' )
155
148
except UnicodeDecodeError :
156
- #this sibling is multi-character . keep following its children til
149
+ #this sibling is a multibyte char . keep following its children til
157
150
#something is decodable
158
- cur_index = self ._sib_index
159
151
while True :
160
152
child_label = self ._guide .child (self ._sib_index )
161
- cur_index = self ._dic .follow_char (child_label , cur_index )
162
- if not cur_index :
153
+ self ._cur_index = self ._dic .follow_char (child_label ,
154
+ self ._cur_index )
155
+ if not self ._cur_index :
163
156
return False
164
157
self .key .append (child_label )
165
158
try :
@@ -169,8 +162,32 @@ def next_edge(self):
169
162
pass
170
163
return True
171
164
165
+ def get_cur_edge (self ):
166
+ return (self .decoded_key , self ._dic .has_value (self ._cur_index ))
167
+
168
+
169
+ class Completer (object ):
170
+
171
+ def __init__ (self , dic = None , guide = None ):
172
+ self ._dic = dic
173
+ self ._guide = guide
174
+
175
+ def value (self ):
176
+ return self ._dic .value (self ._last_index )
177
+
178
+ def start (self , index , prefix = b"" ):
179
+ #initial setup for a completer next() action on some prefix
180
+
181
+ self .key = bytearray (prefix )
182
+
183
+ if self ._guide .size ():
184
+ self ._index_stack = [index ]
185
+ self ._last_index = self ._dic .ROOT
186
+ else :
187
+ self ._index_stack = []
188
+
172
189
def next (self ):
173
- " Gets the next key"
190
+ # Gets the next key
174
191
175
192
if not self ._index_stack :
176
193
return False
0 commit comments