WeakIdSet.st
author Claus Gittinger <cg@exept.de>
Thu, 21 Mar 1996 16:33:40 +0100
changeset 1113 840b03d131d7
parent 1054 62ce964f1d2e
child 1232 25b756de5c51
permissions -rw-r--r--
category changes; lock changeFileUpdates via a semaphore (for slow background checkins)

"
 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
"
    this is a special class to support dependencies which do not
    prevent objects from dying.
"
! !

!WeakIdentitySet class methodsFor:'instance creation'!

new
    "return a new empty Set"

    ^ self new:1
! !

!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.
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
!

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'!

informDispose
    "an element died - must rehash"

    |wasBlocked cnt|

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

    "/
    "/ recompute my tally
    "/
    cnt := 0.
    keyArray nonNilElementsDo:[:el | cnt := cnt + 1].
    tally := cnt.

    self rehash.
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
! !

!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 notNil and:[element ~~ DeletedEntry]) ifTrue:[
	    aBlock value:element
	].
	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 watcher:self.
    ^ w
! !

!WeakIdentitySet class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/WeakIdSet.st,v 1.14 1996-03-01 20:26:42 cg Exp $'
! !