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