Dictionary.st
author HG Automerge
Tue, 10 Jan 2017 13:26:39 +0000
branchjv
changeset 21251 32f12bea6608
parent 21242 19fabe339f8b
child 21292 21faad473411
permissions -rw-r--r--
Merge
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
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
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
"
5362
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
    12
"{ Package: 'stx:libbasic' }"
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
    13
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
    14
"{ NameSpace: Smalltalk }"
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
    15
12
8e03bd717355 *** empty log message ***
claus
parents: 10
diff changeset
    16
Set subclass:#Dictionary
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
    17
	instanceVariableNames:'valueArray'
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
    18
	classVariableNames:''
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
    19
	poolDictionaries:''
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
    20
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
    23
!Dictionary class methodsFor:'documentation'!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    24
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
 COPYRIGHT (c) 1991 by Claus Gittinger
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
    28
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    39
documentation
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    40
"
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    41
    a Dictionary is (conceptionally) a set of Associations storing key-value pairs.
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    42
    (The implementation uses two arrays to store the keys and values separately.)
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    43
    Searching for an element is done using a hash into the key array.
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
    44
    Another way of looking at a dictionary is as an array that uses
345
claus
parents: 302
diff changeset
    45
    arbitrary access keys (i.e. not just integers as arrays do).
claus
parents: 302
diff changeset
    46
claus
parents: 302
diff changeset
    47
    Since the keys are unordered, no internal element order is defined
362
claus
parents: 360
diff changeset
    48
    (i.e. enumerating them may return elements in any order - even changing
345
claus
parents: 302
diff changeset
    49
     over time).
claus
parents: 302
diff changeset
    50
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    51
    Many methods for searching and hashing are inherited from Set.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    52
1279
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    53
    [Instance variables:]
345
claus
parents: 302
diff changeset
    54
15800
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    55
        keyArray        <Array>         (from Set) the keys
1279
ad7a3786e73a commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    56
15800
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    57
        valueArray      <Array>         the values ('valueArray at:index' corresponds
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    58
                                        to the value stored under 'keyArray at:index')
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    59
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
    60
    Performance hints:
362
claus
parents: 360
diff changeset
    61
      since the dictionary does not really store associations internally,
claus
parents: 360
diff changeset
    62
      it is less efficient, to store/retrieve associations. The reason is
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
    63
      that these assocs are created temporarily in some extract methods.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
    64
      I.e. 'at:key put:value' is faster than 'add:anAssoc'
345
claus
parents: 302
diff changeset
    65
      and 'keysAndValuesDo:' is faster than 'associationsDo:' etc.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
    66
362
claus
parents: 360
diff changeset
    67
      If only symbols or smallIntegers are used as keys, use IdentityDictionaries
claus
parents: 360
diff changeset
    68
      for slightly better performance, since both hashing and comparison is faster.
345
claus
parents: 302
diff changeset
    69
claus
parents: 302
diff changeset
    70
      If you have a rough idea how big the dictionary is going to grow,
claus
parents: 302
diff changeset
    71
      create it using #new: instead of #new. Even if the size given is a
claus
parents: 302
diff changeset
    72
      poor guess (say half of the real size), there is some 20-30% performance
claus
parents: 302
diff changeset
    73
      win to expect, since many resizing operations are avoided when associations
claus
parents: 302
diff changeset
    74
      are added.
362
claus
parents: 360
diff changeset
    75
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
    76
    Special note:
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
    77
      in previous versions, nil was not allowed as valid key
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
    78
      This has been changed; internally, a special nil-key is used,
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
    79
      which is converted back to nil whenever keys are accessed.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
    80
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    81
    [See also:]
15800
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    82
        Set, IdentityDictionary, IdentitySet, WeakIdentitySet and
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    83
        WeakIdentityDictionary
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    84
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1279
diff changeset
    85
    [author:]
15800
be0b36a7c1e2 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15786
diff changeset
    86
        Claus Gittinger
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
    87
"
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    88
!
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    89
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    90
examples
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    91
"
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
    92
                                                                        [exBegin]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    93
    |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    94
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    95
    d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    96
    d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
    97
    d at:2 put:'two'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
    98
    d at:2
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
    99
                                                                        [exEnd]
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   100
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   101
                                                                        [exBegin]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   102
    |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   103
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   104
    d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   105
    d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   106
    d at:2   put:nil.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   107
    d.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   108
    d at:2
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   109
                                                                        [exEnd]
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   110
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   111
                                                                        [exBegin]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   112
    |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   113
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   114
    d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   115
    d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   116
    d at:2   put:nil.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   117
    d includes:nil.
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   118
                                                                        [exEnd]
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   119
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   120
                                                                        [exBegin]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   121
    |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   122
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   123
    d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   124
    d at:'1' put:'one'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   125
    d includes:nil.
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   126
                                                                        [exEnd]
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   127
                                                                        [exBegin]
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   128
    |d1 d2|
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   129
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   130
    d1 := Dictionary withKeys:#(a b c) andValues:#( 1 2 3).
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   131
    d2 := Dictionary newFrom:d1.
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   132
    d2.
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   133
                                                                        [exEnd]
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   134
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   135
"
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   136
! !
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   137
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
   138
!Dictionary class methodsFor:'instance creation'!
345
claus
parents: 302
diff changeset
   139
6100
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   140
decodeFromLiteralArray:anArray
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   141
    "create & return a new instance from information encoded in anArray."
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   142
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   143
    |dictionary
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8434
diff changeset
   144
     sz "{ Class: SmallInteger }"|
6100
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   145
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8434
diff changeset
   146
    sz := anArray size.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8434
diff changeset
   147
    dictionary := self new:sz//2.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8434
diff changeset
   148
    2 to:sz by:2 do:[:idx | |key val|
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   149
	key := (anArray at:idx) decodeAsLiteralArray.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   150
	val := (anArray at:idx+1) decodeAsLiteralArray.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   151
	dictionary at:key put:val
6100
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   152
    ].
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8434
diff changeset
   153
    ^ dictionary
6100
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   154
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   155
    "
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   156
     (Dictionary new
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   157
	 at:1 put:'one';
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   158
	 at:2 put:'two';
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   159
	 yourself
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   160
     ) literalArrayEncoding decodeAsLiteralArray
6100
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   161
    "
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   162
!
a3af162517fe add method decodeFromLiteralArray:
ab
parents: 6053
diff changeset
   163
19138
94f01d8445d3 #DOCUMENTATION
mawalch
parents: 19123
diff changeset
   164
withAssociations:aCollectionOfAssociations
5255
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   165
    "return a new instance where associations are taken from the argument"
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   166
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   167
    |newDict sz "{ Class: SmallInteger }"|
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   168
19138
94f01d8445d3 #DOCUMENTATION
mawalch
parents: 19123
diff changeset
   169
    sz := aCollectionOfAssociations size.
5255
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   170
    newDict := self new:sz.
19138
94f01d8445d3 #DOCUMENTATION
mawalch
parents: 19123
diff changeset
   171
    aCollectionOfAssociations do:[:assoc |
94f01d8445d3 #DOCUMENTATION
mawalch
parents: 19123
diff changeset
   172
        newDict at:assoc key put:assoc value
5255
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   173
    ].
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   174
    ^ newDict
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   175
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   176
    "
20972
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
   177
     Dictionary withAssociations:{ #'one'->1 .
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
   178
                                   #'two'->2 .
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
   179
                                   #'three'->3 .
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
   180
                                   #'four'->4 }
5255
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   181
    "
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   182
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   183
    "Created: / 11.2.2000 / 10:05:54 / cg"
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   184
!
cc5c0c1458aa added #withAssociations
Claus Gittinger <cg@exept.de>
parents: 5184
diff changeset
   185
357
claus
parents: 345
diff changeset
   186
withKeys:keyArray andValues:valueArray
claus
parents: 345
diff changeset
   187
    "return a new instance where keys and values are taken from
claus
parents: 345
diff changeset
   188
     the argumentArrays."
claus
parents: 345
diff changeset
   189
claus
parents: 345
diff changeset
   190
    |newDict sz "{ Class: SmallInteger }"|
claus
parents: 345
diff changeset
   191
claus
parents: 345
diff changeset
   192
    sz := keyArray size.
claus
parents: 345
diff changeset
   193
    newDict := self new:sz.
20066
67c2e813b4e5 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19845
diff changeset
   194
    1 to:sz do:[:index |
67c2e813b4e5 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19845
diff changeset
   195
        newDict at:(keyArray at:index) put:(valueArray at:index).
357
claus
parents: 345
diff changeset
   196
    ].
claus
parents: 345
diff changeset
   197
    ^ newDict
claus
parents: 345
diff changeset
   198
claus
parents: 345
diff changeset
   199
    "
claus
parents: 345
diff changeset
   200
     Dictionary withKeys:#('one' 'two' 'three' 'four')
20066
67c2e813b4e5 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19845
diff changeset
   201
               andValues:#(1 2 3 4)
357
claus
parents: 345
diff changeset
   202
    "
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   203
!
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   204
19224
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   205
withKeys:aCollection valueBlock:aOneArgBlock
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   206
    "return a Dictionary with keys from aCollection's elements,
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   207
     using aOneArgBlock to generate the values from aCollection's elements."
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   208
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   209
    |d|
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   210
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   211
    d := self new:aCollection size.
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   212
    aCollection do:[:each|
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   213
        d at:each put:(aOneArgBlock value:each).
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   214
    ].
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   215
    ^ d
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   216
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   217
    "
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   218
     Dictionary withKeys:#(10 20 30 40 50 60 70 80 90) valueBlock:[:e| e asString]
19224
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   219
    "
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   220
!
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   221
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   222
withKeysAndValues:anArray
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   223
    "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
   224
     elements of anArray"
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   225
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   226
    |newDict sz "{ Class: SmallInteger }"|
345
claus
parents: 302
diff changeset
   227
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   228
    sz := anArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   229
    newDict := self new:(sz // 2).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   230
    1 to:sz by:2 do:[:i |
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
   231
	newDict at:(anArray at:i) put:(anArray at:i+1)
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   232
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   233
    ^ newDict
345
claus
parents: 302
diff changeset
   234
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   235
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   236
     Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   237
    "
19224
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   238
!
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   239
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   240
withValues:aCollection keyBlock:aOneArgBlock
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   241
    "return a Dictionary with values from aCollection's elements,
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   242
     using aOneArgBlock to generate the keys from aCollection's elements."
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   243
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   244
    |d|
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   245
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   246
    d := self new:aCollection size.
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   247
    aCollection do:[:each|
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   248
        d at:(aOneArgBlock value:each) put:each.
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   249
    ].
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   250
    ^ d
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   251
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   252
    "
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   253
     Dictionary withValues:#(10 20 30 40 50 60 70 80 90) keyBlock:[:e| e asString]
19224
9799d5bedd22 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 19138
diff changeset
   254
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
7306
909d88d8476f category
Claus Gittinger <cg@exept.de>
parents: 6233
diff changeset
   257
!Dictionary class methodsFor:'Compatibility-Squeak'!
5362
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   258
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   259
newFrom:aCollectionOfAssociations
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   260
    "return a new instance where associations are taken from the argument"
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   261
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   262
    aCollectionOfAssociations isDictionary ifTrue:[
20170
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   263
        ^ (self new:aCollectionOfAssociations size)
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   264
                declareAllFrom:aCollectionOfAssociations.
19123
fc42a1e0c26b #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19036
diff changeset
   265
    ].    
20170
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   266
    ^ self withAssociations:aCollectionOfAssociations
5362
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   267
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   268
    "
20170
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   269
     Dictionary newFrom:{#foo -> #Foo. #bar -> #Bar}
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   270
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   271
     Dictionary
20170
0188f6e3daaa #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 20168
diff changeset
   272
        newFrom:(Dictionary withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4))
5362
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   273
    "
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   274
! !
a6b34f66b25f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 5336
diff changeset
   275
12732
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   276
!Dictionary methodsFor:'Compatibility-Dolphin'!
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   277
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   278
equals:aDictionary
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   279
    ^ self = aDictionary
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   280
! !
93312b4b2d17 added: #equals:
Claus Gittinger <cg@exept.de>
parents: 12669
diff changeset
   281
17671
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   282
!Dictionary methodsFor:'Compatibility-VW5.4'!
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   283
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   284
contentsEquals: aDictionary
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   285
    "Anwer true if  the receiver and aDictionary contain the same key/values.
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   286
     (ignoring the classes)"
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   287
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   288
    self size == aDictionary size ifFalse: [ ^false ].
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   289
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   290
    self keysAndValuesDo: [ :key :value |
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   291
        ( aDictionary at: key ifAbsent: [ ^false ] ) = value
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   292
        ifFalse: [ ^false ]
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   293
    ].
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   294
    ^ true
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   295
! !
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
   296
20972
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
   297
20989
42266e2b5d40 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20972
diff changeset
   298
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   299
!Dictionary methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   301
associationAt:aKey
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   302
    "return an association consisting of aKey and the element indexed
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   303
     by aKey -
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   304
     report an error, if no element is stored under aKey"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   305
15008
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   306
    ^ self associationAt:aKey ifAbsent:[self errorKeyNotFound:aKey]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   307
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   308
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   309
associationAt:aKey ifAbsent:exceptionBlock
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   310
    "return an association consisting of aKey and the element indexed by aKey -
14971
04b3466c98b7 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14897
diff changeset
   311
     return result of exceptionBlock if no element is stored under aKey.
04b3466c98b7 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14897
diff changeset
   312
     Warning: this is a comatibility interface only, with a different semantic as
04b3466c98b7 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14897
diff changeset
   313
              the original ST80 implementation. The returned assoc is created on the fly,
04b3466c98b7 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14897
diff changeset
   314
              and not the one stored in the receiver (there are not assocs there)"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   315
15008
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   316
    |index|
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   317
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   318
    "/ must return the real key in the assoc - not aKey, which might be equal but not identical
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   319
    index := self find:aKey ifAbsent:0.
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   320
    index ~~ 0 ifTrue:[
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   321
        ^ Association key:(keyArray basicAt:index) value:(valueArray basicAt:index)
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   322
    ].
a474f18209d4 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 14997
diff changeset
   323
    ^ exceptionBlock value
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   324
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   325
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   326
associations
19468
0164eb8f7c28 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19275
diff changeset
   327
    "return an ordered collection containing the receiver's associations."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   328
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   329
    |coll|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   330
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   331
    coll := OrderedCollection new:(keyArray size).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   332
    self associationsDo:[:assoc | coll add:assoc].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   333
    ^ coll
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   334
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   335
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   336
at:aKey
a27a279701f8 Initial revision
claus
parents:
diff changeset
   337
    "return the element indexed by aKey - report an error if none found"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   338
6233
0a79cbd07543 *** empty log message ***
martin
parents: 6101
diff changeset
   339
    ^ self at:aKey ifAbsent:[self errorKeyNotFound:aKey]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   340
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   341
a27a279701f8 Initial revision
claus
parents:
diff changeset
   342
at:aKey ifAbsent:exceptionBlock
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   343
    "return the element indexed by aKey -
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
   344
     return result of exceptionBlock if no element is stored under aKey"
2
claus
parents: 1
diff changeset
   345
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   346
    |index k|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   347
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   348
    (k := aKey) isNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   349
	"/ nil is not allowed as key
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   350
	"/
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   351
	"/ previous versions of ST/X raised an error
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   352
	"/ here. However, there seem to exist applications
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   353
	"/ which depend on getting the exceptionBlocks value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   354
	"/ in this case ... well ...
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   355
	"/ ^ self errorInvalidKey:aKey
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   356
"/ no longer invalid.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   357
"/      ^ exceptionBlock value
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   358
	k := NilEntry
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
   359
    ].
362
claus
parents: 360
diff changeset
   360
claus
parents: 360
diff changeset
   361
    "/ I could have written:
claus
parents: 360
diff changeset
   362
    "/ index := self find:aKey ifAbsent:[^ exceptionBlock value]
claus
parents: 360
diff changeset
   363
    "/ but the code below is slighlty more efficient, since it avoids
1146
edadea7273b6 commentary
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
   364
    "/ a block creation - thus speeding up the good case.
362
claus
parents: 360
diff changeset
   365
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   366
    index := self find:k 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
   367
    index ~~ 0 ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   368
	^ valueArray basicAt:index
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
   369
    ].
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
   370
    ^ exceptionBlock value.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   371
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   372
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   373
at:aKey ifAbsent:default update:aBlock
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   374
    "update the element stored under aKey with the result from
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   375
     evaluating aBlock with the previous stored value as argument, or with default,
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   376
     if there was no such key initially.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   377
     I.e. this is the same as self at:aKey put:(aBlock value:(self at:aKey ifAbsent:default)).
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   378
     Return the new value stored.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   379
     This is an optimized accessor, which only computes the hash value once."
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   380
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   381
    |k index "{ Class: SmallInteger }"
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   382
     newValue oldKeyArray oldKey|
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   383
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   384
    (k := aKey) isNil ifTrue:[
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   385
        k := NilEntry
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   386
    ].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   387
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   388
    oldKeyArray := keyArray.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   389
    index := self findKeyOrNilOrDeletedEntry:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   390
    oldKey := keyArray basicAt:index.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   391
    (oldKey notNil and:[oldKey ~~ DeletedEntry]) ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   392
        "/ key is present
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   393
        newValue := aBlock value:(valueArray basicAt:index).
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   394
    ] ifFalse:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   395
        "/ a new key
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   396
        newValue := aBlock value:default.
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   397
    ].
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   398
    (keyArray ~~ oldKeyArray or:[(keyArray basicAt:index) ~~ oldKey]) ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   399
        "I have been changed while performing aBlock.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   400
         have to find the key again."
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   401
        index := self findKeyOrNil:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   402
    ].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   403
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   404
    valueArray basicAt:index put:newValue.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   405
    oldKey := keyArray basicAt:index.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   406
    (oldKey isNil or:[oldKey == DeletedEntry]) ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   407
        "key is not or no longer present"
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   408
        keyArray basicAt:index put:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   409
        tally := tally + 1.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   410
        self possiblyGrow.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   411
    ].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   412
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   413
    ^ newValue
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   414
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   415
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   416
     |d|
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   417
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   418
     d := Dictionary new.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   419
     d at:'one'  ifAbsent:0 update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   420
     d at:'two'  ifAbsent:0 update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   421
     d at:'three' ifAbsent:0  update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   422
     d at:'two' ifAbsent:0  update:[:val | val + 1].
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   423
     d at:'three' ifAbsent:0  update:[:val | val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   424
     d at:'three' ifAbsent:0  update:[:val | val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   425
     d
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   426
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   427
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   428
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   429
     |d|
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   430
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   431
     d := Dictionary new.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   432
     d at:'two'  ifAbsent:0 update:[:val | val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   433
     d at:'two'  ifAbsent:0 update:[:val | 1 to:30 do:[:idx| d at:idx printString put:idx]. val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   434
     d
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   435
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   436
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   437
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   438
     |d|
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   439
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   440
     d := Dictionary new.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   441
     d at:'two'  ifAbsent:0 update:[:val | val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   442
     d at:'two'  ifAbsent:0 update:[:val | d removeKey:'two'. val + 1].
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   443
     d
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   444
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   445
!
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   446
3304
19ee5d1685d8 Make #at:ifAbsentPut: ST80 (and STBroker compatible).
Stefan Vogel <sv@exept.de>
parents: 3252
diff changeset
   447
at:aKey ifAbsentPut:valueBlock
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   448
    "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
   449
     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
   450
     under aKey and return it.
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   451
     WARNING: do not add elements while iterating over the receiver.
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
   452
              Iterate over a copy to do this."
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   453
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   454
    |index "{ Class: SmallInteger }"
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   455
     k newValue oldKeyArray probeKey|
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   456
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   457
    (k := aKey) isNil ifTrue:[
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
   458
        k := NilEntry
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   459
    ].
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   460
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   461
    index := self findKeyOrNilOrDeletedEntry:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   462
    probeKey := keyArray basicAt:index.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   463
    (probeKey notNil and:[probeKey ~~ DeletedEntry]) ifTrue:[
20224
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   464
        "/ key is already present
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
   465
        ^ valueArray at:index.
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   466
    ].
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   467
20224
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   468
    "/ a new key
19545
354bb04991c7 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 19468
diff changeset
   469
    oldKeyArray := keyArray.
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   470
    newValue := valueBlock value.
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   471
    (keyArray ~~ oldKeyArray or:[(keyArray basicAt:index) ~~ probeKey]) ifTrue:[
20224
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   472
        "I have been changed while performing the valueBlock.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   473
         have to find the key again."
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   474
        index := self findKeyOrNil:k.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   475
        (keyArray basicAt:index) notNil ifTrue:[
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   476
            "/ key was not, but is now present.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   477
            "/ since we executed the valueBlock, overwrite the value in the Dictionary
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   478
            valueArray at:index put:newValue.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   479
            ^ newValue
20224
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   480
        ].
19545
354bb04991c7 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 19468
diff changeset
   481
    ].
20224
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   482
    "/ a new key...
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   483
    keyArray basicAt:index put:k.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   484
    valueArray basicAt:index put:newValue.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   485
    tally := tally + 1.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   486
    self possiblyGrow.
d075f60fbd7b #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20170
diff changeset
   487
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   488
    ^ newValue
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   489
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   490
    "
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   491
     |d|
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   492
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   493
     d := Dictionary new.
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   494
     Transcript showCR:(d at:'foo' ifAbsentPut:'bar').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   495
     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   496
     Transcript showCR:(d at:'foo' ifAbsentPut:'barX').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   497
     Transcript showCR:(d at:'foo2' ifAbsentPut:'bar2X').
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   498
    "
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   499
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   500
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   501
     |d|
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   502
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   503
     d := Dictionary new.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   504
     d at:'one' ifAbsentPut:[d at:'one' put:1. 33333].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   505
     d
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   506
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   507
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   508
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   509
     |d|
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   510
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   511
     d := Dictionary new.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   512
     d at:'two'  ifAbsentPut:[1 to:30 do:[:idx| d at:idx printString put:idx]. 2].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   513
     d
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   514
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   515
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   516
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   517
    "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
   518
    "Modified: / 26.2.1998 / 19:10:09 / stefan"
3215
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   519
!
dbb2ee7a5cb9 added #at:ifAbsentPut:
Claus Gittinger <cg@exept.de>
parents: 3192
diff changeset
   520
4871
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   521
at:aKey ifPresent:aBlock
19275
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   522
    "try to retrieve the value stored at aKey.
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   523
     If there is nothing stored under this key, do nothing.
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   524
     Otherwise, evaluate aBlock, passing the retrieved value as argument."
4871
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   525
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   526
    |v|
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   527
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   528
    v := self at:aKey ifAbsent:[^ nil].
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   529
    ^ aBlock value:v.
19275
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   530
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   531
    "
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   532
     |d|
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   533
     d := Dictionary new.
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   534
     d at:#foo put:'yes this is foo'.
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   535
19275
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   536
     d at:#foo ifPresent:[:val | Transcript showCR:'the value of foo is: ',val].
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   537
     d at:#bar ifPresent:[:val | Transcript showCR:'the value of bar is: ',val].
92622b5a769b #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19273
diff changeset
   538
    "
4871
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   539
!
211a7f44946d added #at:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 3919
diff changeset
   540
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   541
at:aKey put:anObject
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   542
    "add the argument anObject under key, aKey to the receiver.
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1146
diff changeset
   543
     Return anObject (sigh).
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   544
     WARNING: do not add elements while iterating over the receiver.
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
   545
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   546
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   547
    |k index "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   548
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   549
    (k := aKey) isNil ifTrue:[
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
   550
        k := NilEntry
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   551
    ].
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   552
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   553
    index := self findKeyOrNil:k.
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   554
    valueArray basicAt:index put:anObject.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   555
    (keyArray basicAt:index) isNil ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   556
        "/ a new key
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   557
        keyArray basicAt:index put:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   558
        tally := tally + 1.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   559
        self possiblyGrow.
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   560
    ].
362
claus
parents: 360
diff changeset
   561
    ^ anObject
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   562
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2324
diff changeset
   563
    "Modified: 30.1.1997 / 14:59:10 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   564
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   565
19273
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   566
at:aKey put:anObject ifPresent:aBlock
11420
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   567
    "if the receiver contains an element stored under aKey,
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   568
     retrieve it and evaluate aBlock passing the element as argument,
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   569
     return the blocks value.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   570
     If not, store aValue under the key.
11420
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   571
     Use this with an error-reporting block, to ensure that no keys are reused"
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   572
19273
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   573
    |k index "{ Class: SmallInteger }"|
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   574
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   575
    (k := aKey) isNil ifTrue:[
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   576
        k := NilEntry
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   577
    ].
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   578
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   579
    index := self findKeyOrNil:k.
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   580
    (keyArray basicAt:index) notNil ifTrue:[
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   581
        "/ key already present
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   582
        ^ aBlock value:(valueArray basicAt:index).
19273
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   583
    ].
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   584
    "/ a new key
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   585
    keyArray basicAt:index put:k.
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   586
    valueArray basicAt:index put:anObject.
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   587
    tally := tally + 1.
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   588
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   589
    self possiblyGrow.
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   590
    ^ anObject
11420
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   591
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   592
    "
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   593
     |d|
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   594
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   595
     d := Dictionary new.
19273
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   596
     d at:'foo' put:1234 ifPresent:[:v| self error: 'duplicate: ', v printString ].
765937bb892a #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 19224
diff changeset
   597
     d at:'foo' put:1234 ifPresent:[:v| self halt:'duplicate: ', v printString. 5555 ].
11420
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   598
    "
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   599
!
9d37ee5d25c0 added at:put:ifPresent:
Claus Gittinger <cg@exept.de>
parents: 10808
diff changeset
   600
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   601
at:aKey update:aBlock
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
   602
    "update the element stored under aKey with the result from
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   603
     evaluating aBlock with the previous stored value as argument.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   604
     Report an error if there was no such key initially.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   605
     I.e. this is the same as self at:aKey put:(aBlock value:(self at:aKey)).
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   606
     Return the new value stored.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   607
     This is an optimized accessor, which only computes the hash value once."
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   608
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   609
    |k index "{ Class: SmallInteger }"
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   610
     newValue oldKey oldKeyArray|
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   611
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   612
    (k := aKey) isNil ifTrue:[
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   613
        k := NilEntry
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   614
    ].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   615
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   616
    index := self find:k ifAbsent:0.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   617
    index == 0 ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   618
        "/ a new key
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   619
        ^ self errorKeyNotFound:k.
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   620
    ].
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   621
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   622
    "/ key present
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   623
    oldKey := keyArray basicAt:index.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   624
    oldKeyArray := keyArray.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   625
    newValue := aBlock value:(valueArray basicAt:index).
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   626
    (keyArray ~~ oldKeyArray or:[(keyArray basicAt:index) ~~ oldKey]) ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   627
        "I have been changed while performing aBlock.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   628
         have to find the key again."
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   629
        index := self find:k ifAbsent:0.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   630
        index == 0 ifTrue:[
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   631
            "/ the key is gone while performing the block.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   632
            ^ self errorKeyNotFound:k.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   633
        ].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   634
    ].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   635
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   636
    valueArray basicAt:index put:newValue.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   637
    ^ newValue
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   638
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   639
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   640
     |d|
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   641
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   642
     d := Dictionary new.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   643
     d at:'one'  update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   644
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   645
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   646
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   647
     |d|
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   648
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   649
     d := Dictionary new.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   650
     d at:'one' put:0.
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   651
     d at:'one'  update:[:val | d removeKey:'one'. val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   652
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   653
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   654
    "
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   655
     |d|
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   656
     d := Dictionary new.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   657
     d at:'one' put:0.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   658
     d at:'two' put:0.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   659
     d at:'three' put:0.
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   660
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   661
     d at:'one'    update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   662
     d at:'two'    update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   663
     d at:'three'  update:[:val | val + 1].
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   664
     d at:'two'    update:[:val | val + 1].
20239
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   665
     d at:'three'  update:[:val | val + 1].
cc11bc01e248 #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 20228
diff changeset
   666
     d at:'three'  update:[:val | val + 1].
16902
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   667
     d
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   668
    "
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   669
!
869a0975a957 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 16808
diff changeset
   670
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   671
keyAtEqualValue:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   672
    "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
   673
     to the argument, nil if none found.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   674
     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
   675
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   676
	The value is searched using equality compare;
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
   677
	use #keyAtValue: to compare for identity."
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   678
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   679
    ^ self keyAtEqualValue:aValue ifAbsent:[nil]
10
claus
parents: 5
diff changeset
   680
!
claus
parents: 5
diff changeset
   681
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   682
keyAtEqualValue:aValue ifAbsent:exceptionBlock
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   683
    "return the key whose value is equal (i.e. using #= for compare)
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   684
     to the argument, if not found, return the value of exceptionBlock.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   685
     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
   686
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   687
	The value is searched using equality compare;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   688
	use #keyAtValue:ifAbsent: to compare for identity."
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   689
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   690
    |idx k|
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   691
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   692
    idx := 0.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   693
    [true] whileTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   694
	idx := valueArray indexOf:aValue startingAt:idx+1.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   695
	idx == 0 ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   696
	    ^ exceptionBlock value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   697
	].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   698
	(k := keyArray at:idx) notNil ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   699
	    k ~~ DeletedEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   700
		k == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   701
		    ^ nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   702
		].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   703
		^ k
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   704
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   705
	].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   706
    ].
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   707
    "/ NOT REACHED
54
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   708
!
06dbdeeed4f9 *** empty log message ***
claus
parents: 44
diff changeset
   709
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   710
keyAtIdentityValue:aValue
345
claus
parents: 302
diff changeset
   711
    "return the key whose value is identical (i.e. using #== for compare)
claus
parents: 302
diff changeset
   712
     to the argument, nil if none found.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
   713
     This is a slow access, since there is no fast reverse mapping.
357
claus
parents: 345
diff changeset
   714
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   715
	The value is searched using identity compare;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   716
	use #keyAtEqualValue: to compare for equality."
10
claus
parents: 5
diff changeset
   717
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   718
    ^ self keyAtIdentityValue:aValue ifAbsent:[nil]
10
claus
parents: 5
diff changeset
   719
!
claus
parents: 5
diff changeset
   720
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   721
keyAtIdentityValue:aValue ifAbsent:exceptionBlock
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   722
    "return the key whose value is identical (i.e. using #== for compare)
345
claus
parents: 302
diff changeset
   723
     to the argument, if not found, return the value of exceptionBlock.
357
claus
parents: 345
diff changeset
   724
     This is a slow access, since there is no fast reverse mapping.
claus
parents: 345
diff changeset
   725
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   726
	The value is searched using identity compare;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   727
	use #keyAtEqualValue:ifAbsent: to compare for equality."
10
claus
parents: 5
diff changeset
   728
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   729
    |idx k|
359
claus
parents: 357
diff changeset
   730
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   731
    idx := 0.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   732
    [true] whileTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   733
	idx := valueArray identityIndexOf:aValue startingAt:idx+1.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   734
	idx == 0 ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   735
	    ^ exceptionBlock value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   736
	].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   737
	(k := keyArray at:idx) notNil ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   738
	    k ~~ DeletedEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   739
		k == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   740
		    ^ nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   741
		].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   742
		^ k
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   743
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   744
	].
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   745
    ].
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
   746
    "/ NOT REACHED
357
claus
parents: 345
diff changeset
   747
!
claus
parents: 345
diff changeset
   748
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   749
keyAtValue:aValue
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   750
    "return the key whose value is identical (i.e. using #== for compare)
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   751
     to the argument, nil if none found.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   752
     This is a slow access, since there is no fast reverse mapping.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   753
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   754
	The value is searched using identity compare;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   755
	use #keyAtEqualValue: to compare for equality."
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   756
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   757
    ^ self keyAtIdentityValue:aValue ifAbsent:[nil]
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   758
!
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   759
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   760
keyAtValue:aValue ifAbsent:exceptionBlock
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   761
    "return the key whose value is identical (i.e. using #== for compare)
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   762
     to the argument, if not found, return the value of exceptionBlock.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   763
     This is a slow access, since there is no fast reverse mapping.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   764
     NOTICE:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   765
	The value is searched using identity compare;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   766
	use #keyAtEqualValue:ifAbsent: to compare for equality."
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   767
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   768
    ^ self keyAtIdentityValue:aValue ifAbsent:exceptionBlock
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   769
!
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
   770
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   771
keys
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   772
    "return a collection containing all keys of the receiver"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   773
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   774
    |keySet|
357
claus
parents: 345
diff changeset
   775
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   776
    keySet := self emptyCollectionForKeys.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   777
    keyArray do:[:key |
14320
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   778
        (key notNil and:[key ~~ DeletedEntry]) ifTrue:[
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   779
            key == NilEntry ifTrue:[
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   780
                keySet add:nil
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   781
            ] ifFalse:[
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   782
                keySet add:key
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   783
            ]
0050f5925f57 changed: #keys
Stefan Vogel <sv@exept.de>
parents: 14318
diff changeset
   784
        ]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   785
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   786
    ^ keySet
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   787
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   788
a27a279701f8 Initial revision
claus
parents:
diff changeset
   789
!Dictionary methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   790
a27a279701f8 Initial revision
claus
parents:
diff changeset
   791
add:anAssociation
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   792
    "add the argument, anAssociation to the receiver.
20386
819b3eefd460 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20239
diff changeset
   793
     Returns the argument, anAssociation.
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   794
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   795
     WARNING: do not add elements while iterating over the receiver.
14897
41e26dc8902b comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14320
diff changeset
   796
              Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   797
a27a279701f8 Initial revision
claus
parents:
diff changeset
   798
    self at:(anAssociation key) put:(anAssociation value).
a27a279701f8 Initial revision
claus
parents:
diff changeset
   799
    ^ anAssociation
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   800
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   801
    "Modified: 1.3.1996 / 21:23:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   802
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   803
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   804
addAll:aCollection
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   805
    "ANSI 5.7.2.1:
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   806
     Message:  addAll: dictionary
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   807
      Synopsis
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   808
	Store the elements of dictionary in the receiver at the
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   809
	corresponding keys from dictionary.
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   810
      Definition: <abstractDictionary>
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   811
	This message is equivalent to repeatedly sending the #at:put:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   812
	message to the receiver with each of the keys and elements in
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   813
	dictionary in turn.  If a key in dictionary is key equivalent
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   814
	to a key in the receiver, the associated element in dictionary
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   815
	replaces the element in the receiver.
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   816
7750
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   817
     Returns the argument, aCollection (sigh).
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   818
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   819
     WARNING: do not add elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   820
	      Iterate over a copy to do this."
7750
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   821
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   822
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   823
    self ~~ aCollection ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   824
	aCollection isSequenceable ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   825
	    aCollection do:[:eachPair |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   826
		self at:eachPair key put:eachPair value.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   827
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   828
	] ifFalse:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   829
	    aCollection keysAndValuesDo:[:eachKey :eachValue |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   830
		self at:eachKey put:eachValue.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   831
	    ]
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   832
	].
7750
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   833
    ].
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   834
    ^ aCollection
7750
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   835
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   836
    "
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   837
     |d1 d2|
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   838
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   839
     d1 := Dictionary new.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   840
     d1 at:1 put:'one'.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   841
     d1 at:2 put:'two'.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   842
     d2 := Dictionary new.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   843
     d2 at:3 put:'three'.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   844
     d2 at:4 put:'four'.
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   845
     d1 addAll:d2.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   846
     d1.
7750
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   847
    "
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   848
!
5722e5569d3d fixed (added) #addAll:
Claus Gittinger <cg@exept.de>
parents: 7653
diff changeset
   849
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   850
addPairsFrom:aSequenceableCollection
11673
baa4568523f0 comment
Claus Gittinger <cg@exept.de>
parents: 11581
diff changeset
   851
    "merge consecutive key-value pairs from aSequenceableCollection into the receiver."
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   852
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   853
    aSequenceableCollection pairWiseDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   854
	self at:key put:value.
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   855
    ]
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   856
7751
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   857
    "
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   858
     |d1 arr|
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   859
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   860
     d1 := Dictionary new.
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   861
     d1 at:1 put:'one'.
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   862
     d1 at:2 put:'two'.
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   863
     arr := #(3 'three'  4 'four').
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   864
     d1 addPairsFrom:arr.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   865
     d1.
7751
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   866
    "
e6bfe9fd8ef8 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 7750
diff changeset
   867
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   868
    "Modified: 1.3.1996 / 21:24:03 / cg"
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   869
!
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   870
19813
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   871
clearContents
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   872
    "remove all elements from the receiver, but do not resize.
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   873
     Returns the receiver."
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   874
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   875
    keyArray atAllPut:nil.
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   876
    valueArray atAllPut:nil.
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   877
    tally := 0.
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   878
!
01a9fdefab4c #OTHER by stefan
Stefan Vogel <sv@exept.de>
parents: 19740
diff changeset
   879
345
claus
parents: 302
diff changeset
   880
declare:key from:aDictionary
claus
parents: 302
diff changeset
   881
    "if the receiver does not include an association for key,
claus
parents: 302
diff changeset
   882
     take the association from aDictionary and add it to the receiver.
claus
parents: 302
diff changeset
   883
     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
   884
     as the value of the new dictionary.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   885
9066
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
   886
     Stubidity Notice:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   887
	 Incompatibility with #declareAllFrom:, where the other values are
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   888
	 defined unconditionally.
9066
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
   889
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   890
     WARNING: do not add elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   891
	      Iterate over a copy to do this."
345
claus
parents: 302
diff changeset
   892
claus
parents: 302
diff changeset
   893
    |value|
claus
parents: 302
diff changeset
   894
claus
parents: 302
diff changeset
   895
    (self includesKey:key) ifFalse:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   896
	value := aDictionary at:key ifAbsent:nil.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   897
	self at:key put:value.
345
claus
parents: 302
diff changeset
   898
    ]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   899
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   900
    "Modified: 1.3.1996 / 21:24:03 / cg"
345
claus
parents: 302
diff changeset
   901
!
claus
parents: 302
diff changeset
   902
16451
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   903
declareAll:keys from:aCollectionOrDictionary
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   904
    "declare all keys in the first argument, keys
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   905
     from values taken from the second argument, aCollectionOrDictionary.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   906
     If aCollectionOrDictionary is a dictionary, access via the key;
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   907
     if it is a sequencable collection, add corresponding values pairwise."
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   908
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   909
    aCollectionOrDictionary isDictionary ifTrue:[
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   910
        keys do:[:k | self at:k put:(aCollectionOrDictionary at:k) ]
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   911
    ] ifFalse:[
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   912
        keys with:aCollectionOrDictionary do:[:k :v | self at:k put:v ]
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   913
    ].
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   914
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   915
    "
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   916
     |d|
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   917
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   918
     d := Dictionary new.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   919
     d declareAll:#(a b c) from:#(10 20 30).
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   920
     d.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   921
    "
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   922
    "
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   923
     |d1 d2 d3|
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   924
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   925
     d1 := Dictionary new.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   926
     d1 declareAll:#(a b c) from:#(10 20 30).
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   927
     d2 := Dictionary new.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   928
     d2 declareAll:#( b c d) from:#(100 200 300).
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   929
     d3 := Dictionary new.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   930
     d3 declareAll:#(a b c) from:d1.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   931
     d3 declareAll:#(c d) from:d2.
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   932
     d3
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   933
    "
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   934
!
59f63673573d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15857
diff changeset
   935
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   936
declareAllFrom:aDictionaryOrNil
8434
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   937
    "merge all key-value pairs from aDictionary into the receiver.
98ae6daa9aac Fix #addAll: and #declareAllFrom:
Stefan Vogel <sv@exept.de>
parents: 8395
diff changeset
   938
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   939
     sigh:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   940
	For compatibility with #declare:from: the behavior should be changed as following:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   941
	If the receiver already contains a key, the existing value is retained.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   942
	To keep the compatibility with other smalltalks, the semantics of this remains
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   943
	as is, and #declareAllNewFrom: was added for convenience.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   944
	See #declareAllNewFrom: which does exactly what this name implies."
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   945
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   946
    self ~~ aDictionaryOrNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   947
	aDictionaryOrNil notNil ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   948
	    aDictionaryOrNil keysAndValuesDo:[:key :value |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   949
		self at:key put:value.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   950
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   951
	]
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   952
    ]
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   953
9939
59877f64325d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9938
diff changeset
   954
    "Modified: / 18-09-2006 / 22:01:12 / cg"
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   955
!
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   956
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   957
declareAllNewFrom:aDictionaryOrNil
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   958
    "merge all new key-value pairs from aDictionary into the receiver
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   959
     i.e. If the receiver already contains a key, the existing value is retained.
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   960
     See also #declareAllFrom:"
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   961
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   962
    self ~~ aDictionaryOrNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   963
	aDictionaryOrNil notNil ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   964
	    aDictionaryOrNil keysAndValuesDo:[:key :value |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   965
		(self includesKey:key) ifFalse:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   966
		    self at:key put:value.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   967
		].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   968
	    ]
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
   969
	]
9938
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   970
    ]
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   971
bec2c2e2c500 declareAllFrom: comment
Claus Gittinger <cg@exept.de>
parents: 9570
diff changeset
   972
    "Created: / 18-09-2006 / 21:58:54 / cg"
2711
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   973
!
307c43b7a3f9 addPairsFrom:
ca
parents: 2466
diff changeset
   974
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   975
remove:oldObject ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   976
    "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
   977
     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
   978
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   979
     This is blocked here; you have to use one of
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   980
     #removeKey:, #saveRemoveKey:, #removeAssociation:,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   981
     #removeValue: or #saveRemoveValue:"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   982
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
   983
    ^ self shouldNotImplement
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   984
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
   985
    "Modified: 1.3.1996 / 21:21:38 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   986
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   987
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   988
removeAllKeys:aKeyCollection
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   989
    "remove all associations under each key in aKeyCollection from the collection.
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   990
     If it was not in the collection report an error.
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   991
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   992
     WARNING: do not remove elements while iterating over the receiver.
21116
3d401b0a90bd #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20989
diff changeset
   993
              See #safeRemoveKey: to do this."
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   994
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   995
    aKeyCollection do:[:eachKey |
21116
3d401b0a90bd #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20989
diff changeset
   996
        self removeKey:eachKey ifAbsent:[self errorKeyNotFound:eachKey]
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   997
    ].
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   998
!
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
   999
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1000
removeAllKeys:aKeyCollection ifAbsent:aBlock
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1001
    "remove all associations under each key in aKeyCollection from the collection.
15527
79c139096f4f class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15363
diff changeset
  1002
     If it was not in the collection return the result from evaluating aBlock
79c139096f4f class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15363
diff changeset
  1003
     (invoked for each missing element).
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1004
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1005
     WARNING: do not remove elements while iterating over the receiver.
15527
79c139096f4f class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15363
diff changeset
  1006
              See #saveRemoveKey: to do this."
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1007
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1008
    aKeyCollection do:[:eachKey |
15527
79c139096f4f class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15363
diff changeset
  1009
        self removeKey:eachKey ifAbsent:aBlock
6053
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1010
    ].
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1011
!
a97f27828c08 added #removeKeys: and #removeKeys:ifAbsent: (ANSI)
Claus Gittinger <cg@exept.de>
parents: 5865
diff changeset
  1012
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1013
removeAssociation:assoc
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1014
    "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
  1015
     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
  1016
     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
  1017
     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
  1018
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1019
     WARNING: do not remove elements while iterating over the receiver.
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  1020
	      See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1021
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1022
    |key|
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1023
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1024
    key := assoc key.
2897
0b6b43d0a300 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2711
diff changeset
  1025
    ^ 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
  1026
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1027
    "Modified: 1.3.1996 / 21:21:11 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1028
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1029
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
  1030
removeIdentityValue:aValue ifAbsent:aBlock
20167
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1031
    "remove (first) the association to aValue from the collection,
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1032
     return the key under which it was stored previously.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1033
     If it was not in the collection return result from evaluating aBlock.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1034
     The value is searched using identity compare.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1035
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1036
     Notice, this does a linear search through the values and may
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1037
     therefore be slow for big dictionaries.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1038
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1039
     WARNING: do not remove elements while iterating over the receiver.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1040
             See #saveRemoveValue: to do this."
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1041
    
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1042
    |next   "{ Class:SmallInteger }"
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1043
     oldKey|
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1044
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
  1045
    keyArray
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
  1046
        keysAndValuesDo:[:index :aKey |
20167
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1047
            |idx "{Class:SmallInteger}"|
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1048
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1049
            (aKey notNil and:[ aKey ~~ DeletedEntry ]) ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1050
                idx := index.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1051
                ((valueArray at:idx) == aValue) ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1052
                    "found it"
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1053
                    valueArray basicAt:idx put:nil.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1054
                    oldKey := keyArray basicAt:idx.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1055
                    oldKey == NilEntry ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1056
                        oldKey := nil
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1057
                    ].
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1058
                    keyArray basicAt:idx put:nil.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1059
                    tally := tally - 1.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1060
                    tally == 0 ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1061
                        self initializeForCapacity:0.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1062
                        ^ oldKey
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1063
                    ].
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1064
                    idx == keyArray basicSize ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1065
                        next := 1
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1066
                    ] ifFalse:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1067
                        next := index + 1.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1068
                    ].
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1069
                    (keyArray basicAt:next) notNil ifTrue:[
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1070
                        keyArray basicAt:idx put:DeletedEntry
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1071
                    ].
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1072
                    self possiblyShrink.
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1073
                    ^ oldKey
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1074
                ]
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1075
            ]
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1076
        ].
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1077
    ^ aBlock value
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1078
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1079
    "Modified: 1.3.1996 / 21:22:11 / cg"
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1080
!
ad934e073d16 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20066
diff changeset
  1081
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1082
removeKey:aKey
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1083
    "remove the association under aKey from the collection.
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1084
     If it was not in the collection report an error.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1085
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1086
     WARNING: do not remove elements while iterating over the receiver.
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  1087
	      See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1088
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1089
    ^ self removeKey:aKey ifAbsent:[self errorKeyNotFound:aKey]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1090
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1091
    "Modified: 1.3.1996 / 21:21:52 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1092
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1093
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1094
removeKey:aKey ifAbsent:aBlock
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1095
    "remove the association under aKey from the collection,
19845
ef17a74c2bd7 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19813
diff changeset
  1096
     return the value previously stored there.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1097
     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
  1098
     from evaluating aBlock.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1099
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1100
     WARNING: do not remove elements while iterating over the receiver.
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1101
             See #saveRemoveKey: to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1102
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1103
    |index "{ Class:SmallInteger }"
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1104
     "/ next  "{ Class:SmallInteger }"
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1105
     oldValue k|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1106
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1107
    (k := aKey) isNil ifTrue:[
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1108
"/ no longer invalid
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1109
"/        ^ self errorInvalidKey:aKey
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1110
        k := NilEntry
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1111
    ].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1112
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1113
    "/   below, I could have written:
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1114
    "/      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
  1115
    "/   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
  1116
    "/   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
  1117
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1118
    index := self find:k ifAbsent:0.
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1119
    index == 0 ifTrue:[^ aBlock value].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1120
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1121
    oldValue := valueArray basicAt:index.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1122
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1123
    valueArray basicAt:index put:nil.
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1124
    keyArray basicAt:index put:DeletedEntry.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1125
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1126
    tally := tally - 1.
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1127
    tally == 0 ifTrue:[
18332
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  1128
        self initializeForCapacity:0
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1129
    ] ifFalse:[
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1130
"/        index == keyArray basicSize ifTrue:[
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1131
"/            next := 1
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1132
"/        ] ifFalse:[
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1133
"/            next := index + 1.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1134
"/        ].
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1135
"/        (keyArray basicAt:next) notNil ifTrue:[
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1136
"/            keyArray basicAt:index put:DeletedEntry
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1137
"/        ].
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1138
        self possiblyShrink
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1139
    ].
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1140
    ^ oldValue
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1141
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1142
    "Modified: 1.3.1996 / 21:21:01 / cg"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1143
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1144
5184
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1145
removeValue:aValue
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1146
    "remove (first) the association to aValue from the collection,
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1147
     return the key under which it was stored previously.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1148
     If it was not in the collection, report an error.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1149
     The value is searched using equality compare here,
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1150
     but identity compare in the IdentityDictionary subclass.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1151
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1152
     Notice, this does a linear search through the values and may
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1153
     therefore be slow for big dictionaries.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1154
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1155
     WARNING: do not remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1156
	     See #saveRemoveValue: to do this."
5184
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1157
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1158
    ^ self removeValue:aValue ifAbsent:[self errorValueNotFound:aValue]
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1159
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1160
    "
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1161
     |d|
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1162
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1163
     d := Dictionary new.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1164
     d at:1 put:'one'.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1165
     d at:2 put:'two'.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1166
     d at:3 put:'three'.
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1167
     d removeValue:'two'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1168
     d
5184
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1169
    "
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1170
!
cbf20f052178 added #removeValue: - for protocol completeness
Claus Gittinger <cg@exept.de>
parents: 5050
diff changeset
  1171
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1172
removeValue:aValue ifAbsent:aBlock
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1173
    "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
  1174
     return the key under which it was stored previously.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1175
     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
  1176
     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
  1177
     but identity compare in the IdentityDictionary subclass.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1178
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1179
     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
  1180
     therefore be slow for big dictionaries.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1181
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1182
     WARNING: do not remove elements while iterating over the receiver.
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1183
             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
  1184
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1185
    |next  "{ Class:SmallInteger }"
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1186
     oldKey|
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1187
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1188
    keyArray keysAndValuesDo:[:index :aKey |
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1189
        |idx "{Class:SmallInteger}"|
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1190
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1191
        (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1192
            idx := index.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1193
            (self compareSame:(valueArray at:idx) with:aValue) ifTrue:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1194
                "found it"
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1195
                valueArray basicAt:idx put:nil.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1196
                oldKey := keyArray basicAt:idx.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1197
                oldKey == NilEntry ifTrue:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1198
                    oldKey := nil
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1199
                ].
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1200
                keyArray basicAt:idx put:nil.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1201
                tally := tally - 1.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1202
                tally == 0 ifTrue:[
18332
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  1203
                    self initializeForCapacity:0.
17264
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1204
                    ^ oldKey
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1205
                ].
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1206
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1207
                idx == keyArray basicSize ifTrue:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1208
                    next := 1
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1209
                ] ifFalse:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1210
                    next := index + 1.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1211
                ].
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1212
                (keyArray basicAt:next) notNil ifTrue:[
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1213
                    keyArray basicAt:idx put:DeletedEntry
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1214
                ].
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1215
                self possiblyShrink.
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1216
                ^ oldKey
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1217
            ]
548ea4dc5aa4 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 16902
diff changeset
  1218
        ]
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1219
    ].
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1220
    ^ aBlock value
641
ef3bcf9de54b return values in remove*** methods fixed (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents: 633
diff changeset
  1221
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1222
    "Modified: 1.3.1996 / 21:22:11 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1223
!
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1224
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1225
safeRemoveKey:aKey
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1226
    "remove the association under aKey from the collection.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1227
     Return the value previously stored there.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1228
     If it was not in the collection return nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1229
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1230
     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
  1231
     and therefore does NOT rehash & change the elements order.
18787
a1b603604641 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18342
diff changeset
  1232
     Therefore this can be used while enumerating the receiver,
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1233
     which is not possible if #removeKey: is used.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1234
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1235
     WARNING: since no resizing is done, the physical amount of memory used
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1236
              by the container remains the same, although the logical size shrinks.
18787
a1b603604641 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18342
diff changeset
  1237
              You may want to manually resize the receiver using #possiblyShrink."
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1238
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1239
    |index "{ Class:SmallInteger }"
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1240
     next  "{ Class:SmallInteger }"
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1241
     oldValue k|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1242
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1243
    (k := aKey) isNil ifTrue:[
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1244
        k := NilEntry
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1245
    ].
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1246
"/    aKey isNil ifTrue:[^ nil].
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1247
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1248
    index := self find:k ifAbsent:0.
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1249
    index == 0 ifTrue:[^ nil].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1250
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1251
    oldValue := valueArray basicAt:index.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1252
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1253
    valueArray basicAt:index put:nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1254
    keyArray basicAt:index put:nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1255
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1256
    tally := tally - 1.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1257
    tally ~~ 0 ifTrue:[
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1258
        index == keyArray basicSize ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1259
            next := 1
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1260
        ] ifFalse:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1261
            next := index + 1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1262
        ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1263
        (keyArray basicAt:next) notNil ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1264
            keyArray basicAt:index put:DeletedEntry
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1265
        ].
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1266
    ].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1267
    ^ oldValue
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1268
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1269
    "does NOT work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1270
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1271
        |d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1272
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1273
        d := Dictionary new.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1274
        d at:'one' put:1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1275
        d at:'two' put:2.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1276
        d at:'three' put:3.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1277
        d at:'four' put:4.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1278
        d at:'five' put:5.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1279
        d at:'six' put:6.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1280
        d at:'seven' put:7.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1281
        d at:'eight' put:8.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1282
        d at:'nine' put:9.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1283
        d keysAndValuesDo:[:k :v |
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1284
            v odd ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1285
                d removeKey:k
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1286
            ]
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1287
        ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1288
        d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1289
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1290
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1291
    "DOES work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1292
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1293
        |d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1294
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1295
        d := Dictionary new.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1296
        d at:'one' put:1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1297
        d at:'two' put:2.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1298
        d at:'three' put:3.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1299
        d at:'four' put:4.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1300
        d at:'five' put:5.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1301
        d at:'six' put:6.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1302
        d at:'seven' put:7.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1303
        d at:'eight' put:8.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1304
        d at:'nine' put:9.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1305
        d keysAndValuesDo:[:k :v |
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1306
            v odd ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1307
                d safeRemoveKey:k
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1308
            ]
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1309
        ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1310
        d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1311
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1312
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1313
    "Created: 1.3.1996 / 21:14:42 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1314
    "Modified: 1.3.1996 / 21:14:53 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1315
!
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1316
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1317
safeRemoveValue:aValue
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1318
    "remove the (first) association to aValue from the collection,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1319
     return the key under which it was stored previously.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1320
     If it was not in the collection return nil.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1321
     The value is searched using equality compare here,
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1322
     but identity compare in the IdentityDictionary subclass.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1323
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1324
     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
  1325
     and therefore does NOT rehash & change the elements order.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1326
     Therefore, this can be used while enumerating the receiver,
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1327
     which is not possible if #removeValue: is used.
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1328
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1329
     WARNING: since no resizing is done, the physical amount of memory used
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1330
              by the container remains the same, although the logical size shrinks.
18787
a1b603604641 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18342
diff changeset
  1331
              You may want to manually resize the receiver using #possiblyShrink."
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1332
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1333
    |next  "{ Class:SmallInteger }"
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1334
     oldKey|
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1335
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1336
    keyArray keysAndValuesDo:[:index :aKey |
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1337
        |idx "{Class:SmallInteger}"|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1338
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1339
        (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1340
            idx := index.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1341
            (self compareSame:(valueArray at:idx) with:aValue) ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1342
                "found it"
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1343
                valueArray basicAt:idx put:nil.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1344
                oldKey := keyArray basicAt:idx.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1345
                oldKey == NilEntry ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1346
                    oldKey := nil
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1347
                ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1348
                keyArray basicAt:idx put:nil.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1349
                tally := tally - 1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1350
                tally ~~ 0 ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1351
                    idx == keyArray basicSize ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1352
                        next := 1
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1353
                    ] ifFalse:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1354
                        next := index + 1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1355
                    ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1356
                    (keyArray basicAt:next) notNil ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1357
                        keyArray basicAt:idx put:DeletedEntry
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1358
                    ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1359
                ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1360
                ^ oldKey
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1361
            ]
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1362
        ]
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1363
    ].
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1364
    ^ aValue
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1365
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1366
    "does NOT work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1367
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1368
        |d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1369
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1370
        d := Dictionary new.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1371
        d at:'one' put:1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1372
        d at:'two' put:2.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1373
        d at:'three' put:3.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1374
        d at:'four' put:4.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1375
        d at:'five' put:5.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1376
        d at:'six' put:6.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1377
        d at:'seven' put:7.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1378
        d at:'eight' put:8.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1379
        d at:'nine' put:9.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1380
        d keysAndValuesDo:[:k :v |
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1381
            v odd ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1382
                d removeValue:v ifAbsent:nil
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1383
            ]
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1384
        ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1385
        d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1386
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1387
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1388
    "DOES work:
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1389
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1390
        |d|
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1391
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1392
        d := Dictionary new.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1393
        d at:'one' put:1.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1394
        d at:'two' put:2.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1395
        d at:'three' put:3.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1396
        d at:'four' put:4.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1397
        d at:'five' put:5.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1398
        d at:'six' put:6.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1399
        d at:'seven' put:7.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1400
        d at:'eight' put:8.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1401
        d at:'nine' put:9.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1402
        d keysAndValuesDo:[:k :v |
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1403
            v odd ifTrue:[
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1404
                d safeRemoveValue:v
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1405
            ]
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1406
        ].
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1407
        d inspect
1053
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1408
    "
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1409
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1410
    "Created: 1.3.1996 / 21:17:10 / cg"
ac1ea83233e6 added saveRemoveKey:/saveRemoveValue: & commentaries
Claus Gittinger <cg@exept.de>
parents: 641
diff changeset
  1411
    "Modified: 1.3.1996 / 21:23:04 / cg"
16512
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1412
!
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1413
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1414
saveRemoveKey:aKey
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1415
    <resource: #obsolete>
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1416
    "bad spelling - kept for backward compatibility (2014-06-04)"
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1417
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1418
    ^ self safeRemoveKey:aKey.
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1419
!
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1420
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1421
saveRemoveValue:aValue
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1422
    <resource: #obsolete>
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1423
    "bad spelling - kept for backward compatibility (2014-06-04)"
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1424
326a51f05fb6 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16451
diff changeset
  1425
    ^ self safeRemoveValue:aValue.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1426
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1427
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1428
!Dictionary methodsFor:'comparing'!
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1429
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1430
= aCollection
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1431
    "return true, if the argument is a Dictionary containing the same
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1432
     key-value pairs as I do"
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1433
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1434
    aCollection species == self species ifFalse:[^ false].
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1435
    aCollection size == self size ifFalse:[^ false].
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1436
    "/ all of of my key-value associations must be in the other collection ...
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1437
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1438
	((aCollection at:key ifAbsent:[^ false]) = value) ifFalse:[^ false]
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1439
    ].
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1440
    ^ true
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1441
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1442
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1443
     |d1 d2|
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1444
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1445
     d1 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1446
     d2 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1447
     d1 at:1 put:'one'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1448
     d1 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1449
     d1 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1450
     d1 at:'two' put:2.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1451
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1452
     d2 at:1 put:'one'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1453
     d2 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1454
     d2 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1455
     d2 at:'two' put:2.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1456
     d1 = d2
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1457
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1458
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1459
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1460
     |d1 d2|
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1461
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1462
     d1 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1463
     d2 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1464
     d1 at:1 put:'uno'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1465
     d1 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1466
     d1 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1467
     d1 at:'two' put:2.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1468
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1469
     d2 at:1 put:'one'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1470
     d2 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1471
     d2 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1472
     d2 at:'two' put:2.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1473
     d1 = d2
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1474
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1475
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1476
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1477
     |d1 d2|
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1478
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1479
     d1 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1480
     d2 := Dictionary new.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1481
     d1 at:10 put:'one'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1482
     d1 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1483
     d1 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1484
     d1 at:'two' put:2.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1485
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1486
     d2 at:1 put:'one'.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1487
     d2 at:'one' put:1.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1488
     d2 at:2 put:#two.
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1489
     d2 at:'two' put:2.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1490
     d1 = d2
5279
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1491
    "
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1492
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1493
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1494
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1495
! !
7f611c000835 added #= EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5255
diff changeset
  1496
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1497
!Dictionary methodsFor:'converting'!
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1498
7906
9956d7c3a7be +asDictionary
Claus Gittinger <cg@exept.de>
parents: 7751
diff changeset
  1499
asDictionary
9956d7c3a7be +asDictionary
Claus Gittinger <cg@exept.de>
parents: 7751
diff changeset
  1500
    ^ self
9956d7c3a7be +asDictionary
Claus Gittinger <cg@exept.de>
parents: 7751
diff changeset
  1501
!
9956d7c3a7be +asDictionary
Claus Gittinger <cg@exept.de>
parents: 7751
diff changeset
  1502
14997
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1503
asNewDictionary
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1504
    "return myself as an unique new dictionary"
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1505
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1506
    ^ self copy
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1507
!
19ac0ce7ab74 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 14971
diff changeset
  1508
14271
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1509
associationsOrderedBy:aCollectionOfKeys
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1510
    "return an OrderedCollection of my key-value pairs, ordered by the given key list"
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1511
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1512
    self assert:(aCollectionOfKeys size == self size).
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1513
    ^ aCollectionOfKeys collect:[:eachKey | eachKey -> (self at:eachKey) ] as: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1514
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1515
    "
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1516
     |d|
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1517
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1518
     d := Dictionary new.
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1519
     d at:'zzz' put:3.
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1520
     d at:'aaa' put:1.
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1521
     d at:'eee' put:2.
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1522
     d.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1523
     d valuesOrderedBy:#('aaa' 'eee' 'zzz').
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1524
     d associationsOrderedBy:#('aaa' 'eee' 'zzz').
14271
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1525
    "
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1526
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1527
    "Created: / 31-07-2012 / 17:32:34 / cg"
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1528
!
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1529
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1530
fromLiteralArrayEncoding:encoding
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1531
    "read my values from an encoding.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1532
     The encoding is supposed to be of the form:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1533
	(Dictionary key1 val1 ... keyN valN)"
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1534
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1535
    2 to:encoding size by:2 do:[:i |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1536
	|key val|
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1537
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1538
	key := encoding at:i.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1539
	val := encoding at:i+1.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1540
	self at:key put:val
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1541
    ].
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1542
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1543
    "
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1544
     Dictionary new fromLiteralArrayEncoding:#(Dictionary 'hello' 'world' 1 'foo')
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1545
    "
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1546
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1547
    "Created: / 30.1.1998 / 04:30:47 / cg"
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1548
!
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1549
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1550
literalArrayEncoding
9413
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1551
    |literalArrray idx|
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1552
9413
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1553
    literalArrray := Array new:(self size * 2)+1.
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1554
    literalArrray at:1 put:self class name.
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1555
    idx := 2.
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1556
    self keysAndValuesDo:[:eachKey :eachValue |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1557
	literalArrray
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1558
	    at:idx   put:eachKey literalArrayEncoding;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1559
	    at:idx+1 put:eachValue literalArrayEncoding.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1560
	idx := idx + 2.
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1561
    ].
9413
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1562
    ^ literalArrray
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1563
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1564
    "
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1565
      |dict|
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1566
9413
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1567
      dict := Dictionary new.
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1568
      dict at:1 put:'bla'.
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1569
      dict at:'fasel' put:#[1 2 3 4].
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1570
      dict literalArrayEncoding
195f7caf6610 Fix comments.
Stefan Vogel <sv@exept.de>
parents: 9183
diff changeset
  1571
    "
14271
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1572
!
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1573
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1574
valuesOrderedBy:aCollectionOfKeys
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1575
    "return an OrderedCollection of my values, ordered by the given key list"
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1576
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1577
    self assert:(aCollectionOfKeys size == self size).
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1578
    ^ aCollectionOfKeys collect:[:eachKey | self at:eachKey ] as: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1579
Claus Gittinger <cg@exept.de>
parents: 14226
diff changeset
  1580
    "Created: / 31-07-2012 / 17:33:12 / cg"
3252
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1581
! !
da066eafc2c5 added literalArrayEncoding support
Claus Gittinger <cg@exept.de>
parents: 3215
diff changeset
  1582
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1583
!Dictionary methodsFor:'copying'!
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1584
11909
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1585
, anotherDictionaryOrAssociation
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1586
    "return a new dictionary containing a merged set of associations.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1587
     If anotherDictionaryOrAssociation includes any of the receiver's keys,
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1588
     the value from anotherDictionaryOrAssociation will be placed into the
11909
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1589
     returned result."
9066
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1590
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1591
    |newDictionary|
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1592
11909
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1593
    newDictionary := self copy.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1594
    newDictionary declareAllFrom:anotherDictionaryOrAssociation.
9066
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1595
    ^ newDictionary
11909
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1596
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1597
    "
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1598
     |d1 d2|
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1599
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1600
     d1 := Dictionary new.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1601
     d1 at:#a put:'aaa'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1602
     d1 at:#b put:'bbb'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1603
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1604
     d2 := Dictionary new.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1605
     d2 at:#b put:'bbbb'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1606
     d2 at:#c put:'ccc'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1607
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1608
     d1 , d2
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1609
    "
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1610
    "
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1611
     |d1 d2|
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1612
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1613
     d1 := Dictionary new.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1614
     d1 at:#a put:'aaa'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1615
     d1 at:#b put:'bbb'.
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1616
     d2 := d1 , (#c -> 'ccc').
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1617
     d2
3bc1c7c5eb7b comment in ,
Claus Gittinger <cg@exept.de>
parents: 11673
diff changeset
  1618
    "
9066
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1619
!
fe9eed32da70 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
  1620
155
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1621
postCopy
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1622
    "have to copy the valueArray too"
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1623
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1624
    super postCopy.
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1625
    valueArray := valueArray shallowCopy
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1626
! !
edd7fc34e104 *** empty log message ***
claus
parents: 92
diff changeset
  1627
217
a0400fdbc933 *** empty log message ***
claus
parents: 155
diff changeset
  1628
!Dictionary methodsFor:'enumerating'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1629
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1630
allKeysDo:aBlock
302
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
  1631
    "perform the block for all keys in the collection.
1f76060d58a4 *** empty log message ***
claus
parents: 293
diff changeset
  1632
     Obsolete: use keysDo: for ST-80 compatibility."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1633
5865
06a28f9ee22c Use <resource:#obsolete>
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
  1634
    <resource:#obsolete>
06a28f9ee22c Use <resource:#obsolete>
Stefan Vogel <sv@exept.de>
parents: 5816
diff changeset
  1635
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1636
    self obsoleteMethodWarning:'please use #keysDo:'.
20989
42266e2b5d40 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20972
diff changeset
  1637
    ^ self keysDo:aBlock
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1638
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1639
    "Modified: 20.4.1996 / 11:22:01 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1640
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1641
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1642
associationsCollect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1643
    "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
  1644
     and return a collection with the results.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1645
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1646
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1647
	#keysAndValuesCollect: (which passes separate keys & values)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1648
	#collect:              (which only passes values)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1649
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1650
     This is much like #keysAndValuesCollect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1651
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1652
     #keysAndValuesCollect: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1653
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1654
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1655
	      Iterate over a copy to do this."
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1656
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1657
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1658
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1659
    newCollection := OrderedCollection new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1660
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1661
	newCollection add:(aBlock value:(Association key:key value:value))
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1662
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1663
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1664
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1665
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1666
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1667
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1668
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1669
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1670
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1671
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1672
     ages at:'tk' put:28.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1673
     ages associationsCollect:[:assoc |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1674
		assoc key , '''s age is ' , assoc value printString]
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1675
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1676
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1677
    "Modified: 20.4.1996 / 11:31:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1678
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1679
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1680
associationsDo:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1681
    "perform the block for all associations in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1682
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1683
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1684
	#do:              (which passes values to its block)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1685
	#keysDo:          (which passes only keys to its block)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1686
	#keysAndValuesDo: (which passes keys&values)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1687
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1688
     This is much like #keysAndValuesDo:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1689
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1690
     #keysAndValuesDo: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1691
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1692
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1693
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1694
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1695
    |key n "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1696
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1697
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1698
    n := keyArray basicSize.
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1699
    1 to:n do:[:index |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1700
	key := keyArray basicAt:index.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1701
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1702
	    key == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1703
		key := nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1704
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1705
	    aBlock value:(Association key:key value:(valueArray basicAt:index))
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1706
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1707
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1708
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1709
    "Modified: 20.4.1996 / 11:31:39 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1710
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1711
13721
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1712
associationsDo:aBlock separatedBy:sepBlock
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1713
    "perform the block for all associations in the collection.
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1714
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1715
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1716
	#do:              (which passes values to its block)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1717
	#keysDo:          (which passes only keys to its block)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1718
	#keysAndValuesDo: (which passes keys&values)
13721
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1719
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1720
     This is much like #keysAndValuesDo:, but aBlock gets the
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1721
     key and value as a single association argument.
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1722
     #keysAndValuesDo: and is a bit faster therefore (no intermediate objects).
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1723
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1724
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1725
	      Iterate over a copy to do this."
13721
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1726
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1727
    |key n "{ Class: SmallInteger }"
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1728
     first|
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1729
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1730
    tally == 0 ifTrue:[^ self].
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1731
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1732
    first := true.
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1733
    n := keyArray basicSize.
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1734
    1 to:n do:[:index |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1735
	key := keyArray basicAt:index.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1736
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1737
	    key == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1738
		key := nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1739
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1740
	    first ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1741
		first := false.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1742
	    ] ifFalse:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1743
		sepBlock value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1744
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1745
	    aBlock value:(Association key:key value:(valueArray basicAt:index))
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1746
	]
13721
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1747
    ]
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1748
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1749
    "Modified: / 20-04-1996 / 11:31:39 / cg"
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1750
    "Created: / 23-09-2011 / 14:07:43 / cg"
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1751
!
733cce16b968 added: #associationsDo:separatedBy:
Claus Gittinger <cg@exept.de>
parents: 13328
diff changeset
  1752
2425
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1753
associationsReverseDo:aBlock
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1754
    "perform the block for all associations in the collection.
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1755
     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
  1756
     this is the same as #associationsDo: here.
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1757
     Provided for protocol compatibility with OrderedDictionary"
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1758
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1759
    ^ self associationsDo:aBlock
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1760
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1761
    "Created: 28.2.1997 / 16:08:52 / cg"
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1762
!
3c23ae3de6d6 added #associationsReverseDo: for OD compatibility
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1763
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1764
associationsSelect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1765
    "return a new collection with all elements from the receiver, for which
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1766
     the argument aBlock evaluates to true.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1767
     The block gets keys and values as an association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1768
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1769
     See also: #keysAndValuesSelect: (which is slightly faster),
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1770
	       #select: (which only passes the value)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1771
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1772
     This is much like #keysAndValuesSelect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1773
     key and value as a single association argument.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1774
     #keysAndValuesSelect: and is a bit faster therefore (no intermediate objects).
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1775
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1776
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1777
	      Iterate over a copy to do this."
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1778
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1779
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1780
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1781
    newCollection := self species new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1782
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1783
	(aBlock value:(Association key:key value:value)) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1784
	    newCollection at:key put:value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1785
	]
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1786
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1787
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1788
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1789
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1790
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1791
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1792
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1793
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1794
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1795
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1796
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1797
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1798
     ages associationsSelect:[:assoc |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1799
		(assoc key startsWith:'c') or:[assoc value < 30]].
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1800
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1801
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1802
    "Modified: 20.4.1996 / 11:31:15 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1803
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1804
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1805
collect:aBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1806
    "for each element in the receiver, evaluate the argument, aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1807
     and return a Bag with the results.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1808
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1809
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1810
	#associationsCollect:   (which passes key-value associations)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1811
	#keysAndValuesCollect:  (which passes keys & values separately)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1812
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1813
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1814
	      Iterate over a copy to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1815
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1816
    |newCollection|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1817
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1818
    newCollection := Bag new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1819
    self do:[:each |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1820
	newCollection add:(aBlock value:each)
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1821
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1822
    ^ newCollection
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1823
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1824
    "Modified: 20.4.1996 / 11:29:58 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1825
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1826
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1827
do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1828
    "perform the block for all values in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1829
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1830
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1831
	#associationsDo:   (which passes key-value associations)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1832
	#keysAndValuesDo:  (which passes keys & values separately)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1833
	#keysDo:           (which passes keys only)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1834
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1835
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1836
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1837
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1838
    |key n "{ Class: SmallInteger }"
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1839
     deletedEntry|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1840
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1841
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1842
    n := keyArray basicSize.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1843
    deletedEntry := DeletedEntry.
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1844
    1 to:n do:[:index |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1845
	key := keyArray basicAt:index.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1846
	(key notNil and:[key ~~ deletedEntry]) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1847
	    aBlock value:(valueArray basicAt:index)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1848
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1849
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1850
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1851
    "Modified: 20.4.1996 / 11:32:11 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1852
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1853
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1854
keysAndValuesDo:aTwoArgBlock
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1855
    "evaluate the argument, aBlock for every element in the collection,
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1856
     passing both key and element as arguments.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1857
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1858
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1859
	#associationsDo:       (which passes keys->value pairs)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1860
	#do:                   (which only passes values)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1861
	#keysDo:               (which only passes keys)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1862
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1863
     This is much like #associationsDo:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1864
     key and value as two separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1865
     #associationsDo: is a bit slower.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1866
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1867
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1868
	      Iterate over a copy to do this."
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1869
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1870
    |key n "{ Class: SmallInteger }"
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1871
     deletedEntry|
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1872
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1873
    tally == 0 ifTrue:[^ self].
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1874
    n := keyArray basicSize.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  1875
    deletedEntry := DeletedEntry.
38
454b1b94a48e *** empty log message ***
claus
parents: 12
diff changeset
  1876
    1 to:n do:[:index |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1877
	key := keyArray basicAt:index.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1878
	(key notNil and:[key ~~ deletedEntry]) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1879
	    key == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1880
		key := nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1881
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1882
	    aTwoArgBlock value:key value:(valueArray basicAt:index)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1883
	].
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1884
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1885
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1886
    "Modified: 20.4.1996 / 11:33:42 / cg"
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1887
!
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1888
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1889
keysAndValuesSelect:aBlock
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1890
    "return a new collection with all elements from the receiver, for which
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1891
     the argument aBlock evaluates to true.
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1892
     The block gets keys and values as separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1893
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1894
     See also:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1895
	#associationsSelect:    (which passes key-value pairs),
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1896
	#keysSelect:            (which passes key values),
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1897
	#select:                (which only passes the value)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1898
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1899
     This is much like #associationsSelect:, but aBlock gets the
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1900
     key and value as two separate arguments.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1901
     #associationsSelect: is a bit slower.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1902
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1903
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1904
	      Iterate over a copy to do this."
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1905
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1906
    |newCollection|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1907
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1908
    newCollection := self species new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1909
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1910
	(aBlock value:key value:value) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1911
	    newCollection at:key put:value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1912
	]
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1913
    ].
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1914
    ^ newCollection
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1915
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1916
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1917
     |ages|
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1918
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1919
     ages := Dictionary new.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1920
     ages at:'cg' put:37.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1921
     ages at:'ca' put:33.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1922
     ages at:'sv' put:36.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1923
     ages at:'tk' put:28.
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1924
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1925
     ages keysAndValuesSelect:[:name :age |
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1926
		(name startsWith:'c') or:[age < 30]].
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1927
    "
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1928
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1929
    "Modified: 20.4.1996 / 11:34:29 / cg"
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1930
!
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
  1931
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  1932
keysDo:aBlock
13044
a22c040ba417 commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 12732
diff changeset
  1933
    "evaluate the argument, aBlock for every key in the collection.
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1934
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1935
     See also:
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1936
	#associationsDo:   (which passes key-value associations)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1937
	#keysAndValuesDo:  (which passes keys & values separately)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1938
	#do:               (which passes values only)
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  1939
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1940
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1941
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1942
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1943
    |sz "{ Class: SmallInteger }"
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1944
     key|
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  1945
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1946
    sz := keyArray size.
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1947
    1 to:sz do:[:index |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1948
	key := keyArray at:index.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1949
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1950
	    key == NilEntry ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1951
		key := nil
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1952
	    ].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1953
	    aBlock value:key
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1954
	]
5336
a285bb62a443 allow nil as key.
Claus Gittinger <cg@exept.de>
parents: 5279
diff changeset
  1955
    ]
13044
a22c040ba417 commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 12732
diff changeset
  1956
a22c040ba417 commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 12732
diff changeset
  1957
    "Modified: / 24-08-2010 / 10:13:58 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1958
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1959
11430
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1960
keysSelect:aBlock
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1961
    "return a new collection with all elements from the receiver, for which
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1962
     the argument aBlock evaluates to true.
11430
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1963
     The block gets the individual keys as its single argument.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1964
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1965
     See also:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1966
	#associationsSelect:            (which passes key->value associations),
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1967
	#keysAndValuesSelect:           (which passes key & value args)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1968
	#select:                        (which passes values as arg),
11430
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1969
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1970
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1971
	      Iterate over a copy to do this."
11430
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1972
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1973
    |newCollection|
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1974
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1975
    newCollection := self species new.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1976
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1977
	(aBlock value:key) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1978
	    newCollection at:key put:value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1979
	]
11430
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1980
    ].
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1981
    ^ newCollection
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1982
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1983
    "
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1984
     |d|
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1985
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1986
     d := Dictionary new.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1987
     d at:#foo put:#bar.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1988
     d at:#bar put:#baz.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1989
     d at:#baz put:#foo.
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1990
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1991
     d select:[:el | el startsWith:'b'].
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1992
    "
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1993
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1994
    "Modified: / 12-10-2006 / 11:24:06 / cg"
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1995
!
7306d849ead8 +keysSelect
Claus Gittinger <cg@exept.de>
parents: 11420
diff changeset
  1996
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1997
select:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1998
    "return a new collection with all elements from the receiver, for which
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  1999
     the argument aBlock evaluates to true.
10077
21e92bc1b64f comment
Claus Gittinger <cg@exept.de>
parents: 9939
diff changeset
  2000
     The block gets the individual values as its single argument.
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  2001
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2002
     See also:
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2003
	#associationsSelect:            (which passes key->value associations),
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2004
	#keysAndValuesSelect:           (which passes key & value args)
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2005
	#keysSelect:                    (which passes key values),
1233
21452b1c61eb more enumaration variations & commentary
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
  2006
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  2007
     WARNING: do not add/remove elements while iterating over the receiver.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2008
	      Iterate over a copy to do this."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2009
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2010
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2011
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2012
    newCollection := self species new.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2013
    self keysAndValuesDo:[:key :value |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2014
	(aBlock value:value) ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2015
	    newCollection at:key put:value
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2016
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2017
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2018
    ^ newCollection
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2019
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2020
    "
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2021
     |d|
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2022
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2023
     d := Dictionary new.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2024
     d at:#foo put:#bar.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2025
     d at:#bar put:#baz.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2026
     d at:#baz put:#foo.
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2027
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2028
     d select:[:el | el startsWith:'b'].
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2029
    "
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1053
diff changeset
  2030
10077
21e92bc1b64f comment
Claus Gittinger <cg@exept.de>
parents: 9939
diff changeset
  2031
    "Modified: / 12-10-2006 / 11:24:06 / cg"
7653
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2032
!
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2033
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2034
valuesDo:aBlock
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2035
    "perform the block for all values in the collection.
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2036
     Same as #do: - for VisualWorks compatibility"
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2037
0a81451a7a59 added #valuesDo: for compatibility
Claus Gittinger <cg@exept.de>
parents: 7306
diff changeset
  2038
    ^ self do:aBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2039
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2040
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2041
17671
8b8de728c259 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17264
diff changeset
  2042
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2043
!Dictionary methodsFor:'printing & storing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2044
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  2045
printElementsDo:aBlock
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  2046
    "redefined, so #printOn: prints associations"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2047
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  2048
    ^ self associationsDo:aBlock
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2049
3192
47bf17818a88 Use #printOn: from Collection.
Stefan Vogel <sv@exept.de>
parents: 2897
diff changeset
  2050
    "Created: / 20.1.1998 / 14:11:02 / stefan"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2051
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2052
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2053
storeOn:aStream
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2054
    "output a printed representation (which can be re-read)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2055
     onto the argument aStream"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2056
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2057
    |isEmpty|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2058
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2059
    thisContext isRecursive ifTrue:[
15857
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2060
        RecursiveStoreError raiseRequestWith:self.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2061
        ('Dictionary [error]: storeOn: of self referencing collection.') errorPrintCR.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2062
        aStream nextPutAll:'#recursive'.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2063
        ^ self
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2064
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2065
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2066
    aStream nextPutAll:'('.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2067
    aStream nextPutAll:(self class name).
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2068
    aStream nextPutAll:' new'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2069
    isEmpty := true.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2070
    self keysAndValuesDo:[:key :value |
15857
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2071
        aStream nextPutAll:' at:'.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2072
        key storeOn:aStream.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2073
        aStream nextPutAll:' put:'.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2074
        value storeOn:aStream.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2075
        aStream nextPutAll:'; '.
bf865a059191 class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 15800
diff changeset
  2076
        isEmpty := false
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2077
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2078
    isEmpty ifFalse:[aStream nextPutAll:' yourself'].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2079
    aStream nextPut:$)
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2080
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2081
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2082
     Dictionary new storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2083
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2084
     (Dictionary new at:1 put:'hello'; yourself) storeOn:Transcript
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2085
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2086
     (Dictionary new at:1 put:'hello'; at:2 put:nil; yourself) storeOn:Transcript
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2087
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2088
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2089
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2090
     |d|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2091
     d := Dictionary new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2092
     d at:1 put:'hello'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2093
     d at:'hello' put:#world.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2094
     d storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2095
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2096
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2097
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2098
     |d|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2099
     d := Dictionary new.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2100
     d at:1 put:'hello'.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2101
     d at:'hello' put:#world.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2102
     d at:2 put:d.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2103
     d storeOn:Transcript
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2104
    "
1417
59e7d3c1df6d showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
  2105
2288
033f06bf1bab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2094
diff changeset
  2106
    "Modified: 28.1.1997 / 00:37:46 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2107
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2108
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2109
!Dictionary methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2110
345
claus
parents: 302
diff changeset
  2111
compareSame:element1 with:element2
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2112
    "compare two elements for being the same. Here, return true if the
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2113
     elements are equal (i.e. using #=).
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2114
     Redefinable in subclasses."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2115
345
claus
parents: 302
diff changeset
  2116
    ^ element1 = element2
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2117
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2118
    "Modified: 22.4.1996 / 17:34:27 / cg"
345
claus
parents: 302
diff changeset
  2119
!
claus
parents: 302
diff changeset
  2120
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2121
emptyCollectionForKeys
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2122
    "return an empty collection to hold keys. Here, a Set is returned.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2123
     Redefinable in subclasses."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2124
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2125
    ^ Set new:(self size)
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2126
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2127
    "Modified: 22.4.1996 / 17:35:17 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2128
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2129
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2130
grow:newSize
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2131
    "grow the receiver to make space for at least newSize elements.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2132
     To do this, we have to rehash into the new arrays.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2133
     (which is done by re-adding all elements to a new, empty key/value array pair)."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2134
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2135
    |key deletedEntry oldKeyArray oldValueArray n
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2136
     oldSize  "{ Class:SmallInteger }"
10
claus
parents: 5
diff changeset
  2137
     newIndex "{ Class:SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2138
10
claus
parents: 5
diff changeset
  2139
    oldKeyArray := keyArray.
claus
parents: 5
diff changeset
  2140
    oldValueArray := valueArray.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2141
249
claus
parents: 217
diff changeset
  2142
    n := self class goodSizeFrom:newSize.
claus
parents: 217
diff changeset
  2143
    oldSize := oldKeyArray size.
claus
parents: 217
diff changeset
  2144
    n == oldSize ifTrue:[^ self].
claus
parents: 217
diff changeset
  2145
10
claus
parents: 5
diff changeset
  2146
    keyArray := self keyContainerOfSize:n.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2147
    valueArray := self valueContainerOfSize:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2148
249
claus
parents: 217
diff changeset
  2149
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2150
    deletedEntry := DeletedEntry.
10
claus
parents: 5
diff changeset
  2151
    1 to:oldSize do:[:index |
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2152
	key := oldKeyArray basicAt:index.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2153
	(key notNil and:[key ~~ deletedEntry]) ifTrue:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2154
	    newIndex := self findNil:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2155
	    keyArray basicAt:newIndex put:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2156
	    valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2157
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2158
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2159
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2160
18332
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2161
initializeForCapacity:minSize
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2162
    "initialize the contents array (for at least minSize slots)
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2163
     and set tally to zero.
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2164
     The size is increased to the next prime for better hashing behavior."
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2165
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2166
    |n|
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2167
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2168
    n := self class goodSizeFrom:minSize.
18342
6d9664be691d class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 18332
diff changeset
  2169
    (keyArray notNil and:[n == keyArray size]) ifTrue:[
18332
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2170
        keyArray atAllPut:nil.
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2171
        valueArray atAllPut:nil.
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2172
    ] ifFalse:[
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2173
        keyArray := self keyContainerOfSize:n.
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2174
        valueArray := self valueContainerOfSize:n.
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2175
    ].
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2176
    tally := 0
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2177
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2178
    "Modified: / 5.8.1998 / 10:48:51 / cg"
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2179
!
68976cee1b99 class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 17671
diff changeset
  2180
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2181
rehash
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2182
    "rehash contents - is done by re-adding all elements to a new, empty key/value array pair)."
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2183
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2184
    | oldKeyArray oldValueArray key
2
claus
parents: 1
diff changeset
  2185
      n        "{ Class:SmallInteger }"
claus
parents: 1
diff changeset
  2186
      newIndex "{ Class:SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2187
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2188
    oldKeyArray := keyArray.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2189
    oldValueArray := valueArray.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2190
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2191
    n := keyArray size.
10
claus
parents: 5
diff changeset
  2192
    keyArray := self keyContainerOfSize:n.
92
0c73b48551ac *** empty log message ***
claus
parents: 88
diff changeset
  2193
    valueArray := self valueContainerOfSize:n.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2194
2
claus
parents: 1
diff changeset
  2195
    1 to:n do:[:index |
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2196
	key := oldKeyArray basicAt:index.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2197
	(key notNil and:[key ~~ DeletedEntry]) ifTrue:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2198
	    newIndex := self findNil:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2199
	    keyArray basicAt:newIndex put:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2200
	    valueArray basicAt:newIndex put:(oldValueArray basicAt:index).
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2201
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2202
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2203
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2204
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2205
rehashFrom:startIndex
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2206
    "rehash elements starting at index - after a remove.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2207
     NOTE: this method is no longer needed;
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2208
	   the trick using DeletedEntry avoids the need to do this time
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2209
	   consuming operation, making remove pretty fast :-)
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2210
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2211
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2212
    |key i length
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2213
     index "{ Class:SmallInteger }" |
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2214
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2215
    length := keyArray basicSize.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2216
    index := startIndex.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2217
    key := keyArray basicAt:index.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2218
    [key notNil] whileTrue:[
12669
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2219
	key ~~ DeletedEntry ifTrue:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2220
	    i := self findNil:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2221
	    i == index ifTrue:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2222
		^ self
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2223
	    ].
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2224
	    keyArray basicAt:i put:key.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2225
	    valueArray basicAt:i put:(valueArray basicAt:index).
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2226
	    keyArray basicAt:index put:nil.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2227
	    valueArray basicAt:index put:nil.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2228
	].
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2229
	index == length ifTrue:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2230
	    index := 1
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2231
	] ifFalse:[
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2232
	    index := index + 1.
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2233
	].
2acc2409c3f4 changed: #declareAllNewFrom:
Claus Gittinger <cg@exept.de>
parents: 12668
diff changeset
  2234
	key := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2235
    ]
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2236
!
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  2237
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2238
valueContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2239
    "return a container for values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2240
     Extracted to make life of weak subclasses easier ..."
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  2241
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2242
    ^ Array basicNew:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2243
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2244
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2245
!Dictionary methodsFor:'searching'!
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2246
16808
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2247
findFirst:aBlock ifNone:exceptionValue
20228
54e93c9d0126 #DOCUMENTATION by mawalch
mawalch
parents: 20224
diff changeset
  2248
    "find the index of the first element, for which evaluation of the argument, aBlock returns true;
16808
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2249
     return its index or the value from exceptionValue if none detected.
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2250
     This is much like #detect:ifNone:, however, here an INDEX is returned,
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2251
     while #detect:ifNone: returns the element.
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2252
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2253
     Here we return the first key for which aBlock matches the value.
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2254
     Note that there is no order in a Dictionary, so any element is first."
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2255
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2256
    self keysAndValuesDo:[:eachKey :eachValue| (aBlock value:eachValue) ifTrue:[^ eachKey]].
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2257
    ^ exceptionValue value.
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2258
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2259
    "
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2260
        (Dictionary withKeys:#('a' 'b' 'c') andValues:#('bla' 'hello' 'hallo'))
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2261
            findFirst:[:v| v first = $h].
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2262
    "
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2263
!
793e0523328e class: Dictionary
Stefan Vogel <sv@exept.de>
parents: 16728
diff changeset
  2264
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2265
findFirstKey:aBlock
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2266
    "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
  2267
     returns true; return nil if none is detected."
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2268
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2269
    self keysDo:[:key |
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2270
	(aBlock value:key) ifTrue:[^ key].
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2271
    ].
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2272
    ^ nil
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2273
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2274
    "Created: 5.6.1996 / 11:55:50 / stefan"
1703
9337c2c135d7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1460
diff changeset
  2275
    "Modified: 8.10.1996 / 22:01:59 / cg"
1460
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2276
! !
370226bb8fd7 Add method #findFirstKey:
Stefan Vogel <sv@exept.de>
parents: 1432
diff changeset
  2277
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2278
!Dictionary methodsFor:'testing'!
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  2279
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2280
includes:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2281
    "return true, if the argument, aValue is stored in the dictionary,
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2282
     i.e. if there is an associaten, with aValue as value.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2283
     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
  2284
     the values have to be all scanned without any hashing.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2285
     You need a special collection (or two Dictionaries) to get this
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2286
     reverse mapping fast."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2287
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2288
    ^ self includesEqualValue:anObject
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2289
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1233
diff changeset
  2290
    "Modified: 22.4.1996 / 17:20:11 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2291
!
68
59faa75185ba *** empty log message ***
claus
parents: 54
diff changeset
  2292
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2293
includesAssociation:anAssociation
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2294
    "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
  2295
     same key and value as the argument, anAssociation.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2296
     NOTICE: in contrast to #includes:, this compares both key and value."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2297
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2298
    |val|
44
b262907c93ea *** empty log message ***
claus
parents: 38
diff changeset
  2299
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2300
    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
  2301
    ^ self compareSame:val with:anAssociation value
d8173a888c93 oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents: 3304
diff changeset
  2302
d8173a888c93 oops - compareSame was wrong (used with #includesAssociation:)
Claus Gittinger <cg@exept.de>
parents: 3304
diff changeset
  2303
    "Modified: / 5.3.1998 / 20:35:00 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2304
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2305
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2306
includesEqualValue:aValue
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2307
    "return true, if the argument, aValue is stored in the dictionary,
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2308
     i.e. if there is an associaten, with aValue as value.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2309
     This is a slow search, since there is no fast reverse mapping;
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2310
     the values have to be all scanned without any hashing.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2311
     You need a special collection (or two Dictionaries) to get this
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2312
     reverse mapping fast."
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2313
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2314
    aValue isNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2315
	"/ need a special case for that ...
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2316
	^ self includesIdenticalValue:aValue.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2317
    ].
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2318
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2319
    ^ valueArray includes:aValue
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2320
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2321
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2322
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2323
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2324
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2325
     d at:'1' put:'one'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2326
     d includes:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2327
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2328
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2329
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2330
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2331
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2332
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2333
     d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2334
     d at:2 put:nil.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2335
     d includes:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2336
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2337
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2338
!
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2339
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2340
includesIdenticalValue:aValue
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2341
    "return true, if the argument, aValue is stored in the dictionary,
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2342
     i.e. if there is an associaten, with aValue as value.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2343
     This is a slow search, since there is no fast reverse mapping;
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2344
     the values have to be all scanned without any hashing.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2345
     You need a special collection (or two Dictionaries) to get this
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2346
     reverse mapping fast."
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2347
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2348
    |idx|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2349
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2350
    aValue isNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2351
	"/ need a special case for that ...
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2352
	idx := 0.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2353
	[true] whileTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2354
	    idx := valueArray identityIndexOf:nil startingAt:idx+1.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2355
	    idx == 0 ifTrue:[^ false].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2356
	    (keyArray at:idx) notNil ifTrue:[^ true].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2357
	]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2358
    ].
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2359
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2360
    ^ valueArray includesIdentical:aValue
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2361
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2362
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2363
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2364
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2365
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2366
     d at:'1' put:'one'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2367
     d includes:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2368
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2369
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2370
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2371
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2372
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2373
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2374
     d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2375
     d at:2 put:nil.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2376
     d includes:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2377
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2378
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2379
!
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2380
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2381
includesKey:aKey
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2382
    "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
  2383
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2384
    ^ (self find:(aKey ? NilEntry) ifAbsent:0) ~~ 0
9570
008049e411b5 includesKey: returned a wrong answer for a nil key.
Claus Gittinger <cg@exept.de>
parents: 9413
diff changeset
  2385
008049e411b5 includesKey: returned a wrong answer for a nil key.
Claus Gittinger <cg@exept.de>
parents: 9413
diff changeset
  2386
    "Modified: / 17-08-2006 / 21:06:41 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2387
!
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  2388
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2389
includesValue:aValue
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2390
    "return true, if the argument, aValue is stored in the dictionary,
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2391
     i.e. if there is an associaten, with aValue as value.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2392
     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
  2393
     the values have to be all scanned without any hashing.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2394
     You need a special collection (or two Dictionaries) to get this
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2395
     reverse mapping fast."
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  2396
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2397
    ^ self includesEqualValue:aValue
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2398
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2399
!
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  2400
15363
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2401
isDictionary
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2402
    "return true, if the receiver is some kind of dictionary;
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2403
     true returned here - the method is redefined from Object."
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2404
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2405
    ^ true
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2406
!
d2a30831055a class: Dictionary
Claus Gittinger <cg@exept.de>
parents: 15008
diff changeset
  2407
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2408
occurrencesOf:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2409
    "count & return how often anObject is stored in the dictionary.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2410
     This counts values - not keys. Uses #= (i.e. equality) compare."
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2411
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2412
    |idx count|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2413
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2414
    anObject isNil ifTrue:[
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2415
	"/ need a special case for that ...
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2416
	idx := 0.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2417
	count := 0.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2418
	[true] whileTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2419
	    idx := valueArray identityIndexOf:nil startingAt:idx+1.
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2420
	    idx == 0 ifTrue:[^ count].
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2421
	    (keyArray at:idx) notNil ifTrue:[
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2422
		count := count + 1
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2423
	    ]
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2424
	]
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2425
    ].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2426
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2427
    ^ valueArray occurrencesOf:anObject
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2428
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2429
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2430
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2431
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2432
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2433
     d at:'1' put:'one'.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2434
     d occurrencesOf:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2435
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2436
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2437
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2438
     |d|
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2439
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2440
     d := Dictionary new.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2441
     d at:'1' put:'one'.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2442
     d at:2 put:nil.
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2443
     d at:5 put:nil.
14318
aa571cad5e4b NilKey moved to Set
Stefan Vogel <sv@exept.de>
parents: 14271
diff changeset
  2444
     d occurrencesOf:nil.
5050
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2445
    "
ea292b958108 allow for nil to be stored as a value.
Claus Gittinger <cg@exept.de>
parents: 4871
diff changeset
  2446
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2447
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
  2448
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2449
!Dictionary methodsFor:'visiting'!
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2450
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2451
acceptVisitor:aVisitor with:aParameter
16728
0182965b1985 comment/format only
Claus Gittinger <cg@exept.de>
parents: 16512
diff changeset
  2452
    "dispatch for visitor pattern; send #visitDictionary:with: to aVisitor"
8395
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2453
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2454
    ^ aVisitor visitDictionary:self with:aParameter
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2455
! !
6bd97113cb4c Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7906
diff changeset
  2456
20972
08f1105af523 #DOCUMENTATION by mawalch
mawalch
parents: 20386
diff changeset
  2457
2094
42cd02703bf4 handle recursive printString/displayString
Claus Gittinger <cg@exept.de>
parents: 1703
diff changeset
  2458
!Dictionary class methodsFor:'documentation'!
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  2459
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  2460
version
18787
a1b603604641 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18342
diff changeset
  2461
    ^ '$Header$'
12659
6884221caecf changed: #declareAllFrom:
Michael Beyl <mb@exept.de>
parents: 11909
diff changeset
  2462
!
6884221caecf changed: #declareAllFrom:
Michael Beyl <mb@exept.de>
parents: 11909
diff changeset
  2463
6884221caecf changed: #declareAllFrom:
Michael Beyl <mb@exept.de>
parents: 11909
diff changeset
  2464
version_CVS
18787
a1b603604641 #DOCUMENTATION
Stefan Vogel <sv@exept.de>
parents: 18342
diff changeset
  2465
    ^ '$Header$'
633
46e996b3c18f version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  2466
! !
14897
41e26dc8902b comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14320
diff changeset
  2467