author | Merge Script |
Thu, 24 Nov 2016 06:40:21 +0100 | |
branch | jv |
changeset 21027 | ad86468de3a0 |
parent 21026 | 81e280fc1b93 |
parent 21006 | 076293e19939 |
child 22129 | 697c819c7674 |
permissions | -rw-r--r-- |
6 | 1 |
" |
2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 3 |
All Rights Reserved |
6 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
6233 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
15 |
|
6 | 16 |
IdentityDictionary subclass:#WeakIdentityDictionary |
1054 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
4350 | 20 |
category:'Collections-Weak' |
6 | 21 |
! |
22 |
||
10 | 23 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
10 | 39 |
documentation |
40 |
" |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
41 |
WeakIdentityDictionaries behave like IdentityDictionaries, |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
42 |
as long as the keys are still referenced by some |
1263 | 43 |
other (non-weak) object. |
44 |
However, once the last non-weak reference ceases to exist, |
|
45 |
the object will be automatically removed from the Weakcollection |
|
46 |
(with some delay: it will be removed after the next garbage collect). |
|
47 |
||
48 |
This class was added to support keeping track of dependents without |
|
88 | 49 |
keeping the values alive - values simply become nil when no one else |
50 |
references it. The original dependency mechanism used a regular Dictionary, |
|
51 |
which usually leads to a lot of garbage being kept due to a forgotten |
|
52 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much |
|
53 |
more comfortable, since no manual release of dependents is needed. |
|
1263 | 54 |
|
2306 | 55 |
[Warning:] |
56 |
If you use this, be very careful since the collections size changes |
|
57 |
'magically' - for example, testing for being nonEmpty and then |
|
58 |
removing the first element may fail, since the element may vanish inbetween. |
|
59 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 60 |
|
61 |
||
1290 | 62 |
[author:] |
2910 | 63 |
Claus Gittinger |
1290 | 64 |
|
1263 | 65 |
[See also:] |
2910 | 66 |
WeakArray WeakValueDictionary WeakIdentitySet |
10 | 67 |
" |
68 |
! ! |
|
69 |
||
21006 | 70 |
!WeakIdentityDictionary methodsFor:'accessing'! |
20990 | 71 |
|
72 |
keys |
|
73 |
"return a collection containing all valid keys of the receiver" |
|
74 |
||
75 |
|keySet| |
|
76 |
||
77 |
keySet := self emptyCollectionForKeys. |
|
78 |
keyArray validElementsDo:[:key | |
|
79 |
(key notNil and:[key ~~ DeletedEntry]) ifTrue:[ |
|
80 |
key == NilEntry ifTrue:[ |
|
81 |
keySet add:nil. |
|
82 |
] ifFalse:[ |
|
83 |
keySet add:key. |
|
84 |
] |
|
85 |
] |
|
86 |
]. |
|
87 |
^ keySet |
|
88 |
! ! |
|
89 |
||
359 | 90 |
!WeakIdentityDictionary methodsFor:'adding & removing'! |
91 |
||
2298 | 92 |
at:key ifAbsent:exceptionBlock |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
93 |
"redefined to block interrupts |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
94 |
(avoid change of the dictionary while accessing)" |
2298 | 95 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
96 |
|val| |
2298 | 97 |
|
20166 | 98 |
key class == SmallInteger ifTrue:[ |
99 |
"SmallIntegers cannot be stored into a WeakArray" |
|
100 |
^ exceptionBlock value. |
|
101 |
]. |
|
102 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
103 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 104 |
"/ already blocked |
105 |
^ super at:key ifAbsent:exceptionBlock. |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
106 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
107 |
|
2298 | 108 |
[ |
20166 | 109 |
val := super at:key ifAbsent:exceptionBlock. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
110 |
] ensure:[ |
20166 | 111 |
OperatingSystem unblockInterrupts. |
2298 | 112 |
]. |
113 |
^ val |
|
114 |
||
115 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
116 |
"Modified: 1.7.1997 / 10:45:47 / cg" |
2298 | 117 |
! |
118 |
||
19193 | 119 |
at:key ifAbsentPut:replacementBlock |
120 |
"redefined to block interrupts |
|
121 |
(avoid change of the dictionary while accessing)" |
|
122 |
||
123 |
|val| |
|
124 |
||
20166 | 125 |
key class == SmallInteger ifTrue:[ |
126 |
"SmallIntegers cannot be stored into a WeakArray" |
|
127 |
self error:'WeakidentityDictionary: cannot store a SmallInteger'. |
|
128 |
]. |
|
129 |
||
19193 | 130 |
(OperatingSystem blockInterrupts) ifTrue:[ |
131 |
"/ already blocked |
|
132 |
^ super at:key ifAbsentPut:replacementBlock. |
|
133 |
]. |
|
134 |
||
135 |
[ |
|
136 |
val := super at:key ifAbsentPut:replacementBlock. |
|
137 |
] ensure:[ |
|
138 |
OperatingSystem unblockInterrupts. |
|
139 |
]. |
|
140 |
^ val |
|
141 |
! |
|
142 |
||
1256 | 143 |
at:key put:anObject |
144 |
"add the argument anObject under key, aKey to the receiver. |
|
145 |
Return anObject (sigh). |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
146 |
Redefined to block interrupts, to avoid trouble when dependencies |
1256 | 147 |
are added within interrupting high prio processes." |
148 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
149 |
|val| |
359 | 150 |
|
20166 | 151 |
key class == SmallInteger ifTrue:[ |
152 |
"SmallIntegers cannot be stored into a WeakArray" |
|
153 |
self error:'WeakidentityDictionary: cannot store a SmallInteger'. |
|
154 |
]. |
|
155 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
156 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 157 |
"/ already blocked |
158 |
^ super at:key put:anObject. |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
159 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
160 |
|
1790 | 161 |
[ |
20166 | 162 |
val := super at:key put:anObject. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
163 |
] ensure:[ |
20166 | 164 |
OperatingSystem unblockInterrupts. |
1790 | 165 |
]. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
166 |
^ val |
1223 | 167 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
168 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
169 |
"Modified: 29.1.1997 / 15:08:45 / cg" |
359 | 170 |
! |
171 |
||
172 |
removeKey:aKey ifAbsent:aBlock |
|
1256 | 173 |
"remove the association under aKey from the collection, |
2910 | 174 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
175 |
If it was not in the collection return the result |
1256 | 176 |
from evaluating aBlock. |
177 |
||
178 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
179 |
of interrupts (otherwise, there could be some other operation |
1256 | 180 |
on the receiver done by another process, which garbles my contents)." |
1054 | 181 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
182 |
|ret| |
359 | 183 |
|
20166 | 184 |
aKey class == SmallInteger ifTrue:[ |
185 |
"SmallIntegers cannot be stored into a WeakArray" |
|
186 |
^ aBlock value. |
|
187 |
]. |
|
188 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
189 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 190 |
"/ already blocked |
191 |
^ super removeKey:aKey ifAbsent:aBlock. |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
192 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
193 |
|
359 | 194 |
[ |
20166 | 195 |
ret := super removeKey:aKey ifAbsent:aBlock |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
196 |
] ensure:[ |
20166 | 197 |
OperatingSystem unblockInterrupts |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
198 |
]. |
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
199 |
^ ret |
1054 | 200 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
201 |
"Modified: 6.5.1996 / 12:44:51 / stefan" |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
202 |
"Modified: 29.1.1997 / 15:09:11 / cg" |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
203 |
! |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
204 |
|
16513 | 205 |
safeRemoveKey:key |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
206 |
"redefined to block interrupts |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
207 |
(avoid change of the dictionary while accessing)" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
208 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
209 |
|val| |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
210 |
|
20166 | 211 |
key class == SmallInteger ifTrue:[ |
212 |
"SmallIntegers cannot be stored into a WeakArray" |
|
213 |
^ nil. |
|
214 |
]. |
|
215 |
||
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
216 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 217 |
"/ already blocked |
218 |
^ super safeRemoveKey:key. |
|
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
219 |
]. |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
220 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
221 |
[ |
20166 | 222 |
val := super safeRemoveKey:key. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
223 |
] ensure:[ |
20166 | 224 |
OperatingSystem unblockInterrupts. |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
225 |
]. |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
226 |
^ val |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
227 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
228 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
229 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
230 |
"Created: 1.7.1997 / 10:46:34 / cg" |
359 | 231 |
! ! |
232 |
||
10 | 233 |
!WeakIdentityDictionary methodsFor:'element disposal'! |
234 |
||
6309 | 235 |
clearDeadSlots |
20702 | 236 |
|wasBlocked anyChange| |
6309 | 237 |
|
238 |
" |
|
239 |
have to block here - dispose may be done at a low priority |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
240 |
from the background finalizer. If new items are added by a |
6309 | 241 |
higher prio process, the dictionary might get corrupted otherwise |
242 |
" |
|
243 |
wasBlocked := OperatingSystem blockInterrupts. |
|
20702 | 244 |
anyChange := false. |
6469 | 245 |
[ |
20702 | 246 |
keyArray |
247 |
forAllDeadIndicesDo:[:idx | |
|
248 |
valueArray basicAt:idx put:nil. |
|
249 |
tally := tally - 1. |
|
250 |
anyChange := true. |
|
251 |
] |
|
252 |
replacingCorpsesWith:DeletedEntry. |
|
6469 | 253 |
] ensure:[ |
20702 | 254 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6469 | 255 |
]. |
20702 | 256 |
anyChange ifTrue:[ |
257 |
self changed:#ElementExpired |
|
258 |
]. |
|
6309 | 259 |
|
260 |
"Modified: / 13.12.2001 / 14:18:17 / martin" |
|
261 |
! |
|
262 |
||
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
263 |
update:something with:aParameter from:changedObject |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
264 |
"an element (either key or value) died - clear out slots for |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
265 |
disposed keys." |
6 | 266 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
267 |
something == #ElementExpired ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
268 |
self clearDeadSlots. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
269 |
] |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
270 |
|
6309 | 271 |
"Created: / 7.1.1997 / 16:59:30 / stefan" |
272 |
"Modified: / 13.12.2001 / 14:17:58 / martin" |
|
6 | 273 |
! ! |
609 | 274 |
|
20992 | 275 |
!WeakIdentityDictionary methodsFor:'enumerating'! |
276 |
||
277 |
keysDo:aBlock |
|
278 |
"evaluate aBlock for each registered object" |
|
279 |
||
280 |
"#Dictionary>>keysDo: would not work, since the keyArray instvar may change if |
|
281 |
elements are unregistered while looping." |
|
282 |
||
283 |
^ keyArray validElementsDo:[:each| |
|
284 |
each ~~ DeletedEntry ifTrue:[ |
|
285 |
each == NilEntry ifTrue:[ |
|
286 |
aBlock value:nil. |
|
287 |
] ifFalse:[ |
|
288 |
aBlock value:each. |
|
289 |
] |
|
290 |
]. |
|
291 |
] |
|
292 |
! ! |
|
293 |
||
609 | 294 |
!WeakIdentityDictionary methodsFor:'private'! |
295 |
||
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
296 |
findKeyOrNil:key |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
297 |
"Look for the key in the receiver. |
2988 | 298 |
If it is found, return the index, |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
299 |
otherwise the index of the first unused slot. |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
300 |
Grow the receiver, if key was not found, and no unused slots were present. |
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
301 |
|
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
302 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
20037 | 303 |
by at:put: / add: - like methods." |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
304 |
|
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
305 |
|index "{ Class:SmallInteger }" |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
306 |
length "{ Class:SmallInteger }" |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
307 |
startIndex probe |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
308 |
delIndex "{ Class:SmallInteger }"| |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
309 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
310 |
(OperatingSystem blockInterrupts) ifFalse:[ |
20037 | 311 |
"/ |
312 |
"/ may never be entered with interrupts enabled |
|
313 |
"/ |
|
314 |
OperatingSystem unblockInterrupts. |
|
315 |
self error:'unblocked call of findKeyOrNil'. |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
316 |
]. |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
317 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
318 |
delIndex := 0. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
319 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
320 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6469
diff
changeset
|
321 |
startIndex := index := self initialIndexForKey:key. |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
322 |
|
16244 | 323 |
[ |
20037 | 324 |
probe := keyArray basicAt:index. |
325 |
key == probe ifTrue:[^ index]. |
|
326 |
probe isNil ifTrue:[ |
|
327 |
delIndex == 0 ifTrue:[^ index]. |
|
328 |
keyArray basicAt:delIndex put:nil. |
|
329 |
^ delIndex |
|
330 |
]. |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
331 |
|
20037 | 332 |
probe class == SmallInteger ifTrue:[ |
333 |
probe := DeletedEntry. |
|
334 |
keyArray basicAt:index put:probe. |
|
335 |
valueArray basicAt:index put:nil. |
|
336 |
tally := tally - 1. |
|
337 |
]. |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
338 |
|
20037 | 339 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
340 |
delIndex := index |
|
341 |
]. |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
342 |
|
20037 | 343 |
index == length ifTrue:[ |
344 |
index := 1 |
|
345 |
] ifFalse:[ |
|
346 |
index := index + 1 |
|
347 |
]. |
|
348 |
index == startIndex ifTrue:[ |
|
349 |
delIndex ~~ 0 ifTrue:[ |
|
350 |
keyArray basicAt:delIndex put:nil. |
|
351 |
^ delIndex |
|
352 |
]. |
|
353 |
self grow. |
|
354 |
length := keyArray basicSize. |
|
355 |
startIndex := index := self initialIndexForKey:key. |
|
356 |
]. |
|
16244 | 357 |
] loop. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
358 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
359 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
2988 | 360 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
361 |
! |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
362 |
|
20237 | 363 |
findKeyOrNilOrDeletedEntry:key |
364 |
"Look for the key in the receiver. |
|
365 |
If it is found, return the index, |
|
366 |
otherwise the index of the first unused slot. |
|
367 |
Grow the receiver, if key was not found, and no unused slots were present." |
|
368 |
||
369 |
|index "{ Class:SmallInteger }" |
|
370 |
length "{ Class:SmallInteger }" |
|
371 |
startIndex probe |
|
372 |
delIndex "{ Class:SmallInteger }"| |
|
373 |
||
374 |
(OperatingSystem blockInterrupts) ifFalse:[ |
|
375 |
"/ |
|
376 |
"/ may never be entered with interrupts enabled |
|
377 |
"/ |
|
378 |
OperatingSystem unblockInterrupts. |
|
379 |
self error:'unblocked call of findKeyOrNil'. |
|
380 |
]. |
|
381 |
||
382 |
delIndex := 0. |
|
383 |
||
384 |
length := keyArray basicSize. |
|
385 |
startIndex := index := self initialIndexForKey:key. |
|
386 |
||
387 |
[ |
|
388 |
probe := keyArray basicAt:index. |
|
389 |
key == probe ifTrue:[^ index]. |
|
390 |
probe isNil ifTrue:[ |
|
391 |
delIndex == 0 ifTrue:[^ index]. |
|
392 |
^ delIndex |
|
393 |
]. |
|
394 |
||
395 |
probe class == SmallInteger ifTrue:[ |
|
396 |
probe := DeletedEntry. |
|
397 |
keyArray basicAt:index put:probe. |
|
398 |
valueArray basicAt:index put:nil. |
|
399 |
tally := tally - 1. |
|
400 |
]. |
|
401 |
||
402 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
403 |
delIndex := index |
|
404 |
]. |
|
405 |
||
406 |
index == length ifTrue:[ |
|
407 |
index := 1 |
|
408 |
] ifFalse:[ |
|
409 |
index := index + 1 |
|
410 |
]. |
|
411 |
index == startIndex ifTrue:[ |
|
412 |
delIndex ~~ 0 ifTrue:[ |
|
413 |
^ delIndex |
|
414 |
]. |
|
415 |
self grow. |
|
416 |
length := keyArray basicSize. |
|
417 |
startIndex := index := self initialIndexForKey:key. |
|
418 |
]. |
|
419 |
] loop. |
|
420 |
||
421 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
|
422 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
|
423 |
! |
|
424 |
||
2298 | 425 |
grow:newSize |
426 |
"grow the receiver. |
|
427 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
428 |
are added within interrupting high prio processes." |
|
429 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
430 |
"/ 'grow:' printCR. |
2298 | 431 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
432 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
433 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
434 |
^ super grow:newSize. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
435 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
436 |
|
2298 | 437 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
438 |
super grow:newSize |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
439 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
440 |
OperatingSystem unblockInterrupts |
2298 | 441 |
]. |
442 |
||
443 |
"Created: 28.1.1997 / 23:41:39 / cg" |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
444 |
"Modified: 29.1.1997 / 15:10:12 / cg" |
2298 | 445 |
! |
446 |
||
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
447 |
initializeForCapacity:minSize |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
448 |
"reset the receiver's contents. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
449 |
Redefined to block interrupts, to avoid trouble when dependencies |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
450 |
are added within interrupting high prio processes." |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
451 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
452 |
"/ 'setTally:' printCR. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
453 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
454 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
455 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
456 |
super initializeForCapacity:minSize. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
457 |
^ self. |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
458 |
]. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
459 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
460 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
461 |
super initializeForCapacity:minSize |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
462 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
463 |
OperatingSystem unblockInterrupts |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
464 |
]. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
465 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
466 |
"Created: 29.1.1997 / 11:40:12 / cg" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
467 |
"Modified: 29.1.1997 / 15:11:11 / cg" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
468 |
! |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
469 |
|
609 | 470 |
keyContainerOfSize:n |
471 |
"return a container for keys of size n. |
|
20037 | 472 |
use WeakArrays here." |
609 | 473 |
|
474 |
|w| |
|
475 |
||
476 |
w := WeakArray new:n. |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
477 |
w addDependent:self. |
609 | 478 |
^ w |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
479 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
480 |
"Modified: 7.1.1997 / 17:01:15 / stefan" |
2298 | 481 |
"Modified: 29.1.1997 / 14:18:49 / cg" |
482 |
! |
|
483 |
||
484 |
rehash |
|
485 |
"grow the receiver. |
|
486 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
487 |
are added within interrupting high prio processes." |
|
488 |
||
489 |
"/ 'rehash' printCR. |
|
490 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
491 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
492 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
493 |
super rehash. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
494 |
^ self. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
495 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
496 |
|
2298 | 497 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
498 |
super rehash |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
499 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
500 |
OperatingSystem unblockInterrupts |
2298 | 501 |
]. |
502 |
||
503 |
"Created: 29.1.1997 / 11:39:42 / cg" |
|
504 |
"Modified: 29.1.1997 / 14:18:52 / cg" |
|
609 | 505 |
! ! |
506 |
||
2741 | 507 |
!WeakIdentityDictionary methodsFor:'testing'! |
508 |
||
509 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
510 |
"redefined to block interrupts |
2741 | 511 |
(avoid change of the dictionary while accessing)" |
512 |
||
513 |
|val| |
|
514 |
||
515 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
516 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
517 |
^ super includes:anObject. |
2741 | 518 |
]. |
519 |
||
520 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
521 |
val := super includes:anObject. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
522 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
523 |
OperatingSystem unblockInterrupts. |
2741 | 524 |
]. |
525 |
^ val |
|
526 |
||
527 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
528 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
529 |
"Created: 1.7.1997 / 10:47:13 / cg" |
|
530 |
! |
|
531 |
||
532 |
includesKey:key |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
533 |
"redefined to block interrupts |
2741 | 534 |
(avoid change of the dictionary while accessing)" |
535 |
||
536 |
|val| |
|
537 |
||
20166 | 538 |
key class == SmallInteger ifTrue:[ |
539 |
"SmallIntegers cannot be stored into a WeakArray" |
|
540 |
^ false. |
|
541 |
]. |
|
542 |
||
2741 | 543 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 544 |
"/ already blocked |
545 |
^ super includesKey:key. |
|
2741 | 546 |
]. |
547 |
||
548 |
[ |
|
20166 | 549 |
val := super includesKey:key. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
550 |
] ensure:[ |
20166 | 551 |
OperatingSystem unblockInterrupts. |
2741 | 552 |
]. |
553 |
^ val |
|
554 |
||
555 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
556 |
"Created: 1.7.1997 / 10:45:14 / cg" |
|
557 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
9243 | 558 |
! |
559 |
||
560 |
isWeakCollection |
|
561 |
"return true, if the receiver has weak references to its elements." |
|
562 |
||
563 |
^ true |
|
2741 | 564 |
! ! |
565 |
||
635 | 566 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
567 |
||
568 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
569 |
^ '$Header$' |
20992 | 570 |
! |
571 |
||
572 |
version_CVS |
|
573 |
^ '$Header$' |
|
635 | 574 |
! ! |
16244 | 575 |