2282
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Collection subclass:#TSTree
|
|
4 |
instanceVariableNames:'root'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Collections-Ordered'
|
|
8 |
!
|
|
9 |
|
|
10 |
Object subclass:#TSTreeNode
|
|
11 |
instanceVariableNames:'key value low high equal'
|
|
12 |
classVariableNames:''
|
|
13 |
poolDictionaries:''
|
|
14 |
privateIn:TSTree
|
|
15 |
!
|
|
16 |
|
|
17 |
!TSTree class methodsFor:'documentation'!
|
|
18 |
|
|
19 |
documentation
|
|
20 |
"
|
|
21 |
A bunch of collection classes that are useful for building large indices of things.
|
|
22 |
It's especially geared towards people using OODBs like GOODS, but can be used it in the image too:
|
|
23 |
the BTree class is great for when you need to select numeric keys by range,
|
|
24 |
and TSTree makes a solid basis for full-text search.
|
|
25 |
TreeSet has an interesting optimized #intersection: that lets you compare two collections without
|
|
26 |
looking at every item of either.
|
|
27 |
I'm also going to be rolling some code in here from Benjamin Pollack specifically aimed at indexing
|
|
28 |
by date ranges, which lets you do quick queries of all the events that overlap with a specific week,
|
|
29 |
for instance.
|
|
30 |
|
|
31 |
Also in the BTree package is a TSTree, which has similar properties for String
|
|
32 |
keys. So as well as keeping them sorted, you can do efficient lookups of all
|
|
33 |
the keys with a given prefix. One other neat trick TSTree can do is a certain
|
|
34 |
amount of fuzzy matching (eg find all keys with an edit distance of 3 from
|
|
35 |
'foo') which makes it especially useful for spell checking and similar
|
|
36 |
applications.
|
|
37 |
|
|
38 |
[author:]
|
|
39 |
Avi Bryant
|
|
40 |
|
|
41 |
[license:]
|
|
42 |
Dual licensed under both SqueakL and MIT.
|
|
43 |
This enables both base Squeak inclusion and 100% reuse.
|
|
44 |
"
|
|
45 |
! !
|
|
46 |
|
|
47 |
!TSTree methodsFor:'accessing'!
|
|
48 |
|
|
49 |
at: aString
|
|
50 |
^ self at: aString ifAbsent: [self error: aString printString, ' not found']
|
|
51 |
!
|
|
52 |
|
|
53 |
at: aString ifAbsent: exceptionBlock
|
|
54 |
aString isEmpty ifTrue: [^ exceptionBlock value].
|
|
55 |
root ifNil: [^ exceptionBlock value].
|
|
56 |
|
|
57 |
^ (root lookupString: aString startingAt: 1) ifNil: exceptionBlock
|
|
58 |
!
|
|
59 |
|
|
60 |
at: aString ifAbsentPut: exceptionBlock
|
|
61 |
^ self at: aString ifAbsent: [self at: aString put: exceptionBlock value]
|
|
62 |
!
|
|
63 |
|
|
64 |
at: aString put: anObject
|
|
65 |
aString isEmpty ifTrue: [self error: 'Keys cannot be empty'].
|
|
66 |
root ifNil: [root _ TSTreeNode key: aString first].
|
|
67 |
root lookupString: aString startingAt: 1 insert: anObject.
|
|
68 |
^ anObject
|
|
69 |
!
|
|
70 |
|
|
71 |
removeKey: aString
|
|
72 |
^ self removeKey: aString ifAbsent: [self error: 'Could not find key ', aString printString]
|
|
73 |
!
|
|
74 |
|
|
75 |
removeKey: aString ifAbsent: errorBlock
|
|
76 |
| val |
|
|
77 |
val _ root removeString: aString startingAt: 1.
|
|
78 |
root canBeCulled ifTrue: [root _ nil].
|
|
79 |
^ val ifNil: errorBlock
|
|
80 |
!
|
|
81 |
|
|
82 |
values
|
|
83 |
^ Array streamContents: [:s | self do: [:ea | s nextPut: ea]]
|
|
84 |
! !
|
|
85 |
|
|
86 |
!TSTree methodsFor:'as yet unclassified'!
|
|
87 |
|
|
88 |
matchesForPrefix: aString
|
|
89 |
^ Array streamContents:
|
|
90 |
[:s |
|
|
91 |
self matchesForPrefix: aString do:
|
|
92 |
[:match |
|
|
93 |
s nextPut: match]]
|
|
94 |
!
|
|
95 |
|
|
96 |
matchesForPrefix: aString do: aBlock
|
2468
|
97 |
aString isEmpty
|
|
98 |
ifTrue: [self do: aBlock]
|
|
99 |
ifFalse: [root notNil ifTrue: [root matchesForPrefix: aString startingAt: 1 do: aBlock]]
|
|
100 |
|
|
101 |
"Modified: / 08-08-2010 / 15:17:01 / cg"
|
2282
|
102 |
!
|
|
103 |
|
|
104 |
matchesForString: aString distance: aNumber
|
|
105 |
^ Array streamContents:
|
|
106 |
[:s |
|
|
107 |
self matchesForString: aString distance: aNumber do:
|
|
108 |
[:match |
|
|
109 |
s nextPut: match]]
|
|
110 |
!
|
|
111 |
|
|
112 |
matchesForString: aString distance: aNumber do: aBlock
|
|
113 |
self matchesForString: aString distance: aNumber limitNodes: nil do: aBlock
|
|
114 |
!
|
|
115 |
|
|
116 |
matchesForString: aString distance: aNumber limitNodes: maxNodes do: aBlock
|
|
117 |
| nodeCount |
|
|
118 |
nodeCount _ 0.
|
|
119 |
aString isEmpty ifTrue: [^ self].
|
|
120 |
root isNil ifTrue: [^ self].
|
|
121 |
root matchesForString: aString startingAt: 1 distance: aNumber do: aBlock nodesDo:
|
|
122 |
[:ea |
|
|
123 |
nodeCount _ nodeCount + 1.
|
|
124 |
nodeCount = maxNodes ifTrue: [^ self]]
|
|
125 |
! !
|
|
126 |
|
|
127 |
!TSTree methodsFor:'enumerating'!
|
|
128 |
|
|
129 |
do: aBlock
|
2468
|
130 |
root notNil ifTrue: [root do: aBlock]
|
|
131 |
|
|
132 |
"Modified: / 08-08-2010 / 15:16:53 / cg"
|
2282
|
133 |
! !
|
|
134 |
|
|
135 |
!TSTree::TSTreeNode class methodsFor:'as yet unclassified'!
|
|
136 |
|
|
137 |
key: aCharacter
|
|
138 |
^ self basicNew initializeWithKey: aCharacter
|
|
139 |
! !
|
|
140 |
|
|
141 |
!TSTree::TSTreeNode class methodsFor:'documentation'!
|
|
142 |
|
|
143 |
documentation
|
|
144 |
"
|
|
145 |
[author:]
|
|
146 |
Avi Bryant
|
|
147 |
|
|
148 |
[license:]
|
|
149 |
Dual licensed under both SqueakL and MIT.
|
|
150 |
This enables both base Squeak inclusion and 100% reuse.
|
|
151 |
"
|
|
152 |
! !
|
|
153 |
|
|
154 |
!TSTree::TSTreeNode methodsFor:'private'!
|
|
155 |
|
|
156 |
canBeCulled
|
|
157 |
^ self value isNil
|
|
158 |
and: [low isNil]
|
|
159 |
and: [equal isNil]
|
|
160 |
and: [high isNil]
|
|
161 |
!
|
|
162 |
|
|
163 |
cullNode: aNode
|
|
164 |
low == aNode ifTrue: [^ low _ nil].
|
|
165 |
equal == aNode ifTrue: [^ equal _ nil].
|
|
166 |
high == aNode ifTrue: [^ high _ nil]
|
|
167 |
!
|
|
168 |
|
|
169 |
do: aBlock
|
|
170 |
self nodesDo: [:ea | ea value ifNotNilDo: aBlock]
|
|
171 |
!
|
|
172 |
|
|
173 |
initializeWithKey: aCharacter
|
|
174 |
key _ aCharacter
|
|
175 |
!
|
|
176 |
|
|
177 |
lookupString: aString startingAt: i
|
|
178 |
"inlined for performance"
|
|
179 |
"
|
2468
|
180 |
self
|
|
181 |
lookupString: aString
|
|
182 |
startingAt: i
|
|
183 |
whenFound: [^ value]
|
|
184 |
whenNil: [:c | ^ nil]
|
|
185 |
recurseWith: [:node :j | ^ node lookupString: aString startingAt: j]"
|
|
186 |
| char |
|
|
187 |
char := aString at: i.
|
|
188 |
char = key
|
|
189 |
ifTrue:
|
|
190 |
[aString size = i
|
|
191 |
ifTrue: [^ value]
|
|
192 |
ifFalse: [^ equal notNil ifTrue: [equal lookupString: aString startingAt: i+1] ifFalse:[nil]]]
|
|
193 |
ifFalse:
|
|
194 |
[char < key
|
|
195 |
ifTrue: [^ low notNil ifTrue: [low lookupString: aString startingAt: i] ifFalse:[nil]]
|
|
196 |
ifFalse: [^ high notNil ifTrue: [high lookupString: aString startingAt: i] ifFalse:[nil]]]
|
|
197 |
|
|
198 |
"Modified: / 08-08-2010 / 15:18:03 / cg"
|
2282
|
199 |
!
|
|
200 |
|
|
201 |
lookupString: aString startingAt: i insert: anObject
|
|
202 |
self
|
|
203 |
lookupString: aString
|
|
204 |
startingAt: i
|
|
205 |
whenFound: [self value: anObject]
|
|
206 |
whenNil: [:c | self newNodeWithKey: c]
|
|
207 |
recurseWith: [:node :j | node lookupString: aString startingAt: j insert: anObject]
|
|
208 |
!
|
|
209 |
|
|
210 |
lookupString: aString startingAt: i whenFound: foundBlock whenNil: nilBlock recurseWith: recurseBlock
|
|
211 |
| char |
|
|
212 |
char := aString at: i.
|
|
213 |
char = key
|
|
214 |
ifTrue:
|
|
215 |
[aString size = i
|
|
216 |
ifTrue: [foundBlock value]
|
|
217 |
ifFalse: [equal ifNil: [equal := nilBlock value: (aString at: i+1)].
|
|
218 |
recurseBlock value: equal value: i+1]]
|
|
219 |
ifFalse:
|
|
220 |
[char < key
|
|
221 |
ifTrue: [low ifNil: [low := nilBlock value: char].
|
|
222 |
recurseBlock value: low value: i]
|
|
223 |
ifFalse: [high ifNil: [high := nilBlock value: char].
|
|
224 |
recurseBlock value: high value: i]]
|
|
225 |
!
|
|
226 |
|
|
227 |
matchesForPrefix: aString startingAt: i do: aBlock
|
2468
|
228 |
self
|
|
229 |
lookupString: aString
|
|
230 |
startingAt: i
|
|
231 |
whenFound: [value notNil ifTrue: [aBlock value: value]. equal notNil ifTrue: [equal do: aBlock]]
|
|
232 |
whenNil: [:c | ^ self]
|
|
233 |
recurseWith: [:n :j | n matchesForPrefix: aString startingAt: j do: aBlock]
|
|
234 |
|
|
235 |
"Modified: / 08-08-2010 / 15:18:44 / cg"
|
2282
|
236 |
!
|
|
237 |
|
|
238 |
matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock
|
2468
|
239 |
|
|
240 |
| char d2 |
|
|
241 |
nodeBlock value: self.
|
|
242 |
d < 0 ifTrue: [^ self].
|
|
243 |
|
|
244 |
char := aString at: i.
|
|
245 |
(d > 0 or: [char < key])
|
|
246 |
ifTrue: [low notNil ifTrue: [low matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]].
|
|
247 |
|
|
248 |
d2 := char = key ifTrue: [d] ifFalse: [d-1].
|
|
249 |
(i + d2 = aString size and: [value notNil]) ifTrue: [aBlock value: value].
|
|
250 |
equal ifNotNil: [equal matchesForString: aString startingAt: (i+1 min: aString size) distance: d2 do: aBlock nodesDo: nodeBlock].
|
|
251 |
|
|
252 |
(d > 0 or: [char > key])
|
|
253 |
ifTrue: [high notNil ifTrue: [high matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]]
|
|
254 |
|
|
255 |
"Modified: / 08-08-2010 / 15:18:57 / cg"
|
2282
|
256 |
!
|
|
257 |
|
|
258 |
newNodeWithKey: aCharacter
|
|
259 |
^ self class key: aCharacter
|
|
260 |
!
|
|
261 |
|
|
262 |
nodesDo: aBlock
|
2468
|
263 |
aBlock value: self.
|
|
264 |
low notNil ifTrue: [low nodesDo: aBlock].
|
|
265 |
equal notNil ifTrue: [equal nodesDo: aBlock].
|
|
266 |
high notNil ifTrue: [high nodesDo: aBlock]
|
|
267 |
|
|
268 |
"Modified: / 08-08-2010 / 15:19:05 / cg"
|
2282
|
269 |
!
|
|
270 |
|
|
271 |
removeString: aString startingAt: i
|
|
272 |
| val |
|
|
273 |
self
|
|
274 |
lookupString: aString
|
|
275 |
startingAt: i
|
|
276 |
whenFound: [val _ self value. self value: nil]
|
|
277 |
whenNil: [:c | ^ nil]
|
|
278 |
recurseWith:
|
|
279 |
[:node :j |
|
|
280 |
val _ node removeString: aString startingAt: j.
|
|
281 |
node canBeCulled ifTrue:
|
|
282 |
[self cullNode: node]].
|
|
283 |
^ val
|
|
284 |
!
|
|
285 |
|
|
286 |
value
|
|
287 |
^ value
|
|
288 |
!
|
|
289 |
|
|
290 |
value: anObject
|
|
291 |
value _ anObject
|
|
292 |
! !
|
|
293 |
|
|
294 |
!TSTree class methodsFor:'documentation'!
|
|
295 |
|
|
296 |
version_CVS
|
2468
|
297 |
^ '$Header: /cvs/stx/stx/libbasic2/TSTree.st,v 1.2 2010-08-08 13:21:30 cg Exp $'
|
2282
|
298 |
! !
|