author | Stefan Vogel <sv@exept.de> |
Tue, 10 May 2016 16:26:19 +0200 | |
changeset 19779 | 3199a4b00532 |
parent 19543 | 2ab62a8fbe29 |
child 19811 | 65fec19facb0 |
child 20163 | 3855e97fe259 |
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 |
|
84 |
ret class == SmallInteger ifTrue:[ |
|
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 |
||
113 |
val class == SmallInteger ifTrue:[ |
|
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 |
||
128 |
anObject class == SmallInteger ifTrue:[ |
|
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 |
||
148 |
removeKey:aKey ifAbsent:aBlock |
|
149 |
"remove the association under aKey from the collection, |
|
2910 | 150 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
151 |
If it was not in the collection return the result |
1341 | 152 |
from evaluating aBlock. |
153 |
||
154 |
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
|
155 |
of interrupts (otherwise, there could be some other operation |
1341 | 156 |
on the receiver done by another process, which garbles my contents)." |
157 |
||
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
|
158 |
|ret| |
1341 | 159 |
|
160 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
161 |
ret := super removeKey:aKey ifAbsent:aBlock |
6347 | 162 |
] valueUninterruptably. |
1341 | 163 |
^ ret |
164 |
||
165 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
166 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
167 |
! |
|
168 |
||
169 |
removeValue:aKey ifAbsent:aBlock |
|
170 |
"remove the association under aValue from the collection, |
|
2910 | 171 |
return the key previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
172 |
If it was not in the collection return the result |
1341 | 173 |
from evaluating aBlock. |
174 |
||
175 |
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
|
176 |
of interrupts (otherwise, there could be some other operation |
1341 | 177 |
on the receiver done by another process, which garbles my contents)." |
178 |
||
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
|
179 |
|ret| |
1341 | 180 |
|
181 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
182 |
ret := super removeValue:aKey ifAbsent:aBlock |
6347 | 183 |
] valueUninterruptably. |
1341 | 184 |
^ ret. |
185 |
||
186 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 187 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 188 |
! ! |
189 |
||
4349 | 190 |
!WeakValueDictionary methodsFor:'element disposal'! |
191 |
||
192 |
update:something with:aParameter from:changedObject |
|
193 |
"an element (either key or value) died - clear out slots for |
|
194 |
disposed keys." |
|
195 |
||
196 |
|wasBlocked| |
|
197 |
||
198 |
something == #ElementExpired ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
199 |
self clearDeadSlots. |
4349 | 200 |
] |
201 |
||
202 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
203 |
! ! |
|
204 |
||
1341 | 205 |
!WeakValueDictionary methodsFor:'private'! |
206 |
||
6041 | 207 |
clearDeadSlots |
208 |
|wasBlocked| |
|
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:
17265
diff
changeset
|
212 |
from the background finalizer. If new items are added by a |
6041 | 213 |
higher prio process, the dictionary might get corrupted otherwise |
214 |
" |
|
215 |
wasBlocked := OperatingSystem blockInterrupts. |
|
216 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
217 |
valueArray |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
218 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
219 |
tally := tally - 1. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
220 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
221 |
replacingCorpsesWith:nil. |
6041 | 222 |
|
223 |
wasBlocked ifFalse:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
224 |
OperatingSystem unblockInterrupts. |
6041 | 225 |
]. |
6330 | 226 |
|
227 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 228 |
! |
229 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
230 |
possiblyShrink |
6041 | 231 |
"check if the receiver has become too empty (after a remove) |
232 |
and shrink if it makes sense. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
233 |
Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'" |
6041 | 234 |
|
235 |
self clearDeadSlots. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
236 |
super possiblyShrink |
6041 | 237 |
! |
238 |
||
1341 | 239 |
valueContainerOfSize:n |
240 |
"return a container for values of size n. |
|
241 |
use WeakArrays here." |
|
242 |
||
4349 | 243 |
|a| |
244 |
||
245 |
a := WeakArray new:n. |
|
246 |
a addDependent:self. |
|
247 |
^ a |
|
1341 | 248 |
|
249 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
250 |
! ! |
|
251 |
||
2739 | 252 |
!WeakValueDictionary methodsFor:'testing'! |
253 |
||
254 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
255 |
"redefined to block interrupts |
2739 | 256 |
(avoid change of the dictionary while accessing)" |
257 |
||
258 |
|val| |
|
259 |
||
260 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
261 |
val := super includes:anObject. |
6347 | 262 |
] valueUninterruptably. |
2739 | 263 |
^ val |
264 |
||
265 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
266 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
267 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
268 |
! |
|
269 |
||
270 |
includesKey:key |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
271 |
"redefined to block interrupts |
2739 | 272 |
(avoid change of the dictionary while accessing)" |
273 |
||
274 |
|val| |
|
275 |
||
276 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
277 |
val := super includesKey:key. |
6347 | 278 |
] valueUninterruptably. |
2739 | 279 |
^ val |
280 |
||
281 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
282 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
283 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
9245 | 284 |
! |
285 |
||
286 |
isWeakCollection |
|
287 |
"return true, if the receiver has weak references to its elements." |
|
288 |
||
289 |
^ true |
|
2739 | 290 |
! ! |
291 |
||
1341 | 292 |
!WeakValueDictionary class methodsFor:'documentation'! |
293 |
||
294 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
295 |
^ '$Header$' |
1341 | 296 |
! ! |
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
297 |