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