author | Claus Gittinger <cg@exept.de> |
Sat, 17 Nov 2001 11:10:08 +0100 | |
changeset 6211 | 69f320c220d5 |
parent 6041 | 553103122a36 |
child 6233 | 0a79cbd07543 |
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 |
|
53 |
||
1341 | 54 |
[author:] |
2910 | 55 |
Stefan Vogel |
1341 | 56 |
|
57 |
[See also:] |
|
2910 | 58 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 59 |
" |
60 |
! ! |
|
61 |
||
62 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
63 |
||
64 |
at:key put:anObject |
|
65 |
"add the argument anObject under key, aKey to the receiver. |
|
66 |
Return anObject (sigh). |
|
67 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
68 |
are added within interrupting high prio processes." |
|
69 |
||
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
|
70 |
|ret| |
1341 | 71 |
|
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
|
72 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 73 |
"/ already blocked |
74 |
^ super at:key put:anObject |
|
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
|
75 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
76 |
|
1790 | 77 |
[ |
2910 | 78 |
ret := super at:key put:anObject. |
1790 | 79 |
] valueNowOrOnUnwindDo:[ |
2910 | 80 |
OperatingSystem unblockInterrupts |
1790 | 81 |
]. |
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
|
82 |
^ ret |
1341 | 83 |
|
84 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
85 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 86 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 87 |
! |
88 |
||
89 |
removeKey:aKey ifAbsent:aBlock |
|
90 |
"remove the association under aKey from the collection, |
|
2910 | 91 |
return the value previously stored there. |
1341 | 92 |
If it was not in the collection return the result |
93 |
from evaluating aBlock. |
|
94 |
||
95 |
Redefined to avoid synchronization problems, in case |
|
96 |
of interrupts (otherwise, there could be some other operation |
|
97 |
on the receiver done by another process, which garbles my contents)." |
|
98 |
||
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
|
99 |
|ret| |
1341 | 100 |
|
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
|
101 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 102 |
"/ already blocked |
103 |
^ super removeKey:aKey ifAbsent:aBlock |
|
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 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
105 |
|
1341 | 106 |
[ |
2910 | 107 |
ret := super removeKey:aKey ifAbsent:aBlock |
1341 | 108 |
] valueNowOrOnUnwindDo:[ |
2910 | 109 |
OperatingSystem unblockInterrupts |
1341 | 110 |
]. |
111 |
^ ret |
|
112 |
||
113 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
114 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
115 |
! |
|
116 |
||
117 |
removeValue:aKey ifAbsent:aBlock |
|
118 |
"remove the association under aValue from the collection, |
|
2910 | 119 |
return the key previously stored there. |
1341 | 120 |
If it was not in the collection return the result |
121 |
from evaluating aBlock. |
|
122 |
||
123 |
Redefined to avoid synchronization problems, in case |
|
124 |
of interrupts (otherwise, there could be some other operation |
|
125 |
on the receiver done by another process, which garbles my contents)." |
|
126 |
||
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
|
127 |
|ret| |
1341 | 128 |
|
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
|
129 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 130 |
"/ already blocked |
131 |
^ super removeValue:aKey ifAbsent:aBlock |
|
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
|
132 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
133 |
|
1341 | 134 |
[ |
2910 | 135 |
ret := super removeValue:aKey ifAbsent:aBlock |
1341 | 136 |
] valueNowOrOnUnwindDo:[ |
2910 | 137 |
OperatingSystem unblockInterrupts |
1341 | 138 |
]. |
139 |
^ ret. |
|
140 |
||
141 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 142 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 143 |
! ! |
144 |
||
4349 | 145 |
!WeakValueDictionary methodsFor:'element disposal'! |
146 |
||
147 |
update:something with:aParameter from:changedObject |
|
148 |
"an element (either key or value) died - clear out slots for |
|
149 |
disposed keys." |
|
150 |
||
151 |
|wasBlocked| |
|
152 |
||
153 |
something == #ElementExpired ifTrue:[ |
|
6041 | 154 |
self clearDeadSlots. |
4349 | 155 |
] |
156 |
||
157 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
158 |
! ! |
|
159 |
||
1341 | 160 |
!WeakValueDictionary methodsFor:'private'! |
161 |
||
6041 | 162 |
clearDeadSlots |
163 |
|wasBlocked| |
|
164 |
||
165 |
" |
|
166 |
have to block here - dispose may be done at a low priority |
|
167 |
from the background finalizer. If new items are added by a |
|
168 |
higher prio process, the dictionary might get corrupted otherwise |
|
169 |
" |
|
170 |
wasBlocked := OperatingSystem blockInterrupts. |
|
171 |
||
172 |
valueArray |
|
173 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:nil. |
|
174 |
tally := tally - 1. |
|
175 |
] |
|
176 |
replacingCorpsesWith:nil. |
|
177 |
||
178 |
wasBlocked ifFalse:[ |
|
179 |
OperatingSystem unblockInterrupts. |
|
180 |
]. |
|
181 |
! |
|
182 |
||
183 |
emptyCheck |
|
184 |
"check if the receiver has become too empty (after a remove) |
|
185 |
and shrink if it makes sense. |
|
186 |
Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'" |
|
187 |
||
188 |
self clearDeadSlots. |
|
189 |
^super emptyCheck |
|
190 |
! |
|
191 |
||
1341 | 192 |
valueContainerOfSize:n |
193 |
"return a container for values of size n. |
|
194 |
use WeakArrays here." |
|
195 |
||
4349 | 196 |
|a| |
197 |
||
198 |
a := WeakArray new:n. |
|
199 |
a addDependent:self. |
|
200 |
^ a |
|
1341 | 201 |
|
202 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
203 |
! ! |
|
204 |
||
2739 | 205 |
!WeakValueDictionary methodsFor:'testing'! |
206 |
||
207 |
includes:anObject |
|
208 |
"redefined to block interrupts |
|
209 |
(avoid change of the dictionary while accessing)" |
|
210 |
||
211 |
|val| |
|
212 |
||
213 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
2910 | 214 |
"/ already blocked |
215 |
^ super includes:anObject. |
|
2739 | 216 |
]. |
217 |
||
218 |
[ |
|
2910 | 219 |
val := super includes:anObject. |
2739 | 220 |
] valueNowOrOnUnwindDo:[ |
2910 | 221 |
OperatingSystem unblockInterrupts. |
2739 | 222 |
]. |
223 |
^ val |
|
224 |
||
225 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
226 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
227 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
228 |
! |
|
229 |
||
230 |
includesKey:key |
|
231 |
"redefined to block interrupts |
|
232 |
(avoid change of the dictionary while accessing)" |
|
233 |
||
234 |
|val| |
|
235 |
||
236 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
2910 | 237 |
"/ already blocked |
238 |
^ super includesKey:key. |
|
2739 | 239 |
]. |
240 |
||
241 |
[ |
|
2910 | 242 |
val := super includesKey:key. |
2739 | 243 |
] valueNowOrOnUnwindDo:[ |
2910 | 244 |
OperatingSystem unblockInterrupts. |
2739 | 245 |
]. |
246 |
^ val |
|
247 |
||
248 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
249 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
250 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
251 |
! ! |
|
252 |
||
1341 | 253 |
!WeakValueDictionary class methodsFor:'documentation'! |
254 |
||
255 |
version |
|
6041 | 256 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueDictionary.st,v 1.12 2001-09-24 14:38:26 penk Exp $' |
1341 | 257 |
! ! |