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 |
"
|
88
|
38 |
this is a special class to support keeping track of dependents without
|
|
39 |
keeping the values alive - values simply become nil when no one else
|
|
40 |
references it. The original dependency mechanism used a regular Dictionary,
|
|
41 |
which usually leads to a lot of garbage being kept due to a forgotten
|
|
42 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much
|
|
43 |
more comfortable, since no manual release of dependents is needed.
|
10
|
44 |
"
|
|
45 |
! !
|
|
46 |
|
359
|
47 |
!WeakIdentityDictionary methodsFor:'adding & removing'!
|
|
48 |
|
|
49 |
at:key put:value
|
|
50 |
|wasBlocked|
|
|
51 |
|
|
52 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
53 |
super at:key put:value.
|
1223
|
54 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
55 |
^ value
|
|
56 |
|
|
57 |
"Modified: 19.4.1996 / 11:40:42 / cg"
|
359
|
58 |
!
|
|
59 |
|
|
60 |
removeKey:aKey ifAbsent:aBlock
|
1054
|
61 |
"redefined to avoid synchronization problems, in case
|
|
62 |
of interrupts
|
|
63 |
(otherwise, there could be some other operation on the receiver
|
|
64 |
done by another process, which garbles my contents)"
|
|
65 |
|
359
|
66 |
|wasBlocked|
|
|
67 |
|
|
68 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
69 |
[
|
1054
|
70 |
super removeKey:aKey ifAbsent:aBlock
|
359
|
71 |
] valueNowOrOnUnwindDo:[
|
1054
|
72 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
|
359
|
73 |
]
|
1054
|
74 |
|
|
75 |
"Modified: 1.3.1996 / 21:16:29 / cg"
|
359
|
76 |
! !
|
|
77 |
|
10
|
78 |
!WeakIdentityDictionary methodsFor:'element disposal'!
|
|
79 |
|
|
80 |
informDispose
|
|
81 |
"an element (either key or value) died - rehash"
|
6
|
82 |
|
10
|
83 |
| sz "{ Class:SmallInteger }"
|
358
|
84 |
any wasBlocked |
|
|
85 |
|
|
86 |
"
|
|
87 |
have to block here - dispose may be done at a low priority
|
|
88 |
from the background finalizer. If new views are opened in
|
|
89 |
the foreground, the dependency dictionary may get corrupted
|
|
90 |
otherwise
|
|
91 |
"
|
|
92 |
wasBlocked := OperatingSystem blockInterrupts.
|
10
|
93 |
|
|
94 |
sz := keyArray size.
|
|
95 |
any := false.
|
|
96 |
1 to:sz do:[:index |
|
358
|
97 |
(keyArray at:index) isNil ifTrue:[
|
|
98 |
(valueArray at:index) notNil ifTrue:[
|
95
|
99 |
"/ soon to come; can then avoid rehash
|
359
|
100 |
"/ "
|
|
101 |
"/ if the next slot is not nil, it could be there due
|
|
102 |
"/ to a hash collision. In this case we have to put a
|
|
103 |
"/ DeletedMark into the slot.
|
|
104 |
"/ "
|
95
|
105 |
"/
|
|
106 |
"/ index == sz ifTrue:[
|
|
107 |
"/ next := 1
|
|
108 |
"/ ] ifFalse:[
|
|
109 |
"/ next := index + 1.
|
|
110 |
"/ ].
|
|
111 |
"/
|
|
112 |
"/ (keyArray basicAt:next) notNil ifTrue:[
|
|
113 |
"/ (valueArray basicAt:next) notNil ifTrue:[
|
|
114 |
"/ keyArray basicAt:index put:DeletedEntry
|
358
|
115 |
"/ ]
|
95
|
116 |
"/ ].
|
|
117 |
|
358
|
118 |
valueArray at:index put:nil.
|
|
119 |
tally := tally - 1.
|
|
120 |
any := true
|
|
121 |
]
|
|
122 |
]
|
10
|
123 |
].
|
358
|
124 |
any ifTrue:[self rehash].
|
|
125 |
|
359
|
126 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
6
|
127 |
! !
|
609
|
128 |
|
|
129 |
!WeakIdentityDictionary methodsFor:'private'!
|
|
130 |
|
|
131 |
keyContainerOfSize:n
|
|
132 |
"return a container for keys of size n.
|
|
133 |
use WeakArrays here."
|
|
134 |
|
|
135 |
|w|
|
|
136 |
|
|
137 |
w := WeakArray new:n.
|
|
138 |
w watcher:self.
|
|
139 |
^ w
|
|
140 |
! !
|
|
141 |
|
635
|
142 |
!WeakIdentityDictionary class methodsFor:'documentation'!
|
|
143 |
|
|
144 |
version
|
1223
|
145 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.15 1996-04-19 10:56:05 cg Exp $'
|
635
|
146 |
! !
|