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