Back: DelayedAdaptor-accessing Up: Base classes Forward: Dictionary class-instance creation   Top: GNU Smalltalk Library Reference Contents: Table of Contents Index: Class index About: About this document

1.61 Dictionary

Defined in namespace Smalltalk
Superclass: HashedCollection
Category: Collections-Keyed
I implement a dictionary, which is an object that is indexed by unique objects (typcially instances of Symbol), and associates another object with that index. I use the equality operator = to determine equality of indices.

In almost all places where you would use a plain Dictionary, a LookupTable would be more efficient; see LookupTable's comment before you use it. I do have a couple of special features that are useful in certain special cases.

1.61.1 Dictionary class: instance creation  (class)
1.61.2 Dictionary: accessing  (instance)
1.61.3 Dictionary: awful ST-80 compatibility hacks  (instance)
1.61.4 Dictionary: dictionary enumerating  (instance)
1.61.5 Dictionary: dictionary removing  (instance)
1.61.6 Dictionary: dictionary testing  (instance)
1.61.7 Dictionary: polymorphism hacks  (instance)
1.61.8 Dictionary: printing  (instance)
1.61.9 Dictionary: rehashing  (instance)
1.61.10 Dictionary: storing  (instance)
1.61.11 Dictionary: testing  (instance)


1.61.1 Dictionary class: instance creation

from: anArray
Answer a new dictionary created from the keys and values of Associations in anArray, such as {1 -> 2. 3 -> 4}. anArray should be specified using brace-syntax.

new
Create a new dictionary with a default size


1.61.2 Dictionary: accessing

add: newObject
Add the newObject association to the receiver

addAll: aCollection
Adds all the elements of 'aCollection' to the receiver, answer aCollection

associationAt: key
Answer the key/value Association for the given key. Fail if the key is not found

associationAt: key ifAbsent: aBlock
Answer the key/value Association for the given key. Evaluate aBlock (answering the result) if the key is not found

associations
Returns the content of a Dictionary as a Set of Associations.

at: key
Answer the value associated to the given key. Fail if the key is not found

at: key ifAbsent: aBlock
Answer the value associated to the given key, or the result of evaluating aBlock if the key is not found

at: aKey ifAbsentPut: aBlock
Answer the value associated to the given key. If the key is not found, evaluate aBlock and associate the result to aKey before returning.

at: aKey ifPresent: aBlock
If aKey is absent, answer nil. Else, evaluate aBlock passing the associated value and answer the result of the invocation

at: key put: value
Store value as associated to the given key

atAll: keyCollection
Answer a Dictionary that only includes the given keys. Fail if any of them is not found

keyAtValue: value
Answer the key associated to the given value, or nil if the value is not found

keyAtValue: value ifAbsent: exceptionBlock
Answer the key associated to the given value. Evaluate exceptionBlock (answering the result) if the value is not found. IMPORTANT: == is used to compare values

keys
Answer a kind of Set containing the keys of the receiver

values
Answer an Array containing the values of the receiver


1.61.3 Dictionary: awful ST-80 compatibility hacks

findKeyIndex: key
Tries to see if key exists as a the key of an indexed variable. As soon as nil or an association with the correct key is found, the index of that slot is answered


1.61.4 Dictionary: dictionary enumerating

associationsDo: aBlock
Pass each association in the dictionary to aBlock

collect: aBlock
Answer a new dictionary where the keys are the same and the values are obtained by passing each value to aBlock and collecting the return values

do: aBlock
Pass each value in the dictionary to aBlock

keysAndValuesDo: aBlock
Pass each key/value pair in the dictionary as two distinct parameters to aBlock

keysDo: aBlock
Pass each key in the dictionary to aBlock

reject: aBlock
Answer a new dictionary containing the key/value pairs for which aBlock returns false. aBlock only receives the value part of the pairs.

select: aBlock
Answer a new dictionary containing the key/value pairs for which aBlock returns true. aBlock only receives the value part of the pairs.


1.61.5 Dictionary: dictionary removing

remove: anAssociation
Remove anAssociation's key from the dictionary

remove: anAssociation ifAbsent: aBlock
Remove anAssociation's key from the dictionary

removeAllKeys: keys
Remove all the keys in keys, without raising any errors

removeAllKeys: keys ifAbsent: aBlock
Remove all the keys in keys, passing the missing keys as parameters to aBlock as they're encountered

removeKey: key
Remove the passed key from the dictionary, fail if it is not found

removeKey: key ifAbsent: aBlock
Remove the passed key from the dictionary, answer the result of evaluating aBlock if it is not found


1.61.6 Dictionary: dictionary testing

includes: anObject
Answer whether the receiver contains anObject as one of its values

includesAssociation: anAssociation
Answer whether the receiver contains the key which is anAssociation's key and its value is anAssociation's value

includesKey: key
Answer whether the receiver contains the given key

occurrencesOf: aValue
Answer whether the number of occurrences of aValue as one of the receiver's values


1.61.7 Dictionary: polymorphism hacks

withAllSuperspaces
This method is needed by the compiler


1.61.8 Dictionary: printing

inspect
Print all the instance variables and objects in the receiver on the Transcript

printOn: aStream
Print a representation of the receiver on aStream


1.61.9 Dictionary: rehashing

rehash
Rehash the receiver


1.61.10 Dictionary: storing

storeOn: aStream
Print Smalltalk code compiling to the receiver on aStream


1.61.11 Dictionary: testing

= aDictionary
Answer whether the receiver and aDictionary are equal

hash
Answer the hash value for the receiver



Back: Dictionary-storing Up: Dictionary Forward: DirectedMessage   Top: GNU Smalltalk Library Reference Contents: Table of Contents Index: Class index About: About this document


This document was generated on May, 22 2008 using texi2html