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