author | Claus Gittinger <cg@exept.de> |
Fri, 24 Jan 1997 19:16:48 +0100 | |
changeset 2256 | ffd565c6e084 |
parent 2102 | 7a0e81ed345b |
child 2260 | 96d898c2874d |
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 |
" |
|
12 |
||
13 |
IdentitySet subclass:#WeakIdentitySet |
|
1054 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
94 | 18 |
! |
19 |
||
20 |
!WeakIdentitySet class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
187 | 25 |
All Rights Reserved |
94 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
1263 | 38 |
WeakIdentitySets behave like IdentitySets, as long as the contained |
39 |
objects are still referenced by some other (non-weak) object. |
|
40 |
However, once the last non-weak reference ceases to exist, |
|
41 |
the object will be automatically removed from the Weakcollection |
|
42 |
(with some delay: it will be removed after the next garbage collect). |
|
43 |
||
44 |
This class was added to support dependencies which do not |
|
45 |
prevent objects from dying. (i.e. which do not fill up your memory |
|
46 |
if you forget to #release it). |
|
47 |
||
1290 | 48 |
[author:] |
49 |
Claus Gittinger |
|
50 |
||
1263 | 51 |
[See also:] |
52 |
WeakArray WeakIdentityDictionary |
|
94 | 53 |
" |
54 |
! ! |
|
55 |
||
111 | 56 |
!WeakIdentitySet class methodsFor:'instance creation'! |
57 |
||
58 |
new |
|
1263 | 59 |
"return a new empty WeakIdentitySet" |
111 | 60 |
|
61 |
^ self new:1 |
|
1263 | 62 |
|
63 |
"Modified: 23.4.1996 / 13:56:08 / cg" |
|
111 | 64 |
! ! |
65 |
||
609 | 66 |
!WeakIdentitySet class methodsFor:'queries'! |
67 |
||
68 |
goodSizeFrom:arg |
|
69 |
"return a good array size for the given argument. |
|
70 |
Since WeakIdentitySets are mostly used for dependency management, we typically |
|
71 |
have only one element in the set. Therefore use exact size for small sets |
|
72 |
(instead of rounding up to the prime 11)." |
|
73 |
||
74 |
arg <= 10 ifTrue:[ |
|
75 |
arg < 1 ifTrue:[^ 1]. |
|
76 |
^ arg. |
|
77 |
]. |
|
78 |
^ super goodSizeFrom:arg |
|
79 |
! ! |
|
80 |
||
359 | 81 |
!WeakIdentitySet methodsFor:'adding & removing'! |
82 |
||
83 |
add:newElement |
|
84 |
"redefined to avoid synchronization promlems, in case |
|
85 |
of interrupts |
|
86 |
(otherwise, there could be some other operation on the receiver |
|
87 |
done by another process, which garbles my contents)" |
|
88 |
||
89 |
|wasBlocked| |
|
90 |
||
91 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1790 | 92 |
[ |
93 |
super add:newElement. |
|
94 |
] valueNowOrOnUnwindDo:[ |
|
95 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
|
96 |
] |
|
97 |
||
98 |
"Modified: 20.10.1996 / 14:04:29 / cg" |
|
359 | 99 |
! |
100 |
||
101 |
remove:anObject ifAbsent:exceptionBlock |
|
1054 | 102 |
"redefined to avoid synchronization problems, in case |
359 | 103 |
of interrupts |
104 |
(otherwise, there could be some other operation on the receiver |
|
105 |
done by another process, which garbles my contents)" |
|
106 |
||
107 |
|wasBlocked| |
|
108 |
||
109 |
wasBlocked := OperatingSystem blockInterrupts. |
|
110 |
[ |
|
1054 | 111 |
super remove:anObject ifAbsent:exceptionBlock |
359 | 112 |
] valueNowOrOnUnwindDo:[ |
1054 | 113 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
359 | 114 |
] |
1054 | 115 |
|
116 |
"Modified: 1.3.1996 / 21:16:10 / cg" |
|
359 | 117 |
! ! |
118 |
||
609 | 119 |
!WeakIdentitySet methodsFor:'element disposal'! |
94 | 120 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
121 |
update:something with:aParameter from:changedObject |
609 | 122 |
"an element died - must rehash" |
123 |
||
1780
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
124 |
|wasBlocked| |
111 | 125 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
126 |
something == #ElementExpired ifTrue:[ |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
127 |
" |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
128 |
must block interrupts here - finalization |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
129 |
may be done at low prio in the background |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
130 |
finalizer, we do not want to be interrupted |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
131 |
while rehashing |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
132 |
" |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
133 |
wasBlocked := OperatingSystem blockInterrupts. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
134 |
keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
135 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
136 |
]. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
137 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
138 |
"Created: 7.1.1997 / 17:00:33 / stefan" |
94 | 139 |
! ! |
140 |
||
216 | 141 |
!WeakIdentitySet methodsFor:'enumerating'! |
142 |
||
143 |
do:aBlock |
|
144 |
"perform the block for all members in the collection. |
|
145 |
This method has been rewritten to be more robust about |
|
146 |
changed contents while enumerating elements (which might |
|
147 |
happen during change&update processing, if dependents |
|
148 |
are added or removed within the update." |
|
149 |
||
150 |
|index "{ Class: SmallInteger }" |
|
151 |
element| |
|
152 |
||
153 |
index := 1. |
|
154 |
[index <= keyArray size] whileTrue:[ |
|
2102
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
155 |
element := keyArray at:index. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
156 |
element == 0 ifFalse:[ |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
157 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
158 |
aBlock value:element |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
159 |
] |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
160 |
] ifTrue:[ |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
161 |
|
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
162 |
"/ disabled, since we had to lock out interrupts here. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
163 |
"/ The entry is cleared anyway, when the next finalization round |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
164 |
"/ is handled. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
165 |
|
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
166 |
"/ keyArray at:index put:DeletedEntry. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
167 |
"/ tally := tally - 1. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
168 |
]. |
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
169 |
index := index + 1 |
216 | 170 |
] |
171 |
! ! |
|
172 |
||
609 | 173 |
!WeakIdentitySet methodsFor:'private'! |
94 | 174 |
|
609 | 175 |
keyContainerOfSize:n |
176 |
"return a container for keys and values of size n. |
|
177 |
use WeakArrays here." |
|
359 | 178 |
|
609 | 179 |
|w| |
180 |
||
181 |
w := WeakArray new:n. |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
182 |
w addDependent:self. |
609 | 183 |
^ w |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
184 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2026
diff
changeset
|
185 |
"Modified: 7.1.1997 / 17:01:28 / stefan" |
94 | 186 |
! ! |
609 | 187 |
|
636 | 188 |
!WeakIdentitySet class methodsFor:'documentation'! |
189 |
||
190 |
version |
|
2102
7a0e81ed345b
dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents:
2091
diff
changeset
|
191 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.22 1997-01-09 00:32:33 cg Exp $' |
636 | 192 |
! ! |