WeakIdentitySet.st
changeset 609 12be97f6d5a7
parent 530 07d0bce293c9
child 636 295267cdb5d5
equal deleted inserted replaced
608:cd5ac440fa95 609:12be97f6d5a7
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 IdentitySet subclass:#WeakIdentitySet
    13 IdentitySet subclass:#WeakIdentitySet
    14        instanceVariableNames:''
    14 	 instanceVariableNames:''
    15        classVariableNames:''
    15 	 classVariableNames:''
    16        poolDictionaries:''
    16 	 poolDictionaries:''
    17        category:'Collections-Unordered'
    17 	 category:'Collections-Unordered'
    18 !
    18 !
    19 
    19 
    20 !WeakIdentitySet class methodsFor:'documentation'!
    20 !WeakIdentitySet class methodsFor:'documentation'!
    21 
    21 
    22 copyright
    22 copyright
    31  other person.  No title to or ownership of the software is
    31  other person.  No title to or ownership of the software is
    32  hereby transferred.
    32  hereby transferred.
    33 "
    33 "
    34 !
    34 !
    35 
    35 
    36 version
       
    37     ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.10 1995-11-11 15:28:39 cg Exp $'
       
    38 !
       
    39 
       
    40 documentation
    36 documentation
    41 "
    37 "
    42     this is a special class to support dependencies which do not
    38     this is a special class to support dependencies which do not
    43     prevent objects from dying.
    39     prevent objects from dying.
    44 "
    40 "
       
    41 !
       
    42 
       
    43 version
       
    44     ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.11 1995-11-23 01:26:15 cg Exp $'
    45 ! !
    45 ! !
    46 
    46 
    47 !WeakIdentitySet class methodsFor:'instance creation'!
    47 !WeakIdentitySet class methodsFor:'instance creation'!
    48 
    48 
    49 new
    49 new
    50     "return a new empty Set"
    50     "return a new empty Set"
    51 
    51 
    52     ^ self new:1
    52     ^ self new:1
       
    53 ! !
       
    54 
       
    55 !WeakIdentitySet class methodsFor:'queries'!
       
    56 
       
    57 goodSizeFrom:arg
       
    58     "return a good array size for the given argument.
       
    59      Since WeakIdentitySets are mostly used for dependency management, we typically
       
    60      have only one element in the set. Therefore use exact size for small sets
       
    61      (instead of rounding up to the prime 11)."
       
    62 
       
    63     arg <= 10 ifTrue:[
       
    64 	arg < 1 ifTrue:[^ 1].
       
    65 	^ arg.
       
    66     ].
       
    67     ^ super goodSizeFrom:arg
    53 ! !
    68 ! !
    54 
    69 
    55 !WeakIdentitySet methodsFor:'adding & removing'!
    70 !WeakIdentitySet methodsFor:'adding & removing'!
    56 
    71 
    57 add:newElement 
    72 add:newElement 
    81     ] valueNowOrOnUnwindDo:[
    96     ] valueNowOrOnUnwindDo:[
    82 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
    97 	wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
    83     ]
    98     ]
    84 ! !
    99 ! !
    85 
   100 
    86 !WeakIdentitySet class methodsFor:'queries'!
   101 !WeakIdentitySet methodsFor:'element disposal'!
    87 
   102 
    88 goodSizeFrom:arg
   103 informDispose
    89     "return a good array size for the given argument.
   104     "an element died - must rehash"
    90      Since WeakIdentitySets are mostly used for dependency management, we typically
       
    91      have only one element in the set. Therefore use exact size for small sets
       
    92      (instead of rounding up to the prime 11)."
       
    93 
   105 
    94     arg <= 10 ifTrue:[
   106     |wasBlocked|
    95 	arg < 1 ifTrue:[^ 1].
       
    96 	^ arg.
       
    97     ].
       
    98     ^ super goodSizeFrom:arg
       
    99 ! !
       
   100 
   107 
   101 !WeakIdentitySet methodsFor:'private'!
   108     "
   102 
   109      must block interrupts here - finalization
   103 keyContainerOfSize:n
   110      may be done at low prio in the background
   104     "return a container for keys and values of size n.
   111      finalizer, we do not want to be interrupted
   105      use WeakArrays here."
   112      while rehashing
   106 
   113     "
   107     |w|
   114     wasBlocked := OperatingSystem blockInterrupts.
   108 
   115     self rehash.
   109     w := WeakArray new:n.
   116     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
   110     w watcher:self.
       
   111     ^ w
       
   112 ! !
   117 ! !
   113 
   118 
   114 !WeakIdentitySet methodsFor:'enumerating'!
   119 !WeakIdentitySet methodsFor:'enumerating'!
   115 
   120 
   116 do:aBlock
   121 do:aBlock
   131 	].
   136 	].
   132 	index := index + 1
   137 	index := index + 1
   133     ]
   138     ]
   134 ! !
   139 ! !
   135 
   140 
   136 !WeakIdentitySet methodsFor:'element disposal'!
   141 !WeakIdentitySet methodsFor:'private'!
   137 
   142 
   138 informDispose
   143 keyContainerOfSize:n
   139     "an element died - must rehash"
   144     "return a container for keys and values of size n.
       
   145      use WeakArrays here."
   140 
   146 
   141     |wasBlocked|
   147     |w|
   142 
   148 
   143     "
   149     w := WeakArray new:n.
   144      must block interrupts here - finalization
   150     w watcher:self.
   145      may be done at low prio in the background
   151     ^ w
   146      finalizer, we do not want to be interrupted
       
   147      while rehashing
       
   148     "
       
   149     wasBlocked := OperatingSystem blockInterrupts.
       
   150     self rehash.
       
   151     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
   152 ! !
   152 ! !
       
   153