#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Thu, 21 Jul 2016 10:02:03 +0200
changeset 20163 3855e97fe259
parent 20162 b9a62e66a27d
child 20164 7333519ac040
#REFACTORING by stefan class: WeakValueDictionary added: #do: #keysAndValuesDo: #values #valuesAs: changed: #includesKey: #possiblyShrink
WeakValueDictionary.st
--- a/WeakValueDictionary.st	Tue Jul 19 17:32:02 2016 +0200
+++ b/WeakValueDictionary.st	Thu Jul 21 10:02:03 2016 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1992 by Claus Gittinger
 	      All Rights Reserved
@@ -59,6 +61,30 @@
 "
 ! !
 
+!WeakValueDictionary methodsFor:'accessing'!
+
+values
+    "return a (non-weak) collection of my values"
+
+    |values|
+
+    valueArray size == 0 ifTrue:[^ #()].
+    values := OrderedCollection new:self size.
+    valueArray validElementsDo:[:v | values add:v].
+    ^ values
+!
+
+valuesAs:collectionClass
+    "return a (non-weak) collection (of type collectionClass) of my values"
+
+    |values|
+
+    valueArray size == 0 ifTrue:[^ collectionClass new].
+    values := collectionClass new:self size.
+    valueArray validElementsDo:[:v | values add:v].
+    ^ values
+! !
+
 !WeakValueDictionary methodsFor:'adding & removing'!
 
 at:key ifAbsent:somethingRespondingToValue
@@ -202,6 +228,26 @@
     "Created: 7.1.1997 / 16:59:30 / stefan"
 ! !
 
+!WeakValueDictionary methodsFor:'enumerating'!
+
+do:aBlock
+    super do:[:eachValue|
+        "garbage collected values will change to a SmallInteger"
+        eachValue class ~~ SmallInteger ifTrue:[
+            aBlock value:eachValue.
+        ].
+    ].
+!
+
+keysAndValuesDo:aBlock
+    super keysAndValuesDo:[:eachKey :eachValue|
+        "garbage collected values will change to a SmallInteger"
+        eachValue class ~~ SmallInteger ifTrue:[
+            aBlock value:eachKey value:eachValue.
+        ].
+    ].
+! !
+
 !WeakValueDictionary methodsFor:'private'!
 
 clearDeadSlots
@@ -232,8 +278,10 @@
      and shrink if it makes sense.
      Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'"
 
-    self clearDeadSlots.
-    super possiblyShrink
+    keyArray basicSize > 56 ifTrue:[
+        self clearDeadSlots.
+        super possiblyShrink.
+    ].
 !
 
 valueContainerOfSize:n
@@ -268,15 +316,9 @@
 !
 
 includesKey:key
-    "redefined to block interrupts
-     (avoid change of the dictionary while accessing)"
-
-    |val|
+    "redefined to check for already collected values"
 
-    [
-	val := super includesKey:key.
-    ] valueUninterruptably.
-    ^ val
+    ^ (self at:key ifAbsent:0) ~~ 0.
 
     "Modified: 6.5.1996 / 12:22:26 / stefan"
     "Modified: 1.7.1997 / 10:45:52 / cg"