author | ca |
Thu, 02 Jan 1997 19:09:36 +0100 | |
changeset 2026 | 654cba4db794 |
parent 1790 | 4187e9fc7357 |
child 2091 | c11bb3e29a1b |
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 |
|
609 | 121 |
informDispose |
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 |
|
609 | 126 |
" |
127 |
must block interrupts here - finalization |
|
128 |
may be done at low prio in the background |
|
129 |
finalizer, we do not want to be interrupted |
|
130 |
while rehashing |
|
131 |
" |
|
132 |
wasBlocked := OperatingSystem blockInterrupts. |
|
2026
654cba4db794
dont forget to count down the tally when objects are collected.
ca
parents:
1790
diff
changeset
|
133 |
keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry. |
609 | 134 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
94 | 135 |
! ! |
136 |
||
216 | 137 |
!WeakIdentitySet methodsFor:'enumerating'! |
138 |
||
139 |
do:aBlock |
|
140 |
"perform the block for all members in the collection. |
|
141 |
This method has been rewritten to be more robust about |
|
142 |
changed contents while enumerating elements (which might |
|
143 |
happen during change&update processing, if dependents |
|
144 |
are added or removed within the update." |
|
145 |
||
146 |
|index "{ Class: SmallInteger }" |
|
147 |
element| |
|
148 |
||
149 |
index := 1. |
|
150 |
[index <= keyArray size] whileTrue:[ |
|
151 |
element := keyArray at:index. |
|
1780
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
152 |
element == 0 ifTrue:[ |
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
153 |
keyArray at:index put:DeletedEntry. |
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
154 |
] ifFalse:[ |
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
155 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
156 |
aBlock value:element |
1b3a4ddc5b94
changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
157 |
] |
216 | 158 |
]. |
159 |
index := index + 1 |
|
160 |
] |
|
161 |
! ! |
|
162 |
||
609 | 163 |
!WeakIdentitySet methodsFor:'private'! |
94 | 164 |
|
609 | 165 |
keyContainerOfSize:n |
166 |
"return a container for keys and values of size n. |
|
167 |
use WeakArrays here." |
|
359 | 168 |
|
609 | 169 |
|w| |
170 |
||
171 |
w := WeakArray new:n. |
|
172 |
w watcher:self. |
|
173 |
^ w |
|
94 | 174 |
! ! |
609 | 175 |
|
636 | 176 |
!WeakIdentitySet class methodsFor:'documentation'! |
177 |
||
178 |
version |
|
2026
654cba4db794
dont forget to count down the tally when objects are collected.
ca
parents:
1790
diff
changeset
|
179 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.20 1997-01-02 18:09:36 ca Exp $' |
636 | 180 |
! ! |