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