Dictionary.st
changeset 3215 dbb2ee7a5cb9
parent 3192 47bf17818a88
child 3252 da066eafc2c5
--- a/Dictionary.st	Thu Jan 22 14:32:15 1998 +0100
+++ b/Dictionary.st	Fri Jan 23 18:28:57 1998 +0100
@@ -198,6 +198,48 @@
     ^ exceptionBlock value.
 !
 
+at:aKey ifAbsentPut:newValue
+    "return the element indexed by aKey if present,
+     if not present, store newValue under aKey and return it.
+     WARNING: do not add elements while iterating over the receiver.
+              Iterate over a copy to do this."
+
+    |index "{ Class: SmallInteger }"
+     oldValue|
+
+    aKey isNil ifTrue:[
+        "nil is not allowed as key"
+        self errorInvalidKey:aKey
+    ] ifFalse:[
+        newValue notNil ifTrue:[
+            index := self findKeyOrNil:aKey.
+            (keyArray basicAt:index) notNil ifTrue:[
+                "/ already present
+                ^ valueArray at:index.
+            ].
+            "/ a new one
+            keyArray basicAt:index put:aKey.
+            valueArray basicAt:index put:newValue.
+            tally := tally + 1.
+
+            self fullCheck.
+        ].
+    ].
+    ^ newValue
+
+    "
+     |d|
+
+     d := Dictionary new.
+     Transcript showCR:(d at:'foo' ifAbsentPut:'bar').
+     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2').
+     Transcript showCR:(d at:'foo' ifAbsentPut:'barX').
+     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2X').
+    "
+
+    "Created: / 23.1.1998 / 18:28:26 / cg"
+!
+
 at:aKey put:anObject
     "add the argument anObject under key, aKey to the receiver.
      Return anObject (sigh).
@@ -1335,5 +1377,5 @@
 !Dictionary class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.53 1998-01-20 18:12:10 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.54 1998-01-23 17:28:57 cg Exp $'
 ! !