author | Stefan Vogel <sv@exept.de> |
Wed, 04 Aug 1999 16:13:32 +0200 | |
changeset 4527 | 2ddbe34bab87 |
parent 4350 | 5aa2a3b9fd5b |
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 |
||
13 |
Dictionary subclass:#WeakValueDictionary |
|
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
4350 | 17 |
category:'Collections-Weak' |
1341 | 18 |
! |
19 |
||
20 |
!WeakValueDictionary class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
WeakValueDictionaries behave like Dictionaries, |
|
39 |
as long as the values are still referenced by some |
|
40 |
other (non-weak) object. |
|
41 |
However, once the last non-weak reference ceases to exist, |
|
42 |
the Dictionary will return nil for the value at position key. |
|
43 |
(with some delay: it will be removed after the next garbage collect). |
|
44 |
||
2307 | 45 |
[Warning:] |
46 |
If you use this, be very careful since the collections size changes |
|
47 |
'magically' - for example, testing for being nonEmpty and then |
|
48 |
removing the first element may fail, since the element may vanish inbetween. |
|
49 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 50 |
|
51 |
||
1341 | 52 |
[author:] |
2910 | 53 |
Stefan Vogel |
1341 | 54 |
|
55 |
[See also:] |
|
2910 | 56 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 57 |
" |
58 |
! ! |
|
59 |
||
60 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
61 |
||
62 |
at:key put:anObject |
|
63 |
"add the argument anObject under key, aKey to the receiver. |
|
64 |
Return anObject (sigh). |
|
65 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
66 |
are added within interrupting high prio processes." |
|
67 |
||
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
|
68 |
|ret| |
1341 | 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 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 71 |
"/ already blocked |
72 |
^ 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
|
73 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
74 |
|
1790 | 75 |
[ |
2910 | 76 |
ret := super at:key put:anObject. |
1790 | 77 |
] valueNowOrOnUnwindDo:[ |
2910 | 78 |
OperatingSystem unblockInterrupts |
1790 | 79 |
]. |
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
|
80 |
^ ret |
1341 | 81 |
|
82 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
83 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 84 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 85 |
! |
86 |
||
87 |
removeKey:aKey ifAbsent:aBlock |
|
88 |
"remove the association under aKey from the collection, |
|
2910 | 89 |
return the value previously stored there. |
1341 | 90 |
If it was not in the collection return the result |
91 |
from evaluating aBlock. |
|
92 |
||
93 |
Redefined to avoid synchronization problems, in case |
|
94 |
of interrupts (otherwise, there could be some other operation |
|
95 |
on the receiver done by another process, which garbles my contents)." |
|
96 |
||
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
|
97 |
|ret| |
1341 | 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 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 100 |
"/ already blocked |
101 |
^ 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
|
102 |
]. |
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 |
|
1341 | 104 |
[ |
2910 | 105 |
ret := super removeKey:aKey ifAbsent:aBlock |
1341 | 106 |
] valueNowOrOnUnwindDo:[ |
2910 | 107 |
OperatingSystem unblockInterrupts |
1341 | 108 |
]. |
109 |
^ ret |
|
110 |
||
111 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
112 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
113 |
! |
|
114 |
||
115 |
removeValue:aKey ifAbsent:aBlock |
|
116 |
"remove the association under aValue from the collection, |
|
2910 | 117 |
return the key 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 |
|
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 |
(OperatingSystem blockInterrupts) ifTrue:[ |
2910 | 128 |
"/ already blocked |
129 |
^ 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
|
130 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
131 |
|
1341 | 132 |
[ |
2910 | 133 |
ret := super removeValue:aKey ifAbsent:aBlock |
1341 | 134 |
] valueNowOrOnUnwindDo:[ |
2910 | 135 |
OperatingSystem unblockInterrupts |
1341 | 136 |
]. |
137 |
^ ret. |
|
138 |
||
139 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 140 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 141 |
! ! |
142 |
||
4349 | 143 |
!WeakValueDictionary methodsFor:'element disposal'! |
144 |
||
145 |
update:something with:aParameter from:changedObject |
|
146 |
"an element (either key or value) died - clear out slots for |
|
147 |
disposed keys." |
|
148 |
||
149 |
|wasBlocked| |
|
150 |
||
151 |
something == #ElementExpired ifTrue:[ |
|
152 |
" |
|
153 |
have to block here - dispose may be done at a low priority |
|
154 |
from the background finalizer. If new items are added by a |
|
155 |
higher prio process, the dictionary might get corrupted otherwise |
|
156 |
" |
|
157 |
wasBlocked := OperatingSystem blockInterrupts. |
|
158 |
||
159 |
valueArray |
|
160 |
forAllDeadIndicesDo:[:idx | tally := tally - 1] |
|
161 |
replacingCorpsesWith:nil. |
|
162 |
||
163 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
164 |
] |
|
165 |
||
166 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
167 |
||
168 |
||
169 |
! ! |
|
170 |
||
1341 | 171 |
!WeakValueDictionary methodsFor:'private'! |
172 |
||
173 |
valueContainerOfSize:n |
|
174 |
"return a container for values of size n. |
|
175 |
use WeakArrays here." |
|
176 |
||
4349 | 177 |
|a| |
178 |
||
179 |
a := WeakArray new:n. |
|
180 |
a addDependent:self. |
|
181 |
^ a |
|
1341 | 182 |
|
183 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
184 |
! ! |
|
185 |
||
2739 | 186 |
!WeakValueDictionary methodsFor:'testing'! |
187 |
||
188 |
includes:anObject |
|
189 |
"redefined to block interrupts |
|
190 |
(avoid change of the dictionary while accessing)" |
|
191 |
||
192 |
|val| |
|
193 |
||
194 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
2910 | 195 |
"/ already blocked |
196 |
^ super includes:anObject. |
|
2739 | 197 |
]. |
198 |
||
199 |
[ |
|
2910 | 200 |
val := super includes:anObject. |
2739 | 201 |
] valueNowOrOnUnwindDo:[ |
2910 | 202 |
OperatingSystem unblockInterrupts. |
2739 | 203 |
]. |
204 |
^ val |
|
205 |
||
206 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
207 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
208 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
209 |
! |
|
210 |
||
211 |
includesKey:key |
|
212 |
"redefined to block interrupts |
|
213 |
(avoid change of the dictionary while accessing)" |
|
214 |
||
215 |
|val| |
|
216 |
||
217 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
2910 | 218 |
"/ already blocked |
219 |
^ super includesKey:key. |
|
2739 | 220 |
]. |
221 |
||
222 |
[ |
|
2910 | 223 |
val := super includesKey:key. |
2739 | 224 |
] valueNowOrOnUnwindDo:[ |
2910 | 225 |
OperatingSystem unblockInterrupts. |
2739 | 226 |
]. |
227 |
^ val |
|
228 |
||
229 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
230 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
231 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
232 |
! ! |
|
233 |
||
1341 | 234 |
!WeakValueDictionary class methodsFor:'documentation'! |
235 |
||
236 |
version |
|
4350 | 237 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/WeakVDict.st,v 1.11 1999-07-07 18:41:37 cg Exp $' |
1341 | 238 |
! ! |