WeakIdentitySet.st
changeset 2303 f19df2d4c238
parent 2260 96d898c2874d
child 2304 05f621e5d674
equal deleted inserted replaced
2302:259d1b9326ed 2303:f19df2d4c238
    72 !WeakIdentitySet class methodsFor:'queries'!
    72 !WeakIdentitySet class methodsFor:'queries'!
    73 
    73 
    74 goodSizeFrom:arg
    74 goodSizeFrom:arg
    75     "return a good array size for the given argument.
    75     "return a good array size for the given argument.
    76      Since WeakIdentitySets are mostly used for dependency management, we typically
    76      Since WeakIdentitySets are mostly used for dependency management, we typically
    77      have only one element in the set. Therefore use exact size for small sets
    77      have only a small number of elements in the set. 
       
    78      Therefore use exact size for small sets
    78      (instead of rounding up to the prime 11)."
    79      (instead of rounding up to the prime 11)."
    79 
    80 
    80     arg <= 10 ifTrue:[
    81     arg <= 10 ifTrue:[
    81 	arg < 1 ifTrue:[^ 1].
    82 	arg < 1 ifTrue:[^ 1].
    82 	^ arg.
    83 	^ arg.
    83     ].
    84     ].
    84     ^ super goodSizeFrom:arg
    85     ^ super goodSizeFrom:arg
    85 ! !
    86 ! !
    86 
    87 
       
    88 !WeakIdentitySet methodsFor:'accessing'!
       
    89 
       
    90 firstIfEmpty:exceptionValue
       
    91     "return the first element of the collection or the
       
    92      value of the exceptionBlock, if empty.
       
    93      Redefine, since the inherited method does not work if
       
    94      elements change silently to nil"
       
    95 
       
    96     |index "{ Class: SmallInteger }"
       
    97      element|
       
    98 
       
    99     index := 1.
       
   100     [index <= keyArray size] whileTrue:[
       
   101         element := keyArray basicAt:index.
       
   102         element notNil ifTrue:[
       
   103             element ~~ 0 ifTrue:[
       
   104                 element ~~ DeletedEntry ifTrue:[
       
   105                     ^ element
       
   106                 ]
       
   107             ]
       
   108         ].
       
   109         index := index + 1
       
   110     ].
       
   111 
       
   112     ^ exceptionValue value.
       
   113 ! !
       
   114 
    87 !WeakIdentitySet methodsFor:'adding & removing'!
   115 !WeakIdentitySet methodsFor:'adding & removing'!
    88 
   116 
    89 add:newElement 
   117 add:newElement 
    90     "redefined to avoid synchronization promlems, in case
   118     "redefined to avoid synchronization problems, in case of interrupts 
    91      of interrupts 
   119      (otherwise, there could be some other operation on the receiver
       
   120       done by another process, which garbles my contents)"
       
   121 
       
   122     |ret|
       
   123 
       
   124     (OperatingSystem blockInterrupts) ifTrue:[
       
   125         "/ already blocked
       
   126         ^ super add:newElement.
       
   127     ].
       
   128 
       
   129     [
       
   130         ret := super add:newElement.
       
   131     ] valueNowOrOnUnwindDo:[
       
   132         OperatingSystem unblockInterrupts
       
   133     ].
       
   134     ^ ret
       
   135 
       
   136     "Modified: 29.1.1997 / 15:06:57 / cg"
       
   137 !
       
   138 
       
   139 remove:anObject ifAbsent:exceptionBlock
       
   140     "redefined to avoid synchronization problems, in case of interrupts 
    92      (otherwise, there could be some other operation on the receiver
   141      (otherwise, there could be some other operation on the receiver
    93        done by another process, which garbles my contents)"
   142        done by another process, which garbles my contents)"
    94 
   143 
    95     |wasBlocked|
   144     |ret|
    96 
   145 
    97     wasBlocked := OperatingSystem blockInterrupts.
   146     (OperatingSystem blockInterrupts) ifTrue:[
       
   147         "/ already blocked
       
   148         ^ super remove:anObject ifAbsent:exceptionBlock.
       
   149     ].
       
   150 
    98     [
   151     [
    99         super add:newElement.
   152         ret := super remove:anObject ifAbsent:exceptionBlock
   100     ] valueNowOrOnUnwindDo:[
   153     ] valueNowOrOnUnwindDo:[
   101         wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
   154         OperatingSystem unblockInterrupts
   102     ]
   155     ].
   103 
   156     ^ ret
   104     "Modified: 20.10.1996 / 14:04:29 / cg"
   157 
   105 !
   158     "Modified: 29.1.1997 / 15:07:19 / cg"
   106 
       
   107 remove:anObject ifAbsent:exceptionBlock
       
   108     "redefined to avoid synchronization problems, in case
       
   109      of interrupts 
       
   110      (otherwise, there could be some other operation on the receiver
       
   111        done by another process, which garbles my contents)"
       
   112 
       
   113     |wasBlocked|
       
   114 
       
   115     wasBlocked := OperatingSystem blockInterrupts.
       
   116     [
       
   117         super remove:anObject ifAbsent:exceptionBlock
       
   118     ] valueNowOrOnUnwindDo:[
       
   119         wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
       
   120     ]
       
   121 
       
   122     "Modified: 1.3.1996 / 21:16:10 / cg"
       
   123 ! !
   159 ! !
   124 
   160 
   125 !WeakIdentitySet methodsFor:'element disposal'!
   161 !WeakIdentitySet methodsFor:'element disposal'!
   126 
   162 
   127 update:something with:aParameter from:changedObject
   163 update:something with:aParameter from:changedObject
   156     |index "{ Class: SmallInteger }"
   192     |index "{ Class: SmallInteger }"
   157      element|
   193      element|
   158 
   194 
   159     index := 1.
   195     index := 1.
   160     [index <= keyArray size] whileTrue:[
   196     [index <= keyArray size] whileTrue:[
   161         element := keyArray at:index.
   197         element := keyArray basicAt:index.
   162         element == 0 ifFalse:[
   198 	element notNil ifTrue:[
   163             (element notNil and:[element ~~ DeletedEntry]) ifTrue:[
   199             element ~~ 0 ifTrue:[
   164                 aBlock value:element
   200                 element ~~ DeletedEntry ifTrue:[
       
   201                     aBlock value:element
       
   202 		]
   165             ]
   203             ]
   166         ] ifTrue:[
       
   167 
       
   168 "/ disabled, since we had to lock out interrupts here.
       
   169 "/ The entry is cleared anyway, when the next finalization round
       
   170 "/ is handled.
       
   171 
       
   172 "/            keyArray at:index put:DeletedEntry.
       
   173 "/            tally := tally - 1.
       
   174         ].
   204         ].
   175         index := index + 1
   205         index := index + 1
   176     ]
   206     ]
   177 ! !
   207 ! !
   178 
   208 
   192 ! !
   222 ! !
   193 
   223 
   194 !WeakIdentitySet class methodsFor:'documentation'!
   224 !WeakIdentitySet class methodsFor:'documentation'!
   195 
   225 
   196 version
   226 version
   197     ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.23 1997-01-24 20:54:09 cg Exp $'
   227     ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.24 1997-01-29 14:28:49 cg Exp $'
   198 ! !
   228 ! !