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