WeakIdentityDictionary.st
branchjv
changeset 20244 20922299fd44
parent 20205 03e626304d06
parent 20237 855a12ebe6f9
child 21024 8734987eb5c7
--- a/WeakIdentityDictionary.st	Thu Aug 11 06:44:08 2016 +0200
+++ b/WeakIdentityDictionary.st	Fri Aug 12 06:44:59 2016 +0200
@@ -1,5 +1,3 @@
-"{ Encoding: utf8 }"
-
 "
  COPYRIGHT (c) 1992 by Claus Gittinger
 	      All Rights Reserved
@@ -318,6 +316,68 @@
     "Modified: 1.10.1997 / 11:25:32 / stefan"
 !
 
+findKeyOrNilOrDeletedEntry:key
+    "Look for the key in the receiver.
+     If it is found, return the index,
+     otherwise the index of the first unused slot.
+     Grow the receiver, if key was not found, and no unused slots were present."
+
+    |index  "{ Class:SmallInteger }"
+     length "{ Class:SmallInteger }"
+     startIndex probe
+     delIndex "{ Class:SmallInteger }"|
+
+    (OperatingSystem blockInterrupts) ifFalse:[
+        "/
+        "/ may never be entered with interrupts enabled
+        "/
+        OperatingSystem unblockInterrupts.
+        self error:'unblocked call of findKeyOrNil'.
+    ].
+
+    delIndex := 0.
+
+    length := keyArray basicSize.
+    startIndex := index := self initialIndexForKey:key.
+
+    [
+        probe := keyArray basicAt:index.
+        key == probe ifTrue:[^ index].
+        probe isNil ifTrue:[
+            delIndex == 0 ifTrue:[^ index].
+            ^ delIndex
+        ].
+
+        probe class == SmallInteger ifTrue:[
+            probe := DeletedEntry.
+            keyArray basicAt:index put:probe.
+            valueArray basicAt:index put:nil.
+            tally := tally - 1.
+        ].
+
+        (delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[
+            delIndex := index
+        ].
+
+        index == length ifTrue:[
+            index := 1
+        ] ifFalse:[
+            index := index + 1
+        ].
+        index == startIndex ifTrue:[
+            delIndex ~~ 0 ifTrue:[
+                ^ delIndex
+            ].
+            self grow.
+            length := keyArray basicSize.
+            startIndex := index := self initialIndexForKey:key.
+        ].
+    ] loop.
+
+    "Modified: 30.1.1997 / 15:04:34 / cg"
+    "Modified: 1.10.1997 / 11:25:32 / stefan"
+!
+
 grow:newSize
     "grow the receiver.
      Redefined to block interrupts, to avoid trouble when dependencies