OrderedDictionary.st
changeset 19274 f7e6e2169194
parent 19106 cfeb2d0d1ee4
child 19736 184be3ec2f62
--- a/OrderedDictionary.st	Tue Mar 01 21:35:08 2016 +0100
+++ b/OrderedDictionary.st	Tue Mar 01 21:35:22 2016 +0100
@@ -177,6 +177,26 @@
     ^ order collect: [:key | self associationAt: key ].
 !
 
+at:aKey ifAbsent:default update:aBlock 
+    "update the element stored under aKey with the result from 
+     evaluating aBlock with the previous stored value as argument, or with default,
+     if there was no such key initially."
+
+    ^ self at:aKey put:(aBlock value:(self at:aKey ifAbsent:default)).
+
+    "
+     |d|
+
+     d := OrderedDictionary new.
+     d at:'one'  ifAbsent:0 update:[:val | val + 1].
+     d at:'two'  ifAbsent:0 update:[:val | val + 1].
+     d at:'three' ifAbsent:0  update:[:val | val + 1].
+     d at:'one' ifAbsent:0  update:[:val | val + 1].
+     d at:'two' ifAbsent:0  update:[:val | val + 1].
+     d
+    "
+!
+
 at:aKey ifAbsentPut:valueBlock
     ^ self at:aKey ifAbsent:[self at:aKey put:valueBlock value]
 !
@@ -191,6 +211,41 @@
     ^ super at:key put:anObject
 !
 
+at:aKey put:aValue ifPresent:aBlock
+    |v|
+
+    v := self at:aKey ifAbsent:[^ self at:aKey put:aValue].
+    ^ aBlock value:v.
+
+    "
+     |d|
+
+     d := OrderedDictionary new.
+     d at:'foo' put:1234 ifPresent:[:v| self error: 'duplicate: ', v printString ].
+     d at:'foo' put:1234 ifPresent:[:v| self halt:'duplicate: ', v printString. 5555 ].
+    "
+
+
+
+
+
+
+
+
+
+
+
+
+
+    "
+     |d|
+
+     d := Dictionary new.
+     d at:'foo' put:1234 ifPresent:[:v| self error: 'duplicate: ', v printString ].
+     d at:'foo' put:1234 ifPresent:[:v| self halt:'duplicate; ', v printString. 5555 ].
+    "
+!
+
 atAll:indexCollection put: anObject 
     "Put anObject into the value field of every association specified by indexCollection,
      which is typically an interval."