author | Claus Gittinger <cg@exept.de> |
Tue, 27 Sep 2016 15:25:16 +0200 | |
changeset 20466 | 1a07839f6e29 |
parent 20237 | 855a12ebe6f9 |
child 20244 | 20922299fd44 |
child 20702 | 5dfa0631a814 |
permissions | -rw-r--r-- |
6 | 1 |
" |
2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 3 |
All Rights Reserved |
6 | 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 |
" |
|
6233 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
15 |
|
6 | 16 |
IdentityDictionary subclass:#WeakIdentityDictionary |
1054 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
4350 | 20 |
category:'Collections-Weak' |
6 | 21 |
! |
22 |
||
10 | 23 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 28 |
All Rights Reserved |
88 | 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 |
||
10 | 39 |
documentation |
40 |
" |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
41 |
WeakIdentityDictionaries behave like IdentityDictionaries, |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
42 |
as long as the keys are still referenced by some |
1263 | 43 |
other (non-weak) object. |
44 |
However, once the last non-weak reference ceases to exist, |
|
45 |
the object will be automatically removed from the Weakcollection |
|
46 |
(with some delay: it will be removed after the next garbage collect). |
|
47 |
||
48 |
This class was added to support keeping track of dependents without |
|
88 | 49 |
keeping the values alive - values simply become nil when no one else |
50 |
references it. The original dependency mechanism used a regular Dictionary, |
|
51 |
which usually leads to a lot of garbage being kept due to a forgotten |
|
52 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much |
|
53 |
more comfortable, since no manual release of dependents is needed. |
|
1263 | 54 |
|
2306 | 55 |
[Warning:] |
56 |
If you use this, be very careful since the collections size changes |
|
57 |
'magically' - for example, testing for being nonEmpty and then |
|
58 |
removing the first element may fail, since the element may vanish inbetween. |
|
59 |
In general, never trust the value as returned by the size/isEmpty messages. |
|
2260 | 60 |
|
61 |
||
1290 | 62 |
[author:] |
2910 | 63 |
Claus Gittinger |
1290 | 64 |
|
1263 | 65 |
[See also:] |
2910 | 66 |
WeakArray WeakValueDictionary WeakIdentitySet |
10 | 67 |
" |
68 |
! ! |
|
69 |
||
359 | 70 |
!WeakIdentityDictionary methodsFor:'adding & removing'! |
71 |
||
2298 | 72 |
at:key ifAbsent:exceptionBlock |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
73 |
"redefined to block interrupts |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
74 |
(avoid change of the dictionary while accessing)" |
2298 | 75 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
76 |
|val| |
2298 | 77 |
|
20166 | 78 |
key class == SmallInteger ifTrue:[ |
79 |
"SmallIntegers cannot be stored into a WeakArray" |
|
80 |
^ exceptionBlock value. |
|
81 |
]. |
|
82 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
83 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 84 |
"/ already blocked |
85 |
^ super at:key ifAbsent:exceptionBlock. |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
86 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
87 |
|
2298 | 88 |
[ |
20166 | 89 |
val := super at:key ifAbsent:exceptionBlock. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
90 |
] ensure:[ |
20166 | 91 |
OperatingSystem unblockInterrupts. |
2298 | 92 |
]. |
93 |
^ val |
|
94 |
||
95 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
96 |
"Modified: 1.7.1997 / 10:45:47 / cg" |
2298 | 97 |
! |
98 |
||
19193 | 99 |
at:key ifAbsentPut:replacementBlock |
100 |
"redefined to block interrupts |
|
101 |
(avoid change of the dictionary while accessing)" |
|
102 |
||
103 |
|val| |
|
104 |
||
20166 | 105 |
key class == SmallInteger ifTrue:[ |
106 |
"SmallIntegers cannot be stored into a WeakArray" |
|
107 |
self error:'WeakidentityDictionary: cannot store a SmallInteger'. |
|
108 |
]. |
|
109 |
||
19193 | 110 |
(OperatingSystem blockInterrupts) ifTrue:[ |
111 |
"/ already blocked |
|
112 |
^ super at:key ifAbsentPut:replacementBlock. |
|
113 |
]. |
|
114 |
||
115 |
[ |
|
116 |
val := super at:key ifAbsentPut:replacementBlock. |
|
117 |
] ensure:[ |
|
118 |
OperatingSystem unblockInterrupts. |
|
119 |
]. |
|
120 |
^ val |
|
121 |
! |
|
122 |
||
1256 | 123 |
at:key put:anObject |
124 |
"add the argument anObject under key, aKey to the receiver. |
|
125 |
Return anObject (sigh). |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
126 |
Redefined to block interrupts, to avoid trouble when dependencies |
1256 | 127 |
are added within interrupting high prio processes." |
128 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
129 |
|val| |
359 | 130 |
|
20166 | 131 |
key class == SmallInteger ifTrue:[ |
132 |
"SmallIntegers cannot be stored into a WeakArray" |
|
133 |
self error:'WeakidentityDictionary: cannot store a SmallInteger'. |
|
134 |
]. |
|
135 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
136 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 137 |
"/ already blocked |
138 |
^ 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:
2298
diff
changeset
|
139 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
140 |
|
1790 | 141 |
[ |
20166 | 142 |
val := super at:key put:anObject. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
143 |
] ensure:[ |
20166 | 144 |
OperatingSystem unblockInterrupts. |
1790 | 145 |
]. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
146 |
^ val |
1223 | 147 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
148 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
149 |
"Modified: 29.1.1997 / 15:08:45 / cg" |
359 | 150 |
! |
151 |
||
152 |
removeKey:aKey ifAbsent:aBlock |
|
1256 | 153 |
"remove the association under aKey from the collection, |
2910 | 154 |
return the value previously stored there. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
155 |
If it was not in the collection return the result |
1256 | 156 |
from evaluating aBlock. |
157 |
||
158 |
Redefined to avoid synchronization problems, in case |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
159 |
of interrupts (otherwise, there could be some other operation |
1256 | 160 |
on the receiver done by another process, which garbles my contents)." |
1054 | 161 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
162 |
|ret| |
359 | 163 |
|
20166 | 164 |
aKey class == SmallInteger ifTrue:[ |
165 |
"SmallIntegers cannot be stored into a WeakArray" |
|
166 |
^ aBlock value. |
|
167 |
]. |
|
168 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
169 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 170 |
"/ already blocked |
171 |
^ 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:
2298
diff
changeset
|
172 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
173 |
|
359 | 174 |
[ |
20166 | 175 |
ret := super removeKey:aKey ifAbsent:aBlock |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
176 |
] ensure:[ |
20166 | 177 |
OperatingSystem unblockInterrupts |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
178 |
]. |
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
179 |
^ ret |
1054 | 180 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
181 |
"Modified: 6.5.1996 / 12:44:51 / stefan" |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
182 |
"Modified: 29.1.1997 / 15:09:11 / cg" |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
183 |
! |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
184 |
|
16513 | 185 |
safeRemoveKey:key |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
186 |
"redefined to block interrupts |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
187 |
(avoid change of the dictionary while accessing)" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
188 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
189 |
|val| |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
190 |
|
20166 | 191 |
key class == SmallInteger ifTrue:[ |
192 |
"SmallIntegers cannot be stored into a WeakArray" |
|
193 |
^ nil. |
|
194 |
]. |
|
195 |
||
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
196 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 197 |
"/ already blocked |
198 |
^ super safeRemoveKey:key. |
|
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
199 |
]. |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
200 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
201 |
[ |
20166 | 202 |
val := super safeRemoveKey:key. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
203 |
] ensure:[ |
20166 | 204 |
OperatingSystem unblockInterrupts. |
2738
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
205 |
]. |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
206 |
^ val |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
207 |
|
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
208 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
209 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
2546e06df154
more interrupt blocking (paranoia)
Claus Gittinger <cg@exept.de>
parents:
2329
diff
changeset
|
210 |
"Created: 1.7.1997 / 10:46:34 / cg" |
359 | 211 |
! ! |
212 |
||
10 | 213 |
!WeakIdentityDictionary methodsFor:'element disposal'! |
214 |
||
6309 | 215 |
clearDeadSlots |
216 |
|wasBlocked| |
|
217 |
||
218 |
" |
|
219 |
have to block here - dispose may be done at a low priority |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
220 |
from the background finalizer. If new items are added by a |
6309 | 221 |
higher prio process, the dictionary might get corrupted otherwise |
222 |
" |
|
223 |
wasBlocked := OperatingSystem blockInterrupts. |
|
6469 | 224 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
225 |
keyArray |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
226 |
forAllDeadIndicesDo:[:idx | |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
227 |
valueArray basicAt:idx put:nil. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
228 |
tally := tally - 1. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
229 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
230 |
replacingCorpsesWith:DeletedEntry. |
6469 | 231 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
232 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6469 | 233 |
]. |
6309 | 234 |
|
235 |
"Modified: / 13.12.2001 / 14:18:17 / martin" |
|
236 |
! |
|
237 |
||
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
238 |
update:something with:aParameter from:changedObject |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
239 |
"an element (either key or value) died - clear out slots for |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
240 |
disposed keys." |
6 | 241 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
242 |
something == #ElementExpired ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
243 |
self clearDeadSlots. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
244 |
] |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
245 |
|
6309 | 246 |
"Created: / 7.1.1997 / 16:59:30 / stefan" |
247 |
"Modified: / 13.12.2001 / 14:17:58 / martin" |
|
6 | 248 |
! ! |
609 | 249 |
|
250 |
!WeakIdentityDictionary methodsFor:'private'! |
|
251 |
||
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
252 |
findKeyOrNil:key |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
253 |
"Look for the key in the receiver. |
2988 | 254 |
If it is found, return the index, |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
255 |
otherwise the index of the first unused slot. |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
256 |
Grow the receiver, if key was not found, and no unused slots were present. |
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
257 |
|
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
258 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
20037 | 259 |
by at:put: / add: - like methods." |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
260 |
|
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
261 |
|index "{ Class:SmallInteger }" |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
262 |
length "{ Class:SmallInteger }" |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
263 |
startIndex probe |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
264 |
delIndex "{ Class:SmallInteger }"| |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
265 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
266 |
(OperatingSystem blockInterrupts) ifFalse:[ |
20037 | 267 |
"/ |
268 |
"/ may never be entered with interrupts enabled |
|
269 |
"/ |
|
270 |
OperatingSystem unblockInterrupts. |
|
271 |
self error:'unblocked call of findKeyOrNil'. |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
272 |
]. |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
273 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
274 |
delIndex := 0. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
275 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
276 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6469
diff
changeset
|
277 |
startIndex := index := self initialIndexForKey:key. |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
278 |
|
16244 | 279 |
[ |
20037 | 280 |
probe := keyArray basicAt:index. |
281 |
key == probe ifTrue:[^ index]. |
|
282 |
probe isNil ifTrue:[ |
|
283 |
delIndex == 0 ifTrue:[^ index]. |
|
284 |
keyArray basicAt:delIndex put:nil. |
|
285 |
^ delIndex |
|
286 |
]. |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
287 |
|
20037 | 288 |
probe class == SmallInteger ifTrue:[ |
289 |
probe := DeletedEntry. |
|
290 |
keyArray basicAt:index put:probe. |
|
291 |
valueArray basicAt:index put:nil. |
|
292 |
tally := tally - 1. |
|
293 |
]. |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
294 |
|
20037 | 295 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
296 |
delIndex := index |
|
297 |
]. |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
298 |
|
20037 | 299 |
index == length ifTrue:[ |
300 |
index := 1 |
|
301 |
] ifFalse:[ |
|
302 |
index := index + 1 |
|
303 |
]. |
|
304 |
index == startIndex ifTrue:[ |
|
305 |
delIndex ~~ 0 ifTrue:[ |
|
306 |
keyArray basicAt:delIndex put:nil. |
|
307 |
^ delIndex |
|
308 |
]. |
|
309 |
self grow. |
|
310 |
length := keyArray basicSize. |
|
311 |
startIndex := index := self initialIndexForKey:key. |
|
312 |
]. |
|
16244 | 313 |
] loop. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
314 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
315 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
2988 | 316 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
317 |
! |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2306
diff
changeset
|
318 |
|
20237 | 319 |
findKeyOrNilOrDeletedEntry:key |
320 |
"Look for the key in the receiver. |
|
321 |
If it is found, return the index, |
|
322 |
otherwise the index of the first unused slot. |
|
323 |
Grow the receiver, if key was not found, and no unused slots were present." |
|
324 |
||
325 |
|index "{ Class:SmallInteger }" |
|
326 |
length "{ Class:SmallInteger }" |
|
327 |
startIndex probe |
|
328 |
delIndex "{ Class:SmallInteger }"| |
|
329 |
||
330 |
(OperatingSystem blockInterrupts) ifFalse:[ |
|
331 |
"/ |
|
332 |
"/ may never be entered with interrupts enabled |
|
333 |
"/ |
|
334 |
OperatingSystem unblockInterrupts. |
|
335 |
self error:'unblocked call of findKeyOrNil'. |
|
336 |
]. |
|
337 |
||
338 |
delIndex := 0. |
|
339 |
||
340 |
length := keyArray basicSize. |
|
341 |
startIndex := index := self initialIndexForKey:key. |
|
342 |
||
343 |
[ |
|
344 |
probe := keyArray basicAt:index. |
|
345 |
key == probe ifTrue:[^ index]. |
|
346 |
probe isNil ifTrue:[ |
|
347 |
delIndex == 0 ifTrue:[^ index]. |
|
348 |
^ delIndex |
|
349 |
]. |
|
350 |
||
351 |
probe class == SmallInteger ifTrue:[ |
|
352 |
probe := DeletedEntry. |
|
353 |
keyArray basicAt:index put:probe. |
|
354 |
valueArray basicAt:index put:nil. |
|
355 |
tally := tally - 1. |
|
356 |
]. |
|
357 |
||
358 |
(delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[ |
|
359 |
delIndex := index |
|
360 |
]. |
|
361 |
||
362 |
index == length ifTrue:[ |
|
363 |
index := 1 |
|
364 |
] ifFalse:[ |
|
365 |
index := index + 1 |
|
366 |
]. |
|
367 |
index == startIndex ifTrue:[ |
|
368 |
delIndex ~~ 0 ifTrue:[ |
|
369 |
^ delIndex |
|
370 |
]. |
|
371 |
self grow. |
|
372 |
length := keyArray basicSize. |
|
373 |
startIndex := index := self initialIndexForKey:key. |
|
374 |
]. |
|
375 |
] loop. |
|
376 |
||
377 |
"Modified: 30.1.1997 / 15:04:34 / cg" |
|
378 |
"Modified: 1.10.1997 / 11:25:32 / stefan" |
|
379 |
! |
|
380 |
||
2298 | 381 |
grow:newSize |
382 |
"grow the receiver. |
|
383 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
384 |
are added within interrupting high prio processes." |
|
385 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
386 |
"/ 'grow:' printCR. |
2298 | 387 |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
388 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
389 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
390 |
^ super grow:newSize. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
391 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
392 |
|
2298 | 393 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
394 |
super grow:newSize |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
395 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
396 |
OperatingSystem unblockInterrupts |
2298 | 397 |
]. |
398 |
||
399 |
"Created: 28.1.1997 / 23:41:39 / cg" |
|
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
400 |
"Modified: 29.1.1997 / 15:10:12 / cg" |
2298 | 401 |
! |
402 |
||
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
403 |
initializeForCapacity:minSize |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
404 |
"reset the receiver's contents. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
405 |
Redefined to block interrupts, to avoid trouble when dependencies |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
406 |
are added within interrupting high prio processes." |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
407 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
408 |
"/ 'setTally:' printCR. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
409 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
410 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
411 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
412 |
super initializeForCapacity:minSize. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
413 |
^ self. |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
414 |
]. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
415 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
416 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
417 |
super initializeForCapacity:minSize |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
418 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
419 |
OperatingSystem unblockInterrupts |
18333
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
420 |
]. |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
421 |
|
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
422 |
"Created: 29.1.1997 / 11:40:12 / cg" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
423 |
"Modified: 29.1.1997 / 15:11:11 / cg" |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
424 |
! |
854cf4f00536
class: WeakIdentityDictionary
Claus Gittinger <cg@exept.de>
parents:
16513
diff
changeset
|
425 |
|
609 | 426 |
keyContainerOfSize:n |
427 |
"return a container for keys of size n. |
|
20037 | 428 |
use WeakArrays here." |
609 | 429 |
|
430 |
|w| |
|
431 |
||
432 |
w := WeakArray new:n. |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
433 |
w addDependent:self. |
609 | 434 |
^ w |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
435 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
436 |
"Modified: 7.1.1997 / 17:01:15 / stefan" |
2298 | 437 |
"Modified: 29.1.1997 / 14:18:49 / cg" |
438 |
! |
|
439 |
||
440 |
rehash |
|
441 |
"grow the receiver. |
|
442 |
Redefined to block interrupts, to avoid trouble when dependencies |
|
443 |
are added within interrupting high prio processes." |
|
444 |
||
445 |
"/ 'rehash' printCR. |
|
446 |
||
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
447 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
448 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
449 |
super rehash. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
450 |
^ self. |
2303
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
451 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2298
diff
changeset
|
452 |
|
2298 | 453 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
454 |
super rehash |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
455 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
456 |
OperatingSystem unblockInterrupts |
2298 | 457 |
]. |
458 |
||
459 |
"Created: 29.1.1997 / 11:39:42 / cg" |
|
460 |
"Modified: 29.1.1997 / 14:18:52 / cg" |
|
609 | 461 |
! ! |
462 |
||
2741 | 463 |
!WeakIdentityDictionary methodsFor:'testing'! |
464 |
||
465 |
includes:anObject |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
466 |
"redefined to block interrupts |
2741 | 467 |
(avoid change of the dictionary while accessing)" |
468 |
||
469 |
|val| |
|
470 |
||
471 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
472 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
473 |
^ super includes:anObject. |
2741 | 474 |
]. |
475 |
||
476 |
[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
477 |
val := super includes:anObject. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
478 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
479 |
OperatingSystem unblockInterrupts. |
2741 | 480 |
]. |
481 |
^ val |
|
482 |
||
483 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
484 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
485 |
"Created: 1.7.1997 / 10:47:13 / cg" |
|
486 |
! |
|
487 |
||
488 |
includesKey:key |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
489 |
"redefined to block interrupts |
2741 | 490 |
(avoid change of the dictionary while accessing)" |
491 |
||
492 |
|val| |
|
493 |
||
20166 | 494 |
key class == SmallInteger ifTrue:[ |
495 |
"SmallIntegers cannot be stored into a WeakArray" |
|
496 |
^ false. |
|
497 |
]. |
|
498 |
||
2741 | 499 |
(OperatingSystem blockInterrupts) ifTrue:[ |
20166 | 500 |
"/ already blocked |
501 |
^ super includesKey:key. |
|
2741 | 502 |
]. |
503 |
||
504 |
[ |
|
20166 | 505 |
val := super includesKey:key. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6309
diff
changeset
|
506 |
] ensure:[ |
20166 | 507 |
OperatingSystem unblockInterrupts. |
2741 | 508 |
]. |
509 |
^ val |
|
510 |
||
511 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
|
512 |
"Created: 1.7.1997 / 10:45:14 / cg" |
|
513 |
"Modified: 1.7.1997 / 10:45:52 / cg" |
|
9243 | 514 |
! |
515 |
||
516 |
isWeakCollection |
|
517 |
"return true, if the receiver has weak references to its elements." |
|
518 |
||
519 |
^ true |
|
2741 | 520 |
! ! |
521 |
||
635 | 522 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
523 |
||
524 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
18333
diff
changeset
|
525 |
^ '$Header$' |
635 | 526 |
! ! |
16244 | 527 |