author | Jan Vrany <jan.vrany@labware.com> |
Sat, 29 Oct 2022 01:05:05 +0100 | |
branch | jv |
changeset 25446 | 98fb817db6de |
parent 20244 | 20922299fd44 |
permissions | -rw-r--r-- |
94 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
187 | 3 |
All Rights Reserved |
94 | 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 |
" |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
13 |
|
20238 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
94 | 16 |
IdentitySet subclass:#WeakIdentitySet |
1054 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
4350 | 20 |
category:'Collections-Weak' |
94 | 21 |
! |
22 |
||
23 |
!WeakIdentitySet class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
187 | 28 |
All Rights Reserved |
94 | 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 |
" |
|
1263 | 41 |
WeakIdentitySets behave like IdentitySets, as long as the contained |
42 |
objects are still referenced by some other (non-weak) object. |
|
43 |
However, once the last non-weak reference ceases to exist, |
|
44 |
the object will be automatically removed from the Weakcollection |
|
45 |
(with some delay: it will be removed after the next garbage collect). |
|
46 |
||
47 |
This class was added to support dependencies which do not |
|
48 |
prevent objects from dying. (i.e. which do not fill up your memory |
|
49 |
if you forget to #release it). |
|
50 |
||
2307 | 51 |
[Warning:] |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
52 |
If you use this, be very careful since the collections size changes |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
53 |
'magically' - for example, testing for being nonEmpty and then |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
54 |
removing the first element may fail, since the element may vanish inbetween. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
55 |
In general, never trust the value as returned by the size/isEmpty messages. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
56 |
WeakIdentitySet is not meant as a 'public' class. |
2260 | 57 |
|
1290 | 58 |
[author:] |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
59 |
Claus Gittinger |
1290 | 60 |
|
1263 | 61 |
[See also:] |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
62 |
WeakArray WeakIdentityDictionary |
94 | 63 |
" |
64 |
! ! |
|
65 |
||
111 | 66 |
!WeakIdentitySet class methodsFor:'instance creation'! |
67 |
||
68 |
new |
|
1263 | 69 |
"return a new empty WeakIdentitySet" |
111 | 70 |
|
71 |
^ self new:1 |
|
1263 | 72 |
|
73 |
"Modified: 23.4.1996 / 13:56:08 / cg" |
|
111 | 74 |
! ! |
75 |
||
609 | 76 |
!WeakIdentitySet class methodsFor:'queries'! |
77 |
||
78 |
goodSizeFrom:arg |
|
79 |
"return a good array size for the given argument. |
|
80 |
Since WeakIdentitySets are mostly used for dependency management, we typically |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
81 |
have only a small number of elements in the set. |
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
|
82 |
Therefore use exact size for small sets |
9317 | 83 |
(instead of rounding up to the next prime 11)." |
609 | 84 |
|
85 |
arg <= 10 ifTrue:[ |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
86 |
arg < 1 ifTrue:[^ 1]. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
87 |
^ arg. |
609 | 88 |
]. |
89 |
^ super goodSizeFrom:arg |
|
90 |
! ! |
|
91 |
||
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
|
92 |
!WeakIdentitySet methodsFor:'accessing'! |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
93 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
94 |
firstIfEmpty:exceptionValue |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
95 |
"return the first element of the collection or the |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
96 |
value of the exceptionBlock, if empty. |
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 |
Redefine, since the inherited method does not work if |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
98 |
elements change silently to nil" |
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 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
100 |
|index "{ Class: SmallInteger }" |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
101 |
element| |
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 |
index := 1. |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
104 |
"/ allow for the size to change during enumeration |
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 |
[index <= keyArray size] whileTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
106 |
element := keyArray basicAt:index. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
107 |
element notNil ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
108 |
element class ~~ SmallInteger ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
109 |
element ~~ DeletedEntry ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
110 |
element == NilEntry ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
111 |
element := nil. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
112 |
]. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
113 |
^ element |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
114 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
115 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
116 |
]. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
117 |
index := index + 1 |
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
|
118 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
119 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
120 |
^ exceptionValue value. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
121 |
! ! |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
122 |
|
359 | 123 |
!WeakIdentitySet methodsFor:'adding & removing'! |
124 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
125 |
add:newElement |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
126 |
"add the argument, newElement to the receiver. |
14894 | 127 |
Returns the argument, newElement (sigh). |
128 |
||
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
129 |
Redefined to avoid synchronization problems, in case of interrupts |
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 |
(otherwise, there could be some other operation on the receiver |
2304 | 131 |
done by another process, which garbles my contents)" |
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
|
132 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
133 |
|ret| |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
134 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
135 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
136 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
137 |
^ super add:newElement. |
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
|
138 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
139 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
140 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
141 |
ret := super add:newElement. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
142 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
143 |
OperatingSystem unblockInterrupts |
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
|
144 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
145 |
^ ret |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
146 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
147 |
"Modified: 29.1.1997 / 15:06:57 / cg" |
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 |
! |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
149 |
|
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
150 |
remove:anObject ifAbsent:exceptionBlock |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
151 |
"redefined to avoid synchronization problems, in case of interrupts |
359 | 152 |
(otherwise, there could be some other operation on the receiver |
153 |
done by another process, which garbles my contents)" |
|
154 |
||
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
|
155 |
|ret| |
359 | 156 |
|
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
|
157 |
(OperatingSystem blockInterrupts) ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
158 |
"/ already blocked |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
159 |
^ super remove:anObject ifAbsent:exceptionBlock. |
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
|
160 |
]. |
359 | 161 |
|
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
|
162 |
[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
163 |
ret := super remove:anObject ifAbsent:exceptionBlock |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
4530
diff
changeset
|
164 |
] ensure:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
165 |
OperatingSystem unblockInterrupts |
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
|
166 |
]. |
f19df2d4c238
avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents:
2260
diff
changeset
|
167 |
^ ret |
359 | 168 |
|
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
|
169 |
"Modified: 29.1.1997 / 15:07:19 / cg" |
359 | 170 |
! ! |
171 |
||
609 | 172 |
!WeakIdentitySet methodsFor:'element disposal'! |
94 | 173 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
174 |
update:something with:aParameter from:changedObject |
609 | 175 |
"an element died - must rehash" |
176 |
||
1780
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
177 |
|wasBlocked| |
111 | 178 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
179 |
something == #ElementExpired ifTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
180 |
" |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
181 |
must block interrupts here - finalization |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
182 |
may be done at low prio in the background |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
183 |
finalizer, we do not want to be interrupted |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
184 |
while rehashing |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
185 |
" |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
186 |
wasBlocked := OperatingSystem blockInterrupts. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
187 |
keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
188 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
189 |
]. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
190 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
191 |
"Created: 7.1.1997 / 17:00:33 / stefan" |
94 | 192 |
! ! |
193 |
||
216 | 194 |
!WeakIdentitySet methodsFor:'enumerating'! |
195 |
||
196 |
do:aBlock |
|
197 |
"perform the block for all members in the collection. |
|
198 |
This method has been rewritten to be more robust about |
|
199 |
changed contents while enumerating elements (which might |
|
200 |
happen during change&update processing, if dependents |
|
16604 | 201 |
are added or removed within the update)." |
216 | 202 |
|
203 |
|index "{ Class: SmallInteger }" |
|
204 |
element| |
|
205 |
||
206 |
index := 1. |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
207 |
"/ allow for the size to change during enumeration |
216 | 208 |
[index <= keyArray size] whileTrue:[ |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
209 |
element := keyArray basicAt:index. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
210 |
element notNil ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
211 |
element class ~~ SmallInteger ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
212 |
element ~~ DeletedEntry ifTrue:[ |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
213 |
aBlock value:element |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
214 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
215 |
] |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
216 |
]. |
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
217 |
index := index + 1 |
216 | 218 |
] |
219 |
! ! |
|
220 |
||
609 | 221 |
!WeakIdentitySet methodsFor:'private'! |
94 | 222 |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
223 |
findKeyOrNil:key |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
224 |
"Look for the key in the receiver. |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
225 |
If it is found, return return the index, otherwise |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
226 |
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
|
227 |
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
|
228 |
|
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
229 |
Warning: an empty slot MUST be filled by the sender - it is only to be sent |
20238 | 230 |
by at:put: / add: - like methods." |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
231 |
|
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
232 |
|index "{ Class:SmallInteger }" |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
233 |
length "{ Class:SmallInteger }" |
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
234 |
startIndex probe |
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
235 |
delIndex "{ Class:SmallInteger }"| |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
236 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
237 |
(OperatingSystem blockInterrupts) ifFalse:[ |
20238 | 238 |
"/ |
239 |
"/ may never be entered with interrupts enabled |
|
240 |
"/ |
|
241 |
OperatingSystem unblockInterrupts. |
|
242 |
self error:'unblocked call of findKeyOrNil'. |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
243 |
]. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
244 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
245 |
delIndex := 0. |
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
246 |
|
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
247 |
length := keyArray basicSize. |
6953
6b3a197638f0
invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents:
6421
diff
changeset
|
248 |
startIndex := index := self initialIndexForKey:key. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
249 |
|
16238 | 250 |
[ |
20238 | 251 |
probe := keyArray basicAt:index. |
252 |
key == probe ifTrue:[^ index]. |
|
253 |
probe isNil ifTrue:[ |
|
254 |
delIndex == 0 ifTrue:[^ index]. |
|
255 |
keyArray basicAt:delIndex put:nil. |
|
256 |
^ delIndex |
|
257 |
]. |
|
258 |
||
259 |
probe class == SmallInteger ifTrue:[ |
|
260 |
probe := DeletedEntry. |
|
261 |
keyArray basicAt:index put:probe. |
|
262 |
tally := tally - 1. |
|
263 |
]. |
|
264 |
delIndex == 0 ifTrue:[ |
|
265 |
probe == DeletedEntry ifTrue:[ |
|
266 |
delIndex := index |
|
267 |
] |
|
268 |
]. |
|
269 |
||
270 |
index == length ifTrue:[ |
|
271 |
index := 1 |
|
272 |
] ifFalse:[ |
|
273 |
index := index + 1 |
|
274 |
]. |
|
275 |
index == startIndex ifTrue:[ |
|
276 |
delIndex ~~ 0 ifTrue:[ |
|
277 |
keyArray basicAt:delIndex put:nil. |
|
278 |
^ delIndex |
|
279 |
]. |
|
280 |
self grow. |
|
281 |
length := keyArray basicSize. |
|
282 |
startIndex := index := self initialIndexForKey:key. |
|
283 |
]. |
|
284 |
] loop. |
|
285 |
||
286 |
"Modified: 30.1.1997 / 15:04:38 / cg" |
|
287 |
! |
|
288 |
||
289 |
findKeyOrNilOrDeletedEntry:key |
|
290 |
"Look for the key in the receiver. |
|
291 |
If it is found, return return the index, otherwise |
|
292 |
the index of the first unused slot. |
|
293 |
Grow the receiver, if key was not found, and no unused slots were present" |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
294 |
|
20238 | 295 |
|index "{ Class:SmallInteger }" |
296 |
length "{ Class:SmallInteger }" |
|
297 |
startIndex probe |
|
298 |
delIndex "{ Class:SmallInteger }"| |
|
299 |
||
300 |
(OperatingSystem blockInterrupts) ifFalse:[ |
|
301 |
"/ |
|
302 |
"/ may never be entered with interrupts enabled |
|
303 |
"/ |
|
304 |
OperatingSystem unblockInterrupts. |
|
305 |
self error:'unblocked call of findKeyOrNil'. |
|
306 |
]. |
|
307 |
||
308 |
delIndex := 0. |
|
309 |
||
310 |
length := keyArray basicSize. |
|
311 |
startIndex := index := self initialIndexForKey:key. |
|
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
312 |
|
20238 | 313 |
[ |
314 |
probe := keyArray basicAt:index. |
|
315 |
key == probe ifTrue:[^ index]. |
|
316 |
probe isNil ifTrue:[ |
|
317 |
delIndex == 0 ifTrue:[^ index]. |
|
318 |
^ delIndex |
|
319 |
]. |
|
320 |
||
321 |
probe class == SmallInteger ifTrue:[ |
|
322 |
probe := DeletedEntry. |
|
323 |
keyArray basicAt:index put:probe. |
|
324 |
tally := tally - 1. |
|
325 |
]. |
|
326 |
delIndex == 0 ifTrue:[ |
|
327 |
probe == DeletedEntry ifTrue:[ |
|
328 |
delIndex := index |
|
329 |
] |
|
330 |
]. |
|
331 |
||
332 |
index == length ifTrue:[ |
|
333 |
index := 1 |
|
334 |
] ifFalse:[ |
|
335 |
index := index + 1 |
|
336 |
]. |
|
337 |
index == startIndex ifTrue:[ |
|
338 |
delIndex ~~ 0 ifTrue:[ |
|
339 |
^ delIndex |
|
340 |
]. |
|
341 |
self grow. |
|
342 |
length := keyArray basicSize. |
|
343 |
startIndex := index := self initialIndexForKey:key. |
|
344 |
]. |
|
16238 | 345 |
] loop. |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
346 |
|
2329
937fc97d5544
must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
347 |
"Modified: 30.1.1997 / 15:04:38 / cg" |
2315
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
348 |
! |
a9ff2fda8bae
mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents:
2307
diff
changeset
|
349 |
|
9241 | 350 |
isWeakCollection |
351 |
"return true, if the receiver has weak references to its elements." |
|
352 |
||
353 |
^ true |
|
354 |
! |
|
355 |
||
609 | 356 |
keyContainerOfSize:n |
357 |
"return a container for keys and values of size n. |
|
358 |
use WeakArrays here." |
|
359 | 359 |
|
609 | 360 |
|w| |
361 |
||
362 |
w := WeakArray new:n. |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
363 |
w addDependent:self. |
609 | 364 |
^ w |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
365 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
366 |
"Modified: 7.1.1997 / 17:01:28 / stefan" |
94 | 367 |
! ! |
609 | 368 |
|
636 | 369 |
!WeakIdentitySet class methodsFor:'documentation'! |
370 |
||
371 |
version |
|
18620
b4e9f25d6ce6
preparations for lined index list in WeakArray
Claus Gittinger <cg@exept.de>
parents:
16604
diff
changeset
|
372 |
^ '$Header$' |
636 | 373 |
! ! |
14894 | 374 |