Add methods for diagnosis
authorStefan Vogel <sv@exept.de>
Mon, 30 Aug 2004 18:48:46 +0200
changeset 8499 de9cfb15379a
parent 8498 854b2c98379e
child 8500 10d47cede03c
Add methods for diagnosis
Set.st
--- a/Set.st	Mon Aug 30 18:44:27 2004 +0200
+++ b/Set.st	Mon Aug 30 18:48:46 2004 +0200
@@ -982,6 +982,74 @@
 
 !Set methodsFor:'queries'!
 
+collisionCount
+    "return the number of key collisions in the set.
+     There is a collision if two keys hash to the same value."
+
+    |count|
+
+    count := 0.
+
+    keyArray do:[:eachKey|
+        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
+            count := count + (self collisionsFor:eachKey).
+        ]
+    ].
+
+    ^ count
+!
+
+collisionsFor:key
+    "Return the number of searches - 1 required for key"
+
+    |index  "{ Class:SmallInteger }"
+     length "{ Class:SmallInteger }"
+     hash "{ Class:SmallInteger }"
+     step "{ Class:SmallInteger }"
+     startIndex probe count|
+
+    length := keyArray basicSize.
+    hash := (self hashFor:key) bitAnd:16r3FFFFFFF.
+    hash < 16r1FFFFFFF ifTrue:[
+        hash := hash * 2
+    ].
+    startIndex := index := hash \\ length + 1.
+    step:= hash // length + 1.
+
+    count := 0.
+    [true] whileTrue:[
+        probe := (keyArray basicAt:index).
+        (probe notNil and:[key = probe]) ifTrue:[^ count].
+        (self slotIsEmpty:probe) ifTrue:[self error:'non existing key'].
+
+        index == length ifTrue:[
+            index := 1.
+        ] ifFalse:[
+            index := index + 1.
+        ].
+        count := count + 1.
+        index == startIndex ifTrue:[self error:'non existing key'].
+    ]
+!
+
+maxChainLength
+    "return the number of the maximum chain length in the set.
+     This is the worst case overhead when accessing a key."
+
+    |chainLength key|
+
+    chainLength := 0.
+
+    keyArray do:[:eachKey| |t|
+        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
+            t := self collisionsFor:eachKey.
+            chainLength < t ifTrue:[chainLength := t. key := eachKey].
+        ]
+    ].
+
+    ^ (key -> chainLength)
+!
+
 size
     "return the number of set elements"
 
@@ -1099,7 +1167,7 @@
 !Set class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.89 2004-06-11 18:10:46 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.90 2004-08-30 16:48:46 stefan Exp $'
 ! !
 
 Set initialize!