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