author | Claus Gittinger <cg@exept.de> |
Tue, 13 Oct 2009 18:50:39 +0200 | |
changeset 12263 | c6d50824fbba |
parent 9245 | a259701d3031 |
child 13744 | 1e450d863057 |
child 17711 | 39faaaf888b4 |
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 |
" |
|
12 |
||
6041 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1341 | 15 |
Dictionary subclass:#WeakValueDictionary |
16 |
instanceVariableNames:'' |
|
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
4350 | 19 |
category:'Collections-Weak' |
1341 | 20 |
! |
21 |
||
22 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
WeakValueDictionaries behave like Dictionaries, |
|
41 |
as long as the values are still referenced by some |
|
42 |
other (non-weak) object. |
|
43 |
However, once the last non-weak reference ceases to exist, |
|
44 |
the Dictionary will return nil for the value at position key. |
|
45 |
(with some delay: it will be removed after the next garbage collect). |
|
46 |
||
2307 | 47 |
[Warning:] |
48 |
If you use this, be very careful since the collections size changes |
|
49 |
'magically' - for example, testing for being nonEmpty and then |
|
50 |
removing the first element may fail, since the element may vanish inbetween. |
|
51 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 52 |
|
1341 | 53 |
[author:] |
7998 | 54 |
Stefan Vogel |
1341 | 55 |
|
56 |
[See also:] |
|
7998 | 57 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 58 |
" |
59 |
! ! |
|
60 |
||
61 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
62 |
||
6330 | 63 |
at:key ifAbsent:somethingRespondingToValue |
6347 | 64 |
" |
65 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
66 |
are added within interrupting high prio processes. |
|
67 |
" |
|
68 |
||
6330 | 69 |
|ret| |
70 |
||
71 |
[ |
|
72 |
ret := super at:key ifAbsent:[^ somethingRespondingToValue value]. |
|
73 |
ret = 0 ifTrue:[ |
|
74 |
ret := somethingRespondingToValue value |
|
75 |
]. |
|
76 |
] valueUninterruptably. |
|
77 |
^ ret |
|
78 |
! |
|
79 |
||
6233 | 80 |
at:key ifAbsentPut:anObject |
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. |
|
88 |
Iterate over a copy to do this." |
|
89 |
||
90 |
|ret| |
|
91 |
||
92 |
[ |
|
93 |
ret := super at:key ifAbsentPut:anObject. |
|
6347 | 94 |
] valueUninterruptably. |
6233 | 95 |
^ ret |
96 |
! |
|
97 |
||
1341 | 98 |
at:key put:anObject |
99 |
"add the argument anObject under key, aKey to the receiver. |
|
100 |
Return anObject (sigh). |
|
101 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
102 |
are added within interrupting high prio processes." |
|
103 |
||
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
|
104 |
|ret| |
1341 | 105 |
|
1790 | 106 |
[ |
6347 | 107 |
ret := super at:key put:anObject. |
108 |
] valueUninterruptably. |
|
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
|
109 |
^ ret |
1341 | 110 |
|
111 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
112 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 113 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 114 |
! |
115 |
||
116 |
removeKey:aKey ifAbsent:aBlock |
|
117 |
"remove the association under aKey from the collection, |
|
2910 | 118 |
return the value previously stored there. |
1341 | 119 |
If it was not in the collection return the result |
120 |
from evaluating aBlock. |
|
121 |
||
122 |
Redefined to avoid synchronization problems, in case |
|
123 |
of interrupts (otherwise, there could be some other operation |
|
124 |
on the receiver done by another process, which garbles my contents)." |
|
125 |
||
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
|
126 |
|ret| |
1341 | 127 |
|
128 |
[ |
|
6347 | 129 |
ret := super removeKey:aKey ifAbsent:aBlock |
130 |
] valueUninterruptably. |
|
1341 | 131 |
^ ret |
132 |
||
133 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
134 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
135 |
! |
|
136 |
||
137 |
removeValue:aKey ifAbsent:aBlock |
|
138 |
"remove the association under aValue from the collection, |
|
2910 | 139 |
return the key previously stored there. |
1341 | 140 |
If it was not in the collection return the result |
141 |
from evaluating aBlock. |
|
142 |
||
143 |
Redefined to avoid synchronization problems, in case |
|
144 |
of interrupts (otherwise, there could be some other operation |
|
145 |
on the receiver done by another process, which garbles my contents)." |
|
146 |
||
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
|
147 |
|ret| |
1341 | 148 |
|
149 |
[ |
|
6347 | 150 |
ret := super removeValue:aKey ifAbsent:aBlock |
151 |
] valueUninterruptably. |
|
1341 | 152 |
^ ret. |
153 |
||
154 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 155 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 156 |
! ! |
157 |
||
4349 | 158 |
!WeakValueDictionary methodsFor:'element disposal'! |
159 |
||
160 |
update:something with:aParameter from:changedObject |
|
161 |
"an element (either key or value) died - clear out slots for |
|
162 |
disposed keys." |
|
163 |
||
164 |
|wasBlocked| |
|
165 |
||
166 |
something == #ElementExpired ifTrue:[ |
|
6041 | 167 |
self clearDeadSlots. |
4349 | 168 |
] |
169 |
||
170 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
171 |
! ! |
|
172 |
||
1341 | 173 |
!WeakValueDictionary methodsFor:'private'! |
174 |
||
6041 | 175 |
clearDeadSlots |
176 |
|wasBlocked| |
|
177 |
||
178 |
" |
|
179 |
have to block here - dispose may be done at a low priority |
|
180 |
from the background finalizer. If new items are added by a |
|
181 |
higher prio process, the dictionary might get corrupted otherwise |
|
182 |
" |
|
183 |
wasBlocked := OperatingSystem blockInterrupts. |
|
184 |
||
185 |
valueArray |
|
6330 | 186 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
6041 | 187 |
tally := tally - 1. |
188 |
] |
|
189 |
replacingCorpsesWith:nil. |
|
190 |
||
191 |
wasBlocked ifFalse:[ |
|
192 |
OperatingSystem unblockInterrupts. |
|
193 |
]. |
|
6330 | 194 |
|
195 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 196 |
! |
197 |
||
198 |
emptyCheck |
|
199 |
"check if the receiver has become too empty (after a remove) |
|
200 |
and shrink if it makes sense. |
|
201 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
202 |
||
203 |
self clearDeadSlots. |
|
204 |
^super emptyCheck |
|
205 |
! |
|
206 |
||
6233 | 207 |
slotIsEmpty:probe |
208 |
^ probe isNil or:[probe == 0 "collected"] |
|
209 |
! |
|
210 |
||
1341 | 211 |
valueContainerOfSize:n |
212 |
"return a container for values of size n. |
|
213 |
use WeakArrays here." |
|
214 |
||
4349 | 215 |
|a| |
216 |
||
217 |
a := WeakArray new:n. |
|
218 |
a addDependent:self. |
|
219 |
^ a |
|
1341 | 220 |
|
221 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
222 |
! ! |
|
223 |
||
2739 | 224 |
!WeakValueDictionary methodsFor:'testing'! |
225 |
||
226 |
includes:anObject |
|
227 |
"redefined to block interrupts |
|
228 |
(avoid change of the dictionary while accessing)" |
|
229 |
||
230 |
|val| |
|
231 |
||
232 |
[ |
|
6347 | 233 |
val := super includes:anObject. |
234 |
] valueUninterruptably. |
|
2739 | 235 |
^ val |
236 |
||
237 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
238 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
239 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
240 |
! |
|
241 |
||
242 |
includesKey:key |
|
243 |
"redefined to block interrupts |
|
244 |
(avoid change of the dictionary while accessing)" |
|
245 |
||
246 |
|val| |
|
247 |
||
248 |
[ |
|
6347 | 249 |
val := super includesKey:key. |
250 |
] valueUninterruptably. |
|
2739 | 251 |
^ val |
252 |
||
253 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
254 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
255 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
9245 | 256 |
! |
257 |
||
258 |
isWeakCollection |
|
259 |
"return true, if the receiver has weak references to its elements." |
|
260 |
||
261 |
^ true |
|
2739 | 262 |
! ! |
263 |
||
1341 | 264 |
!WeakValueDictionary class methodsFor:'documentation'! |
265 |
||
266 |
version |
|
9245 | 267 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueDictionary.st,v 1.17 2006-03-06 10:04:47 cg Exp $' |
1341 | 268 |
! ! |