KeyedCollection.st
author Stefan Vogel <sv@exept.de>
Thu, 16 Mar 2017 21:30:11 +0100
changeset 21653 baee5890dca8
parent 21649 4a09b7965b34
child 21655 058cafb6c2f6
permissions -rw-r--r--
#FEATURE by stefan class: KeyedCollection added: #associationAt:ifAbsent: #associations #at:ifAbsent:update: #at:ifAbsentPut: #at:ifPresent: #at:put: #at:put:ifPresent: #at:update: #findFirst:ifNone: changed: #associationAt: #includes: #includesValue: #keyAtValue: (send #keyAtEqualValue: instead of #keyAtIdenticalValue:) #keyAtValue:ifAbsent: (send #keyAtEqualValue:ifAbsent: instead of #keyAtIdenticalValue:ifAbsent:) category of: #findFirstKey:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 1998 by eXept Software AG
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
9482
7bebdb8e4c60 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9480
diff changeset
    12
"{ Package: 'stx:libbasic' }"
8893
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
    13
19469
e43475084bb5 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17120
diff changeset
    14
"{ NameSpace: Smalltalk }"
e43475084bb5 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17120
diff changeset
    15
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
Collection subclass:#KeyedCollection
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Abstract'
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!KeyedCollection class methodsFor:'documentation'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
copyright
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
 COPYRIGHT (c) 1998 by eXept Software AG
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
              All Rights Reserved
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 This software is furnished under a license and may be used
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 only in accordance with the terms of that license and with the
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 be provided or otherwise made available to, or used by, any
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 other person.  No title to or ownership of the software is
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 hereby transferred.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
documentation
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    Abstract superclass for collections which have a key->value mapping.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    This abstract class provides functionality common to those collections,
17120
246d9198d84e class: KeyedCollection
Claus Gittinger <cg@exept.de>
parents: 13045
diff changeset
    44
    without knowing how the concrete class implements things. 
246d9198d84e class: KeyedCollection
Claus Gittinger <cg@exept.de>
parents: 13045
diff changeset
    45
    Thus, all methods found here depend on some basic mechanisms 
246d9198d84e class: KeyedCollection
Claus Gittinger <cg@exept.de>
parents: 13045
diff changeset
    46
    to be defined in the concrete class. 
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    These basic methods are usually defined as #subclassResponsibility here.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    Some methods are also redefined for better performance.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    Subclasses should at least implement:
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
        at:ifAbsent:        - accessing elements
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
        removeKey:ifAbsent  - removing
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
        keysAndValuesDo:    - enumerating
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    [author:]
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
        Claus Gittinger
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
! !
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
21649
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    60
!KeyedCollection class methodsFor:'instance creation'!
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    61
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    62
withAssociations:aCollectionOfAssociations
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    63
    "return a new instance where associations are taken from the argument"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    64
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    65
    |newDict sz "{ Class: SmallInteger }"|
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    66
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    67
    sz := aCollectionOfAssociations size.
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    68
    newDict := self new:sz.
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    69
    aCollectionOfAssociations do:[:assoc |
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    70
        newDict at:assoc key put:assoc value
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    71
    ].
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    72
    ^ newDict
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    73
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    74
    "
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    75
     KeyValueList withAssociations:{ #'one'->1 .
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    76
                                   #'two'->2 .
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    77
                                   #'three'->3 .
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    78
                                   #'four'->4 }
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    79
    "
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    80
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    81
    "Created: / 16-03-2017 / 12:15:38 / stefan"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    82
!
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    83
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    84
withKeys:keyArray andValues:valueArray
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    85
    "return a new instance where keys and values are taken from
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    86
     the argumentArrays."
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    87
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    88
    |newColl sz "{ Class: SmallInteger }"|
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    89
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    90
    sz := keyArray size.
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    91
    newColl := self new:sz.
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    92
    1 to:sz do:[:index |
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    93
        newColl at:(keyArray at:index) put:(valueArray at:index).
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    94
    ].
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    95
    ^ newColl
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    96
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    97
    "Created: / 16-03-2017 / 11:06:51 / stefan"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    98
    "Modified (format): / 16-03-2017 / 12:12:09 / stefan"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
    99
!
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   100
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   101
withKeysAndValues:anArray
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   102
    "return a new instance where keys and values are taken from alternating
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   103
     elements of anArray"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   104
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   105
    |newDict sz "{ Class: SmallInteger }"|
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   106
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   107
    sz := anArray size.
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   108
    newDict := self new:(sz // 2).
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   109
    1 to:sz by:2 do:[:i |
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   110
        newDict at:(anArray at:i) put:(anArray at:i+1)
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   111
    ].
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   112
    ^ newDict
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   113
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   114
    "
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   115
     KeyValueList withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   116
    "
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   117
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   118
    "Created: / 16-03-2017 / 12:13:27 / stefan"
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   119
! !
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   120
8893
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   121
!KeyedCollection class methodsFor:'queries'!
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   122
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   123
isAbstract
11222
7e8b37ffc6ca comment
Claus Gittinger <cg@exept.de>
parents: 10533
diff changeset
   124
    "Return if this class is an abstract class.
7e8b37ffc6ca comment
Claus Gittinger <cg@exept.de>
parents: 10533
diff changeset
   125
     True is returned for KeyedCollection here; false for subclasses.
19469
e43475084bb5 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17120
diff changeset
   126
     Abstract subclasses must redefine this again."
11222
7e8b37ffc6ca comment
Claus Gittinger <cg@exept.de>
parents: 10533
diff changeset
   127
8893
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   128
    ^ self == KeyedCollection
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   129
! !
99996b25482e +isAbstract
Claus Gittinger <cg@exept.de>
parents: 3615
diff changeset
   130
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
!KeyedCollection methodsFor:'accessing'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   133
associationAt:aKey 
21649
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   134
    "return an association consisting of aKey and the element indexed 
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   135
     by aKey - 
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   136
     report an error, if no element is stored under aKey."
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   137
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   138
    ^ self associationAt:aKey ifAbsent:[self errorKeyNotFound:aKey]
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   139
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   140
    "Modified: / 16-03-2017 / 17:19:52 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   141
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   142
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   143
associationAt:aKey ifAbsent:exceptionBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   144
    "return an association consisting of aKey and the element indexed by aKey -
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   145
     return result of exceptionBlock if no element is stored under aKey.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   146
     Warning: this is a comatibility interface only, with a different semantic as
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   147
              the original ST80 implementation. The returned assoc is created on the fly,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   148
              and not the one stored in the receiver (there are not assocs there)"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   149
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   150
    |value|
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   151
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   152
    value := self at:aKey ifAbsent:[^ exceptionBlock value].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   153
    ^ Association key:aKey value:value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   154
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   155
    "Created: / 16-03-2017 / 17:17:05 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   156
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   157
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   158
associations
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   159
    "return an ordered collection containing the receiver's associations."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   160
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   161
    |coll|
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   162
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   163
    coll := OrderedCollection new.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   164
    self associationsDo:[:assoc | coll add:assoc].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   165
    ^ coll
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   166
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   167
    "Created: / 16-03-2017 / 17:30:47 / stefan"
21649
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   168
!
4a09b7965b34 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21369
diff changeset
   169
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
at:key
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
    "return the value stored under akey.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
     Raise an error if not found"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    ^ self at:key ifAbsent:[self errorKeyNotFound:key].
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
    "Modified: / 19.6.1998 / 00:48:27 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
at:key ifAbsent:exceptionBlock
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
    "return the value stored under akey.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
     Return the value from evaluating exceptionBlock if not found"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    ^ self subclassResponsibility
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
    "Created: / 19.6.1998 / 00:48:23 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   188
at:aKey ifAbsent:default update:aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   189
    "update the element stored under aKey with the result from
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   190
     evaluating aBlock with the previous stored value as argument, or with default,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   191
     if there was no such key initially.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   192
     Return the new value stored."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   193
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   194
    ^ self at:aKey put:(aBlock value:(self at:aKey ifAbsent:default))
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   195
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   196
    "Created: / 16-03-2017 / 17:28:15 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   197
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   198
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   199
at:aKey ifAbsentPut:valueBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   200
    "return the element indexed by aKey if present,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   201
     if not present, store the result of evaluating valueBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   202
     under aKey and return it.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   203
     WARNING: do not add elements while iterating over the receiver.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   204
              Iterate over a copy to do this."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   205
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   206
    ^ self at:aKey ifAbsent:[self at:aKey put:valueBlock value].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   207
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   208
    "Created: / 16-03-2017 / 17:23:07 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   209
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   210
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   211
at:aKey ifPresent:aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   212
    "try to retrieve the value stored at aKey.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   213
     If there is nothing stored under this key, do nothing.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   214
     Otherwise, evaluate aBlock, passing the retrieved value as argument."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   215
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   216
    |v|
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   217
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   218
    v := self at:aKey ifAbsent:[^ nil].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   219
    ^ aBlock value:v.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   220
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   221
    "Created: / 16-03-2017 / 17:11:27 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   222
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   223
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   224
at:aKey put:anObject
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   225
    "add the argument anObject under key, aKey to the receiver.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   226
     Return anObject (sigh).
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   227
     WARNING: do not add elements while iterating over the receiver.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   228
              Iterate over a copy to do this."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   229
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   230
    ^ self subclassResponsibility
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   231
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   232
    "Created: / 16-03-2017 / 17:33:12 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   233
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   234
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   235
at:aKey put:anObject ifPresent:aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   236
    "if the receiver contains an element stored under aKey,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   237
     retrieve it and evaluate aBlock passing the element as argument,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   238
     return the blocks value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   239
     If not, store aValue under the key.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   240
     Use this with an error-reporting block, to ensure that no keys are reused"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   241
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   242
    |value isAbsent|
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   243
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   244
    value := self at:aKey ifAbsent:[isAbsent := true. self at:aKey put:anObject].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   245
    isAbsent notNil ifTrue:[
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   246
        ^ value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   247
    ].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   248
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   249
    ^ aBlock value:value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   250
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   251
    "Created: / 16-03-2017 / 17:38:00 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   252
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   253
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   254
at:aKey update:aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   255
    "update the element stored under aKey with the result from
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   256
     evaluating aBlock with the previous stored value as argument.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   257
     Report an error if there was no such key initially.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   258
     Return the new value stored."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   259
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   260
    ^ self at:aKey put:(aBlock value:(self at:aKey))
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   261
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   262
    "Created: / 16-03-2017 / 17:29:22 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   263
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   264
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   265
keyAtEqualValue:value
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
    "return the key under which value is stored.
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   267
     Raise an error if not found.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   268
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   269
     NOTICE:
21369
d7b0866974d9 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21368
diff changeset
   270
        The value is searched using equality compare"
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   272
    ^ self keyAtEqualValue:value ifAbsent:[self errorValueNotFound:value].
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   274
    "Created: / 07-02-2017 / 11:11:41 / cg"
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   277
keyAtEqualValue:value ifAbsent:exceptionBlock
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   278
    "return the key under which value is stored.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   279
     If not found, return the value from evaluating exceptionBlock.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   280
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   281
     NOTICE:
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   282
        The value is searched using equality compare"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   283
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   284
    self keysAndValuesDo:[:elKey :elValue |
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   285
        value = elValue ifTrue:[^ elKey]
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   286
    ].
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   287
    ^ exceptionBlock value
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   288
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   289
    "Created: / 07-02-2017 / 11:12:03 / cg"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   290
!
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   291
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   292
keyAtIdenticalValue:value
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    "return the key under which value is stored.
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   294
     Raise an error if not found.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   295
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   296
     NOTICE:
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   297
        The value is searched using identity compare"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   298
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   299
    ^ self keyAtIdenticalValue:value ifAbsent:[self errorValueNotFound:value].
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   300
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   301
    "Created: / 07-02-2017 / 11:12:23 / cg"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   302
!
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   303
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   304
keyAtIdenticalValue:value ifAbsent:exceptionBlock
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   305
    "return the key under which value is stored.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   306
     If not found, return the value from evaluating exceptionBlock.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   307
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   308
     NOTICE:
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   309
        The value is searched using identity compare"
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
    self keysAndValuesDo:[:elKey :elValue |
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
        value == elValue ifTrue:[^ elKey]
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
    ].
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
    ^ exceptionBlock value
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   316
    "Created: / 07-02-2017 / 11:12:46 / cg"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   317
!
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   318
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   319
keyAtValue:value
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   320
    "return the key under which value is stored.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   321
     Raise an error if not found.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   322
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   323
     NOTICE:
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   324
        The value is searched using equality compare"
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   325
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   326
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   327
    ^ self keyAtEqualValue:value.
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   328
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   329
    "Created: / 19-06-1998 / 00:49:16 / cg"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   330
    "Modified (comment): / 07-02-2017 / 11:13:29 / cg"
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   331
    "Modified (comment): / 16-03-2017 / 18:00:28 / stefan"
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   332
!
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   333
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   334
keyAtValue:value ifAbsent:exceptionBlock
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   335
    "return the key under which value is stored.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   336
     If not found, return the value from evaluating exceptionBlock.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   337
     This is a slow access, since the receiver is searched sequentially.
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   338
     NOTICE:
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   339
        The value is searched using equality compare"
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   340
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   341
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   342
    ^ self keyAtEqualValue:value ifAbsent:exceptionBlock.
21368
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   343
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   344
    "Created: / 19-06-1998 / 00:50:34 / cg"
1e4c41f4d3af #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19844
diff changeset
   345
    "Modified: / 07-02-2017 / 11:13:20 / cg"
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   346
    "Modified (comment): / 16-03-2017 / 18:00:37 / stefan"
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
keys
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
    "return a collection containing the keys of the receiver"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
    |keyCollection|
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
    keyCollection := OrderedCollection new.
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
    self keysDo:[:aKey |
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
        keyCollection add:aKey
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
    ].
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
    ^ keyCollection
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
    "Modified: / 19.6.1998 / 00:48:27 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
    "Created: / 19.6.1998 / 00:51:49 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
! !
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
!KeyedCollection methodsFor:'enumerating'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
do:aBlock
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
    "evaluate aBlock for each value"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
    self keysAndValuesDo:[:elKey :elValue | aBlock value:elValue]
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
    "Created: / 19.6.1998 / 00:56:24 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
keysAndValuesDo:aBlock
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
    "evaluate aBlock for each key and value"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
    ^ self subclassResponsibility
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
    "Created: / 19.6.1998 / 00:56:52 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
! !
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
!KeyedCollection methodsFor:'removing'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
removeKey:aKey
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
    "remove key (and the value stored under that key) from the
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
     receiver; raise an error if no such element is contained"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
    ^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey]
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
    "Created: / 19.6.1998 / 00:53:25 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
    "Modified: / 19.6.1998 / 00:54:02 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
removeKey:aKey ifAbsent:exceptionBlock
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
    "remove key (and the value stored under that key) from the
19844
9a7234d4b08c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19469
diff changeset
   396
     receiver; return the value which was stored previously there.
9a7234d4b08c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19469
diff changeset
   397
     If no such element is contained, return the value
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
     from evaluating exceptionBlock"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
    ^ self subclassResponsibility
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
    "Created: / 19.6.1998 / 00:53:58 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
! !
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
21653
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   405
!KeyedCollection methodsFor:'searching'!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   406
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   407
findFirst:aBlock ifNone:exceptionValue
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   408
    "find the index of the first element, for which evaluation of the argument, aBlock returns true;
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   409
     return its index or the value from exceptionValue if none detected.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   410
     This is much like #detect:ifNone:, however, here an INDEX is returned,
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   411
     while #detect:ifNone: returns the element.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   412
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   413
     Here we return the first key for which aBlock matches the value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   414
     Note that there is no order in a Dictionary, so any element is first."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   415
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   416
    self keysAndValuesDo:[:eachKey :eachValue| (aBlock value:eachValue) ifTrue:[^ eachKey]].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   417
    ^ exceptionValue value.
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   418
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   419
    "
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   420
        (KeyValueList withKeys:#('a' 'b' 'c') andValues:#('bla' 'hello' 'hallo'))
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   421
            findFirst:[:v| v first = $h].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   422
    "
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   423
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   424
    "Created: / 16-03-2017 / 17:46:02 / stefan"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   425
!
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   426
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   427
findFirstKey:aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   428
    "find and return the first key, for which evaluation of the argument, aBlock
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   429
     returns true; return nil if none is detected."
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   430
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   431
    self keysDo:[:key |
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   432
        (aBlock value:key) ifTrue:[^ key].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   433
    ].
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   434
    ^ nil
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   435
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   436
    "Created: 8.10.1996 / 22:01:31 / cg"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   437
    "Modified: 8.10.1996 / 22:02:03 / cg"
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   438
! !
baee5890dca8 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 21649
diff changeset
   439
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
!KeyedCollection methodsFor:'testing'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
10533
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   442
includesIdenticalKey:aKey
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   443
    "return true, if the argument, aKey is a key in the receiver"
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   444
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   445
    self keysDo:[:elKey | aKey == elKey ifTrue:[^ true]].
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   446
    ^ false
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   447
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   448
    "Created: / 19.6.1998 / 00:55:05 / cg"
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   449
!
04130c197558 changed #includesIdenticalKey:
ca
parents: 9482
diff changeset
   450
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
includesKey:aKey
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
    "return true, if the argument, aKey is a key in the receiver"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
    self keysDo:[:elKey | aKey = elKey ifTrue:[^ true]].
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
    ^ false
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
    "Created: / 19.6.1998 / 00:55:05 / cg"
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
! !
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
!KeyedCollection class methodsFor:'documentation'!
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
version
19469
e43475084bb5 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17120
diff changeset
   463
    ^ '$Header$'
12667
4297ecfffa96 #values moved up
Claus Gittinger <cg@exept.de>
parents: 11222
diff changeset
   464
!
4297ecfffa96 #values moved up
Claus Gittinger <cg@exept.de>
parents: 11222
diff changeset
   465
4297ecfffa96 #values moved up
Claus Gittinger <cg@exept.de>
parents: 11222
diff changeset
   466
version_CVS
19469
e43475084bb5 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 17120
diff changeset
   467
    ^ '$Header$'
3615
c0f82feaf6a7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
! !
17120
246d9198d84e class: KeyedCollection
Claus Gittinger <cg@exept.de>
parents: 13045
diff changeset
   469