Dictionary.st
author Claus Gittinger <cg@exept.de>
Wed, 10 Nov 1999 17:20:51 +0100
changeset 4978 0d75a92f1e56
parent 4871 211a7f44946d
child 5050 ea292b958108
permissions -rw-r--r--
comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1991 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
    13
Set subclass:#Dictionary
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
    14
	instanceVariableNames:'valueArray'
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
    15
	classVariableNames:''
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
    16
	poolDictionaries:''
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
    17
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
    20
!Dictionary class methodsFor:'documentation'!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    21
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
 COPYRIGHT (c) 1991 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
    25
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    36
documentation
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    37
"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    38
    a Dictionary is (conceptionally) a set of Associations storing key-value pairs.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    39
    (The implementation uses two arrays to store the keys and values separately.)
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    40
    Searching for an element is done using a hash into the key array.
362
claus
parents: 360
diff changeset
    41
    Another way of looking at a dictionary is as a array which uses
345
claus
parents: 302
diff changeset
    42
    arbitrary access keys (i.e. not just integers as arrays do).
claus
parents: 302
diff changeset
    43
claus
parents: 302
diff changeset
    44
    Since the keys are unordered, no internal element order is defined
362
claus
parents: 360
diff changeset
    45
    (i.e. enumerating them may return elements in any order - even changing
345
claus
parents: 302
diff changeset
    46
     over time).
claus
parents: 302
diff changeset
    47
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    48
    Many methods for searching and hashing are inherited from Set.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    49
1279
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    50
    [Instance variables:]
345
claus
parents: 302
diff changeset
    51
1279
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    52
        keyArray        <Array>         (from Set) the keys
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    53
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    54
        valueArray      <Array>         the values ('valueArray at:index' corresponds
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    55
                                        to the value stored under 'keyArray at:index')
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    56
345
claus
parents: 302
diff changeset
    57
    Performance hints: 
362
claus
parents: 360
diff changeset
    58
      since the dictionary does not really store associations internally,
claus
parents: 360
diff changeset
    59
      it is less efficient, to store/retrieve associations. The reason is
399
claus
parents: 384
diff changeset
    60
      that these assocs are created temporarily in some extract methods. 
345
claus
parents: 302
diff changeset
    61
      I.e. 'at:key put:value' is faster than 'add:anAssoc' 
claus
parents: 302
diff changeset
    62
      and 'keysAndValuesDo:' is faster than 'associationsDo:' etc.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    63
362
claus
parents: 360
diff changeset
    64
      If only symbols or smallIntegers are used as keys, use IdentityDictionaries
claus
parents: 360
diff changeset
    65
      for slightly better performance, since both hashing and comparison is faster.
345
claus
parents: 302
diff changeset
    66
claus
parents: 302
diff changeset
    67
      If you have a rough idea how big the dictionary is going to grow,
claus
parents: 302
diff changeset
    68
      create it using #new: instead of #new. Even if the size given is a
claus
parents: 302
diff changeset
    69
      poor guess (say half of the real size), there is some 20-30% performance
claus
parents: 302
diff changeset
    70
      win to expect, since many resizing operations are avoided when associations
claus
parents: 302
diff changeset
    71
      are added.
362
claus
parents: 360
diff changeset
    72
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    73
    [See also:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    74
        Set, IdentityDictionary, IdentitySet, WeakIdentitySet and
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    75
        WeakIdentityDictionary
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    76
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    77
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    78
        Claus Gittinger
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    79
"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    80
! !
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    81
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
    82
!Dictionary class methodsFor:'instance creation'!
345
claus
parents: 302
diff changeset
    83
357
claus
parents: 345
diff changeset
    84
withKeys:keyArray andValues:valueArray
claus
parents: 345
diff changeset
    85
    "return a new instance where keys and values are taken from
claus
parents: 345
diff changeset
    86
     the argumentArrays."
claus
parents: 345
diff changeset
    87
claus
parents: 345
diff changeset
    88
    |newDict sz "{ Class: SmallInteger }"|
claus
parents: 345
diff changeset
    89
claus
parents: 345
diff changeset
    90
    sz := keyArray size.
claus
parents: 345
diff changeset
    91
    newDict := self new:sz.
claus
parents: 345
diff changeset
    92
    keyArray with:valueArray do:[:key :value |
claus
parents: 345
diff changeset
    93
	newDict at:key put:value
claus
parents: 345
diff changeset
    94
    ].
claus
parents: 345
diff changeset
    95
    ^ newDict
claus
parents: 345
diff changeset
    96
claus
parents: 345
diff changeset
    97
    "
claus
parents: 345
diff changeset
    98
     Dictionary withKeys:#('one' 'two' 'three' 'four')
claus
parents: 345
diff changeset
    99
	       andValues:#(1 2 3 4)
claus
parents: 345
diff changeset
   100
    "
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   101
!
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   102
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   103
withKeysAndValues:anArray
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   104
    "return a new instance where keys and values are taken from alternating
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   105
     elements of anArray"
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   106
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   107
    |newDict sz "{ Class: SmallInteger }"|
345
claus
parents: 302
diff changeset
   108
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   109
    sz := anArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   110
    newDict := self new:(sz // 2).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   111
    1 to:sz by:2 do:[:i |
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   112
	newDict at:(anArray at:i) put:(anArray at:i+1)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   113
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   114
    ^ newDict
345
claus
parents: 302
diff changeset
   115
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   116
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   117
     Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   118
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   119
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   120
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   121
!Dictionary class methodsFor:'STBroker'!
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   122
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   123
unmarshallCorbaStructureType: anODLTypeStructure using: aMarshaller
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   124
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   125
	^aMarshaller
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   126
		unmarshallDictionaryTypeStructure: anODLTypeStructure
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   127
			class: self! !
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   128
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
!Dictionary methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   131
associationAt:aKey
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   132
    "return an association consisting of aKey and the element indexed 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   133
     by aKey - 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   134
     report an error, if no element is stored under aKey"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   135
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
   136
    ^ Association key:aKey value:(self at:aKey)
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   137
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   138
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   139
associationAt:aKey ifAbsent:exceptionBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   140
    "return an association consisting of aKey and the element indexed by aKey - 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   141
     return result of exceptionBlock if no element is stored under aKey"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   142
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
   143
    ^ Association key:aKey value:(self at:aKey ifAbsent:[^ exceptionBlock value])
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   144
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   145
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   146
associations
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   147
    "return an ordered collection containing the receivers associations."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   148
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   149
    |coll|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   150
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   151
    coll := OrderedCollection new:(keyArray size).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   152
    self associationsDo:[:assoc | coll add:assoc].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   153
    ^ coll
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   154
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   155
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
at:aKey
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
    "return the element indexed by aKey - report an error if none found"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   158
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
    |index|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   160
a27a279701f8 Initial revision
claus
parents:
diff changeset
   161
    aKey isNil ifTrue:[
362
claus
parents: 360
diff changeset
   162
	"/ nil is not allowed as key
345
claus
parents: 302
diff changeset
   163
	^ self errorInvalidKey:aKey
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   164
    ].
362
claus
parents: 360
diff changeset
   165
claus
parents: 360
diff changeset
   166
    "/
claus
parents: 360
diff changeset
   167
    "/ I could have written:
claus
parents: 360
diff changeset
   168
    "/ index := self find:aKey ifAbsent:[^ self errorKeyNotFound:aKey]
claus
parents: 360
diff changeset
   169
    "/ but the code below is slighlty more efficient, since it avoids
1146
edadea7273b6 commentary
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
   170
    "/ a block creation - thus speeding up the good case.
362
claus
parents: 360
diff changeset
   171
 
claus
parents: 360
diff changeset
   172
    index := self find:aKey ifAbsent:0.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   173
    index ~~ 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   174
        ^ valueArray basicAt:index
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   175
    ].
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   176
    "no such key"
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   177
    ^ self errorKeyNotFound:aKey
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   178
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   179
a27a279701f8 Initial revision
claus
parents:
diff changeset
   180
at:aKey ifAbsent:exceptionBlock
2
claus
parents: 1
diff changeset
   181
    "return the element indexed by aKey - 
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   182
     return result of exceptionBlock if no element is stored under aKey"
2
claus
parents: 1
diff changeset
   183
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   184
    |index|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   185
a27a279701f8 Initial revision
claus
parents:
diff changeset
   186
    aKey isNil ifTrue:[
362
claus
parents: 360
diff changeset
   187
	"/ nil is not allowed as key
claus
parents: 360
diff changeset
   188
	"/
claus
parents: 360
diff changeset
   189
	"/ previous versions of ST/X raised an error
claus
parents: 360
diff changeset
   190
	"/ here. However, there seem to exist applications
claus
parents: 360
diff changeset
   191
	"/ which depend on getting the exceptionBlocks value
claus
parents: 360
diff changeset
   192
	"/ in this case ... well ...
claus
parents: 360
diff changeset
   193
	"/ ^ self errorInvalidKey:aKey
claus
parents: 360
diff changeset
   194
	^ exceptionBlock value
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   195
    ].
362
claus
parents: 360
diff changeset
   196
claus
parents: 360
diff changeset
   197
    "/ I could have written:
claus
parents: 360
diff changeset
   198
    "/ index := self find:aKey ifAbsent:[^ exceptionBlock value]
claus
parents: 360
diff changeset
   199
    "/ but the code below is slighlty more efficient, since it avoids
1146
edadea7273b6 commentary
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
   200
    "/ a block creation - thus speeding up the good case.
362
claus
parents: 360
diff changeset
   201
claus
parents: 360
diff changeset
   202
    index := self find:aKey ifAbsent:0.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   203
    index ~~ 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   204
        ^ valueArray basicAt:index
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   205
    ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   206
    ^ exceptionBlock value.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   207
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   208
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   209
at:aKey ifAbsentPut:valueBlock
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   210
    "return the element indexed by aKey if present,
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   211
     if not present, store the result of evaluating valueBlock
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   212
     under aKey and return it.
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   213
     WARNING: do not add elements while iterating over the receiver.
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   214
              Iterate over a copy to do this."
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   216
    |index "{ Class: SmallInteger }"
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   217
     newValue|
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   218
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   219
    aKey isNil ifTrue:[
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   220
        "nil is not allowed as key"
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   221
        ^ self errorInvalidKey:aKey
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   222
    ] ifFalse:[
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   223
        index := self findKeyOrNil:aKey.
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   224
        (keyArray basicAt:index) notNil ifTrue:[
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   225
            "/ already present
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   226
            ^ valueArray at:index.
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   227
        ].
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   228
        "/ a new one
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   229
        newValue := valueBlock value.
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   230
        keyArray basicAt:index put:aKey.
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   231
        valueArray basicAt:index put:newValue.
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   232
        tally := tally + 1.
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   233
        self fullCheck.
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   234
    ].
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   235
    ^ newValue
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   236
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   237
    "
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   238
     |d|
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   239
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   240
     d := Dictionary new.
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   241
     Transcript showCR:(d at:'foo' ifAbsentPut:'bar').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   242
     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   243
     Transcript showCR:(d at:'foo' ifAbsentPut:'barX').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   244
     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2X').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   245
    "
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   246
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   247
    "Created: / 23.1.1998 / 18:28:26 / cg"
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   248
    "Modified: / 26.2.1998 / 19:10:09 / stefan"
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   249
!
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   250
4871
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   251
at:aKey ifPresent:aBlock
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   252
    "if the receiver contains an element stored under aKey,
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   253
     retrieve it and evaluate aBlock passing the element as argument,
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   254
     return the blocks value.
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   255
     If not, do nothing and return nil."
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   256
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   257
    |v|
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   258
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   259
    v := self at:aKey ifAbsent:[^ nil].
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   260
    ^ aBlock value:v.
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   261
!
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   262
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
at:aKey put:anObject
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   264
    "add the argument anObject under key, aKey to the receiver.
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   265
     Return anObject (sigh).
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   266
     WARNING: do not add elements while iterating over the receiver.
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   267
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   268
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   269
    |index "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
a27a279701f8 Initial revision
claus
parents:
diff changeset
   271
    aKey isNil ifTrue:[
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   272
        "nil is not allowed as key"
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   273
        self errorInvalidKey:aKey
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   274
    ] ifFalse:[
1432
441054addd1e #at:put with nil value is actually a #removeKey:
Claus Gittinger <cg@exept.de>
parents: 1417
diff changeset
   275
        anObject isNil ifTrue:[
441054addd1e #at:put with nil value is actually a #removeKey:
Claus Gittinger <cg@exept.de>
parents: 1417
diff changeset
   276
            self removeKey:aKey ifAbsent:nil.
441054addd1e #at:put with nil value is actually a #removeKey:
Claus Gittinger <cg@exept.de>
parents: 1417
diff changeset
   277
            ^ nil
441054addd1e #at:put with nil value is actually a #removeKey:
Claus Gittinger <cg@exept.de>
parents: 1417
diff changeset
   278
        ].
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   279
        index := self findKeyOrNil:aKey.
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   280
        (keyArray basicAt:index) notNil ifTrue:[
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   281
            "/ key already present
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   282
            valueArray basicAt:index put:anObject.
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   283
            ^ anObject
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   284
        ].
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   285
        "/ a new key
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   286
        keyArray basicAt:index put:aKey.
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   287
        valueArray basicAt:index put:anObject.
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   288
        tally := tally + 1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   290
        self fullCheck.
362
claus
parents: 360
diff changeset
   291
    ].
claus
parents: 360
diff changeset
   292
    ^ anObject
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   293
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   294
    "Modified: 30.1.1997 / 14:59:10 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   297
keyAtEqualValue:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   298
    "return the key whose value is equal (i.e. using #= for compare)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   299
     to the argument, nil if none found.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   300
     This is a slow access, since there is no fast reverse mapping.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   301
     NOTICE:
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   302
	The value is searched using equality compare; 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   303
	use #keyAtValue: to compare for identity."
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   304
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   305
    ^ self keyAtEqualValue:aValue ifAbsent:[nil]
10
claus
parents: 5
diff changeset
   306
!
claus
parents: 5
diff changeset
   307
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   308
keyAtEqualValue:aValue ifAbsent:exceptionBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   309
    "return the key whose value is equal (i.e. using #= for compare) 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   310
     to the argument, if not found, return the value of exceptionBlock.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   311
     This is a slow access, since there is no fast reverse mapping.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   312
     NOTICE:
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   313
	The value is searched using equality compare; 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   314
	use #keyAtValue:ifAbsent: to compare for identity."
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   315
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   316
    |idx|
360
claus
parents: 359
diff changeset
   317
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   318
    idx := valueArray indexOf:aValue.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   319
    idx ~~ 0 ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   320
	^ keyArray at:idx
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   321
    ].
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   322
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   323
"/  keyArray keysAndValuesDo:[:index :aKey |
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   324
"/      (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   325
"/          (valueArray at:index) = aValue ifTrue:[^ aKey].  
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   326
"/      ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   327
"/  ].
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   328
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   329
    ^ exceptionBlock value
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   330
!
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   331
10
claus
parents: 5
diff changeset
   332
keyAtValue:aValue
345
claus
parents: 302
diff changeset
   333
    "return the key whose value is identical (i.e. using #== for compare)
claus
parents: 302
diff changeset
   334
     to the argument, nil if none found.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   335
     This is a slow access, since there is no fast reverse mapping.
357
claus
parents: 345
diff changeset
   336
     NOTICE:
362
claus
parents: 360
diff changeset
   337
	The value is searched using identity compare; 
357
claus
parents: 345
diff changeset
   338
	use #keyAtEqualValue: to compare for equality."
10
claus
parents: 5
diff changeset
   339
claus
parents: 5
diff changeset
   340
    ^ self keyAtValue:aValue ifAbsent:[nil]
claus
parents: 5
diff changeset
   341
!
claus
parents: 5
diff changeset
   342
claus
parents: 5
diff changeset
   343
keyAtValue:aValue ifAbsent:exceptionBlock
345
claus
parents: 302
diff changeset
   344
    "return the key whose value is identical (i.e. using #== for compare) 
claus
parents: 302
diff changeset
   345
     to the argument, if not found, return the value of exceptionBlock.
357
claus
parents: 345
diff changeset
   346
     This is a slow access, since there is no fast reverse mapping.
claus
parents: 345
diff changeset
   347
     NOTICE:
2466
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   348
        The value is searched using identity compare; 
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   349
        use #keyAtEqualValue:ifAbsent: to compare for equality."
10
claus
parents: 5
diff changeset
   350
359
claus
parents: 357
diff changeset
   351
    |idx|
claus
parents: 357
diff changeset
   352
2466
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   353
    idx := valueArray identityIndexOf:aValue startingAt:1.
359
claus
parents: 357
diff changeset
   354
    idx ~~ 0 ifTrue:[
2466
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   355
        ^ keyArray at:idx
10
claus
parents: 5
diff changeset
   356
    ].
359
claus
parents: 357
diff changeset
   357
claus
parents: 357
diff changeset
   358
"/  keyArray keysAndValuesDo:[:index :aKey |
360
claus
parents: 359
diff changeset
   359
"/      (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
claus
parents: 359
diff changeset
   360
"/          (valueArray at:index) == aValue ifTrue:[^ aKey].  
claus
parents: 359
diff changeset
   361
"/      ].
359
claus
parents: 357
diff changeset
   362
"/  ].
claus
parents: 357
diff changeset
   363
10
claus
parents: 5
diff changeset
   364
    ^ exceptionBlock value
2466
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   365
a96995fb79e5 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2425
diff changeset
   366
    "Modified: 20.3.1997 / 15:58:49 / cg"
357
claus
parents: 345
diff changeset
   367
!
claus
parents: 345
diff changeset
   368
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   369
keys
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   370
    "return a collection containing all keys of the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   371
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   372
    |keySet|
357
claus
parents: 345
diff changeset
   373
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   374
    keySet := self emptyCollectionForKeys.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   375
    keyArray do:[:key | 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   376
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   377
	    keySet add:key
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   378
	]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   379
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   380
    ^ keySet
357
claus
parents: 345
diff changeset
   381
!
claus
parents: 345
diff changeset
   382
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   383
values
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   384
    "return a collection containing all values of the receiver"
359
claus
parents: 357
diff changeset
   385
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   386
"old:
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   387
kk: this fails if the receiver contains nils
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   388
    ^ valueArray asBag
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   389
new:
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   390
"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   391
    |aCollection|
359
claus
parents: 357
diff changeset
   392
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   393
    aCollection := OrderedCollection new:valueArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   394
    self do:[:value| aCollection add:value].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   395
    ^ aCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   396
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   397
a27a279701f8 Initial revision
claus
parents:
diff changeset
   398
!Dictionary methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   399
a27a279701f8 Initial revision
claus
parents:
diff changeset
   400
add:anAssociation
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   401
    "add the argument, anAssociation to the receiver.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   402
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   403
     WARNING: do not add elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   404
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   405
a27a279701f8 Initial revision
claus
parents:
diff changeset
   406
    self at:(anAssociation key) put:(anAssociation value).
a27a279701f8 Initial revision
claus
parents:
diff changeset
   407
    ^ anAssociation
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   408
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   409
    "Modified: 1.3.1996 / 21:23:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   410
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   411
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   412
addPairsFrom:aSequenceableCollection
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   413
    "merge all key-value pairs from aDictionary into the receiver."
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   414
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   415
    aSequenceableCollection pairWiseDo:[:key :value |
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   416
        self at:key put:value.
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   417
    ]
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   418
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   419
    "Modified: 1.3.1996 / 21:24:03 / cg"
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   420
!
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   421
345
claus
parents: 302
diff changeset
   422
declare:key from:aDictionary
claus
parents: 302
diff changeset
   423
    "if the receiver does not include an association for key,
claus
parents: 302
diff changeset
   424
     take the association from aDictionary and add it to the receiver.
claus
parents: 302
diff changeset
   425
     If aDictionary does not contain such an association, use nil
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   426
     as the value of the new dictionary.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   427
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   428
     WARNING: do not add elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   429
	      Iterate over a copy to do this."
345
claus
parents: 302
diff changeset
   430
claus
parents: 302
diff changeset
   431
    |value|
claus
parents: 302
diff changeset
   432
claus
parents: 302
diff changeset
   433
    (self includesKey:key) ifFalse:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   434
	value := aDictionary at:key ifAbsent:nil.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   435
	self at:key put:value.
345
claus
parents: 302
diff changeset
   436
    ]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   437
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   438
    "Modified: 1.3.1996 / 21:24:03 / cg"
345
claus
parents: 302
diff changeset
   439
!
claus
parents: 302
diff changeset
   440
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   441
declareAllFrom:aDictionary
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   442
    "merge all key-value pairs from aDictionary into the receiver."
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   443
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   444
    aDictionary keysAndValuesDo:[:key :value |
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   445
        self at:key put:value.
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   446
    ]
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   447
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   448
    "Modified: 1.3.1996 / 21:24:03 / cg"
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   449
!
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   450
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   451
remove:oldObject ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   452
    "remove oldObject from the collection and return it.
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   453
     If it was not in the collection return the value of aBlock.
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   454
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   455
     This is blocked here; you have to use one of
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   456
     #removeKey:, #saveRemoveKey:, #removeAssociation:,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   457
     #removeValue: or #saveRemoveValue:"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   458
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   459
    ^ self shouldNotImplement
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   460
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   461
    "Modified: 1.3.1996 / 21:21:38 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   462
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   463
a27a279701f8 Initial revision
claus
parents:
diff changeset
   464
removeAssociation:assoc
a27a279701f8 Initial revision
claus
parents:
diff changeset
   465
    "remove the association from the collection.
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   466
     If it was not in the collection report an error.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   467
     Only the key is used in the passed argument, and a new
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   468
     association, for the key and the previously stored value is returned.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   469
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   470
     WARNING: do not remove elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   471
	      See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   472
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   473
    |key|
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   474
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   475
    key := assoc key.
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
   476
    ^ Association key:key value:(self removeKey:key)
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   477
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   478
    "Modified: 1.3.1996 / 21:21:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   479
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   480
a27a279701f8 Initial revision
claus
parents:
diff changeset
   481
removeKey:aKey
a27a279701f8 Initial revision
claus
parents:
diff changeset
   482
    "remove the association under aKey from the collection.
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   483
     If it was not in the collection report an error.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   484
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   485
     WARNING: do not remove elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   486
	      See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   487
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   488
    ^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   489
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   490
    "Modified: 1.3.1996 / 21:21:52 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   491
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   492
a27a279701f8 Initial revision
claus
parents:
diff changeset
   493
removeKey:aKey ifAbsent:aBlock
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   494
    "remove the association under aKey from the collection,
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   495
     return the value previously stored there..
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   496
     If it was not in the collection return the result 
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   497
     from evaluating aBlock.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   498
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   499
     WARNING: do not remove elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   500
	     See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   501
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   502
    |index "{ Class:SmallInteger }"
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   503
     next  "{ Class:SmallInteger }" 
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   504
     oldValue|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   505
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
    aKey isNil ifTrue:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   507
	^ self errorInvalidKey:aKey
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   508
    ].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   509
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   510
    "/   below, I could have written:
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   511
    "/      index := self find:aKey ifAbsent:[^ aBlock value]
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   512
    "/   but the code below is slighlty more efficient, since it avoids
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   513
    "/   a garbage block creation - thus speeding up the good case.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   514
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   515
    index := self find:aKey ifAbsent:0.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   516
    index == 0 ifTrue:[^ aBlock value].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   517
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   518
    oldValue := valueArray basicAt:index.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   519
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   520
    valueArray basicAt:index put:nil.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   521
    keyArray basicAt:index put:nil.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   522
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   523
    tally := tally - 1.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   524
    tally == 0 ifTrue:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   525
	self setTally:0
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   526
    ] ifFalse:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   527
	index == keyArray basicSize ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   528
	    next := 1
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   529
	] ifFalse:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   530
	    next := index + 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   531
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   532
	(keyArray basicAt:next) notNil ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   533
	    keyArray basicAt:index put:DeletedEntry
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   534
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   535
	self emptyCheck
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   536
    ].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   537
    ^ oldValue
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   538
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   539
    "Modified: 1.3.1996 / 21:21:01 / cg"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   540
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   541
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   542
removeValue:aValue ifAbsent:aBlock
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   543
    "remove (first) the association to aValue from the collection,
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   544
     return the key under which it was stored previously.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   545
     If it was not in the collection return result from evaluating aBlock.
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   546
     The value is searched using equality compare here,
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   547
     but identity compare in the IdentityDictionary subclass.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   548
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   549
     Notice, this does a linear search through the values and may
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   550
     therefore be slow for big dictionaries.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   551
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   552
     WARNING: do not remove elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   553
	     See #saveRemoveValue: to do this."
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   554
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   555
    |next  "{ Class:SmallInteger }" 
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   556
     oldKey|
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   557
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   558
    aValue notNil ifTrue:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   559
	keyArray keysAndValuesDo:[:index :aKey |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   560
	    |idx "{Class:SmallInteger}"|
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   561
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   562
	    (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   563
		idx := index.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   564
		(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[  
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   565
		    "found it"
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   566
		    valueArray basicAt:idx put:nil.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   567
		    oldKey := keyArray basicAt:idx.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   568
		    keyArray basicAt:idx put:nil.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   569
		    tally := tally - 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   570
		    tally == 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   571
			self setTally:0.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   572
			^ oldKey
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   573
		    ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   574
		    idx == keyArray basicSize ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   575
			next := 1
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   576
		    ] ifFalse:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   577
			next := index + 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   578
		    ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   579
		    (keyArray basicAt:next) notNil ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   580
			keyArray basicAt:idx put:DeletedEntry
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   581
		    ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   582
		    self emptyCheck.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   583
		    ^ oldKey
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   584
		]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   585
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   586
	]
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   587
    ].
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   588
    ^ aBlock value
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   589
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   590
    "Modified: 1.3.1996 / 21:22:11 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   591
!
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   592
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   593
saveRemoveKey:aKey
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   594
    "remove the association under aKey from the collection.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   595
     Return the value previously stored there.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   596
     If it was not in the collection return nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   597
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   598
     In contrast to #removeKey:, this does not resize the underlying collection
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   599
     and therefore does NOT rehash & change the elements order.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   600
     Therefor this can be used while enumerating the receiver,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   601
     which is not possible if #removeKey: is used.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   602
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   603
     WARNING: since no resizing is done, the physical amount of memory used
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   604
	      by the container remains the same, although the logical size shrinks.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   605
	      You may want to manually resize the receiver using #emptyCheck."
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   606
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   607
    |index "{ Class:SmallInteger }"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   608
     next  "{ Class:SmallInteger }" 
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   609
     oldValue|
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   610
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   611
    aKey isNil ifTrue:[^ nil].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   612
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   613
    index := self find:aKey ifAbsent:0.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   614
    index == 0 ifTrue:[^ nil].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   615
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   616
    oldValue := valueArray basicAt:index.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   617
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   618
    valueArray basicAt:index put:nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   619
    keyArray basicAt:index put:nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   620
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   621
    tally := tally - 1.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   622
    tally ~~ 0 ifTrue:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   623
	index == keyArray basicSize ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   624
	    next := 1
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   625
	] ifFalse:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   626
	    next := index + 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   627
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   628
	(keyArray basicAt:next) notNil ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   629
	    keyArray basicAt:index put:DeletedEntry
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   630
	].
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   631
    ].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   632
    ^ oldValue
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   633
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   634
    "does NOT work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   635
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   636
	|d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   637
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   638
	d := Dictionary new.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   639
	d at:'one' put:1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   640
	d at:'two' put:2.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   641
	d at:'three' put:3.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   642
	d at:'four' put:4.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   643
	d at:'five' put:5.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   644
	d at:'six' put:6.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   645
	d at:'seven' put:7.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   646
	d at:'eight' put:8.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   647
	d at:'nine' put:9.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   648
	d keysAndValuesDo:[:k :v |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   649
	    v odd ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   650
		d removeKey:k
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   651
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   652
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   653
	d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   654
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   655
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   656
    "DOES work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   657
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   658
	|d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   659
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   660
	d := Dictionary new.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   661
	d at:'one' put:1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   662
	d at:'two' put:2.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   663
	d at:'three' put:3.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   664
	d at:'four' put:4.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   665
	d at:'five' put:5.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   666
	d at:'six' put:6.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   667
	d at:'seven' put:7.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   668
	d at:'eight' put:8.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   669
	d at:'nine' put:9.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   670
	d keysAndValuesDo:[:k :v |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   671
	    v odd ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   672
		d saveRemoveKey:k
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   673
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   674
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   675
	d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   676
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   677
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   678
    "Created: 1.3.1996 / 21:14:42 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   679
    "Modified: 1.3.1996 / 21:14:53 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   680
!
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   681
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   682
saveRemoveValue:aValue
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   683
    "remove the (first) association to aValue from the collection,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   684
     return the key under which it was stored previously.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   685
     If it was not in the collection return nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   686
     The value is searched using equality compare here,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   687
     but identity compare in the IdentityDictionary subclass.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   688
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   689
     In contrast to #removeValue:, this does not resize the underlying collection
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   690
     and therefore does NOT rehash & change the elements order.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   691
     Therefor this can be used while enumerating the receiver,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   692
     which is not possible if #removeValue: is used.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   693
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   694
     WARNING: since no resizing is done, the physical amount of memory used
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   695
	      by the container remains the same, although the logical size shrinks.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   696
	      You may want to manually resize the receiver using #emptyCheck."
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   697
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   698
    |next  "{ Class:SmallInteger }" 
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   699
     oldKey|
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   700
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   701
    aValue notNil ifTrue:[
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   702
	keyArray keysAndValuesDo:[:index :aKey |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   703
	    |idx "{Class:SmallInteger}"|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   704
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   705
	    (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   706
		idx := index.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   707
		(self compareSame:(valueArray at:idx) with:aValue) ifTrue:[  
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   708
		    "found it"
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   709
		    valueArray basicAt:idx put:nil.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   710
		    oldKey := keyArray basicAt:idx.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   711
		    keyArray basicAt:idx put:nil.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   712
		    tally := tally - 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   713
		    tally ~~ 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   714
			idx == keyArray basicSize ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   715
			    next := 1
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   716
			] ifFalse:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   717
			    next := index + 1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   718
			].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   719
			(keyArray basicAt:next) notNil ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   720
			    keyArray basicAt:idx put:DeletedEntry
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   721
			].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   722
		    ].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   723
		    ^ oldKey
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   724
		]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   725
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   726
	]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   727
    ].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   728
    ^ aValue
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   729
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   730
    "does NOT work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   731
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   732
	|d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   733
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   734
	d := Dictionary new.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   735
	d at:'one' put:1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   736
	d at:'two' put:2.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   737
	d at:'three' put:3.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   738
	d at:'four' put:4.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   739
	d at:'five' put:5.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   740
	d at:'six' put:6.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   741
	d at:'seven' put:7.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   742
	d at:'eight' put:8.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   743
	d at:'nine' put:9.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   744
	d keysAndValuesDo:[:k :v |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   745
	    v odd ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   746
		d removeValue:v ifAbsent:nil 
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   747
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   748
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   749
	d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   750
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   751
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   752
    "DOES work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   753
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   754
	|d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   755
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   756
	d := Dictionary new.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   757
	d at:'one' put:1.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   758
	d at:'two' put:2.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   759
	d at:'three' put:3.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   760
	d at:'four' put:4.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   761
	d at:'five' put:5.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   762
	d at:'six' put:6.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   763
	d at:'seven' put:7.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   764
	d at:'eight' put:8.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   765
	d at:'nine' put:9.
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   766
	d keysAndValuesDo:[:k :v |
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   767
	    v odd ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   768
		d saveRemoveValue:v 
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   769
	    ]
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   770
	].
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   771
	d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   772
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   773
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   774
    "Created: 1.3.1996 / 21:17:10 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   775
    "Modified: 1.3.1996 / 21:23:04 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   776
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   777
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   778
!Dictionary methodsFor:'converting'!
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   779
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   780
fromLiteralArrayEncoding:encoding
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   781
    "read my values from an encoding.
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   782
     The encoding is supposed to be of the form: 
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   783
        (Dictionary key1 val1 ... keyN valN)"
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   784
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   785
    2 to:encoding size by:2 do:[:i |
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   786
        |key val|
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   787
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   788
        key := encoding at:i.
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   789
        val := encoding at:i+1.
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   790
        self at:key put:val
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   791
    ].
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   792
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   793
    "
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   794
     Dictionary new fromLiteralArrayEncoding:#(Dictionary 'hello' 'world' 1 'foo')
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   795
    "
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   796
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   797
    "Created: / 30.1.1998 / 04:30:47 / cg"
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   798
!
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   799
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   800
literalArrayEncoding
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   801
    |coll|
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   802
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   803
    coll := OrderedCollection new.
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   804
    coll add:(self class name asSymbol).
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   805
    self keysAndValuesDo:[:key :value |
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   806
        coll add:key literalArrayEncoding.    
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   807
        coll add:value literalArrayEncoding.    
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   808
    ].
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   809
    ^ coll asArray
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   810
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   811
    "Created: / 30.1.1998 / 04:28:31 / cg"
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   812
    "Modified: / 30.1.1998 / 04:31:23 / cg"
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   813
! !
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
   814
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   815
!Dictionary methodsFor:'copying'!
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   816
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   817
postCopy
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   818
    "have to copy the valueArray too"
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   819
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   820
    super postCopy.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   821
    valueArray := valueArray shallowCopy
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   822
! !
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   823
217
a0400fdbc933 *** empty log message ***
claus
parents: 155
diff changeset
   824
!Dictionary methodsFor:'enumerating'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   825
a27a279701f8 Initial revision
claus
parents:
diff changeset
   826
allKeysDo:aBlock
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
   827
    "perform the block for all keys in the collection.
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
   828
     Obsolete: use keysDo: for ST-80 compatibility."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   829
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   830
    self obsoleteMethodWarning:'please use #keysDo:'.
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   831
    ^ super do:aBlock
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   832
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   833
    "Modified: 20.4.1996 / 11:22:01 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   834
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   835
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   836
associationsCollect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   837
    "for each key-value pair in the receiver, evaluate the argument, aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   838
     and return a collection with the results.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   839
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   840
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   841
        #keysAndValuesCollect: (which passes separate keys & values)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   842
        #collect:              (which only passes values)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   843
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   844
     This is much like #keysAndValuesCollect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   845
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   846
     #keysAndValuesCollect: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   847
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   848
     WARNING: do not add/remove elements while iterating over the receiver.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   849
              Iterate over a copy to do this."
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   850
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   851
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   852
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   853
    newCollection := OrderedCollection new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   854
    self keysAndValuesDo:[:key :value |
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
   855
        newCollection add:(aBlock value:(Association key:key value:value))
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   856
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   857
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   858
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   859
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   860
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   861
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   862
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   863
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   864
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   865
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   866
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   867
     ages associationsCollect:[:assoc | 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   868
                assoc key , '''s age is ' , assoc value printString]
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   869
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   870
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   871
    "Modified: 20.4.1996 / 11:31:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   872
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   873
a27a279701f8 Initial revision
claus
parents:
diff changeset
   874
associationsDo:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   875
    "perform the block for all associations in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   876
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   877
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   878
        #do:              (which passes values to its block)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   879
        #keysDo:          (which passes only keys to its block)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   880
        #keysAndValuesDo: (which passes keys&values)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   881
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   882
     This is much like #keysAndValuesDo:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   883
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   884
     #keysAndValuesDo: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   885
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   886
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   887
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   888
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   889
    |key n "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   890
a27a279701f8 Initial revision
claus
parents:
diff changeset
   891
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   892
    n := keyArray basicSize.
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   893
    1 to:n do:[:index |
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   894
        key := keyArray basicAt:index.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   895
        (key notNil and:[key ~~ DeletedEntry]) ifTrue:[
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   896
            aBlock value:(Association key:key value:(valueArray basicAt:index))
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   897
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   898
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   899
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   900
    "Modified: 20.4.1996 / 11:31:39 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   901
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   902
2425
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   903
associationsReverseDo:aBlock
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   904
    "perform the block for all associations in the collection.
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   905
     Since dictionary does not define any order of its elements,
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   906
     this is the same as #associationsDo: here.
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   907
     Provided for protocol compatibility with OrderedDictionary"
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   908
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   909
    ^ self associationsDo:aBlock
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   910
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   911
    "Created: 28.2.1997 / 16:08:52 / cg"
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   912
!
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
   913
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   914
associationsSelect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   915
    "return a new collection with all elements from the receiver, for which
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   916
     the argument aBlock evaluates to true. 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   917
     The block gets keys and values as an association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   918
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   919
     See also: #keysAndValuesSelect: (which is slightly faster),
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   920
               #select: (which only passes the value)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   921
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   922
     This is much like #keysAndValuesSelect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   923
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   924
     #keysAndValuesSelect: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   925
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   926
     WARNING: do not add/remove elements while iterating over the receiver.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   927
              Iterate over a copy to do this."
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   928
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   929
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   930
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   931
    newCollection := self species new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   932
    self keysAndValuesDo:[:key :value |
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
   933
        (aBlock value:(Association key:key value:value)) ifTrue:[
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   934
            newCollection at:key put:value
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   935
        ]
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   936
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   937
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   938
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   939
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   940
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   941
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   942
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   943
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   944
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   945
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   946
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   947
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   948
     ages associationsSelect:[:assoc | 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   949
                (assoc key startsWith:'c') or:[assoc value < 30]].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   950
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   951
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   952
    "Modified: 20.4.1996 / 11:31:15 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   953
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   954
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   955
collect:aBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   956
    "for each element in the receiver, evaluate the argument, aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   957
     and return a Bag with the results.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   958
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   959
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   960
        #associationsCollect:   (which passes key-value associations)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   961
        #keysAndValuesCollect:  (which passes keys & values separately)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   962
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   963
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   964
              Iterate over a copy to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   965
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   966
    |newCollection|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   967
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   968
    newCollection := Bag new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   969
    self do:[:each |
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   970
        newCollection add:(aBlock value:each)
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   971
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   972
    ^ newCollection
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   973
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   974
    "Modified: 20.4.1996 / 11:29:58 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   975
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   976
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   977
do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   978
    "perform the block for all values in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   979
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   980
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   981
        #associationsDo:   (which passes key-value associations)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   982
        #keysAndValuesDo:  (which passes keys & values separately)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   983
        #keysDo:           (which passes keys only)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   984
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   985
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   986
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   987
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   988
    |key n "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   989
a27a279701f8 Initial revision
claus
parents:
diff changeset
   990
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   991
    n := keyArray basicSize.
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
   992
    1 to:n do:[:index |
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   993
        key := keyArray basicAt:index.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   994
        (key notNil and:[key ~~ DeletedEntry]) ifTrue:[
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   995
            aBlock value:(valueArray basicAt:index)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   996
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   997
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
   998
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   999
    "Modified: 20.4.1996 / 11:32:11 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1000
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1001
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1002
keysAndValuesCollect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1003
    "for each key-value pair in the receiver, evaluate the argument, aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1004
     and return a collection with the results.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1005
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1006
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1007
        #associationsCollect:  (which passes keys->value pairs)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1008
        #collect:              (which only passes values)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1009
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1010
     This is much like #associationsCollect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1011
     key and value as two separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1012
     #associationsCollect: is a bit slower.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1013
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1014
     WARNING: do not add/remove elements while iterating over the receiver.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1015
              Iterate over a copy to do this."
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1016
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1017
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1018
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1019
    newCollection := OrderedCollection new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1020
    self keysAndValuesDo:[:key :value |
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1021
        newCollection add:(aBlock value:key value:value)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1022
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1023
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1024
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1025
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1026
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1027
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1028
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1029
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1030
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1031
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1032
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1033
     ages keysAndValuesCollect:[:name :age | 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1034
                name , '''s age is ' , age printString]
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1035
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1036
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1037
    "Modified: 20.4.1996 / 11:33:50 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1038
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1039
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1040
keysAndValuesDo:aTwoArgBlock
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1041
    "evaluate the argument, aBlock for every element in the collection,
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1042
     passing both key and element as arguments.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1043
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1044
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1045
        #associationsDo:       (which passes keys->value pairs)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1046
        #do:                   (which only passes values)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1047
        #keysDo:               (which only passes keys)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1048
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1049
     This is much like #associationsDo:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1050
     key and value as two separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1051
     #associationsDo: is a bit slower.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1052
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1053
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1054
              Iterate over a copy to do this."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1055
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1056
    |key n "{ Class: SmallInteger }"|
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1057
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1058
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1059
    n := keyArray basicSize.
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1060
    1 to:n do:[:index |
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1061
        key := keyArray basicAt:index.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1062
        (key notNil and:[key ~~ DeletedEntry]) ifTrue:[
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1063
            aTwoArgBlock value:key value:(valueArray basicAt:index)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1064
        ].
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1065
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1066
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1067
    "Modified: 20.4.1996 / 11:33:42 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1068
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1069
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1070
keysAndValuesSelect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1071
    "return a new collection with all elements from the receiver, for which
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1072
     the argument aBlock evaluates to true. 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1073
     The block gets keys and values as separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1074
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1075
     See also: 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1076
        #associationsSelect:    (which passes key-value pairs),
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1077
        #select:                (which only passes the value)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1078
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1079
     This is much like #associationsSelect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1080
     key and value as two separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1081
     #associationsSelect: is a bit slower.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1082
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1083
     WARNING: do not add/remove elements while iterating over the receiver.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1084
              Iterate over a copy to do this."
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1085
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1086
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1087
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1088
    newCollection := self species new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1089
    self keysAndValuesDo:[:key :value |
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1090
        (aBlock value:key value:value) ifTrue:[
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1091
            newCollection at:key put:value
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1092
        ]
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1093
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1094
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1095
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1096
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1097
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1098
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1099
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1100
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1101
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1102
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1103
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1104
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1105
     ages keysAndValuesSelect:[:name :age | 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1106
                (name startsWith:'c') or:[age < 30]].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1107
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1108
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1109
    "Modified: 20.4.1996 / 11:34:29 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1110
!
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1111
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1112
keysDo:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1113
    "perform the block for all keys in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1114
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1115
     See also:
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1116
        #associationsDo:   (which passes key-value associations)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1117
        #keysAndValuesDo:  (which passes keys & values separately)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1118
        #do:               (which passes values only)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1119
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1120
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1121
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1122
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1123
    ^ super do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1124
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1125
    "Modified: 20.4.1996 / 11:34:45 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1126
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1127
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1128
select:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1129
    "return a new collection with all elements from the receiver, for which
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1130
     the argument aBlock evaluates to true. The block gets the individual values
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1131
     as its single argument.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1132
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1133
     See also: 
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1134
        #associationsSelect:            (which passes key->value associations),
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1135
        #keysAndValuesSelect:           (which passes key & value args)
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1136
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1137
     WARNING: do not add/remove elements while iterating over the receiver.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1138
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1139
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1140
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1141
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1142
    newCollection := self species new.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1143
    self keysAndValuesDo:[:key :value |
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1144
        (aBlock value:value) ifTrue:[
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1145
            newCollection at:key put:value
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1146
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1147
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1148
    ^ newCollection
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1149
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1150
    "
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1151
     |d|
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1152
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1153
     d := Dictionary new.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1154
     d at:#foo put:#bar.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1155
     d at:#bar put:#baz.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1156
     d at:#baz put:#foo.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1157
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1158
     d select:[:el | el startsWith:'b'].
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1159
    "
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1160
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1161
    "Modified: 20.4.1996 / 11:35:07 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1162
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1163
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1164
!Dictionary methodsFor:'inspecting'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1165
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1166
inspectorClass
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1167
    "redefined to use DictionaryInspector
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1168
     (instead of the default Inspector)."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1169
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1170
    ^ DictionaryInspectorView
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1171
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1172
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1173
!Dictionary methodsFor:'printing & storing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1174
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  1175
printElementsDo:aBlock
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  1176
    "redefined, so #printOn: prints associations"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1177
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  1178
    ^ self associationsDo:aBlock
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1179
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  1180
    "Created: / 20.1.1998 / 14:11:02 / stefan"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1181
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1182
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1183
storeOn:aStream
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1184
    "output a printed representation (which can be re-read)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1185
     onto the argument aStream"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1186
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1187
    |isEmpty|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1188
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1189
    thisContext isRecursive ifTrue:[
2288
033f06bf1bab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2094
diff changeset
  1190
        ('Dictionary [error]: storeOn: of self referencing collection.') errorPrintCR.
1417
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1191
        aStream nextPutAll:'#recursive'.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1192
        ^ self
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1193
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1194
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1195
    aStream nextPutAll:'('.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1196
    aStream nextPutAll:(self class name).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1197
    aStream nextPutAll:' new'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1198
    isEmpty := true.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1199
    self keysAndValuesDo:[:key :value |
1417
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1200
        aStream nextPutAll:' at:'.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1201
        key storeOn:aStream.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1202
        aStream nextPutAll:' put:'.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1203
        value storeOn:aStream.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1204
        aStream nextPutAll:'; '.
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1205
        isEmpty := false
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1206
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1207
    isEmpty ifFalse:[aStream nextPutAll:' yourself'].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1208
    aStream nextPut:$)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1209
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1210
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1211
     Dictionary new storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1212
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1213
     (Dictionary new at:1 put:'hello'; yourself) storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1214
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1215
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1216
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1217
     |d|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1218
     d := Dictionary new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1219
     d at:1 put:'hello'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1220
     d at:'hello' put:#world.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1221
     d storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1222
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1223
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1224
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1225
     |d|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1226
     d := Dictionary new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1227
     d at:1 put:'hello'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1228
     d at:'hello' put:#world.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1229
     d at:2 put:d.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1230
     d storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1231
    "
1417
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  1232
2288
033f06bf1bab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2094
diff changeset
  1233
    "Modified: 28.1.1997 / 00:37:46 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1234
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1235
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1236
!Dictionary methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1237
345
claus
parents: 302
diff changeset
  1238
compareSame:element1 with:element2
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1239
    "compare two elements for being the same. Here, return true if the
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1240
     elements are equal (i.e. using #=).
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1241
     Redefinable in subclasses."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1242
345
claus
parents: 302
diff changeset
  1243
    ^ element1 = element2
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1244
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1245
    "Modified: 22.4.1996 / 17:34:27 / cg"
345
claus
parents: 302
diff changeset
  1246
!
claus
parents: 302
diff changeset
  1247
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1248
emptyCollectionForKeys
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1249
    "return an empty collection to hold keys. Here, a Set is returned.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1250
     Redefinable in subclasses."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1251
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1252
    ^ Set new:(self size)
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1253
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1254
    "Modified: 22.4.1996 / 17:35:17 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1255
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1256
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1257
grow:newSize
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1258
    "grow the receiver to make space for at least newSize elements.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1259
     To do this, we have to rehash into the new arrays.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1260
     (which is done by re-adding all elements to a new, empty key/value array pair)."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1261
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1262
    |key deletedEntry oldKeyArray oldValueArray n
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1263
     oldSize  "{ Class:SmallInteger }" 
10
claus
parents: 5
diff changeset
  1264
     newIndex "{ Class:SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1265
10
claus
parents: 5
diff changeset
  1266
    oldKeyArray := keyArray.
claus
parents: 5
diff changeset
  1267
    oldValueArray := valueArray.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1268
249
claus
parents: 217
diff changeset
  1269
    n := self class goodSizeFrom:newSize.
claus
parents: 217
diff changeset
  1270
    oldSize := oldKeyArray size.
claus
parents: 217
diff changeset
  1271
    n == oldSize ifTrue:[^ self].
claus
parents: 217
diff changeset
  1272
10
claus
parents: 5
diff changeset
  1273
    keyArray := self keyContainerOfSize:n.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1274
    valueArray := self valueContainerOfSize:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1275
249
claus
parents: 217
diff changeset
  1276
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1277
    deletedEntry := DeletedEntry.
10
claus
parents: 5
diff changeset
  1278
    1 to:oldSize do:[:index |
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1279
	key := oldKeyArray basicAt:index.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1280
	(key notNil and:[key ~~ deletedEntry]) ifTrue:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1281
	    newIndex := self findNil:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1282
	    keyArray basicAt:newIndex put:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1283
	    valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1284
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1285
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1286
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1287
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1288
rehash
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1289
    "rehash contents - is done by re-adding all elements to a new, empty key/value array pair)."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1290
2
claus
parents: 1
diff changeset
  1291
    | oldKeyArray oldValueArray key 
claus
parents: 1
diff changeset
  1292
      n        "{ Class:SmallInteger }"
claus
parents: 1
diff changeset
  1293
      newIndex "{ Class:SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1294
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1295
    oldKeyArray := keyArray.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1296
    oldValueArray := valueArray.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1297
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1298
    n := keyArray size.
10
claus
parents: 5
diff changeset
  1299
    keyArray := self keyContainerOfSize:n.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  1300
    valueArray := self valueContainerOfSize:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1301
2
claus
parents: 1
diff changeset
  1302
    1 to:n do:[:index |
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1303
	key := oldKeyArray basicAt:index.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1304
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1305
	    newIndex := self findNil:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1306
	    keyArray basicAt:newIndex put:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1307
	    valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1308
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1309
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1310
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1311
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1312
rehashFrom:startIndex
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1313
    "rehash elements starting at index - after a remove.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1314
     NOTE: this method is no longer needed; 
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1315
	   the trick using DeletedEntry avoids the need to do this time 
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1316
	   consuming operation, making remove pretty fast :-)
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1317
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1318
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1319
    |key i length
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1320
     index "{ Class:SmallInteger }" |
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1321
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1322
    length := keyArray basicSize.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1323
    index := startIndex.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1324
    key := keyArray basicAt:index.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1325
    [key notNil] whileTrue:[
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1326
	key ~~ DeletedEntry ifTrue:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1327
	    i := self findNil:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1328
	    i == index ifTrue:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1329
		^ self
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1330
	    ].
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1331
	    keyArray basicAt:i put:key.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1332
	    valueArray basicAt:i put:(valueArray basicAt:index).
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1333
	    keyArray basicAt:index put:nil.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1334
	    valueArray basicAt:index put:nil.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1335
	].
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1336
	index == length ifTrue:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1337
	    index := 1
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1338
	] ifFalse:[
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1339
	    index := index + 1.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1340
	].
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1341
	key := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1342
    ]
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1343
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1344
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1345
setTally:count
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1346
    "initialize the contents array (for at least count slots)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1347
     and set tally to zero.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1348
     The size is increased to the next prime for better hashing behavior."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1349
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1350
    |n|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1351
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1352
    n := self class goodSizeFrom:count.
3727
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1353
    n == keyArray size ifTrue:[
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1354
        keyArray atAllPut:nil.
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1355
        valueArray atAllPut:nil.
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1356
    ] ifFalse:[
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1357
        keyArray := self keyContainerOfSize:n.
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1358
        valueArray := self valueContainerOfSize:n.
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1359
    ].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1360
    tally := 0
3727
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1361
2b05022de748 try to avoid allocation of a new key/valueArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3319
diff changeset
  1362
    "Modified: / 5.8.1998 / 10:48:51 / cg"
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  1363
!
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  1364
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1365
valueContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1366
    "return a container for values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1367
     Extracted to make life of weak subclasses easier ..."
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  1368
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1369
    ^ Array basicNew:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1370
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1371
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1372
!Dictionary methodsFor:'searching'!
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1373
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1374
findFirstKey:aBlock
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1375
    "find and return the first key, for which evaluation of the argument, aBlock
1703
9337c2c135d7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1460
diff changeset
  1376
     returns true; return nil if none is detected."
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1377
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1378
    self keysDo:[:key |
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1379
        (aBlock value:key) ifTrue:[^ key].
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1380
    ].
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1381
    ^ nil
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1382
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1383
    "Created: 5.6.1996 / 11:55:50 / stefan"
1703
9337c2c135d7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1460
diff changeset
  1384
    "Modified: 8.10.1996 / 22:01:59 / cg"
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1385
! !
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  1386
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1387
!Dictionary methodsFor:'testing'!
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  1388
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1389
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1390
    "return true, if the argument, aValue is stored in the dictionary,
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1391
     i.e. if there is an associaten, with aValue as value.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1392
     This is a slow search, since there is no fast reverse mapping;
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1393
     the values have to be all scanned without any hashing.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1394
     You need a special collection (or two Dictionaries) to get this
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1395
     reverse mapping fast."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1396
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1397
"/ OLD:
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1398
"/    ^ self includesKey:(anObject key)
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1399
"/ NEW:
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1400
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1401
    ^ self includesValue:anObject
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1402
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  1403
    "Modified: 22.4.1996 / 17:20:11 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1404
!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  1405
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1406
includesAssociation:anAssociation
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1407
    "return true, if there is an association in the receiver with the
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1408
     same key and value as the argument, anAssociation.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1409
     NOTICE: in contrast to #includes:, this compares both key and value."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1410
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1411
    |val|
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  1412
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1413
    val := self at:(anAssociation key) ifAbsent:[^ false].
3318
d8173a888c93 oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents: 3304
diff changeset
  1414
    ^ self compareSame:val with:anAssociation value
d8173a888c93 oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents: 3304
diff changeset
  1415
d8173a888c93 oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents: 3304
diff changeset
  1416
    "Modified: / 5.3.1998 / 20:35:00 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1417
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1418
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1419
includesKey:aKey
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1420
    "return true, if the argument, aKey is a key in the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1421
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1422
    ^ (self find:aKey ifAbsent:0) ~~ 0
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1423
!
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1424
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1425
includesValue:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1426
    "return true, if the argument, aValue is stored in the dictionary,
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1427
     i.e. if there is an associaten, with aValue as value.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1428
     This is a slow search, since there is no fast reverse mapping;
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1429
     the values have to be all scanned without any hashing.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1430
     You need a special collection (or two Dictionaries) to get this
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1431
     reverse mapping fast."
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1432
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1433
    ^ valueArray includes:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1434
!
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1435
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1436
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1437
    "count & return how often anObject is stored in the dictionary.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1438
     This counts values - not keys."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1439
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1440
    ^ valueArray occurrencesOf:anObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1441
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1442
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
  1443
!Dictionary class methodsFor:'documentation'!
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1444
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1445
version
4871
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
  1446
    ^ '$Header: /cvs/stx/stx/libbasic/Dictionary.st,v 1.61 1999-10-07 11:37:14 cg Exp $'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1447
! !