# HG changeset patch # User Stefan Vogel # Date 1489747444 -3600 # Node ID 058cafb6c2f64240d083c4a6385526c604723f1a # Parent cd8f5a3cbdd676e59df2af0f72be5a4cba1cb97f #REFACTORING by stefan class: KeyedCollection added: #associationsDo: #inspectorClass #withAll: changed: #at:put:ifPresent: #withAssociations: #withKeysAndValues: diff -r cd8f5a3cbdd6 -r 058cafb6c2f6 KeyedCollection.st --- a/KeyedCollection.st Thu Mar 16 21:30:44 2017 +0100 +++ b/KeyedCollection.st Fri Mar 17 11:44:04 2017 +0100 @@ -1,3 +1,5 @@ +"{ Encoding: utf8 }" + " COPYRIGHT (c) 1998 by eXept Software AG All Rights Reserved @@ -59,13 +61,33 @@ !KeyedCollection class methodsFor:'instance creation'! +withAll:aDictionary + "create a MethodDictionary from another Dictionary" + + |newDict| + + newDict := self new:aDictionary size. + aDictionary keysAndValuesDo:[:key :value | + newDict at:key put:value. + ]. + ^ newDict + + " + |d| + + d := Dictionary withKeys:#(a b c d e) andValues:#(1 2 3 4 5). + KeyValueList withAll:d. + " + + "Created: / 17-03-2017 / 11:29:09 / stefan" +! + withAssociations:aCollectionOfAssociations "return a new instance where associations are taken from the argument" - |newDict sz "{ Class: SmallInteger }"| + |newDict| - sz := aCollectionOfAssociations size. - newDict := self new:sz. + newDict := self new:aCollectionOfAssociations size. aCollectionOfAssociations do:[:assoc | newDict at:assoc key put:assoc value ]. @@ -79,6 +101,7 @@ " "Created: / 16-03-2017 / 12:15:38 / stefan" + "Modified: / 17-03-2017 / 11:30:55 / stefan" ! withKeys:keyArray andValues:valueArray @@ -105,7 +128,7 @@ |newDict sz "{ Class: SmallInteger }"| sz := anArray size. - newDict := self new:(sz // 2). + newDict := self new:sz. 1 to:sz by:2 do:[:i | newDict at:(anArray at:i) put:(anArray at:i+1) ]. @@ -116,6 +139,7 @@ " "Created: / 16-03-2017 / 12:13:27 / stefan" + "Modified: / 17-03-2017 / 11:32:42 / stefan" ! ! !KeyedCollection class methodsFor:'queries'! @@ -241,7 +265,7 @@ |value isAbsent| - value := self at:aKey ifAbsent:[isAbsent := true. self at:aKey put:anObject]. + value := self at:aKey ifAbsentPut:[isAbsent := true. anObject]. isAbsent notNil ifTrue:[ ^ value. ]. @@ -249,6 +273,7 @@ ^ aBlock value:value. "Created: / 16-03-2017 / 17:38:00 / stefan" + "Modified: / 17-03-2017 / 11:37:26 / stefan" ! at:aKey update:aBlock @@ -363,6 +388,28 @@ !KeyedCollection methodsFor:'enumerating'! +associationsDo:aBlock + "perform the block for all associations in the collection. + + See also: + #do: (which passes values to its block) + #keysDo: (which passes only keys to its block) + #keysAndValuesDo: (which passes keys&values) + + This is much like #keysAndValuesDo:, but aBlock gets the + key and value as a single association argument. + #keysAndValuesDo: and is a bit faster therefore (no intermediate objects). + + WARNING: do not add/remove elements while iterating over the receiver. + Iterate over a copy to do this." + + self keysAndValuesDo:[:eachKey :eachValue| + aBlock value:(Association key:eachKey value:eachValue). + ]. + + "Created: / 17-03-2017 / 11:05:29 / stefan" +! + do:aBlock "evaluate aBlock for each value" @@ -379,6 +426,7 @@ "Created: / 19.6.1998 / 00:56:52 / cg" ! ! + !KeyedCollection methodsFor:'removing'! removeKey:aKey