author | Stefan Vogel <sv@exept.de> |
Tue, 05 Apr 2016 20:45:42 +0200 | |
changeset 19543 | 2ab62a8fbe29 |
parent 19085 | db72ed8f0a19 |
child 19559 | d35a89d5c0ec |
child 19779 | 3199a4b00532 |
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 |
19085 | 65 |
"Redefined to block interrupts, to avoid trouble when dependencies |
66 |
are added within interrupting high prio processes." |
|
6347 | 67 |
|
6330 | 68 |
|ret| |
69 |
||
70 |
[ |
|
19085 | 71 |
ret := super at:key ifAbsent:somethingRespondingToValue. |
6330 | 72 |
] valueUninterruptably. |
19085 | 73 |
|
74 |
ret class == SmallInteger ifTrue:[ |
|
75 |
ret := somethingRespondingToValue value |
|
76 |
]. |
|
6330 | 77 |
^ ret |
78 |
! |
|
79 |
||
19543 | 80 |
at:key ifAbsentPut:replacementBlock |
6233 | 81 |
"return the element indexed by aKey if present, |
82 |
if not present, store the result of evaluating valueBlock |
|
83 |
under aKey and return it. |
|
6347 | 84 |
|
6233 | 85 |
Redefined to block interrupts, to avoid trouble when dependencies |
86 |
are added within interrupting high prio processes. |
|
87 |
WARNING: do not add elements while iterating over the receiver. |
|
19543 | 88 |
Iterate over a copy to do this." |
6233 | 89 |
|
19543 | 90 |
|val| |
6233 | 91 |
|
19543 | 92 |
OperatingSystem blockInterrupts ifTrue:[ |
93 |
"/ already blocked |
|
94 |
val := super at:key ifAbsentPut:replacementBlock. |
|
95 |
] ifFalse:[ |
|
96 |
[ |
|
97 |
val := super at:key ifAbsentPut:replacementBlock. |
|
98 |
] ensure:[ |
|
99 |
OperatingSystem unblockInterrupts. |
|
100 |
]. |
|
101 |
]. |
|
102 |
||
103 |
val class == SmallInteger ifTrue:[ |
|
104 |
self error:'WeakValueDictionary: invalid value'. |
|
105 |
]. |
|
106 |
||
107 |
^ val |
|
6233 | 108 |
! |
109 |
||
1341 | 110 |
at:key put:anObject |
111 |
"add the argument anObject under key, aKey to the receiver. |
|
112 |
Return anObject (sigh). |
|
113 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
114 |
are added within interrupting high prio processes." |
|
115 |
||
19543 | 116 |
|val| |
117 |
||
118 |
anObject class == SmallInteger ifTrue:[ |
|
119 |
self error:'WeakValueDictionary: invalid value'. |
|
120 |
]. |
|
121 |
||
122 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
123 |
"/ already blocked |
|
124 |
^ super at:key put:anObject. |
|
125 |
]. |
|
1341 | 126 |
|
1790 | 127 |
[ |
19543 | 128 |
val := super at:key put:anObject. |
129 |
] ensure:[ |
|
130 |
OperatingSystem unblockInterrupts. |
|
131 |
]. |
|
132 |
^ val |
|
1341 | 133 |
|
134 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
19543 | 135 |
"Modified: 29.1.1997 / 15:08:45 / cg" |
1341 | 136 |
! |
137 |
||
138 |
removeKey:aKey ifAbsent:aBlock |
|
139 |
"remove the association under aKey from the collection, |
|
2910 | 140 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
141 |
If it was not in the collection return the result |
1341 | 142 |
from evaluating aBlock. |
143 |
||
144 |
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
|
145 |
of interrupts (otherwise, there could be some other operation |
1341 | 146 |
on the receiver done by another process, which garbles my contents)." |
147 |
||
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
|
148 |
|ret| |
1341 | 149 |
|
150 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
151 |
ret := super removeKey:aKey ifAbsent:aBlock |
6347 | 152 |
] valueUninterruptably. |
1341 | 153 |
^ ret |
154 |
||
155 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
156 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
157 |
! |
|
158 |
||
159 |
removeValue:aKey ifAbsent:aBlock |
|
160 |
"remove the association under aValue from the collection, |
|
2910 | 161 |
return the key previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
162 |
If it was not in the collection return the result |
1341 | 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:
17265
diff
changeset
|
166 |
of interrupts (otherwise, there could be some other operation |
1341 | 167 |
on the receiver done by another process, which garbles my contents)." |
168 |
||
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
|
169 |
|ret| |
1341 | 170 |
|
171 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
172 |
ret := super removeValue:aKey ifAbsent:aBlock |
6347 | 173 |
] valueUninterruptably. |
1341 | 174 |
^ ret. |
175 |
||
176 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 177 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 178 |
! ! |
179 |
||
4349 | 180 |
!WeakValueDictionary methodsFor:'element disposal'! |
181 |
||
182 |
update:something with:aParameter from:changedObject |
|
183 |
"an element (either key or value) died - clear out slots for |
|
184 |
disposed keys." |
|
185 |
||
186 |
|wasBlocked| |
|
187 |
||
188 |
something == #ElementExpired ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
189 |
self clearDeadSlots. |
4349 | 190 |
] |
191 |
||
192 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
193 |
! ! |
|
194 |
||
1341 | 195 |
!WeakValueDictionary methodsFor:'private'! |
196 |
||
6041 | 197 |
clearDeadSlots |
198 |
|wasBlocked| |
|
199 |
||
200 |
" |
|
201 |
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
|
202 |
from the background finalizer. If new items are added by a |
6041 | 203 |
higher prio process, the dictionary might get corrupted otherwise |
204 |
" |
|
205 |
wasBlocked := OperatingSystem blockInterrupts. |
|
206 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
207 |
valueArray |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
208 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
209 |
tally := tally - 1. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
210 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
211 |
replacingCorpsesWith:nil. |
6041 | 212 |
|
213 |
wasBlocked ifFalse:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
214 |
OperatingSystem unblockInterrupts. |
6041 | 215 |
]. |
6330 | 216 |
|
217 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 218 |
! |
219 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
220 |
possiblyShrink |
6041 | 221 |
"check if the receiver has become too empty (after a remove) |
222 |
and shrink if it makes sense. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
223 |
Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'" |
6041 | 224 |
|
225 |
self clearDeadSlots. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
226 |
super possiblyShrink |
6041 | 227 |
! |
228 |
||
1341 | 229 |
valueContainerOfSize:n |
230 |
"return a container for values of size n. |
|
231 |
use WeakArrays here." |
|
232 |
||
4349 | 233 |
|a| |
234 |
||
235 |
a := WeakArray new:n. |
|
236 |
a addDependent:self. |
|
237 |
^ a |
|
1341 | 238 |
|
239 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
240 |
! ! |
|
241 |
||
2739 | 242 |
!WeakValueDictionary methodsFor:'testing'! |
243 |
||
244 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
245 |
"redefined to block interrupts |
2739 | 246 |
(avoid change of the dictionary while accessing)" |
247 |
||
248 |
|val| |
|
249 |
||
250 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
251 |
val := super includes:anObject. |
6347 | 252 |
] valueUninterruptably. |
2739 | 253 |
^ val |
254 |
||
255 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
256 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
257 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
258 |
! |
|
259 |
||
260 |
includesKey:key |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
261 |
"redefined to block interrupts |
2739 | 262 |
(avoid change of the dictionary while accessing)" |
263 |
||
264 |
|val| |
|
265 |
||
266 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
267 |
val := super includesKey:key. |
6347 | 268 |
] valueUninterruptably. |
2739 | 269 |
^ val |
270 |
||
271 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
272 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
273 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
9245 | 274 |
! |
275 |
||
276 |
isWeakCollection |
|
277 |
"return true, if the receiver has weak references to its elements." |
|
278 |
||
279 |
^ true |
|
2739 | 280 |
! ! |
281 |
||
1341 | 282 |
!WeakValueDictionary class methodsFor:'documentation'! |
283 |
||
284 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
17265
diff
changeset
|
285 |
^ '$Header$' |
1341 | 286 |
! ! |
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
287 |