author | Merge Script |
Mon, 08 Jun 2015 06:58:54 +0200 | |
branch | jv |
changeset 18461 | bc3d3101c493 |
parent 18120 | e3a375d5f6a8 |
child 18630 | a74d669db937 |
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 |
" |
|
41 |
WeakValueDictionaries behave like Dictionaries, |
|
42 |
as long as the values are still referenced by some |
|
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:] |
7998 | 55 |
Stefan Vogel |
1341 | 56 |
|
57 |
[See also:] |
|
7998 | 58 |
WeakArray WeakIdentityDictionary WeakIdentitySet |
1341 | 59 |
" |
60 |
! ! |
|
61 |
||
62 |
!WeakValueDictionary methodsFor:'adding & removing'! |
|
63 |
||
6330 | 64 |
at:key ifAbsent:somethingRespondingToValue |
6347 | 65 |
" |
66 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
67 |
are added within interrupting high prio processes. |
|
68 |
" |
|
69 |
||
6330 | 70 |
|ret| |
71 |
||
72 |
[ |
|
73 |
ret := super at:key ifAbsent:[^ somethingRespondingToValue value]. |
|
74 |
ret = 0 ifTrue:[ |
|
75 |
ret := somethingRespondingToValue value |
|
76 |
]. |
|
77 |
] valueUninterruptably. |
|
78 |
^ ret |
|
79 |
! |
|
80 |
||
6233 | 81 |
at:key ifAbsentPut:anObject |
82 |
"return the element indexed by aKey if present, |
|
83 |
if not present, store the result of evaluating valueBlock |
|
84 |
under aKey and return it. |
|
6347 | 85 |
|
6233 | 86 |
Redefined to block interrupts, to avoid trouble when dependencies |
87 |
are added within interrupting high prio processes. |
|
88 |
WARNING: do not add elements while iterating over the receiver. |
|
89 |
Iterate over a copy to do this." |
|
90 |
||
91 |
|ret| |
|
92 |
||
93 |
[ |
|
94 |
ret := super at:key ifAbsentPut:anObject. |
|
6347 | 95 |
] valueUninterruptably. |
6233 | 96 |
^ ret |
97 |
! |
|
98 |
||
1341 | 99 |
at:key put:anObject |
100 |
"add the argument anObject under key, aKey to the receiver. |
|
101 |
Return anObject (sigh). |
|
102 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
103 |
are added within interrupting high prio processes." |
|
104 |
||
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
|
105 |
|ret| |
1341 | 106 |
|
1790 | 107 |
[ |
6347 | 108 |
ret := super at:key put:anObject. |
109 |
] 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
|
110 |
^ ret |
1341 | 111 |
|
112 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
113 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1790 | 114 |
"Modified: 20.10.1996 / 14:05:04 / cg" |
1341 | 115 |
! |
116 |
||
117 |
removeKey:aKey ifAbsent:aBlock |
|
118 |
"remove the association under aKey from the collection, |
|
2910 | 119 |
return the value 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 |
|
129 |
[ |
|
6347 | 130 |
ret := super removeKey:aKey ifAbsent:aBlock |
131 |
] valueUninterruptably. |
|
1341 | 132 |
^ ret |
133 |
||
134 |
"Modified: 6.5.1996 / 12:44:07 / stefan" |
|
135 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
136 |
! |
|
137 |
||
138 |
removeValue:aKey ifAbsent:aBlock |
|
139 |
"remove the association under aValue from the collection, |
|
2910 | 140 |
return the key previously stored there. |
1341 | 141 |
If it was not in the collection return the result |
142 |
from evaluating aBlock. |
|
143 |
||
144 |
Redefined to avoid synchronization problems, in case |
|
145 |
of interrupts (otherwise, there could be some other operation |
|
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 |
[ |
|
6347 | 151 |
ret := super removeValue:aKey ifAbsent:aBlock |
152 |
] valueUninterruptably. |
|
1341 | 153 |
^ ret. |
154 |
||
155 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
1351 | 156 |
"Modified: 8.5.1996 / 14:54:09 / stefan" |
1341 | 157 |
! ! |
158 |
||
4349 | 159 |
!WeakValueDictionary methodsFor:'element disposal'! |
160 |
||
161 |
update:something with:aParameter from:changedObject |
|
162 |
"an element (either key or value) died - clear out slots for |
|
163 |
disposed keys." |
|
164 |
||
165 |
|wasBlocked| |
|
166 |
||
167 |
something == #ElementExpired ifTrue:[ |
|
6041 | 168 |
self clearDeadSlots. |
4349 | 169 |
] |
170 |
||
171 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
|
172 |
! ! |
|
173 |
||
1341 | 174 |
!WeakValueDictionary methodsFor:'private'! |
175 |
||
6041 | 176 |
clearDeadSlots |
177 |
|wasBlocked| |
|
178 |
||
179 |
" |
|
180 |
have to block here - dispose may be done at a low priority |
|
181 |
from the background finalizer. If new items are added by a |
|
182 |
higher prio process, the dictionary might get corrupted otherwise |
|
183 |
" |
|
184 |
wasBlocked := OperatingSystem blockInterrupts. |
|
185 |
||
186 |
valueArray |
|
6330 | 187 |
forAllDeadIndicesDo:[:idx | keyArray at:idx put:DeletedEntry. |
6041 | 188 |
tally := tally - 1. |
189 |
] |
|
190 |
replacingCorpsesWith:nil. |
|
191 |
||
192 |
wasBlocked ifFalse:[ |
|
193 |
OperatingSystem unblockInterrupts. |
|
194 |
]. |
|
6330 | 195 |
|
196 |
"Modified: / 13.12.2001 / 14:18:56 / martin" |
|
6041 | 197 |
! |
198 |
||
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
199 |
possiblyShrink |
6041 | 200 |
"check if the receiver has become too empty (after a remove) |
201 |
and shrink if it makes sense. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
202 |
Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'" |
6041 | 203 |
|
204 |
self clearDeadSlots. |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
205 |
super possiblyShrink |
6041 | 206 |
! |
207 |
||
6233 | 208 |
slotIsEmpty:probe |
209 |
^ probe isNil or:[probe == 0 "collected"] |
|
210 |
! |
|
211 |
||
1341 | 212 |
valueContainerOfSize:n |
213 |
"return a container for values of size n. |
|
214 |
use WeakArrays here." |
|
215 |
||
4349 | 216 |
|a| |
217 |
||
218 |
a := WeakArray new:n. |
|
219 |
a addDependent:self. |
|
220 |
^ a |
|
1341 | 221 |
|
222 |
"Created: 6.5.1996 / 14:47:37 / stefan" |
|
223 |
! ! |
|
224 |
||
2739 | 225 |
!WeakValueDictionary methodsFor:'testing'! |
226 |
||
227 |
includes:anObject |
|
228 |
"redefined to block interrupts |
|
229 |
(avoid change of the dictionary while accessing)" |
|
230 |
||
231 |
|val| |
|
232 |
||
233 |
[ |
|
6347 | 234 |
val := super includes:anObject. |
235 |
] valueUninterruptably. |
|
2739 | 236 |
^ val |
237 |
||
238 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
239 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
240 |
"Created: 1.7.1997 / 15:41:14 / cg" |
|
241 |
! |
|
242 |
||
243 |
includesKey:key |
|
244 |
"redefined to block interrupts |
|
245 |
(avoid change of the dictionary while accessing)" |
|
246 |
||
247 |
|val| |
|
248 |
||
249 |
[ |
|
6347 | 250 |
val := super includesKey:key. |
251 |
] valueUninterruptably. |
|
2739 | 252 |
^ val |
253 |
||
254 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
255 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
256 |
"Created: 1.7.1997 / 15:41:32 / cg" |
|
9245 | 257 |
! |
258 |
||
259 |
isWeakCollection |
|
260 |
"return true, if the receiver has weak references to its elements." |
|
261 |
||
262 |
^ true |
|
2739 | 263 |
! ! |
264 |
||
1341 | 265 |
!WeakValueDictionary class methodsFor:'documentation'! |
266 |
||
267 |
version |
|
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
268 |
^ '$Header: /cvs/stx/stx/libbasic/WeakValueDictionary.st,v 1.20 2014-12-30 12:35:34 cg Exp $' |
1341 | 269 |
! ! |
17265
556ffc7d34b4
renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents:
14311
diff
changeset
|
270 |