New: #testAndAdd:
authorStefan Vogel <sv@exept.de>
Mon, 04 Jun 2007 09:26:44 +0200
changeset 10588 7043b70ff150
parent 10587 d0e5f934141a
child 10589 47f2edd1ffc6
New: #testAndAdd:
Set.st
--- a/Set.st	Thu May 31 18:28:08 2007 +0200
+++ b/Set.st	Mon Jun 04 09:26:44 2007 +0200
@@ -433,6 +433,34 @@
 
     "Created: / 1.3.1996 / 21:14:26 / cg"
     "Modified: / 16.11.2001 / 10:22:59 / cg"
+!
+
+testAndAdd:anObject
+    "add the argument, anObject to the receiver.
+     Answer true, if the element did already exist in the collection,
+     false otherwise.
+
+     WARNING: do not add elements while iterating over the receiver.
+              Iterate over a copy to do this."
+
+    |index "{ Class: SmallInteger }"|
+
+    anObject isNil ifTrue:[
+        ^ self invalidElementError.
+    ].
+
+    index := self findKeyOrNil:anObject.
+    (keyArray basicAt:index) isNil ifTrue:[
+        "/ not already there
+        keyArray basicAt:index put:anObject.
+        tally := tally + 1.
+
+        self fullCheck.
+        ^ false.
+    ].
+    ^ true.
+
+    "Modified: 30.1.1997 / 14:58:08 / cg"
 ! !
 
 
@@ -988,6 +1016,7 @@
     ^ tally
 ! !
 
+
 !Set methodsFor:'testing'!
 
 capacity 
@@ -1089,7 +1118,7 @@
 !Set class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.99 2006-10-13 11:02:18 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.100 2007-06-04 07:26:44 stefan Exp $'
 ! !
 
 Set initialize!