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