WeakIdentitySet.st
changeset 2303 f19df2d4c238
parent 2260 96d898c2874d
child 2304 05f621e5d674
--- a/WeakIdentitySet.st	Wed Jan 29 15:27:21 1997 +0100
+++ b/WeakIdentitySet.st	Wed Jan 29 15:29:04 1997 +0100
@@ -74,7 +74,8 @@
 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
+     have only a small number of elements in the set. 
+     Therefore use exact size for small sets
      (instead of rounding up to the prime 11)."
 
     arg <= 10 ifTrue:[
@@ -84,42 +85,77 @@
     ^ super goodSizeFrom:arg
 ! !
 
+!WeakIdentitySet methodsFor:'accessing'!
+
+firstIfEmpty:exceptionValue
+    "return the first element of the collection or the
+     value of the exceptionBlock, if empty.
+     Redefine, since the inherited method does not work if
+     elements change silently to nil"
+
+    |index "{ Class: SmallInteger }"
+     element|
+
+    index := 1.
+    [index <= keyArray size] whileTrue:[
+        element := keyArray basicAt:index.
+        element notNil ifTrue:[
+            element ~~ 0 ifTrue:[
+                element ~~ DeletedEntry ifTrue:[
+                    ^ element
+                ]
+            ]
+        ].
+        index := index + 1
+    ].
+
+    ^ exceptionValue value.
+! !
+
 !WeakIdentitySet methodsFor:'adding & removing'!
 
 add:newElement 
-    "redefined to avoid synchronization promlems, in case
-     of interrupts 
+    "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)"
+
+    |ret|
+
+    (OperatingSystem blockInterrupts) ifTrue:[
+        "/ already blocked
+        ^ super add:newElement.
+    ].
+
+    [
+        ret := super add:newElement.
+    ] valueNowOrOnUnwindDo:[
+        OperatingSystem unblockInterrupts
+    ].
+    ^ ret
+
+    "Modified: 29.1.1997 / 15:06:57 / 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|
+    |ret|
 
-    wasBlocked := OperatingSystem blockInterrupts.
-    [
-        super add:newElement.
-    ] valueNowOrOnUnwindDo:[
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
-    ]
-
-    "Modified: 20.10.1996 / 14:04:29 / cg"
-!
+    (OperatingSystem blockInterrupts) ifTrue:[
+        "/ already blocked
+        ^ super remove:anObject ifAbsent:exceptionBlock.
+    ].
 
-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|
+    [
+        ret := super remove:anObject ifAbsent:exceptionBlock
+    ] valueNowOrOnUnwindDo:[
+        OperatingSystem unblockInterrupts
+    ].
+    ^ ret
 
-    wasBlocked := OperatingSystem blockInterrupts.
-    [
-        super remove:anObject ifAbsent:exceptionBlock
-    ] valueNowOrOnUnwindDo:[
-        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
-    ]
-
-    "Modified: 1.3.1996 / 21:16:10 / cg"
+    "Modified: 29.1.1997 / 15:07:19 / cg"
 ! !
 
 !WeakIdentitySet methodsFor:'element disposal'!
@@ -158,19 +194,13 @@
 
     index := 1.
     [index <= keyArray size] whileTrue:[
-        element := keyArray at:index.
-        element == 0 ifFalse:[
-            (element notNil and:[element ~~ DeletedEntry]) ifTrue:[
-                aBlock value:element
+        element := keyArray basicAt:index.
+	element notNil ifTrue:[
+            element ~~ 0 ifTrue:[
+                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
     ]
@@ -194,5 +224,5 @@
 !WeakIdentitySet class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.23 1997-01-24 20:54:09 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.24 1997-01-29 14:28:49 cg Exp $'
 ! !