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