#FEATURE by stefan
authorStefan Vogel <sv@exept.de>
Tue, 03 Jul 2018 23:39:14 +0200
changeset 23190 611aa626a4a4
parent 23189 15ea23cfead5
child 23191 957e811d8a44
#FEATURE by stefan class: OrderedSet added: #addOrReplace:
OrderedSet.st
--- a/OrderedSet.st	Tue Jul 03 23:38:44 2018 +0200
+++ b/OrderedSet.st	Tue Jul 03 23:39:14 2018 +0200
@@ -221,6 +221,43 @@
     "
 !
 
+addOrReplace:anObject 
+    "Add the argument, anObject to the receiver.
+     If it is already included, replace it by anObject.
+     Return nil, if anObject was not present in the receiver, 
+     otherwise the element that has been replaced.
+
+     Also, remember in the order (i.e. add to the end)
+     If anObject is already present in the set,
+     the order will not be changed."
+
+    |oldObject|
+
+    oldObject := super addOrReplace:anObject.
+    oldObject ~~ anObject ifTrue:[
+        oldObject isNil ifTrue:[
+            order add:anObject.
+        ] ifFalse:[
+            order replaceAll:oldObject with:anObject.
+        ].
+    ].
+    ^ oldObject
+
+    "
+        Note that 1 is replaced by 1.0, but 1.0 is still at the beginning:
+
+        self new 
+                addOrReplace:1;
+                addOrReplace:2;
+                addOrReplace:nil;
+                addOrReplace:1.0;
+                yourself
+    "
+
+    "Created: / 03-07-2018 / 19:12:07 / Stefan Vogel"
+    "Modified (comment): / 03-07-2018 / 23:39:04 / Stefan Vogel"
+!
+
 clearContents
     "remove all elements from the receiver, but do not shrink. Returns the receiver."