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
|
609
|
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 |
"
|
609
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.11 1995-11-23 01:26:15 cg Exp $'
|
94
|
45 |
! !
|
|
46 |
|
111
|
47 |
!WeakIdentitySet class methodsFor:'instance creation'!
|
|
48 |
|
|
49 |
new
|
|
50 |
"return a new empty Set"
|
|
51 |
|
|
52 |
^ self new:1
|
|
53 |
! !
|
|
54 |
|
609
|
55 |
!WeakIdentitySet class methodsFor:'queries'!
|
|
56 |
|
|
57 |
goodSizeFrom:arg
|
|
58 |
"return a good array size for the given argument.
|
|
59 |
Since WeakIdentitySets are mostly used for dependency management, we typically
|
|
60 |
have only one element in the set. Therefore use exact size for small sets
|
|
61 |
(instead of rounding up to the prime 11)."
|
|
62 |
|
|
63 |
arg <= 10 ifTrue:[
|
|
64 |
arg < 1 ifTrue:[^ 1].
|
|
65 |
^ arg.
|
|
66 |
].
|
|
67 |
^ super goodSizeFrom:arg
|
|
68 |
! !
|
|
69 |
|
359
|
70 |
!WeakIdentitySet methodsFor:'adding & removing'!
|
|
71 |
|
|
72 |
add:newElement
|
|
73 |
"redefined to avoid synchronization promlems, in case
|
|
74 |
of interrupts
|
|
75 |
(otherwise, there could be some other operation on the receiver
|
|
76 |
done by another process, which garbles my contents)"
|
|
77 |
|
|
78 |
|wasBlocked|
|
|
79 |
|
|
80 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
81 |
super add:newElement.
|
|
82 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
|
|
83 |
!
|
|
84 |
|
|
85 |
remove:anObject ifAbsent:exceptionBlock
|
|
86 |
"redefined to avoid synchronization promlems, in case
|
|
87 |
of interrupts
|
|
88 |
(otherwise, there could be some other operation on the receiver
|
|
89 |
done by another process, which garbles my contents)"
|
|
90 |
|
|
91 |
|wasBlocked|
|
|
92 |
|
|
93 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
94 |
[
|
|
95 |
super remove:anObject ifAbsent:exceptionBlock
|
|
96 |
] valueNowOrOnUnwindDo:[
|
|
97 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
|
|
98 |
]
|
|
99 |
! !
|
|
100 |
|
609
|
101 |
!WeakIdentitySet methodsFor:'element disposal'!
|
94
|
102 |
|
609
|
103 |
informDispose
|
|
104 |
"an element died - must rehash"
|
|
105 |
|
|
106 |
|wasBlocked|
|
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.
|
|
115 |
self rehash.
|
|
116 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
94
|
117 |
! !
|
|
118 |
|
216
|
119 |
!WeakIdentitySet methodsFor:'enumerating'!
|
|
120 |
|
|
121 |
do:aBlock
|
|
122 |
"perform the block for all members in the collection.
|
|
123 |
This method has been rewritten to be more robust about
|
|
124 |
changed contents while enumerating elements (which might
|
|
125 |
happen during change&update processing, if dependents
|
|
126 |
are added or removed within the update."
|
|
127 |
|
|
128 |
|index "{ Class: SmallInteger }"
|
|
129 |
element|
|
|
130 |
|
|
131 |
index := 1.
|
|
132 |
[index <= keyArray size] whileTrue:[
|
|
133 |
element := keyArray at:index.
|
|
134 |
(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
|
|
135 |
aBlock value:element
|
|
136 |
].
|
|
137 |
index := index + 1
|
|
138 |
]
|
|
139 |
! !
|
|
140 |
|
609
|
141 |
!WeakIdentitySet methodsFor:'private'!
|
94
|
142 |
|
609
|
143 |
keyContainerOfSize:n
|
|
144 |
"return a container for keys and values of size n.
|
|
145 |
use WeakArrays here."
|
359
|
146 |
|
609
|
147 |
|w|
|
|
148 |
|
|
149 |
w := WeakArray new:n.
|
|
150 |
w watcher:self.
|
|
151 |
^ w
|
94
|
152 |
! !
|
609
|
153 |
|