5393
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
3251
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
3676
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
3251
|
7 |
Object subclass:#TSTreeNode
|
|
8 |
instanceVariableNames:'key value low high equal'
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Collections-Ordered-Trees-Private'
|
|
12 |
!
|
|
13 |
|
|
14 |
!TSTreeNode class methodsFor:'documentation'!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
[author:]
|
|
19 |
Avi Bryant
|
|
20 |
|
|
21 |
[license:]
|
|
22 |
Dual licensed under both SqueakL and MIT.
|
|
23 |
This enables both base Squeak inclusion and 100% reuse.
|
|
24 |
"
|
|
25 |
! !
|
|
26 |
|
|
27 |
!TSTreeNode class methodsFor:'as yet unclassified'!
|
|
28 |
|
|
29 |
key: aCharacter
|
|
30 |
^ self basicNew initializeWithKey: aCharacter
|
|
31 |
! !
|
|
32 |
|
|
33 |
!TSTreeNode methodsFor:'private'!
|
|
34 |
|
|
35 |
canBeCulled
|
3676
|
36 |
^ self value isNil
|
|
37 |
and: [low isNil
|
|
38 |
and: [equal isNil
|
|
39 |
and: [high isNil]]]
|
3251
|
40 |
!
|
|
41 |
|
|
42 |
cullNode: aNode
|
5393
|
43 |
low == aNode ifTrue: [^ low := nil].
|
|
44 |
equal == aNode ifTrue: [^ equal := nil].
|
|
45 |
high == aNode ifTrue: [^ high := nil]
|
3251
|
46 |
!
|
|
47 |
|
|
48 |
do: aBlock
|
|
49 |
self nodesDo: [:ea |
|
|
50 |
|t|
|
|
51 |
|
|
52 |
(t := ea value) notNil ifTrue:[
|
|
53 |
aBlock value:t
|
|
54 |
]
|
|
55 |
].
|
|
56 |
!
|
|
57 |
|
|
58 |
initializeWithKey: aCharacter
|
5393
|
59 |
key := aCharacter
|
3251
|
60 |
!
|
|
61 |
|
|
62 |
lookupString: aString startingAt: i
|
|
63 |
"inlined for performance"
|
|
64 |
"
|
|
65 |
self
|
|
66 |
lookupString: aString
|
|
67 |
startingAt: i
|
|
68 |
whenFound: [^ value]
|
|
69 |
whenNil: [:c | ^ nil]
|
|
70 |
recurseWith: [:node :j | ^ node lookupString: aString startingAt: j]"
|
|
71 |
| char |
|
|
72 |
char := aString at: i.
|
|
73 |
char = key
|
|
74 |
ifTrue:
|
|
75 |
[aString size = i
|
|
76 |
ifTrue: [^ value]
|
|
77 |
ifFalse: [^ equal notNil ifTrue: [equal lookupString: aString startingAt: i+1] ifFalse:[nil]]]
|
|
78 |
ifFalse:
|
|
79 |
[char < key
|
|
80 |
ifTrue: [^ low notNil ifTrue: [low lookupString: aString startingAt: i] ifFalse:[nil]]
|
|
81 |
ifFalse: [^ high notNil ifTrue: [high lookupString: aString startingAt: i] ifFalse:[nil]]]
|
|
82 |
|
|
83 |
"Modified: / 08-08-2010 / 15:18:03 / cg"
|
|
84 |
!
|
|
85 |
|
|
86 |
lookupString: aString startingAt: i insert: anObject
|
|
87 |
self
|
|
88 |
lookupString: aString
|
|
89 |
startingAt: i
|
|
90 |
whenFound: [self value: anObject]
|
|
91 |
whenNil: [:c | self newNodeWithKey: c]
|
|
92 |
recurseWith: [:node :j | node lookupString: aString startingAt: j insert: anObject]
|
|
93 |
!
|
|
94 |
|
|
95 |
lookupString: aString startingAt: i whenFound: foundBlock whenNil: nilBlock recurseWith: recurseBlock
|
|
96 |
| char |
|
|
97 |
char := aString at: i.
|
|
98 |
char = key
|
|
99 |
ifTrue:
|
|
100 |
[aString size = i
|
|
101 |
ifTrue: [foundBlock value]
|
|
102 |
ifFalse: [equal ifNil: [equal := nilBlock value: (aString at: i+1)].
|
|
103 |
recurseBlock value: equal value: i+1]]
|
|
104 |
ifFalse:
|
|
105 |
[char < key
|
|
106 |
ifTrue: [low ifNil: [low := nilBlock value: char].
|
|
107 |
recurseBlock value: low value: i]
|
|
108 |
ifFalse: [high ifNil: [high := nilBlock value: char].
|
|
109 |
recurseBlock value: high value: i]]
|
|
110 |
!
|
|
111 |
|
|
112 |
matchesForPrefix: aString startingAt: i do: aBlock
|
|
113 |
self
|
|
114 |
lookupString: aString
|
|
115 |
startingAt: i
|
|
116 |
whenFound: [value notNil ifTrue: [aBlock value: value]. equal notNil ifTrue: [equal do: aBlock]]
|
|
117 |
whenNil: [:c | ^ self]
|
|
118 |
recurseWith: [:n :j | n matchesForPrefix: aString startingAt: j do: aBlock]
|
|
119 |
|
|
120 |
"Modified: / 08-08-2010 / 15:18:44 / cg"
|
|
121 |
!
|
|
122 |
|
|
123 |
matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock
|
|
124 |
|
|
125 |
| char d2 |
|
|
126 |
nodeBlock value: self.
|
|
127 |
d < 0 ifTrue: [^ self].
|
|
128 |
|
|
129 |
char := aString at: i.
|
|
130 |
(d > 0 or: [char < key])
|
|
131 |
ifTrue: [low notNil ifTrue: [low matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]].
|
|
132 |
|
|
133 |
d2 := char = key ifTrue: [d] ifFalse: [d-1].
|
|
134 |
(i + d2 = aString size and: [value notNil]) ifTrue: [aBlock value: value].
|
|
135 |
equal ifNotNil: [equal matchesForString: aString startingAt: (i+1 min: aString size) distance: d2 do: aBlock nodesDo: nodeBlock].
|
|
136 |
|
|
137 |
(d > 0 or: [char > key])
|
|
138 |
ifTrue: [high notNil ifTrue: [high matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]]
|
|
139 |
|
|
140 |
"Modified: / 08-08-2010 / 15:18:57 / cg"
|
|
141 |
!
|
|
142 |
|
|
143 |
newNodeWithKey: aCharacter
|
|
144 |
^ self class key: aCharacter
|
|
145 |
!
|
|
146 |
|
|
147 |
nodesDo: aBlock
|
|
148 |
aBlock value: self.
|
|
149 |
low notNil ifTrue: [low nodesDo: aBlock].
|
|
150 |
equal notNil ifTrue: [equal nodesDo: aBlock].
|
|
151 |
high notNil ifTrue: [high nodesDo: aBlock]
|
|
152 |
|
|
153 |
"Modified: / 08-08-2010 / 15:19:05 / cg"
|
|
154 |
!
|
|
155 |
|
|
156 |
removeString: aString startingAt: i
|
5393
|
157 |
| val |
|
|
158 |
self
|
|
159 |
lookupString: aString
|
|
160 |
startingAt: i
|
|
161 |
whenFound: [val := self value. self value: nil]
|
|
162 |
whenNil: [:c | ^ nil]
|
|
163 |
recurseWith:
|
|
164 |
[:node :j |
|
|
165 |
val := node removeString:aString startingAt:j.
|
|
166 |
node canBeCulled ifTrue:
|
|
167 |
[self cullNode: node]].
|
|
168 |
^ val
|
3251
|
169 |
!
|
|
170 |
|
|
171 |
value
|
|
172 |
^ value
|
|
173 |
!
|
|
174 |
|
|
175 |
value: anObject
|
5393
|
176 |
value := anObject
|
3251
|
177 |
! !
|
|
178 |
|
|
179 |
!TSTreeNode class methodsFor:'documentation'!
|
|
180 |
|
|
181 |
version
|
3676
|
182 |
^ '$Header$'
|
3251
|
183 |
!
|
|
184 |
|
|
185 |
version_CVS
|
3676
|
186 |
^ '$Header$'
|
3251
|
187 |
! !
|
|
188 |
|