eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'KeyedCollection':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: KeyedCollection


Inheritance:

   Object
   |
   +--Collection
      |
      +--KeyedCollection
         |
         +--BTree
         |
         +--MethodDictionary
         |
         +--RBSmallDictionary
         |
         +--TSTree

Package:
stx:libbasic
Category:
Collections-Abstract
Version:
rev: 1.17 date: 2017/04/28 12:39:45
user: stefan
file: KeyedCollection.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


Abstract superclass for collections which have a key->value mapping.
This abstract class provides functionality common to those collections,
without knowing how the concrete class implements things. 
Thus, all methods found here depend on some basic mechanisms 
to be defined in the concrete class. 
These basic methods are usually defined as #subclassResponsibility here.
Some methods are also redefined for better performance.

Subclasses should at least implement:
    at:ifAbsent:        - accessing elements
    removeKey:ifAbsent  - removing
    keysAndValuesDo:    - enumerating


Class protocol:

instance creation
o  withAll: aDictionary
create a MethodDictionary from another Dictionary

o  withAssociations: aCollectionOfAssociations
return a new instance where associations are taken from the argument
usage example(s):
     KeyValueList withAssociations:{ #'one'->1 .
                                   #'two'->2 .
                                   #'three'->3 .
                                   #'four'->4 }

o  withKeys: keyArray andValues: valueArray
return a new instance where keys and values are taken from
the argumentArrays.

o  withKeysAndValues: anArray
return a new instance where keys and values are taken from alternating
elements of anArray
usage example(s):
     KeyValueList withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)

queries
o  isAbstract
Return if this class is an abstract class.
True is returned for KeyedCollection here; false for subclasses.
Abstract subclasses must redefine this again.


Instance protocol:

accessing
o  associationAt: aKey
return an association consisting of aKey and the element indexed
by aKey -
report an error, if no element is stored under aKey.

o  associationAt: aKey ifAbsent: exceptionBlock
return an association consisting of aKey and the element indexed by aKey -
return result of exceptionBlock if no element is stored under aKey.
Warning: this is a comatibility interface only, with a different semantic as
the original ST80 implementation. The returned assoc is created on the fly,
and not the one stored in the receiver (there are not assocs there)

o  associations
return an ordered collection containing the receiver's associations.

o  at: key
return the value stored under akey.
Raise an error if not found

o  at: key ifAbsent: exceptionBlock
return the value stored under akey.
Return the value from evaluating exceptionBlock if not found

** This method raises an error - it must be redefined in concrete classes **

o  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.
Return the new value stored.

o  at: aKey ifAbsentPut: valueBlock
return the element indexed by aKey if present,
if not present, store the result of evaluating valueBlock
under aKey and return it.
WARNING: do not add elements while iterating over the receiver.
Iterate over a copy to do this.

o  at: aKey ifPresent: aBlock
try to retrieve the value stored at aKey.
If there is nothing stored under this key, do nothing.
Otherwise, evaluate aBlock, passing the retrieved value as argument.

o  at: aKey put: anObject
add the argument anObject under key, aKey to the receiver.
Return anObject (sigh).
WARNING: do not add elements while iterating over the receiver.
Iterate over a copy to do this.

** This method raises an error - it must be redefined in concrete classes **

o  at: aKey put: anObject ifPresent: aBlock
if the receiver contains an element stored under aKey,
retrieve it and evaluate aBlock passing the element as argument,
return the blocks value.
If not, store aValue under the key.
Use this with an error-reporting block, to ensure that no keys are reused

o  at: aKey update: aBlock
update the element stored under aKey with the result from
evaluating aBlock with the previous stored value as argument.
Report an error if there was no such key initially.
Return the new value stored.

o  keyAtEqualValue: value
return the key under which value is stored.
Raise an error if not found.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using equality compare

o  keyAtEqualValue: value ifAbsent: exceptionBlock
return the key under which value is stored.
If not found, return the value from evaluating exceptionBlock.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using equality compare

o  keyAtIdenticalValue: value
return the key under which value is stored.
Raise an error if not found.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using identity compare

o  keyAtIdenticalValue: value ifAbsent: exceptionBlock
return the key under which value is stored.
If not found, return the value from evaluating exceptionBlock.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using identity compare

o  keyAtValue: value
return the key under which value is stored.
Raise an error if not found.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using equality compare

o  keyAtValue: value ifAbsent: exceptionBlock
return the key under which value is stored.
If not found, return the value from evaluating exceptionBlock.
This is a slow access, since the receiver is searched sequentially.
NOTICE:
The value is searched using equality compare

o  keys
return a collection containing the keys of the receiver

enumerating
o  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.

o  do: aBlock
evaluate aBlock for each value

o  keysAndValuesDo: aBlock
evaluate aBlock for each key and value

** This method raises an error - it must be redefined in concrete classes **

misc ui support
o  inspectorClass
( an extension from the stx:libtool package )
redefined to use DictionaryInspector
(instead of the default Inspector).

removing
o  removeAllKeys: aCollection
remove all keys of the argument, aCollection from the receiver.
Raises an error, if some element-to-remove is not in the receiver.

o  removeKey: aKey
remove key (and the value stored under that key) from the
receiver; raise an error if no such element is contained

o  removeKey: aKey ifAbsent: exceptionBlock
remove key (and the value stored under that key) from the
receiver; return the value which was stored previously there.
If no such element is contained, return the value
from evaluating exceptionBlock

** This method raises an error - it must be redefined in concrete classes **

searching
o  findFirst: aBlock ifNone: exceptionValue
find the index of the first element, for which evaluation of the argument, aBlock returns true;
return its index or the value from exceptionValue if none detected.
This is much like #detect:ifNone:, however, here an INDEX is returned,
while #detect:ifNone: returns the element.

Here we return the first key for which aBlock matches the value.
Note that there is no order in a Dictionary, so any element is first.
usage example(s):
        (KeyValueList withKeys:#('a' 'b' 'c') andValues:#('bla' 'hello' 'hallo'))
            findFirst:[:v| v first = $h].

o  findFirstKey: aBlock
find and return the first key, for which evaluation of the argument, aBlock
returns true; return nil if none is detected.

testing
o  includesIdenticalKey: aKey
return true, if the argument, aKey is a key in the receiver

o  includesKey: aKey
return true, if the argument, aKey is a key in the receiver



ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Mon, 04 Aug 2025 14:29:09 GMT