"
COPYRIGHT (c) 1992 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
"{ Package: 'stx:libbasic' }"
IdentityDictionary subclass:#WeakIdentityDictionary
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'Collections-Weak'
!
!WeakIdentityDictionary class methodsFor:'documentation'!
copyright
"
COPYRIGHT (c) 1992 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
!
documentation
"
WeakIdentityDictionaries behave like IdentityDictionaries,
as long as the keys are still referenced by some
other (non-weak) object.
However, once the last non-weak reference ceases to exist,
the object will be automatically removed from the Weakcollection
(with some delay: it will be removed after the next garbage collect).
This class was added to support keeping track of dependents without
keeping the values alive - values simply become nil when no one else
references it. The original dependency mechanism used a regular Dictionary,
which usually leads to a lot of garbage being kept due to a forgotten
release. Using a WeakDictionary may be incompatible to ST-80 but is much
more comfortable, since no manual release of dependents is needed.
[Warning:]
If you use this, be very careful since the collections size changes
'magically' - for example, testing for being nonEmpty and then
removing the first element may fail, since the element may vanish inbetween.
In general, never trust the value as returned by the size/isEmpty messages.
[author:]
Claus Gittinger
[See also:]
WeakArray WeakValueDictionary WeakIdentitySet
"
! !
!WeakIdentityDictionary methodsFor:'adding & removing'!
at:key ifAbsent:exceptionBlock
"redefined to block interrupts
(avoid change of the dictionary while accessing)"
|val|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super at:key ifAbsent:exceptionBlock.
].
[
val := super at:key ifAbsent:exceptionBlock.
] ensure:[
OperatingSystem unblockInterrupts.
].
^ val
"Modified: 6.5.1996 / 12:22:26 / stefan"
"Modified: 1.7.1997 / 10:45:47 / cg"
!
at:key put:anObject
"add the argument anObject under key, aKey to the receiver.
Return anObject (sigh).
Redefined to block interrupts, to avoid trouble when dependencies
are added within interrupting high prio processes."
|val|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super at:key put:anObject.
].
[
val := super at:key put:anObject.
] ensure:[
OperatingSystem unblockInterrupts.
].
^ val
"Modified: 6.5.1996 / 12:22:26 / stefan"
"Modified: 29.1.1997 / 15:08:45 / cg"
!
removeKey:aKey ifAbsent:aBlock
"remove the association under aKey from the collection,
return the value previously stored there.
If it was not in the collection return the result
from evaluating aBlock.
Redefined to avoid synchronization problems, in case
of interrupts (otherwise, there could be some other operation
on the receiver done by another process, which garbles my contents)."
|ret|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super removeKey:aKey ifAbsent:aBlock.
].
[
ret := super removeKey:aKey ifAbsent:aBlock
] ensure:[
OperatingSystem unblockInterrupts
].
^ ret
"Modified: 6.5.1996 / 12:44:51 / stefan"
"Modified: 29.1.1997 / 15:09:11 / cg"
!
saveRemoveKey:key
"redefined to block interrupts
(avoid change of the dictionary while accessing)"
|val|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super saveRemoveKey:key.
].
[
val := super saveRemoveKey:key.
] ensure:[
OperatingSystem unblockInterrupts.
].
^ val
"Modified: 6.5.1996 / 12:22:26 / stefan"
"Modified: 1.7.1997 / 10:45:52 / cg"
"Created: 1.7.1997 / 10:46:34 / cg"
! !
!WeakIdentityDictionary methodsFor:'element disposal'!
clearDeadSlots
|wasBlocked|
"
have to block here - dispose may be done at a low priority
from the background finalizer. If new items are added by a
higher prio process, the dictionary might get corrupted otherwise
"
wasBlocked := OperatingSystem blockInterrupts.
[
keyArray
forAllDeadIndicesDo:[:idx |
valueArray basicAt:idx put:nil.
tally := tally - 1.
]
replacingCorpsesWith:DeletedEntry.
] ensure:[
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
].
"Modified: / 13.12.2001 / 14:18:17 / martin"
!
update:something with:aParameter from:changedObject
"an element (either key or value) died - clear out slots for
disposed keys."
something == #ElementExpired ifTrue:[
self clearDeadSlots.
]
"Created: / 7.1.1997 / 16:59:30 / stefan"
"Modified: / 13.12.2001 / 14:17:58 / martin"
! !
!WeakIdentityDictionary methodsFor:'private'!
findKeyOrNil:key
"Look for the key in the receiver.
If it is found, return the index,
otherwise the index of the first unused slot.
Grow the receiver, if key was not found, and no unused slots were present.
Warning: an empty slot MUST be filled by the sender - it is only to be sent
by at:put: / add: - like methods."
|index "{ Class:SmallInteger }"
length "{ Class:SmallInteger }"
startIndex probe
delIndex "{ Class:SmallInteger }"|
(OperatingSystem blockInterrupts) ifFalse:[
"/
"/ may never be entered with interrupts enabled
"/
OperatingSystem unblockInterrupts.
self error:'unblocked call of findKeyOrNil'.
].
delIndex := 0.
length := keyArray basicSize.
startIndex := index := self initialIndexForKey:key.
[true] whileTrue:[
probe := keyArray basicAt:index.
key == probe ifTrue:[^ index].
probe isNil ifTrue:[
delIndex == 0 ifTrue:[^ index].
keyArray basicAt:delIndex put:nil.
^ delIndex
].
probe == 0 ifTrue:[
probe := DeletedEntry.
keyArray basicAt:index put:probe.
valueArray basicAt:index put:nil.
tally := tally - 1.
].
delIndex == 0 ifTrue:[
probe == DeletedEntry ifTrue:[
delIndex := index
]
].
index == length ifTrue:[
index := 1
] ifFalse:[
index := index + 1
].
index == startIndex ifTrue:[
delIndex ~~ 0 ifTrue:[
keyArray basicAt:delIndex put:nil.
^ delIndex
].
^ self grow findKeyOrNil:key
].
]
"Modified: 30.1.1997 / 15:04:34 / cg"
"Modified: 1.10.1997 / 11:25:32 / stefan"
!
grow:newSize
"grow the receiver.
Redefined to block interrupts, to avoid trouble when dependencies
are added within interrupting high prio processes."
"/ 'grow:' printCR.
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super grow:newSize.
].
[
super grow:newSize
] ensure:[
OperatingSystem unblockInterrupts
].
"Created: 28.1.1997 / 23:41:39 / cg"
"Modified: 29.1.1997 / 15:10:12 / cg"
!
keyContainerOfSize:n
"return a container for keys of size n.
use WeakArrays here, but dont make me a depenent of it."
|w|
w := WeakArray new:n.
w addDependent:self.
^ w
"Modified: 7.1.1997 / 17:01:15 / stefan"
"Modified: 29.1.1997 / 14:18:49 / cg"
!
rehash
"grow the receiver.
Redefined to block interrupts, to avoid trouble when dependencies
are added within interrupting high prio processes."
"/ 'rehash' printCR.
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
super rehash.
^ self.
].
[
super rehash
] ensure:[
OperatingSystem unblockInterrupts
].
"Created: 29.1.1997 / 11:39:42 / cg"
"Modified: 29.1.1997 / 14:18:52 / cg"
!
setTally:count
"grow the receiver.
Redefined to block interrupts, to avoid trouble when dependencies
are added within interrupting high prio processes."
"/ 'setTally:' printCR.
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
super setTally:count.
^ self.
].
[
super setTally:count
] ensure:[
OperatingSystem unblockInterrupts
].
"Created: 29.1.1997 / 11:40:12 / cg"
"Modified: 29.1.1997 / 15:11:11 / cg"
! !
!WeakIdentityDictionary methodsFor:'testing'!
includes:anObject
"redefined to block interrupts
(avoid change of the dictionary while accessing)"
|val|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super includes:anObject.
].
[
val := super includes:anObject.
] ensure:[
OperatingSystem unblockInterrupts.
].
^ val
"Modified: 6.5.1996 / 12:22:26 / stefan"
"Modified: 1.7.1997 / 10:45:52 / cg"
"Created: 1.7.1997 / 10:47:13 / cg"
!
includesKey:key
"redefined to block interrupts
(avoid change of the dictionary while accessing)"
|val|
(OperatingSystem blockInterrupts) ifTrue:[
"/ already blocked
^ super includesKey:key.
].
[
val := super includesKey:key.
] ensure:[
OperatingSystem unblockInterrupts.
].
^ val
"Modified: 6.5.1996 / 12:22:26 / stefan"
"Created: 1.7.1997 / 10:45:14 / cg"
"Modified: 1.7.1997 / 10:45:52 / cg"
!
isWeakCollection
"return true, if the receiver has weak references to its elements."
^ true
! !
!WeakIdentityDictionary class methodsFor:'documentation'!
version
^ '$Id: WeakIdentityDictionary.st 10447 2009-06-14 13:09:55Z vranyj1 $'
! !