WeakIdentitySet.st
author Claus Gittinger <cg@exept.de>
Fri, 24 Jan 1997 21:54:29 +0100
changeset 2260 96d898c2874d
parent 2102 7a0e81ed345b
child 2303 f19df2d4c238
permissions -rw-r--r--
doku

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

IdentitySet subclass:#WeakIdentitySet
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Unordered'
!

!WeakIdentitySet class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1994 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
"
    WeakIdentitySets behave like IdentitySets, as long as the contained
    objects 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 dependencies which do not
    prevent objects from dying. (i.e. which do not fill up your memory
    if you forget to #release it).

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

!WeakIdentitySet class methodsFor:'instance creation'!

new
    "return a new empty WeakIdentitySet"

    ^ self new:1

    "Modified: 23.4.1996 / 13:56:08 / cg"
! !

!WeakIdentitySet class methodsFor:'queries'!

goodSizeFrom:arg
    "return a good array size for the given argument.
     Since WeakIdentitySets are mostly used for dependency management, we typically
     have only one element in the set. Therefore use exact size for small sets
     (instead of rounding up to the prime 11)."

    arg <= 10 ifTrue:[
	arg < 1 ifTrue:[^ 1].
	^ arg.
    ].
    ^ super goodSizeFrom:arg
! !

!WeakIdentitySet methodsFor:'adding & removing'!

add:newElement 
    "redefined to avoid synchronization promlems, in case
     of interrupts 
     (otherwise, there could be some other operation on the receiver
       done by another process, which garbles my contents)"

    |wasBlocked|

    wasBlocked := OperatingSystem blockInterrupts.
    [
        super add:newElement.
    ] valueNowOrOnUnwindDo:[
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
    ]

    "Modified: 20.10.1996 / 14:04:29 / cg"
!

remove:anObject ifAbsent:exceptionBlock
    "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)"

    |wasBlocked|

    wasBlocked := OperatingSystem blockInterrupts.
    [
        super remove:anObject ifAbsent:exceptionBlock
    ] valueNowOrOnUnwindDo:[
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
    ]

    "Modified: 1.3.1996 / 21:16:10 / cg"
! !

!WeakIdentitySet methodsFor:'element disposal'!

update:something with:aParameter from:changedObject
    "an element died - must rehash"

    |wasBlocked|

    something == #ElementExpired ifTrue:[
        "
         must block interrupts here - finalization
         may be done at low prio in the background
         finalizer, we do not want to be interrupted
         while rehashing
        "
        wasBlocked := OperatingSystem blockInterrupts.
        keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry.
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
    ].

    "Created: 7.1.1997 / 17:00:33 / stefan"
! !

!WeakIdentitySet methodsFor:'enumerating'!

do:aBlock
    "perform the block for all members in the collection.
     This method has been rewritten to be more robust about
     changed contents while enumerating elements (which might
     happen during change&update processing, if dependents
     are added or removed within the update."

    |index "{ Class: SmallInteger }"
     element|

    index := 1.
    [index <= keyArray size] whileTrue:[
        element := keyArray at:index.
        element == 0 ifFalse:[
            (element notNil and:[element ~~ DeletedEntry]) ifTrue:[
                aBlock value:element
            ]
        ] ifTrue:[

"/ disabled, since we had to lock out interrupts here.
"/ The entry is cleared anyway, when the next finalization round
"/ is handled.

"/            keyArray at:index put:DeletedEntry.
"/            tally := tally - 1.
        ].
        index := index + 1
    ]
! !

!WeakIdentitySet methodsFor:'private'!

keyContainerOfSize:n
    "return a container for keys and values of size n.
     use WeakArrays here."

    |w|

    w := WeakArray new:n.
    w addDependent:self.
    ^ w

    "Modified: 7.1.1997 / 17:01:28 / stefan"
! !

!WeakIdentitySet class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.23 1997-01-24 20:54:09 cg Exp $'
! !