#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Thu, 11 Aug 2016 15:25:26 +0200
changeset 4024 626a498ce5bf
parent 4023 875963f1e601
child 4025 7d6791051cb5
#REFACTORING by stefan class: CacheDictionary added: #findKeyOrNilOrDeletedEntry:
CacheDictionary.st
--- a/CacheDictionary.st	Thu Aug 11 15:24:03 2016 +0200
+++ b/CacheDictionary.st	Thu Aug 11 15:25:26 2016 +0200
@@ -119,6 +119,57 @@
     "Modified (format): / 26-12-2011 / 10:42:08 / cg"
 !
 
+findKeyOrNilOrDeletedEntry:key  
+    "Look for the key in the receiver.  If it is found, return
+     the index of the association containing the key, otherwise
+     return the index of the first unused slot. If no empty slot
+     is available, make one empty (but never grow)."
+
+    |index  "{ Class:SmallInteger }"
+     length "{ Class:SmallInteger }"
+     startIndex probe
+     delIndex "{ Class:SmallInteger }"|
+
+    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].
+
+            valueArray basicAt:delIndex put:nil.
+            ^ delIndex
+        ].
+
+        (delIndex == 0 and:[probe == DeletedEntry]) ifTrue:[
+            delIndex := index
+        ].
+
+        index := index + 1.
+        index > length ifTrue:[
+            index := 1.
+        ].
+        index == startIndex ifTrue:[
+            "/ mhmh - actually, a kind of round-robin would be better
+            delIndex == 0 ifTrue:[
+                delIndex := startIndex
+            ].
+
+            valueArray basicAt:delIndex put:DeletedEntry.
+            valueArray basicAt:delIndex put:nil.
+            tally := tally - 1.
+            ^ delIndex
+        ].
+    ] loop.
+
+    "Modified: / 01-03-1997 / 00:59:55 / cg"
+    "Modified (format): / 26-12-2011 / 10:42:08 / cg"
+!
+
 possiblyGrow
     "redefined - never grow"