5071
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2757
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
4106
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
2757
|
7 |
SequenceableCollection subclass:#Trie
|
|
8 |
instanceVariableNames:'value children'
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Collections-Ordered'
|
|
12 |
!
|
|
13 |
|
|
14 |
Object subclass:#SmallDictionaryWith1Element
|
|
15 |
instanceVariableNames:'k1 v1'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
privateIn:Trie
|
|
19 |
!
|
|
20 |
|
|
21 |
Object subclass:#SmallDictionaryWith2Elements
|
|
22 |
instanceVariableNames:'k1 k2 v1 v2'
|
|
23 |
classVariableNames:''
|
|
24 |
poolDictionaries:''
|
|
25 |
privateIn:Trie
|
|
26 |
!
|
|
27 |
|
|
28 |
Object subclass:#SmallDictionaryWith3Elements
|
|
29 |
instanceVariableNames:'k1 k2 k3 v1 v2 v3'
|
|
30 |
classVariableNames:''
|
|
31 |
poolDictionaries:''
|
|
32 |
privateIn:Trie
|
|
33 |
!
|
|
34 |
|
|
35 |
!Trie class methodsFor:'documentation'!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
2758
|
39 |
A trie is a collection which maps string keys to values (see also SuffixTree and RadixTree).
|
2757
|
40 |
It keeps its elements as a tree, where each node's children are indexed by a fragment of the element's key.
|
|
41 |
The fragment is a single character in this implementation.
|
|
42 |
Thus, insertion and access times are linear to size of the key (the number of characters in the key),
|
|
43 |
not the number of elements in the collection.
|
|
44 |
Due to the extra memory required to represent the tree, a Dictionary is usually much faster.
|
|
45 |
However, a trie allows for prefix matches and sorted enumerations, which are hard with dictionaries.
|
|
46 |
|
|
47 |
See AATree examples for speed comparison.
|
|
48 |
|
|
49 |
[author:]
|
|
50 |
Claus Gittinger
|
|
51 |
"
|
|
52 |
!
|
|
53 |
|
|
54 |
example
|
|
55 |
"
|
|
56 |
[exBegin]
|
|
57 |
|t|
|
|
58 |
|
|
59 |
t := Trie new.
|
|
60 |
t at:'12345' put:'hallo'.
|
|
61 |
t at:'54923' put:'Welt'.
|
|
62 |
t at:'1256' put:'bla'.
|
5071
|
63 |
t at:'12' put:'zwölf'.
|
2757
|
64 |
t at:'123' put:'einszweidrei'.
|
|
65 |
t at:'1234' put:'einszweidreivier'.
|
|
66 |
|
|
67 |
t at:'54923'.
|
|
68 |
t includesKey:'54923'.
|
|
69 |
t includesKey:'5492'.
|
|
70 |
t size.
|
|
71 |
t.
|
|
72 |
[exEnd]
|
|
73 |
|
|
74 |
[exBegin]
|
|
75 |
build a trie of all selectors in the system:
|
|
76 |
|
2854
|
77 |
|t0 t c|
|
2757
|
78 |
|
2854
|
79 |
t0 := Time millisecondsToRun:[
|
|
80 |
c := Trie new.
|
2757
|
81 |
Smalltalk allClassesDo:[:cls |
|
|
82 |
cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
83 |
].
|
|
84 |
].
|
|
85 |
].
|
2854
|
86 |
Transcript show:'tOverhead: '; showCR:t0.
|
2757
|
87 |
|
2854
|
88 |
t := Time millisecondsToRun:[
|
|
89 |
c := Trie new.
|
2757
|
90 |
Smalltalk allClassesDo:[:cls |
|
|
91 |
cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
2854
|
92 |
(c at:sel ifAbsentPut:[Set new]) add:mthd
|
|
93 |
].
|
|
94 |
].
|
|
95 |
].
|
|
96 |
Transcript show:'tTrie: '; showCR:t.
|
|
97 |
|
|
98 |
t := Time millisecondsToRun:[
|
|
99 |
c := Dictionary new.
|
|
100 |
Smalltalk allClassesDo:[:cls |
|
|
101 |
cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
102 |
(c at:sel ifAbsentPut:[Set new]) add:mthd
|
2757
|
103 |
].
|
|
104 |
].
|
|
105 |
].
|
2854
|
106 |
Transcript show:'tDict: '; showCR:t.
|
2757
|
107 |
[exEnd]
|
|
108 |
"
|
|
109 |
! !
|
|
110 |
|
|
111 |
!Trie class methodsFor:'instance creation'!
|
|
112 |
|
|
113 |
new
|
|
114 |
"return an initialized instance"
|
|
115 |
|
|
116 |
^ self basicNew initialize.
|
|
117 |
! !
|
|
118 |
|
|
119 |
!Trie methodsFor:'accessing'!
|
|
120 |
|
|
121 |
add:aValue
|
|
122 |
^ self at:aValue put:aValue
|
|
123 |
|
|
124 |
"Created: / 04-08-2012 / 11:31:30 / cg"
|
|
125 |
!
|
|
126 |
|
|
127 |
at:key
|
|
128 |
^ self at:key ifAbsent:[self error]
|
|
129 |
|
|
130 |
"Created: / 04-08-2012 / 10:44:02 / cg"
|
|
131 |
!
|
|
132 |
|
|
133 |
at:key ifAbsent:exceptionValue
|
|
134 |
^ self at:key keyIndex:1 ifAbsent:exceptionValue
|
|
135 |
|
|
136 |
"Created: / 04-08-2012 / 10:10:23 / cg"
|
|
137 |
!
|
|
138 |
|
|
139 |
at:key ifAbsentPut:newValue
|
|
140 |
^ self at:key keyIndex:1 ifAbsentPut:newValue
|
|
141 |
|
|
142 |
"Created: / 04-08-2012 / 17:51:49 / cg"
|
|
143 |
!
|
|
144 |
|
|
145 |
at:key put:anElement
|
|
146 |
self at:key keyIndex:1 put:anElement
|
|
147 |
|
|
148 |
"Created: / 04-08-2012 / 10:09:39 / cg"
|
|
149 |
!
|
|
150 |
|
|
151 |
remove:aValue
|
|
152 |
^ self at:aValue put:nil
|
|
153 |
|
|
154 |
"Created: / 04-08-2012 / 11:32:16 / cg"
|
|
155 |
! !
|
|
156 |
|
|
157 |
!Trie methodsFor:'enumeration'!
|
|
158 |
|
|
159 |
do:aBlock
|
|
160 |
value notNil ifTrue:[ aBlock value: value ].
|
|
161 |
children notNil ifTrue:[
|
|
162 |
children do:[:eachNode | eachNode do:aBlock ].
|
|
163 |
].
|
|
164 |
|
|
165 |
"Created: / 04-08-2012 / 11:24:49 / cg"
|
|
166 |
! !
|
|
167 |
|
|
168 |
!Trie methodsFor:'private'!
|
|
169 |
|
|
170 |
at:key keyIndex:idx ifAbsent:exceptionalValue
|
|
171 |
|childIndex|
|
|
172 |
|
|
173 |
idx > key size ifTrue:[
|
|
174 |
"/ at leaf
|
|
175 |
value notNil ifTrue:[ ^ value ].
|
|
176 |
^ exceptionalValue value
|
|
177 |
] ifFalse:[
|
|
178 |
childIndex := key at:idx.
|
|
179 |
^ (children at:childIndex) at:key keyIndex:idx+1 ifAbsent:exceptionalValue
|
|
180 |
].
|
|
181 |
|
|
182 |
"Created: / 04-08-2012 / 17:58:29 / cg"
|
|
183 |
!
|
|
184 |
|
|
185 |
at:key keyIndex:idx ifAbsentPut:newValue
|
|
186 |
|childIndex node|
|
|
187 |
|
|
188 |
idx > key size ifTrue:[
|
|
189 |
"/ at leaf
|
|
190 |
value isNil ifTrue:[
|
|
191 |
value := newValue value
|
|
192 |
].
|
|
193 |
^ value
|
|
194 |
] ifFalse:[
|
|
195 |
childIndex := key at:idx.
|
|
196 |
children isNil ifTrue:[
|
5189
|
197 |
children := SmallDictionaryWith1Element new
|
2757
|
198 |
k1:childIndex v1:(node := self class new).
|
|
199 |
] ifFalse:[
|
|
200 |
(children includesKey:childIndex) ifFalse:[
|
|
201 |
children := children newAt:childIndex put:(node := self class new).
|
|
202 |
] ifTrue:[
|
|
203 |
node := children at:childIndex
|
|
204 |
].
|
|
205 |
].
|
|
206 |
^ node at:key keyIndex:idx+1 ifAbsentPut:newValue.
|
|
207 |
]
|
|
208 |
|
|
209 |
"Created: / 04-08-2012 / 17:53:17 / cg"
|
|
210 |
!
|
|
211 |
|
|
212 |
at:key keyIndex:idx put:anElement
|
|
213 |
|childIndex node|
|
|
214 |
|
|
215 |
idx > key size ifTrue:[
|
|
216 |
"/ at leaf
|
|
217 |
value := anElement
|
|
218 |
] ifFalse:[
|
|
219 |
childIndex := key at:idx.
|
|
220 |
anElement isNil ifTrue:[
|
|
221 |
children isNil ifTrue:[^ self ].
|
|
222 |
node := children at:childIndex.
|
|
223 |
node isNil ifTrue:[^ self].
|
|
224 |
node at:key keyIndex:idx+1 put:nil.
|
|
225 |
node isEmpty ifTrue:[
|
|
226 |
children := children newAt:childIndex put:nil.
|
|
227 |
]
|
|
228 |
] ifFalse:[
|
|
229 |
children isNil ifTrue:[
|
5189
|
230 |
children := SmallDictionaryWith1Element new
|
2757
|
231 |
k1:childIndex v1:(node := self class new).
|
|
232 |
] ifFalse:[
|
|
233 |
(children includesKey:childIndex) ifFalse:[
|
|
234 |
children := children newAt:childIndex put:(node := self class new).
|
|
235 |
] ifTrue:[
|
|
236 |
node := children at:childIndex
|
|
237 |
].
|
|
238 |
].
|
|
239 |
node at:key keyIndex:idx+1 put:anElement.
|
|
240 |
].
|
|
241 |
].
|
|
242 |
|
|
243 |
"Created: / 04-08-2012 / 10:36:44 / cg"
|
|
244 |
!
|
|
245 |
|
|
246 |
includesKey:key keyIndex:idx
|
|
247 |
|childIndex node|
|
|
248 |
|
|
249 |
idx > key size ifTrue:[
|
|
250 |
"/ at leaf
|
|
251 |
^ value notNil
|
|
252 |
] ifFalse:[
|
|
253 |
children isNil ifTrue:[ ^ false ].
|
|
254 |
childIndex := key at:idx.
|
|
255 |
(children includesKey:childIndex) ifFalse:[^ false].
|
|
256 |
node := children at:childIndex.
|
|
257 |
^ node includesKey:key keyIndex:idx+1
|
|
258 |
].
|
|
259 |
|
|
260 |
"Created: / 04-08-2012 / 18:04:25 / cg"
|
|
261 |
! !
|
|
262 |
|
|
263 |
!Trie methodsFor:'queries'!
|
|
264 |
|
|
265 |
includesKey:key
|
|
266 |
^ self includesKey:key keyIndex:1
|
|
267 |
|
|
268 |
"Created: / 04-08-2012 / 11:32:42 / cg"
|
|
269 |
!
|
|
270 |
|
|
271 |
isEmpty
|
|
272 |
^ value isNil
|
|
273 |
and:[ children isEmptyOrNil ]
|
|
274 |
|
|
275 |
"Created: / 04-08-2012 / 11:45:26 / cg"
|
|
276 |
!
|
|
277 |
|
|
278 |
size
|
|
279 |
|n|
|
|
280 |
|
|
281 |
n := (value notNil) ifTrue:[1] ifFalse:[0].
|
|
282 |
children notNil ifTrue:[
|
|
283 |
children do:[:eachChild |
|
|
284 |
eachChild notNil ifTrue:[
|
|
285 |
n := n + eachChild size
|
|
286 |
]
|
|
287 |
].
|
|
288 |
].
|
|
289 |
^ n
|
|
290 |
|
|
291 |
"Created: / 04-08-2012 / 10:48:45 / cg"
|
|
292 |
! !
|
|
293 |
|
|
294 |
!Trie::SmallDictionaryWith1Element methodsFor:'accessing'!
|
|
295 |
|
|
296 |
at:key
|
|
297 |
key = k1 ifTrue:[^ v1 ].
|
|
298 |
^ self error
|
|
299 |
|
|
300 |
"Created: / 04-08-2012 / 11:17:14 / cg"
|
|
301 |
!
|
|
302 |
|
|
303 |
at:key ifAbsent:exceptionValue
|
|
304 |
key = k1 ifTrue:[^ v1 ].
|
|
305 |
^ exceptionValue value
|
|
306 |
|
|
307 |
"Created: / 04-08-2012 / 11:00:52 / cg"
|
|
308 |
!
|
|
309 |
|
|
310 |
includesKey:key
|
|
311 |
^ key = k1
|
|
312 |
|
|
313 |
"Created: / 04-08-2012 / 11:13:20 / cg"
|
|
314 |
!
|
|
315 |
|
|
316 |
isEmpty
|
|
317 |
^ k1 isNil
|
|
318 |
|
|
319 |
"Created: / 04-08-2012 / 11:34:12 / cg"
|
|
320 |
!
|
|
321 |
|
|
322 |
k1:k1Arg v1:v1Arg
|
|
323 |
k1 := k1Arg.
|
|
324 |
v1 := v1Arg.
|
|
325 |
!
|
|
326 |
|
|
327 |
newAt:key put:value
|
|
328 |
key = k1 ifTrue:[ v1 := value. ^ self ].
|
|
329 |
^ Trie::SmallDictionaryWith2Elements new
|
|
330 |
k1:k1 v1:v1 k2:key v2:value
|
|
331 |
|
|
332 |
"Created: / 04-08-2012 / 11:02:49 / cg"
|
|
333 |
!
|
|
334 |
|
|
335 |
size
|
|
336 |
^ k1 isNil ifTrue:[0] ifFalse:[1]
|
|
337 |
|
|
338 |
"Created: / 04-08-2012 / 11:54:21 / cg"
|
|
339 |
! !
|
|
340 |
|
|
341 |
!Trie::SmallDictionaryWith1Element methodsFor:'enumeration'!
|
|
342 |
|
|
343 |
do:aBlock
|
|
344 |
k1 notNil ifTrue:[ aBlock value: v1 ].
|
|
345 |
|
|
346 |
"Created: / 04-08-2012 / 11:19:10 / cg"
|
|
347 |
! !
|
|
348 |
|
5071
|
349 |
!Trie::SmallDictionaryWith1Element methodsFor:'visiting'!
|
|
350 |
|
|
351 |
acceptVisitor:aVisitor with:aParameter
|
|
352 |
"dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
|
|
353 |
|
|
354 |
^ aVisitor visitDictionary:self with:aParameter
|
|
355 |
! !
|
|
356 |
|
2757
|
357 |
!Trie::SmallDictionaryWith2Elements methodsFor:'accessing'!
|
|
358 |
|
|
359 |
at:key
|
|
360 |
key = k1 ifTrue:[^ v1 ].
|
|
361 |
key = k2 ifTrue:[^ v2 ].
|
|
362 |
^ self error
|
|
363 |
|
|
364 |
"Created: / 04-08-2012 / 11:17:24 / cg"
|
|
365 |
!
|
|
366 |
|
|
367 |
at:key ifAbsent:exceptionValue
|
|
368 |
key = k1 ifTrue:[^ v1 ].
|
|
369 |
key = k2 ifTrue:[^ v2 ].
|
|
370 |
^ exceptionValue value
|
|
371 |
|
|
372 |
"Created: / 04-08-2012 / 11:01:05 / cg"
|
|
373 |
!
|
|
374 |
|
|
375 |
includesKey:key
|
|
376 |
^ key = k1 or:[ key = k2 ]
|
|
377 |
|
|
378 |
"Created: / 04-08-2012 / 11:13:41 / cg"
|
|
379 |
!
|
|
380 |
|
|
381 |
isEmpty
|
|
382 |
^ k1 isNil and:[ k2 isNil ]
|
|
383 |
|
|
384 |
"Created: / 04-08-2012 / 11:34:34 / cg"
|
|
385 |
!
|
|
386 |
|
|
387 |
k1:k1Arg v1:v1Arg k2:k2Arg v2:v2Arg
|
|
388 |
k1 := k1Arg.
|
|
389 |
k2 := k2Arg.
|
|
390 |
v1 := v1Arg.
|
|
391 |
v2 := v2Arg.
|
|
392 |
|
|
393 |
"Created: / 04-08-2012 / 11:14:16 / cg"
|
|
394 |
!
|
|
395 |
|
|
396 |
newAt:key put:value
|
|
397 |
key = k1 ifTrue:[ v1 := value. ^ self ].
|
|
398 |
key = k2 ifTrue:[ v2 := value. ^ self ].
|
|
399 |
^ Trie::SmallDictionaryWith3Elements new
|
|
400 |
k1:k1 v1:v1 k2:k2 v2:v2 k3:key v3:value
|
|
401 |
|
|
402 |
"Created: / 04-08-2012 / 11:03:16 / cg"
|
|
403 |
!
|
|
404 |
|
|
405 |
size
|
|
406 |
^ (k1 isNil ifTrue:[0] ifFalse:[1]) + (k2 isNil ifTrue:[0] ifFalse:[1])
|
|
407 |
|
|
408 |
"Created: / 04-08-2012 / 11:54:49 / cg"
|
|
409 |
! !
|
|
410 |
|
|
411 |
!Trie::SmallDictionaryWith2Elements methodsFor:'enumeration'!
|
|
412 |
|
|
413 |
do:aBlock
|
|
414 |
k1 notNil ifTrue:[ aBlock value: v1 ].
|
|
415 |
k2 notNil ifTrue:[ aBlock value: v2 ].
|
|
416 |
|
|
417 |
"Created: / 04-08-2012 / 11:19:23 / cg"
|
|
418 |
! !
|
|
419 |
|
5071
|
420 |
!Trie::SmallDictionaryWith2Elements methodsFor:'visiting'!
|
|
421 |
|
|
422 |
acceptVisitor:aVisitor with:aParameter
|
|
423 |
"dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
|
|
424 |
|
|
425 |
^ aVisitor visitDictionary:self with:aParameter
|
|
426 |
! !
|
|
427 |
|
2757
|
428 |
!Trie::SmallDictionaryWith3Elements methodsFor:'accessing'!
|
|
429 |
|
|
430 |
at:key
|
|
431 |
key = k1 ifTrue:[^ v1 ].
|
|
432 |
key = k2 ifTrue:[^ v2 ].
|
|
433 |
key = k3 ifTrue:[^ v3 ].
|
|
434 |
^ self error
|
|
435 |
|
|
436 |
"Created: / 04-08-2012 / 11:17:38 / cg"
|
|
437 |
!
|
|
438 |
|
|
439 |
at:key ifAbsent:exceptionValue
|
|
440 |
key = k1 ifTrue:[^ v1 ].
|
|
441 |
key = k2 ifTrue:[^ v2 ].
|
|
442 |
key = k3 ifTrue:[^ v3 ].
|
|
443 |
^ exceptionValue value
|
|
444 |
|
|
445 |
"Created: / 04-08-2012 / 11:01:15 / cg"
|
|
446 |
!
|
|
447 |
|
|
448 |
includesKey:key
|
|
449 |
^ key = k1 or:[ key = k2 or:[ key = k3 ]]
|
|
450 |
|
|
451 |
"Created: / 04-08-2012 / 11:13:54 / cg"
|
|
452 |
!
|
|
453 |
|
|
454 |
isEmpty
|
|
455 |
^ k1 isNil and:[ k2 isNil and:[ k3 isNil ]]
|
|
456 |
|
|
457 |
"Created: / 04-08-2012 / 11:34:48 / cg"
|
|
458 |
!
|
|
459 |
|
|
460 |
k1:k1Arg v1:v1Arg k2:k2Arg v2:v2Arg k3:k3Arg v3:v3Arg
|
|
461 |
k1 := k1Arg.
|
|
462 |
k2 := k2Arg.
|
|
463 |
k3 := k3Arg.
|
|
464 |
v1 := v1Arg.
|
|
465 |
v2 := v2Arg.
|
|
466 |
v3 := v3Arg.
|
|
467 |
|
|
468 |
"Created: / 04-08-2012 / 11:14:35 / cg"
|
|
469 |
!
|
|
470 |
|
|
471 |
newAt:key put:value
|
|
472 |
key = k1 ifTrue:[ v1 := value. ^ self ].
|
|
473 |
key = k2 ifTrue:[ v2 := value. ^ self ].
|
|
474 |
key = k3 ifTrue:[ v3 := value. ^ self ].
|
5071
|
475 |
^ Dictionary new
|
2757
|
476 |
at:k1 put:v1;
|
|
477 |
at:k2 put:v2;
|
|
478 |
at:k3 put:v3;
|
|
479 |
at:key put:value;
|
|
480 |
yourself
|
|
481 |
|
|
482 |
"Created: / 04-08-2012 / 11:04:31 / cg"
|
|
483 |
!
|
|
484 |
|
|
485 |
size
|
|
486 |
^ (k1 isNil ifTrue:[0] ifFalse:[1])
|
|
487 |
+ (k2 isNil ifTrue:[0] ifFalse:[1])
|
|
488 |
+ (k3 isNil ifTrue:[0] ifFalse:[1])
|
|
489 |
|
|
490 |
"Created: / 04-08-2012 / 11:55:03 / cg"
|
|
491 |
! !
|
|
492 |
|
|
493 |
!Trie::SmallDictionaryWith3Elements methodsFor:'enumeration'!
|
|
494 |
|
|
495 |
do:aBlock
|
|
496 |
k1 notNil ifTrue:[ aBlock value: v1 ].
|
|
497 |
k2 notNil ifTrue:[ aBlock value: v2 ].
|
|
498 |
k3 notNil ifTrue:[ aBlock value: v3 ].
|
|
499 |
|
|
500 |
"Created: / 04-08-2012 / 11:19:36 / cg"
|
|
501 |
! !
|
|
502 |
|
5071
|
503 |
!Trie::SmallDictionaryWith3Elements methodsFor:'visiting'!
|
|
504 |
|
|
505 |
acceptVisitor:aVisitor with:aParameter
|
|
506 |
"dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
|
|
507 |
|
|
508 |
^ aVisitor visitDictionary:self with:aParameter
|
|
509 |
! !
|
|
510 |
|
2757
|
511 |
!Trie class methodsFor:'documentation'!
|
|
512 |
|
|
513 |
version
|
4106
|
514 |
^ '$Header$'
|
2757
|
515 |
!
|
|
516 |
|
|
517 |
version_CVS
|
4106
|
518 |
^ '$Header$'
|
2757
|
519 |
! !
|
4106
|
520 |
|