#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Tue, 05 Mar 2019 12:51:38 +0100
changeset 23842 15d3d668d963
parent 23841 f592db8411ac
child 23843 770c583538ec
#REFACTORING by stefan class: Dictionary added: #copyWithout: moved from OrdereDictionary
Dictionary.st
--- a/Dictionary.st	Tue Mar 05 12:50:49 2019 +0100
+++ b/Dictionary.st	Tue Mar 05 12:51:38 2019 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1991 by Claus Gittinger
 	      All Rights Reserved
@@ -1756,6 +1758,55 @@
     "
 !
 
+copyWithout:anAssociation 
+    "Return a copy of the dictionary that is 1 smaller than the receiver and 
+     does not include the argument, anAssociation
+     No error is reported, if elementToSkip is not in the collection."
+
+    |newDict keyToIgnore valueToIgnore|
+
+    "assume, that one element will be removed. If not,
+     we might to have to grow the Dictionary."
+    newDict := self species new:self size - 1.
+
+    keyToIgnore := anAssociation key.
+    valueToIgnore := anAssociation value.
+    self keysAndValuesDo:[:k :v |
+        (keyToIgnore = k and:[valueToIgnore = v]) ifFalse:[
+            newDict at:k put:v
+        ]
+    ].
+    ^ newDict.
+
+    "
+     |d d2|
+
+     d := Dictionary new
+            at:1 put:'1';
+            at:2 put:'2';
+            at:3 put:'3';
+            at:4 put:'4';
+            at:5 put:'5';
+            yourself.
+     d2 := d copyWithout:(4->'4').
+     d2   
+
+     |d d2|
+
+     d := OrderedDictionary new
+            at:1 put:'1';
+            at:2 put:'2';
+            at:3 put:'3';
+            at:4 put:'4';
+            at:5 put:'5';
+            yourself.
+     d2 := d copyWithout:(4->'4').
+     d2      
+    "
+
+    "Created: / 05-03-2019 / 12:41:54 / Stefan Vogel"
+!
+
 postCopy
     "have to copy the valueArray too"