author | ca |
Sat, 20 Apr 1996 11:28:14 +0200 | |
changeset 1232 | 25b756de5c51 |
parent 1054 | 62ce964f1d2e |
child 1263 | 92c1db6b0776 |
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 |
" |
|
38 |
this is a special class to support dependencies which do not |
|
39 |
prevent objects from dying. |
|
40 |
" |
|
41 |
! ! |
|
42 |
||
111 | 43 |
!WeakIdentitySet class methodsFor:'instance creation'! |
44 |
||
45 |
new |
|
46 |
"return a new empty Set" |
|
47 |
||
48 |
^ self new:1 |
|
49 |
! ! |
|
50 |
||
609 | 51 |
!WeakIdentitySet class methodsFor:'queries'! |
52 |
||
53 |
goodSizeFrom:arg |
|
54 |
"return a good array size for the given argument. |
|
55 |
Since WeakIdentitySets are mostly used for dependency management, we typically |
|
56 |
have only one element in the set. Therefore use exact size for small sets |
|
57 |
(instead of rounding up to the prime 11)." |
|
58 |
||
59 |
arg <= 10 ifTrue:[ |
|
60 |
arg < 1 ifTrue:[^ 1]. |
|
61 |
^ arg. |
|
62 |
]. |
|
63 |
^ super goodSizeFrom:arg |
|
64 |
! ! |
|
65 |
||
359 | 66 |
!WeakIdentitySet methodsFor:'adding & removing'! |
67 |
||
68 |
add:newElement |
|
69 |
"redefined to avoid synchronization promlems, in case |
|
70 |
of interrupts |
|
71 |
(otherwise, there could be some other operation on the receiver |
|
72 |
done by another process, which garbles my contents)" |
|
73 |
||
74 |
|wasBlocked| |
|
75 |
||
76 |
wasBlocked := OperatingSystem blockInterrupts. |
|
77 |
super add:newElement. |
|
78 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
|
79 |
! |
|
80 |
||
81 |
remove:anObject ifAbsent:exceptionBlock |
|
1054 | 82 |
"redefined to avoid synchronization problems, in case |
359 | 83 |
of interrupts |
84 |
(otherwise, there could be some other operation on the receiver |
|
85 |
done by another process, which garbles my contents)" |
|
86 |
||
87 |
|wasBlocked| |
|
88 |
||
89 |
wasBlocked := OperatingSystem blockInterrupts. |
|
90 |
[ |
|
1054 | 91 |
super remove:anObject ifAbsent:exceptionBlock |
359 | 92 |
] valueNowOrOnUnwindDo:[ |
1054 | 93 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
359 | 94 |
] |
1054 | 95 |
|
96 |
"Modified: 1.3.1996 / 21:16:10 / cg" |
|
359 | 97 |
! ! |
98 |
||
609 | 99 |
!WeakIdentitySet methodsFor:'element disposal'! |
94 | 100 |
|
609 | 101 |
informDispose |
102 |
"an element died - must rehash" |
|
103 |
||
1232
25b756de5c51
oops - disposal left tally with wrong value (if deletedObjects are present)
ca
parents:
1054
diff
changeset
|
104 |
|wasBlocked cnt deleted| |
25b756de5c51
oops - disposal left tally with wrong value (if deletedObjects are present)
ca
parents:
1054
diff
changeset
|
105 |
|
25b756de5c51
oops - disposal left tally with wrong value (if deletedObjects are present)
ca
parents:
1054
diff
changeset
|
106 |
deleted := DeletedEntry. |
111 | 107 |
|
609 | 108 |
" |
109 |
must block interrupts here - finalization |
|
110 |
may be done at low prio in the background |
|
111 |
finalizer, we do not want to be interrupted |
|
112 |
while rehashing |
|
113 |
" |
|
114 |
wasBlocked := OperatingSystem blockInterrupts. |
|
924
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
115 |
|
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
116 |
"/ |
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
117 |
"/ recompute my tally |
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
118 |
"/ |
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
119 |
cnt := 0. |
1232
25b756de5c51
oops - disposal left tally with wrong value (if deletedObjects are present)
ca
parents:
1054
diff
changeset
|
120 |
keyArray nonNilElementsDo:[:el | el ~~ deleted ifTrue:[cnt := cnt + 1]]. |
924
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
121 |
tally := cnt. |
5745a1132511
oops - size was not updated when elements were disposed (update tally)
Claus Gittinger <cg@exept.de>
parents:
636
diff
changeset
|
122 |
|
609 | 123 |
self rehash. |
124 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
94 | 125 |
! ! |
126 |
||
216 | 127 |
!WeakIdentitySet methodsFor:'enumerating'! |
128 |
||
129 |
do:aBlock |
|
130 |
"perform the block for all members in the collection. |
|
131 |
This method has been rewritten to be more robust about |
|
132 |
changed contents while enumerating elements (which might |
|
133 |
happen during change&update processing, if dependents |
|
134 |
are added or removed within the update." |
|
135 |
||
136 |
|index "{ Class: SmallInteger }" |
|
137 |
element| |
|
138 |
||
139 |
index := 1. |
|
140 |
[index <= keyArray size] whileTrue:[ |
|
141 |
element := keyArray at:index. |
|
142 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[ |
|
143 |
aBlock value:element |
|
144 |
]. |
|
145 |
index := index + 1 |
|
146 |
] |
|
147 |
! ! |
|
148 |
||
609 | 149 |
!WeakIdentitySet methodsFor:'private'! |
94 | 150 |
|
609 | 151 |
keyContainerOfSize:n |
152 |
"return a container for keys and values of size n. |
|
153 |
use WeakArrays here." |
|
359 | 154 |
|
609 | 155 |
|w| |
156 |
||
157 |
w := WeakArray new:n. |
|
158 |
w watcher:self. |
|
159 |
^ w |
|
94 | 160 |
! ! |
609 | 161 |
|
636 | 162 |
!WeakIdentitySet class methodsFor:'documentation'! |
163 |
||
164 |
version |
|
1232
25b756de5c51
oops - disposal left tally with wrong value (if deletedObjects are present)
ca
parents:
1054
diff
changeset
|
165 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.15 1996-04-20 09:28:14 ca Exp $' |
636 | 166 |
! ! |