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