WeakIdentityDictionary.st
author Claus Gittinger <cg@exept.de>
Thu, 30 Jan 1997 15:07:11 +0100
changeset 2329 937fc97d5544
parent 2315 a9ff2fda8bae
child 2738 2546e06df154
permissions -rw-r--r--
must always nil empty slots in #findKeyOrNil: (to keep reimplementors of add: / at:put: in subclasses of set happy)

"
 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.
"

IdentityDictionary subclass:#WeakIdentityDictionary
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Unordered'
!

!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

    |val|

    (OperatingSystem blockInterrupts) ifTrue:[
        "/ already blocked
        ^ super at:key.
    ].

    [
        val := super at:key.
    ] valueNowOrOnUnwindDo:[
        OperatingSystem unblockInterrupts.
    ].
    ^ val

    "Modified: 6.5.1996 / 12:22:26 / stefan"
    "Modified: 29.1.1997 / 15:07:44 / cg"
!

at:key ifAbsent:exceptionBlock

    |val|

    (OperatingSystem blockInterrupts) ifTrue:[
        "/ already blocked
        ^ super at:key ifAbsent:exceptionBlock.
    ].

    [
        val := super at:key ifAbsent:exceptionBlock.
    ] valueNowOrOnUnwindDo:[
        OperatingSystem unblockInterrupts.
    ].
    ^ val

    "Modified: 6.5.1996 / 12:22:26 / stefan"
    "Modified: 29.1.1997 / 15:08:07 / 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.
    ] valueNowOrOnUnwindDo:[
        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
    ] valueNowOrOnUnwindDo:[
        OperatingSystem unblockInterrupts
    ].
    ^ ret

    "Modified: 6.5.1996 / 12:44:51 / stefan"
    "Modified: 29.1.1997 / 15:09:11 / cg"
! !

!WeakIdentityDictionary methodsFor:'element disposal'!

update:something with:aParameter from:changedObject
    "an element (either key or value) died - clear out slots for
     disposed keys."

    |wasBlocked|

    something == #ElementExpired ifTrue:[
        "
         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.

        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
    ]

    "Created: 7.1.1997 / 16:59:30 / stefan"
! !

!WeakIdentityDictionary methodsFor:'private'!

findKeyOrNil:key
    "Look for the key in the receiver.  
     If it is found, return 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:'oops - unblocked call of findKeyOrNil'.
        ^ nil "/ leads to another error, if proceeded
    ].

    delIndex := 0.

    length := keyArray basicSize.
    index := key identityHash.
    index < 16r1FFFFFFF ifTrue:[
        index := index * 2
    ].
    index := index \\ length + 1.
    startIndex := index.

    [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"
!

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
    ] valueNowOrOnUnwindDo:[
        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.
    ].

    [
        super rehash
    ] valueNowOrOnUnwindDo:[
        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.
    ].

    [
        super setTally:count
    ] valueNowOrOnUnwindDo:[
        OperatingSystem unblockInterrupts
    ].

    "Created: 29.1.1997 / 11:40:12 / cg"
    "Modified: 29.1.1997 / 15:11:11 / cg"
! !

!WeakIdentityDictionary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.29 1997-01-30 14:07:11 cg Exp $'
! !