Dictionary

I am a collection of elements that associate a key object with a value object.

Description -------------------------- I can be viewed from one of two perspectives: a set of associations, or a Container of values that are externally named where the name can be any object that responds to #=. The external name is referred to as the key. I inherit many operations from Set. I use the #= method to locate a key. If you want a collection that use the #== method (same pointers) you can use an IdentityDictionary.

I'm used when we need a collection of objects which I can access with a key. For example if you associate some words with a definition, the word will be the key and the definition will be the value. Both of them can be any kind of objects. Internally I use Associations. The Association class can help to use me. (See examples lower) Public API and Key Messages -------------------------- - #at: aKey put: aValue / #at: aKey ifAbsentPut: aValue allow to add an element. - #at: aKey / #at: aKey ifAbsent: aBlock / #at: aKey ifPresent: aBlock ifAbsent: aBlock allow to access my values. - #keysDo: aBlock / #valuesDo: aBlock / #associationsDo: allow to iterate on me effectively Examples -------------------------- To create a dictiony with indexes as key: Dictionary withAll: #(7 3 1 3) "returns: a Dictionary(1->7 2->3 3->1 4->3 " To use Objects as key (here symbols): colors := Dictionary new at: #yellow put: Color yellow; at: #blue put: Color blue; at: #red put: Color red; yourself. colors at: #yellow. "returns: Color yellow" colors keys "returns: a Set(#blue #yellow #red)" colors values "returns: {Color blue. Color yellow. Color red}" You can also directly use Associations: colors := Dictionary with: #yellow -> Color yellow with: #blue -> Color blue. colors add: #red -> Color red. colors associations "returns: {#yellow->Color yellow. #red->Color red. #blue->Color blue}" Here some more examples: colors := Dictionary newFrom: { #blue->Color blue . #red->Color red . #yellow->Color yellow }. colors removeKey: #blue. colors at: #red ifPresent: [ :color | color darker] ifAbsent: [ Error signal: 'The red color should be here.' ] . "return: (Color r: 0.92 g: 0.0 b: 0.0 alpha: 1.0)" colors associations "{#yellow->Color yellow. #red->Color red}" Internal Representation and Key Implementation Points. -------------------------- I am just a collection of associations. If I need my keys I will just return the keys of my associations. Idem for my values. I use the #= method in order to manipulate my keys. I cannot have two associations that are equals with the #= method.