author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 16:34:40 +0200 | |
changeset 24379 | 852be153b0ec |
parent 24272 | 913bd63922a3 |
permissions | -rw-r--r-- |
23722 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1341 | 3 |
" |
4 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
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 |
" |
|
6041 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
17 |
|
1341 | 18 |
Dictionary subclass:#WeakValueDictionary |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
4350 | 22 |
category:'Collections-Weak' |
1341 | 23 |
! |
24 |
||
25 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
30 |
All Rights Reserved |
|
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 |
||
41 |
documentation |
|
42 |
" |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
43 |
WeakValueDictionaries behave like Dictionaries, |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
44 |
as long as the values are still referenced by some |
1341 | 45 |
other (non-weak) object. |
46 |
However, once the last non-weak reference ceases to exist, |
|
47 |
the Dictionary will return nil for the value at position key. |
|
48 |
(with some delay: it will be removed after the next garbage collect). |
|
49 |
||
2307 | 50 |
[Warning:] |
51 |
If you use this, be very careful since the collections size changes |
|
52 |
'magically' - for example, testing for being nonEmpty and then |
|
53 |
removing the first element may fail, since the element may vanish inbetween. |
|
54 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 55 |
|
1341 | 56 |
[author:] |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
57 |
Stefan Vogel |
1341 | 58 |
|
59 |
[See also:] |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
60 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 61 |
" |
62 |
! ! |
|
63 |
||
64 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
65 |
||
6330 | 66 |
at:key ifAbsent:somethingRespondingToValue |
19779 | 67 |
"Redefined to handle collected values (which are set to a SmallInteger) |
68 |
snd to block interrupts, to avoid trouble when dependencies |
|
19085 | 69 |
are added within interrupting high prio processes." |
6347 | 70 |
|
6330 | 71 |
|ret| |
72 |
||
19779 | 73 |
OperatingSystem blockInterrupts ifTrue:[ |
74 |
"/ already blocked |
|
75 |
ret := super at:key ifAbsent:0. |
|
76 |
] ifFalse:[ |
|
77 |
"do not use somethingRespondingToValue here, it might be a block answering |
|
78 |
an integer and be therefore called twice" |
|
23722 | 79 |
ret := [ |
80 |
super at:key ifAbsent:0. |
|
19779 | 81 |
] ensure:[ |
82 |
OperatingSystem unblockInterrupts. |
|
83 |
]. |
|
84 |
]. |
|
19085 | 85 |
|
20165 | 86 |
(ret isNil or:[ret class == SmallInteger]) ifTrue:[ |
19085 | 87 |
ret := somethingRespondingToValue value |
88 |
]. |
|
6330 | 89 |
^ ret |
23722 | 90 |
|
91 |
"Modified: / 12-02-2019 / 20:35:30 / Stefan Vogel" |
|
6330 | 92 |
! |
93 |
||
19543 | 94 |
at:key ifAbsentPut:replacementBlock |
6233 | 95 |
"return the element indexed by aKey if present, |
96 |
if not present, store the result of evaluating valueBlock |
|
97 |
under aKey and return it. |
|
6347 | 98 |
|
6233 | 99 |
Redefined to block interrupts, to avoid trouble when dependencies |
100 |
are added within interrupting high prio processes. |
|
101 |
WARNING: do not add elements while iterating over the receiver. |
|
19543 | 102 |
Iterate over a copy to do this." |
6233 | 103 |
|
19543 | 104 |
|val| |
6233 | 105 |
|
19543 | 106 |
OperatingSystem blockInterrupts ifTrue:[ |
107 |
"/ already blocked |
|
108 |
val := super at:key ifAbsentPut:replacementBlock. |
|
109 |
] ifFalse:[ |
|
23722 | 110 |
val := [ |
111 |
super at:key ifAbsentPut:replacementBlock. |
|
19543 | 112 |
] ensure:[ |
113 |
OperatingSystem unblockInterrupts. |
|
114 |
]. |
|
115 |
]. |
|
116 |
||
20165 | 117 |
(val isNil or:[val class == SmallInteger]) ifTrue:[ |
19543 | 118 |
self error:'WeakValueDictionary: invalid value'. |
119 |
]. |
|
120 |
||
121 |
^ val |
|
23722 | 122 |
|
123 |
"Modified: / 12-02-2019 / 20:35:51 / Stefan Vogel" |
|
6233 | 124 |
! |
125 |
||
1341 | 126 |
at:key put:anObject |
127 |
"add the argument anObject under key, aKey to the receiver. |
|
128 |
Return anObject (sigh). |
|
129 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
130 |
are added within interrupting high prio processes." |
|
131 |
||
20165 | 132 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
24272 | 133 |
ArgumentError raise. |
19543 | 134 |
]. |
135 |
||
136 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
137 |
"/ already blocked |
|
138 |
^ super at:key put:anObject. |
|
139 |
]. |
|
1341 | 140 |
|
23722 | 141 |
^ [ |
142 |
super at:key put:anObject. |
|
19543 | 143 |
] ensure:[ |
144 |
OperatingSystem unblockInterrupts. |
|
145 |
]. |
|
1341 | 146 |
|
23722 | 147 |
"Modified: / 06-05-1996 / 12:22:26 / stefan" |
148 |
"Modified: / 29-01-1997 / 15:08:45 / cg" |
|
149 |
"Modified: / 12-02-2019 / 20:36:16 / Stefan Vogel" |
|
24272 | 150 |
"Modified: / 06-06-2019 / 23:25:42 / Claus Gittinger" |
1341 | 151 |
! |
152 |
||
20165 | 153 |
removeIdentityValue:aValue ifAbsent:aBlock |
154 |
"remove the association under aValue from the collection, |
|
155 |
return the key previously stored there. |
|
156 |
If it was not in the collection return the result |
|
157 |
from evaluating aBlock. |
|
158 |
||
159 |
Redefined to avoid synchronization problems, in case |
|
160 |
of interrupts (otherwise, there could be some other operation |
|
161 |
on the receiver done by another process, which garbles my contents)." |
|
162 |
||
163 |
(aValue isNil or:[ aValue class == SmallInteger ]) ifTrue:[ |
|
164 |
^ aBlock value. |
|
165 |
]. |
|
166 |
OperatingSystem blockInterrupts ifTrue:[ |
|
167 |
"/ already blocked |
|
23722 | 168 |
^ super removeIdentityValue:aValue ifAbsent:aBlock |
20165 | 169 |
]. |
23722 | 170 |
|
171 |
^ [super removeIdentityValue:aValue ifAbsent:aBlock] ensure:[ OperatingSystem unblockInterrupts. ]. |
|
20165 | 172 |
|
23722 | 173 |
"Created: / 06-05-1996 / 14:47:37 / stefan" |
174 |
"Modified: / 08-05-1996 / 14:54:09 / stefan" |
|
175 |
"Modified: / 12-02-2019 / 20:37:29 / Stefan Vogel" |
|
20165 | 176 |
! |
177 |
||
1341 | 178 |
removeKey:aKey ifAbsent:aBlock |
179 |
"remove the association under aKey from the collection, |
|
2910 | 180 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
181 |
If it was not in the collection return the result |
1341 | 182 |
from evaluating aBlock. |
183 |
||
184 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
185 |
of interrupts (otherwise, there could be some other operation |
1341 | 186 |
on the receiver done by another process, which garbles my contents)." |
187 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
188 |
|ret| |
1341 | 189 |
|
20164 | 190 |
OperatingSystem blockInterrupts ifTrue:[ |
191 |
"/ already blocked |
|
192 |
ret := super removeKey:aKey ifAbsent:aBlock |
|
193 |
] ifFalse:[ |
|
23722 | 194 |
ret := [ |
195 |
super removeKey:aKey ifAbsent:aBlock |
|
20164 | 196 |
] ensure:[ |
197 |
OperatingSystem unblockInterrupts. |
|
198 |
]. |
|
199 |
]. |
|
200 |
||
20165 | 201 |
(ret isNil or:[ret class == SmallInteger]) ifTrue:[ |
202 |
^ aBlock value. |
|
203 |
]. |
|
204 |
||
1341 | 205 |
^ ret |
206 |
||
23722 | 207 |
"Modified: / 06-05-1996 / 12:44:07 / stefan" |
208 |
"Created: / 06-05-1996 / 14:47:37 / stefan" |
|
209 |
"Modified: / 12-02-2019 / 20:38:07 / Stefan Vogel" |
|
1341 | 210 |
! |
211 |
||
20165 | 212 |
removeValue:aValue ifAbsent:aBlock |
1341 | 213 |
"remove the association under aValue from the collection, |
2910 | 214 |
return the key previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
215 |
If it was not in the collection return the result |
1341 | 216 |
from evaluating aBlock. |
217 |
||
218 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
219 |
of interrupts (otherwise, there could be some other operation |
1341 | 220 |
on the receiver done by another process, which garbles my contents)." |
221 |
||
20165 | 222 |
(aValue isNil or:[aValue class == SmallInteger]) ifTrue:[ |
223 |
^ aBlock value. |
|
224 |
]. |
|
225 |
||
20164 | 226 |
OperatingSystem blockInterrupts ifTrue:[ |
227 |
"/ already blocked |
|
23722 | 228 |
^ super removeValue:aValue ifAbsent:aBlock |
20164 | 229 |
]. |
23722 | 230 |
^ [super removeValue:aValue ifAbsent:aBlock] ensure:[OperatingSystem unblockInterrupts]. |
1341 | 231 |
|
23722 | 232 |
"Created: / 06-05-1996 / 14:47:37 / stefan" |
233 |
"Modified: / 08-05-1996 / 14:54:09 / stefan" |
|
234 |
"Modified: / 12-02-2019 / 20:39:44 / Stefan Vogel" |
|
1341 | 235 |
! ! |
236 |
||
4349 | 237 |
!WeakValueDictionary methodsFor:'element disposal'! |
238 |
||
239 |
update:something with:aParameter from:changedObject |
|
240 |
"an element (either key or value) died - clear out slots for |
|
241 |
disposed keys." |
|
242 |
||
243 |
|wasBlocked| |
|
244 |
||
245 |
something == #ElementExpired ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
246 |
self clearDeadSlots. |
4349 | 247 |
] |
248 |
||
249 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
250 |
! ! |
|
251 |
||
20163 | 252 |
!WeakValueDictionary methodsFor:'enumerating'! |
253 |
||
254 |
do:aBlock |
|
255 |
super do:[:eachValue| |
|
20165 | 256 |
"garbage collected values will change to nil or a SmallInteger" |
257 |
(eachValue notNil and:[eachValue class ~~ SmallInteger]) ifTrue:[ |
|
20163 | 258 |
aBlock value:eachValue. |
259 |
]. |
|
260 |
]. |
|
261 |
! |
|
262 |
||
263 |
keysAndValuesDo:aBlock |
|
264 |
super keysAndValuesDo:[:eachKey :eachValue| |
|
265 |
"garbage collected values will change to a SmallInteger" |
|
266 |
eachValue class ~~ SmallInteger ifTrue:[ |
|
267 |
aBlock value:eachKey value:eachValue. |
|
268 |
]. |
|
269 |
]. |
|
270 |
! ! |
|
271 |
||
1341 | 272 |
!WeakValueDictionary methodsFor:'private'! |
273 |
||
6041 | 274 |
clearDeadSlots |
275 |
|wasBlocked| |
|
276 |
||
277 |
" |
|
278 |
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:
17265
diff
changeset
|
279 |
from the background finalizer. If new items are added by a |
6041 | 280 |
higher prio process, the dictionary might get corrupted otherwise |
281 |
" |
|
282 |
wasBlocked := OperatingSystem blockInterrupts. |
|
283 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
284 |
valueArray |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
285 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
286 |
tally := tally - 1. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
287 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
288 |
replacingCorpsesWith:nil. |
6041 | 289 |
|
290 |
wasBlocked ifFalse:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
291 |
OperatingSystem unblockInterrupts. |
6041 | 292 |
]. |
6330 | 293 |
|
294 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 295 |
! |
296 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
297 |
possiblyShrink |
6041 | 298 |
"check if the receiver has become too empty (after a remove) |
299 |
and shrink if it makes sense. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
300 |
Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'" |
6041 | 301 |
|
20163 | 302 |
keyArray basicSize > 56 ifTrue:[ |
303 |
self clearDeadSlots. |
|
304 |
super possiblyShrink. |
|
305 |
]. |
|
6041 | 306 |
! |
307 |
||
1341 | 308 |
valueContainerOfSize:n |
309 |
"return a container for values of size n. |
|
310 |
use WeakArrays here." |
|
311 |
||
4349 | 312 |
|a| |
313 |
||
314 |
a := WeakArray new:n. |
|
315 |
a addDependent:self. |
|
316 |
^ a |
|
1341 | 317 |
|
318 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
319 |
! ! |
|
320 |
||
23692 | 321 |
!WeakValueDictionary methodsFor:'queries'! |
2739 | 322 |
|
323 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
324 |
"redefined to block interrupts |
2739 | 325 |
(avoid change of the dictionary while accessing)" |
326 |
||
20164 | 327 |
|ret wasBlocked| |
2739 | 328 |
|
20165 | 329 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
20164 | 330 |
"Integers cannot be stored into a WeakValueDictionary" |
331 |
^ false. |
|
332 |
]. |
|
333 |
||
334 |
wasBlocked := OperatingSystem blockInterrupts. |
|
335 |
ret := super includes:anObject. |
|
336 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
337 |
^ ret |
|
2739 | 338 |
|
339 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
340 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
341 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
342 |
! |
|
343 |
||
20165 | 344 |
includesIdentical:anObject |
345 |
"redefined to block interrupts |
|
346 |
(avoid change of the dictionary while accessing)" |
|
347 |
||
348 |
|ret wasBlocked| |
|
349 |
||
350 |
(anObject isNil or:[anObject class == SmallInteger]) ifTrue:[ |
|
351 |
"Integers cannot be stored into a WeakValueDictionary" |
|
352 |
^ false. |
|
353 |
]. |
|
354 |
||
355 |
wasBlocked := OperatingSystem blockInterrupts. |
|
356 |
ret := super includesIdentical:anObject. |
|
357 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
358 |
^ ret |
|
359 |
! |
|
360 |
||
2739 | 361 |
includesKey:key |
20163 | 362 |
"redefined to check for already collected values" |
2739 | 363 |
|
20164 | 364 |
^ (self at:key ifAbsent:DeletedEntry) ~~ DeletedEntry. |
2739 | 365 |
|
366 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
367 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
368 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
23692 | 369 |
! ! |
370 |
||
371 |
!WeakValueDictionary methodsFor:'testing'! |
|
9245 | 372 |
|
373 |
isWeakCollection |
|
374 |
"return true, if the receiver has weak references to its elements." |
|
375 |
||
376 |
^ true |
|
2739 | 377 |
! ! |
378 |
||
1341 | 379 |
!WeakValueDictionary class methodsFor:'documentation'! |
380 |
||
381 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
382 |
^ '$Header$' |
1341 | 383 |
! ! |
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
384 |