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