author | Stefan Vogel <sv@exept.de> |
Wed, 08 Jan 1997 20:42:17 +0100 | |
changeset 2091 | c11bb3e29a1b |
parent 2027 | 4a2cdf9946ae |
child 2260 | 96d898c2874d |
permissions | -rw-r--r-- |
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 |
||
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
13 |
'From Smalltalk/X, Version:3.1.3 on 7-jan-1997 at 17:23:20' ! |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
14 |
|
6 | 15 |
IdentityDictionary subclass:#WeakIdentityDictionary |
1054 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Unordered' |
|
6 | 20 |
! |
21 |
||
10 | 22 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
358 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
10 | 38 |
documentation |
39 |
" |
|
1263 | 40 |
WeakIdentityDictionaries behave like IdentityDictionaries, |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
41 |
as long as the keys are still referenced by some |
1263 | 42 |
other (non-weak) object. |
43 |
However, once the last non-weak reference ceases to exist, |
|
44 |
the object will be automatically removed from the Weakcollection |
|
45 |
(with some delay: it will be removed after the next garbage collect). |
|
46 |
||
47 |
This class was added to support keeping track of dependents without |
|
88 | 48 |
keeping the values alive - values simply become nil when no one else |
49 |
references it. The original dependency mechanism used a regular Dictionary, |
|
50 |
which usually leads to a lot of garbage being kept due to a forgotten |
|
51 |
release. Using a WeakDictionary may be incompatible to ST-80 but is much |
|
52 |
more comfortable, since no manual release of dependents is needed. |
|
1263 | 53 |
|
1290 | 54 |
[author:] |
55 |
Claus Gittinger |
|
56 |
||
1263 | 57 |
[See also:] |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
58 |
WeakArray WeakValueDictionary WeakIdentitySet |
10 | 59 |
" |
60 |
! ! |
|
61 |
||
359 | 62 |
!WeakIdentityDictionary methodsFor:'adding & removing'! |
63 |
||
1256 | 64 |
at:key put:anObject |
65 |
"add the argument anObject under key, aKey to the receiver. |
|
66 |
Return anObject (sigh). |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
67 |
Redefined to block interrupts, to avoid trouble when dependencies |
1256 | 68 |
are added within interrupting high prio processes." |
69 |
||
359 | 70 |
|wasBlocked| |
71 |
||
72 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1790 | 73 |
[ |
74 |
super at:key put:anObject. |
|
75 |
] valueNowOrOnUnwindDo:[ |
|
76 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
77 |
]. |
|
1256 | 78 |
^ anObject |
1223 | 79 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
80 |
"Modified: 6.5.1996 / 12:22:26 / stefan" |
1790 | 81 |
"Modified: 20.10.1996 / 14:04:46 / cg" |
359 | 82 |
! |
83 |
||
84 |
removeKey:aKey ifAbsent:aBlock |
|
1256 | 85 |
"remove the association under aKey from the collection, |
86 |
return the value previously stored there.. |
|
87 |
If it was not in the collection return the result |
|
88 |
from evaluating aBlock. |
|
89 |
||
90 |
Redefined to avoid synchronization problems, in case |
|
91 |
of interrupts (otherwise, there could be some other operation |
|
92 |
on the receiver done by another process, which garbles my contents)." |
|
1054 | 93 |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
94 |
|wasBlocked ret| |
359 | 95 |
|
96 |
wasBlocked := OperatingSystem blockInterrupts. |
|
97 |
[ |
|
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
98 |
ret := super removeKey:aKey ifAbsent:aBlock |
359 | 99 |
] valueNowOrOnUnwindDo:[ |
1054 | 100 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
101 |
]. |
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
102 |
^ ret |
1054 | 103 |
|
1256 | 104 |
"Modified: 22.4.1996 / 17:39:57 / cg" |
1343
b0ee705c63e0
Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents:
1290
diff
changeset
|
105 |
"Modified: 6.5.1996 / 12:44:51 / stefan" |
359 | 106 |
! ! |
107 |
||
10 | 108 |
!WeakIdentityDictionary methodsFor:'element disposal'! |
109 |
||
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
110 |
update:something with:aParameter from:changedObject |
10 | 111 |
"an element (either key or value) died - rehash" |
6 | 112 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
113 |
|wasBlocked| |
358 | 114 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
115 |
something == #ElementExpired ifTrue:[ |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
116 |
" |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
117 |
have to block here - dispose may be done at a low priority |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
118 |
from the background finalizer. If new views are opened in |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
119 |
the foreground, the dependency dictionary may get corrupted |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
120 |
otherwise |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
121 |
" |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
122 |
wasBlocked := OperatingSystem blockInterrupts. |
10 | 123 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
124 |
keyArray |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
125 |
forAllDeadIndicesDo:[:idx | |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
126 |
(valueArray at:idx) notNil ifTrue:[ |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
127 |
keyArray basicAt:idx put:DeletedEntry. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
128 |
valueArray basicAt:idx put:nil. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
129 |
tally := tally - 1. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
130 |
] |
2027
4a2cdf9946ae
replace items with DeletedEntry, when objects are collected.
ca
parents:
1790
diff
changeset
|
131 |
] |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
132 |
replacingCorpsesWith:DeletedEntry. |
358 | 133 |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
134 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
135 |
] |
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
136 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
137 |
"Created: 7.1.1997 / 16:59:30 / stefan" |
6 | 138 |
! ! |
609 | 139 |
|
140 |
!WeakIdentityDictionary methodsFor:'private'! |
|
141 |
||
142 |
keyContainerOfSize:n |
|
143 |
"return a container for keys of size n. |
|
144 |
use WeakArrays here." |
|
145 |
||
146 |
|w| |
|
147 |
||
148 |
w := WeakArray new:n. |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
149 |
w addDependent:self. |
609 | 150 |
^ w |
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
151 |
|
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
152 |
"Modified: 7.1.1997 / 17:01:15 / stefan" |
609 | 153 |
! ! |
154 |
||
635 | 155 |
!WeakIdentityDictionary class methodsFor:'documentation'! |
156 |
||
157 |
version |
|
2091
c11bb3e29a1b
Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents:
2027
diff
changeset
|
158 |
^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.23 1997-01-08 19:42:15 stefan Exp $' |
635 | 159 |
! ! |