WeakDependencyDictionary.st
author Claus Gittinger <cg@exept.de>
Mon, 27 Jan 1997 15:55:16 +0100
changeset 2279 ef0f7f5216c4
parent 2261 61096f935f76
child 2297 118f794c4e42
permissions -rw-r--r--
oops - irqBlockingstate was corrupted

"
 COPYRIGHT (c) 1997 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.
"


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

!WeakDependencyDictionary class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 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
"
    A specialized WeakIdentityDictionary, which 'knowns' how
    to get rid of obsolete entries. This is only used with the
    dependency mechanism.

    [author:]
        Claus Gittinger

    [See also:]
        WeakArray WeakIdentityDictionary WeakValueDictionary WeakIdentitySet
"
! !

!WeakDependencyDictionary methodsFor:'special dependency support'!

removeEmptyDependencyValues
    "special entry for dependency management:
     remove any empty (due to finalization) value WeakArray elements."

    |index t wasBlocked deps o|

    "/ careful: this is sent by the finalizer at low prio.
    "/ be prepared for the receiver to change while we walk over
    "/ the value array here ...

    index := 1.
    [index <= keyArray size] whileTrue:[
        "/ get the size again - it could have changed

        wasBlocked := OperatingSystem blockInterrupts.
        index <= keyArray size ifTrue:[
            deps := valueArray basicAt:index.
            deps notNil ifTrue:[
                "/ is it an empty WeakArray ?

                (deps isMemberOf:WeakArray) ifTrue:[
                    t := deps findFirst:[:el | el notNil and:[el ~~ 0]].
                    t == 0 ifTrue:[
                        "/ yes - nil it
                        valueArray basicAt:index put:nil.
                        (keyArray basicAt:index) notNil ifTrue:[
                            keyArray basicAt:index put:DeletedEntry.
                            tally := tally - 1.
                        ]
                    ]
                ] ifFalse:[
                   "/ is it an empty WeakIdSet ?

                   (deps isMemberOf:WeakIdentitySet) ifTrue:[
                        (t := deps size) == 0 ifTrue:[
                            "/ yes - nil it
                            valueArray basicAt:index put:nil.
                            (keyArray basicAt:index) notNil ifTrue:[
                                keyArray basicAt:index put:DeletedEntry.
                                tally := tally - 1.
                            ]
                        ] ifFalse:[
                            t == 1 ifTrue:[
                                "/ careful - it could actually be empty
                                o := deps firstIfEmpty:nil.
                                o notNil ifTrue:[
                                    "/ the set lost an object, and shrunk to size 1
                                    "/ can now use a WeakArray
                                    valueArray basicAt:index put:(WeakArray with:o)
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ].

        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
        index := index + 1.
    ].

    "
     Dependencies removeEmptyDependencyValues
    "

    "Created: 9.1.1997 / 00:00:28 / cg"
    "Modified: 27.1.1997 / 15:54:28 / cg"
! !

!WeakDependencyDictionary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/WeakDependencyDictionary.st,v 1.4 1997-01-27 14:55:16 cg Exp $'
! !