6
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
358
|
3 |
All Rights Reserved
|
6
|
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 |
IdentityDictionary subclass:#WeakIdentityDictionary
|
1054
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
6
|
18 |
!
|
|
19 |
|
10
|
20 |
!WeakIdentityDictionary class methodsFor:'documentation'!
|
|
21 |
|
88
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
358
|
25 |
All Rights Reserved
|
88
|
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 |
|
10
|
36 |
documentation
|
|
37 |
"
|
1263
|
38 |
WeakIdentityDictionaries behave like IdentityDictionaries,
|
|
39 |
as long as the contained objects are still referenced by some
|
|
40 |
other (non-weak) object.
|
|
41 |
However, once the last non-weak reference ceases to exist,
|
|
42 |
the object will be automatically removed from the Weakcollection
|
|
43 |
(with some delay: it will be removed after the next garbage collect).
|
|
44 |
|
|
45 |
This class was added to support keeping track of dependents without
|
88
|
46 |
keeping the values alive - values simply become nil when no one else
|
|
47 |
references it. The original dependency mechanism used a regular Dictionary,
|
|
48 |
which usually leads to a lot of garbage being kept due to a forgotten
|
|
49 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much
|
|
50 |
more comfortable, since no manual release of dependents is needed.
|
1263
|
51 |
|
|
52 |
[See also:]
|
|
53 |
WeakArray WeakIdentitySet
|
10
|
54 |
"
|
|
55 |
! !
|
|
56 |
|
359
|
57 |
!WeakIdentityDictionary methodsFor:'adding & removing'!
|
|
58 |
|
1256
|
59 |
at:key put:anObject
|
|
60 |
"add the argument anObject under key, aKey to the receiver.
|
|
61 |
Return anObject (sigh).
|
|
62 |
Redefined to block interrupts, to avoid triuble when dependencies
|
|
63 |
are added within interrupting high prio processes."
|
|
64 |
|
359
|
65 |
|wasBlocked|
|
|
66 |
|
|
67 |
wasBlocked := OperatingSystem blockInterrupts.
|
1256
|
68 |
super at:key put:anObject.
|
1223
|
69 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
1256
|
70 |
^ anObject
|
1223
|
71 |
|
1256
|
72 |
"Modified: 22.4.1996 / 17:39:09 / cg"
|
359
|
73 |
!
|
|
74 |
|
|
75 |
removeKey:aKey ifAbsent:aBlock
|
1256
|
76 |
"remove the association under aKey from the collection,
|
|
77 |
return the value previously stored there..
|
|
78 |
If it was not in the collection return the result
|
|
79 |
from evaluating aBlock.
|
|
80 |
|
|
81 |
Redefined to avoid synchronization problems, in case
|
|
82 |
of interrupts (otherwise, there could be some other operation
|
|
83 |
on the receiver done by another process, which garbles my contents)."
|
1054
|
84 |
|
359
|
85 |
|wasBlocked|
|
|
86 |
|
|
87 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
88 |
[
|
1054
|
89 |
super removeKey:aKey ifAbsent:aBlock
|
359
|
90 |
] valueNowOrOnUnwindDo:[
|
1054
|
91 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
|
359
|
92 |
]
|
1054
|
93 |
|
1256
|
94 |
"Modified: 22.4.1996 / 17:39:57 / cg"
|
359
|
95 |
! !
|
|
96 |
|
10
|
97 |
!WeakIdentityDictionary methodsFor:'element disposal'!
|
|
98 |
|
|
99 |
informDispose
|
|
100 |
"an element (either key or value) died - rehash"
|
6
|
101 |
|
10
|
102 |
| sz "{ Class:SmallInteger }"
|
358
|
103 |
any wasBlocked |
|
|
104 |
|
|
105 |
"
|
|
106 |
have to block here - dispose may be done at a low priority
|
|
107 |
from the background finalizer. If new views are opened in
|
|
108 |
the foreground, the dependency dictionary may get corrupted
|
|
109 |
otherwise
|
|
110 |
"
|
|
111 |
wasBlocked := OperatingSystem blockInterrupts.
|
10
|
112 |
|
|
113 |
sz := keyArray size.
|
|
114 |
any := false.
|
|
115 |
1 to:sz do:[:index |
|
358
|
116 |
(keyArray at:index) isNil ifTrue:[
|
|
117 |
(valueArray at:index) notNil ifTrue:[
|
95
|
118 |
"/ soon to come; can then avoid rehash
|
359
|
119 |
"/ "
|
|
120 |
"/ if the next slot is not nil, it could be there due
|
|
121 |
"/ to a hash collision. In this case we have to put a
|
|
122 |
"/ DeletedMark into the slot.
|
|
123 |
"/ "
|
95
|
124 |
"/
|
|
125 |
"/ index == sz ifTrue:[
|
|
126 |
"/ next := 1
|
|
127 |
"/ ] ifFalse:[
|
|
128 |
"/ next := index + 1.
|
|
129 |
"/ ].
|
|
130 |
"/
|
|
131 |
"/ (keyArray basicAt:next) notNil ifTrue:[
|
|
132 |
"/ (valueArray basicAt:next) notNil ifTrue:[
|
|
133 |
"/ keyArray basicAt:index put:DeletedEntry
|
358
|
134 |
"/ ]
|
95
|
135 |
"/ ].
|
|
136 |
|
358
|
137 |
valueArray at:index put:nil.
|
|
138 |
tally := tally - 1.
|
|
139 |
any := true
|
|
140 |
]
|
|
141 |
]
|
10
|
142 |
].
|
358
|
143 |
any ifTrue:[self rehash].
|
|
144 |
|
359
|
145 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
6
|
146 |
! !
|
609
|
147 |
|
|
148 |
!WeakIdentityDictionary methodsFor:'private'!
|
|
149 |
|
|
150 |
keyContainerOfSize:n
|
|
151 |
"return a container for keys of size n.
|
|
152 |
use WeakArrays here."
|
|
153 |
|
|
154 |
|w|
|
|
155 |
|
|
156 |
w := WeakArray new:n.
|
|
157 |
w watcher:self.
|
|
158 |
^ w
|
|
159 |
! !
|
|
160 |
|
635
|
161 |
!WeakIdentityDictionary class methodsFor:'documentation'!
|
|
162 |
|
|
163 |
version
|
1263
|
164 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.17 1996-04-23 11:56:31 cg Exp $'
|
635
|
165 |
! !
|