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