4347
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
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 |
"
|
19086
|
12 |
"{ Package: 'stx:libbasic' }"
|
4347
|
13 |
|
19086
|
14 |
"{ NameSpace: Smalltalk }"
|
6233
|
15 |
|
4347
|
16 |
IdentityDictionary subclass:#WeakValueIdentityDictionary
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
4350
|
20 |
category:'Collections-Weak'
|
4347
|
21 |
!
|
|
22 |
|
|
23 |
!WeakValueIdentityDictionary class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
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 |
WeakValueIdentityDictionaries behave like IdentityDictionaries,
|
|
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 |
|
|
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.
|
|
53 |
|
|
54 |
|
|
55 |
[author:]
|
|
56 |
Claus (stolen from Stefans WeakValueDictionary)
|
|
57 |
|
|
58 |
[See also:]
|
|
59 |
WeakArray WeakIdentityDictionary WeakIdentitySet WeakValueDictionary
|
|
60 |
"
|
|
61 |
! !
|
|
62 |
|
|
63 |
!WeakValueIdentityDictionary methodsFor:'adding & removing'!
|
|
64 |
|
|
65 |
at:key put:anObject
|
|
66 |
"add the argument anObject under key, aKey to the receiver.
|
|
67 |
Return anObject (sigh).
|
|
68 |
Redefined to block interrupts, to avoid trouble when dependencies
|
|
69 |
are added within interrupting high prio processes."
|
|
70 |
|
|
71 |
|ret|
|
|
72 |
|
|
73 |
(OperatingSystem blockInterrupts) ifTrue:[
|
19086
|
74 |
"/ already blocked
|
|
75 |
^ super at:key put:anObject
|
4347
|
76 |
].
|
|
77 |
|
|
78 |
[
|
19086
|
79 |
ret := super at:key put:anObject.
|
|
80 |
] ensure:[
|
|
81 |
OperatingSystem unblockInterrupts
|
4347
|
82 |
].
|
|
83 |
^ ret
|
|
84 |
|
|
85 |
"Modified: 6.5.1996 / 12:22:26 / stefan"
|
|
86 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
87 |
"Modified: 20.10.1996 / 14:05:04 / cg"
|
|
88 |
!
|
|
89 |
|
|
90 |
removeKey:aKey ifAbsent:aBlock
|
|
91 |
"remove the association under aKey from the collection,
|
|
92 |
return the value previously stored there.
|
|
93 |
If it was not in the collection return the result
|
|
94 |
from evaluating aBlock.
|
|
95 |
|
|
96 |
Redefined to avoid synchronization problems, in case
|
|
97 |
of interrupts (otherwise, there could be some other operation
|
|
98 |
on the receiver done by another process, which garbles my contents)."
|
|
99 |
|
|
100 |
|ret|
|
|
101 |
|
|
102 |
(OperatingSystem blockInterrupts) ifTrue:[
|
19086
|
103 |
"/ already blocked
|
|
104 |
^ super removeKey:aKey ifAbsent:aBlock
|
4347
|
105 |
].
|
|
106 |
|
|
107 |
[
|
19086
|
108 |
ret := super removeKey:aKey ifAbsent:aBlock
|
|
109 |
] ensure:[
|
|
110 |
OperatingSystem unblockInterrupts
|
4347
|
111 |
].
|
|
112 |
^ ret
|
|
113 |
|
|
114 |
"Modified: 6.5.1996 / 12:44:07 / stefan"
|
|
115 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
116 |
!
|
|
117 |
|
|
118 |
removeValue:aKey ifAbsent:aBlock
|
|
119 |
"remove the association under aValue from the collection,
|
|
120 |
return the key previously stored there.
|
|
121 |
If it was not in the collection return the result
|
|
122 |
from evaluating aBlock.
|
|
123 |
|
|
124 |
Redefined to avoid synchronization problems, in case
|
|
125 |
of interrupts (otherwise, there could be some other operation
|
|
126 |
on the receiver done by another process, which garbles my contents)."
|
|
127 |
|
|
128 |
|ret|
|
|
129 |
|
|
130 |
(OperatingSystem blockInterrupts) ifTrue:[
|
19086
|
131 |
"/ already blocked
|
|
132 |
^ super removeValue:aKey ifAbsent:aBlock
|
4347
|
133 |
].
|
|
134 |
|
|
135 |
[
|
19086
|
136 |
ret := super removeValue:aKey ifAbsent:aBlock
|
|
137 |
] ensure:[
|
|
138 |
OperatingSystem unblockInterrupts
|
4347
|
139 |
].
|
|
140 |
^ ret.
|
|
141 |
|
|
142 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
143 |
"Modified: 8.5.1996 / 14:54:09 / stefan"
|
|
144 |
! !
|
|
145 |
|
|
146 |
!WeakValueIdentityDictionary methodsFor:'element disposal'!
|
|
147 |
|
|
148 |
update:something with:aParameter from:changedObject
|
|
149 |
"an element (either key or value) died - clear out slots for
|
|
150 |
disposed keys."
|
|
151 |
|
|
152 |
|wasBlocked|
|
|
153 |
|
|
154 |
something == #ElementExpired ifTrue:[
|
|
155 |
"
|
|
156 |
have to block here - dispose may be done at a low priority
|
|
157 |
from the background finalizer. If new items are added by a
|
|
158 |
higher prio process, the dictionary might get corrupted otherwise
|
|
159 |
"
|
|
160 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
161 |
|
|
162 |
keyArray
|
|
163 |
forAllDeadIndicesDo:[:idx |
|
|
164 |
valueArray basicAt:idx put:nil.
|
|
165 |
tally := tally - 1.
|
|
166 |
]
|
|
167 |
replacingCorpsesWith:DeletedEntry.
|
|
168 |
|
|
169 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
170 |
]
|
|
171 |
|
|
172 |
"Created: 7.1.1997 / 16:59:30 / stefan"
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
! !
|
|
178 |
|
|
179 |
!WeakValueIdentityDictionary methodsFor:'private'!
|
|
180 |
|
|
181 |
valueContainerOfSize:n
|
|
182 |
"return a container for values of size n.
|
|
183 |
use WeakArrays here."
|
|
184 |
|
|
185 |
|a|
|
|
186 |
|
|
187 |
a := WeakArray new:n.
|
|
188 |
a addDependent:self.
|
|
189 |
^ a
|
|
190 |
|
|
191 |
"Created: 6.5.1996 / 14:47:37 / stefan"
|
|
192 |
! !
|
|
193 |
|
23693
|
194 |
!WeakValueIdentityDictionary methodsFor:'queries'!
|
4347
|
195 |
|
|
196 |
includes:anObject
|
|
197 |
"redefined to block interrupts
|
|
198 |
(avoid change of the dictionary while accessing)"
|
|
199 |
|
|
200 |
|val|
|
|
201 |
|
|
202 |
(OperatingSystem blockInterrupts) ifTrue:[
|
19540
|
203 |
"/ already blocked
|
|
204 |
^ super includes:anObject.
|
4347
|
205 |
].
|
|
206 |
|
|
207 |
[
|
19540
|
208 |
val := super includes:anObject.
|
|
209 |
] ensure:[
|
|
210 |
OperatingSystem unblockInterrupts.
|
4347
|
211 |
].
|
|
212 |
^ val
|
|
213 |
|
|
214 |
"Modified: 6.5.1996 / 12:22:26 / stefan"
|
|
215 |
"Modified: 1.7.1997 / 10:45:52 / cg"
|
|
216 |
"Created: 1.7.1997 / 15:41:14 / cg"
|
|
217 |
!
|
|
218 |
|
|
219 |
includesKey:key
|
|
220 |
"redefined to block interrupts
|
|
221 |
(avoid change of the dictionary while accessing)"
|
|
222 |
|
|
223 |
|val|
|
|
224 |
|
|
225 |
(OperatingSystem blockInterrupts) ifTrue:[
|
19540
|
226 |
"/ already blocked
|
|
227 |
^ super includesKey:key.
|
4347
|
228 |
].
|
|
229 |
|
|
230 |
[
|
19540
|
231 |
val := super includesKey:key.
|
|
232 |
] ensure:[
|
|
233 |
OperatingSystem unblockInterrupts.
|
4347
|
234 |
].
|
|
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:32 / cg"
|
23693
|
240 |
! !
|
|
241 |
|
|
242 |
!WeakValueIdentityDictionary methodsFor:'testing'!
|
9242
|
243 |
|
|
244 |
isWeakCollection
|
|
245 |
"return true, if the receiver has weak references to its elements."
|
|
246 |
|
|
247 |
^ true
|
4347
|
248 |
! !
|
|
249 |
|
|
250 |
!WeakValueIdentityDictionary class methodsFor:'documentation'!
|
|
251 |
|
|
252 |
version
|
19086
|
253 |
^ '$Header$'
|
19544
|
254 |
!
|
|
255 |
|
|
256 |
version_CVS
|
|
257 |
^ '$Header$'
|
4347
|
258 |
! !
|
19086
|
259 |
|