Set.st
author Stefan Vogel <sv@exept.de>
Wed, 22 Feb 2012 13:55:54 +0100
changeset 14019 9b08cdb0db50
parent 14017 000ab1370eaa
child 14134 72811a1ed607
permissions -rw-r--r--
changed: #copyWithoutAll:
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
159
514c749165c3 *** empty log message ***
claus
parents: 119
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
"
5471
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
    12
"{ Package: 'stx:libbasic' }"
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
Collection subclass:#Set
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    15
	instanceVariableNames:'tally keyArray'
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    16
	classVariableNames:'DeletedEntry'
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    17
	poolDictionaries:''
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    18
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
2272
9942d9c853c3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
    21
Object subclass:#EmptySlot
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    22
	instanceVariableNames:''
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
    23
	classVariableNames:'TheOneAndOnlyInstance'
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    24
	poolDictionaries:''
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    25
	privateIn:Set
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    26
!
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    27
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    28
!Set class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
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
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
    33
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    34
88
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
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    43
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    45
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    46
    a Set is a collection where each element occurs at most once.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    47
    The inclusion test is done using = for comparison; 
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    48
    see IdentitySet for sets using identity compare.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    49
    Keep in mind, that a regular Set therefore treats 3.0 and 3 as equal
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    50
    and therefore:
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    51
	(Set with:3.0) includes:3
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    52
    will return true (since 3.0 and 3 are equal).
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    53
    In contrast, an IdentitySet will return false, because 3.0 and 3 are not
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    54
    identical.
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    55
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    56
    Sets use hashing for fast access, this access is considerably faster,
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    57
    if a good hash-number is returned by the elements.
362
claus
parents: 359
diff changeset
    58
claus
parents: 359
diff changeset
    59
    Notice that the default hash (Object>>hash) is not perfect; due to
claus
parents: 359
diff changeset
    60
    the implementation of hash-keys in ST/X, increased hash collisions
claus
parents: 359
diff changeset
    61
    are to be expected for large sets (say: > 20000 element). 
claus
parents: 359
diff changeset
    62
    If your objects are heavyly used in sets or dictionaries, and you need
claus
parents: 359
diff changeset
    63
    big collections, your instances may provide a better hash values.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    64
345
claus
parents: 324
diff changeset
    65
    Performance hints: 
657
449935f15b9e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    66
      If only symbols or smallIntegers are used as keys, 
362
claus
parents: 359
diff changeset
    67
      use an instance of IdentitySet for slightly better performance, 
claus
parents: 359
diff changeset
    68
      since both hashing and comparison is faster.
345
claus
parents: 324
diff changeset
    69
claus
parents: 324
diff changeset
    70
      If you have a rough idea how big the set is going to grow,
claus
parents: 324
diff changeset
    71
      create it using #new: instead of #new. Even if the size given is a
claus
parents: 324
diff changeset
    72
      poor guess (say half of the real size), there is some 20-30% performance
claus
parents: 324
diff changeset
    73
      win to expect, since many resizing operations of the set are avoided.
claus
parents: 324
diff changeset
    74
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    75
    Examples:
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    76
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    77
	|s|
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    78
	s := Set new.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    79
	s add:'hello'.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    80
	s add:'world'.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    81
	s add:#foo.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    82
	s add:1.2345678.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    83
	s add:'hello'.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    84
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    85
	s printCR.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    86
	's size -> ' print. s size printCR.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    87
	'(s includes:''hello'') -> ' print. (s includes:'hello') printCR.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    88
	'(s includes:#foo)    -> ' print. (s includes:#foo) printCR.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    89
	'(s includes:''foo'')   -> ' print. (s includes:'foo') printCR.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    90
	'(s includes:#bar)    -> ' print. (s includes:#bar) printCR.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    91
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    92
    [author:]
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
    93
	Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    94
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    95
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    97
!Set class methodsFor:'initialization'!
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    98
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    99
initialize
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   100
    "initialize the Set class"
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   101
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   102
    DeletedEntry isNil ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   103
	DeletedEntry := EmptySlot new
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   104
    ].
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   105
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   106
    "Set initialize"
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   107
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   108
    "Modified: 24.1.1997 / 21:09:00 / cg"
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   109
! !
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   110
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   111
!Set class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   113
decodeFromLiteralArray:anArray
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   114
    "create & return a new instance from information encoded in anArray."
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   115
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   116
    |set 
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   117
     sz "{ Class: SmallInteger }"|
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   118
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   119
    sz := anArray size.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   120
    set := self new:sz-1.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   121
    2 to:sz do:[:idx| set add:(anArray at:idx) decodeAsLiteralArray].
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   122
    ^ set
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   123
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   124
    "
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   125
     (Set with:1234
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   126
          with:(1 @ 2)
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   127
          with:'hello'
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   128
     ) literalArrayEncoding decodeAsLiteralArray    
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   129
    "
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   130
!
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   131
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
    "return a new empty Set"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
a27a279701f8 Initial revision
claus
parents:
diff changeset
   135
    ^ self new:7
a27a279701f8 Initial revision
claus
parents:
diff changeset
   136
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   137
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
new:anInteger
a27a279701f8 Initial revision
claus
parents:
diff changeset
   139
    "return a new empty Set with space for anInteger elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   141
    "
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   142
     make it somewhat bigger; hashing works better if fill grade is
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   143
     below 10% (make it 75% here ..)
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   144
    "
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   145
    ^ self basicNew setTally:(anInteger * 4 // 3)
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   146
! !
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   147
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   148
!Set class methodsFor:'queries'!
252
   149
   150
goodSizeFrom:arg 
   151
    "return a good array size for the given argument.
   152
     Returns the next prime after arg, since prime sizes are good for hashing."
   153
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   154
    |n|
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   155
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   156
    "/ arg <= 11 ifTrue:[^ 11].
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   157
    "/ n := arg * 3 // 2.
252
   158
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   159
    arg <= 7 ifTrue:[^ 7].
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   160
    n := arg.
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   161
252
   162
    "
   163
     mhmh - this returns good numbers for collections with up-to about
   164
     500k elements; if you have bigger ones, add some more primes here ...
   165
    "
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   166
    n <= 524288 ifTrue:[
9318
42d7dac12b73 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9310
diff changeset
   167
           "2  4  8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288"
42d7dac12b73 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9310
diff changeset
   168
        ^ #(7  7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit)
252
   169
    ].
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   170
9318
42d7dac12b73 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9310
diff changeset
   171
    ^ n nextPrime
252
   172
! !
   173
3048
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   174
!Set class methodsFor:'utilities'!
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   175
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   176
rehashAllSubInstances
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   177
    "rehash all sets & dictionaries.
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   178
     Useful utility if hash/identityHash method was changed
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   179
     of some object which is known to be kept in a set"
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   180
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   181
     self allSubInstances do:[:s | s rehash]
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   182
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   183
    "
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   184
     Set rehashAllSubInstances
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   185
    "
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   186
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   187
    "Created: / 24.10.1997 / 23:13:44 / cg"
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   188
! !
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   189
7268
4a2b7acd6913 method category rename
Claus Gittinger <cg@exept.de>
parents: 7260
diff changeset
   190
!Set methodsFor:'Compatibility-ST80'!
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   191
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   192
initialIndexFor:hashKey boundedBy:length
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   193
    "for ST-80 compatibility only; it is (currently) not used in this
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   194
     implementation of sets. Therefore, in ST/X it does not make sense
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   195
     to redefine it. (which may be a bad design decision, but slightly
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   196
     improves performance, by avoiding an extra message send ...)"
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   197
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   198
    |index "{ Class:SmallInteger }"|
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   199
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   200
    index := hashKey bitAnd:16r3FFFFFFF.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   201
    index < 16r1FFFFFFF ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   202
	index := index * 2
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   203
    ].
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   204
    index := index \\ length + 1.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   205
    ^ index.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   206
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   207
    "Modified: 1.3.1997 / 01:01:13 / cg"
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   208
! !
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   209
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   210
!Set methodsFor:'Compatibility-Squeak'!
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   211
11052
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   212
copyWithout:elementToSkip
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   213
    "return a new collection consisting of a copy of the receiver, with
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   214
     ALL elements equal to elementToSkip are left out.
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   215
     No error is reported, if elementToSkip is not in the collection."
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   216
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   217
    ^ self select:[:each | each ~= elementToSkip]
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   218
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   219
    "
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   220
     #(1 2 3 4 5 6 7) asSet copyWithout:5
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   221
    "
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   222
!
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   223
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   224
copyWithoutAll:elementsToSkip
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   225
    "return a new collection consisting of a copy of the receiver, with
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   226
     ALL elements equal to elementToSkip are left out.
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   227
     No error is reported, if elementsToSkip is not in the collection."
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   228
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   229
    elementsToSkip size * self size > 10000 ifTrue:[
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   230
        "speed up everything"
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   231
        ^ self \ (elementsToSkip asSet).
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   232
    ].
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   233
    ^ self \ elementsToSkip
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   234
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   235
    "
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   236
     #(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9)
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   237
     #(a b c d e f) asIdentitySet copyWithoutAll:#(d e f)
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   238
     #('a' 'b' 'c' 'd' 'e' 'f') asIdentitySet copyWithoutAll:#('d' 'e' 'f')
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   239
     #('a' 'b' 'c' 'd' 'e' 'f') asSet copyWithoutAll:#('d' 'e' 'f')
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   240
    "
12342
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   241
!
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   242
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   243
like:anObject
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   244
    "Answer an object in the receiver that is equal to anObject,
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   245
     nil if no such object is found. 
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   246
     Relies heavily on hash properties (i.e. that two object's hashes are equal
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   247
     if the two object compare equal)"
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   248
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   249
    ^ self elementAt:anObject ifAbsent:[ nil ]
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   250
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   251
    "
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   252
     (Set withAll:#(10.0 20.0 30.0 40.0)) like:20
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   253
    "
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   254
! !
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   255
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   256
!Set methodsFor:'accessing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   257
12734
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   258
addFirst:anObject
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   259
    "add the argument, anObject to the receiver.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   260
     If the receiver is ordered, the new element will be added at the beginning.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   261
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   262
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   263
    ^ self shouldNotImplement
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   264
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   265
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
at:index
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   267
    "report an error: at: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   268
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   269
    ^ self errorNotKeyed
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   270
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   271
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   272
at:index put:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   273
    "report an error: at:put: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   274
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   275
    ^ self errorNotKeyed
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   276
!
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   277
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   278
elementAt:anObject
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   279
    "return the element, if contained in the set.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   280
     If there is none, report an error.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   281
     This may seem confusing at first - however, it is useful with
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   282
     non-identitysets, to find an existing element, for a 
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   283
     given equal (but not identical) object.
12342
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   284
     This is the same functionality as provided by the goodies/KeyedSet goody."
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   285
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   286
    ^ self elementAt:anObject ifAbsent:[^ self errorNotFound].
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   287
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   288
    "Modified: 20.3.1997 / 20:35:24 / cg"
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   289
!
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   290
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   291
elementAt:anObject ifAbsent:exceptionBlock
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   292
    "return the element, if contained in the set.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   293
     If there is none, return the result from evaluating exceptionBlock.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   294
     This may seem confusing at first - however, it is useful with
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   295
     non-identitysets, to find an existing element, for a 
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   296
     given equal (but not identical) object.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   297
     This is the same functionality as provided by the goodies/KeyedSet
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   298
     goody."
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   299
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   300
    |index "{ Class: SmallInteger }" |
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   301
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   302
    index := self find:anObject ifAbsent:0.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   303
    index == 0 ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   304
	^ exceptionBlock value
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   305
    ].
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   306
    ^ keyArray basicAt:index
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   307
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   308
    "Created: 20.3.1997 / 20:34:07 / cg"
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   309
    "Modified: 20.3.1997 / 20:35:49 / cg"
12734
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   310
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   311
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   312
removeLast
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   313
    "remove the last element from the receiver.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   314
     Return the removed element.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   315
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   316
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   317
    ^ self shouldNotImplement
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   318
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   319
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   320
reverseDo:aBlock
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   321
    "evaluate the argument, aBlock for each element in reverse order.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   322
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   323
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   324
    ^ self shouldNotImplement
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
!Set methodsFor:'adding & removing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   329
add:anObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   330
    "add the argument, anObject to the receiver.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   331
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   332
     WARNING: do not add elements while iterating over the receiver.
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   333
              Iterate over a copy to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2323
diff changeset
   335
    |index "{ Class: SmallInteger }"|
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   337
    anObject isNil ifTrue:[  
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   338
        ^ self invalidElementError.
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   339
    ].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   341
    index := self findKeyOrNil:anObject.
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   342
    (keyArray basicAt:index) isNil ifTrue:[
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   343
        "/ not already there
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   344
        keyArray basicAt:index put:anObject.
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   345
        tally := tally + 1.
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   346
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   347
        self fullCheck.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   349
    ^ anObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   350
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2323
diff changeset
   351
    "Modified: 30.1.1997 / 14:58:08 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   352
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   354
addAllNonNilElements:aCollection
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   355
    "add all non-nil elements of the argument, aCollection to the receiver.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   356
     Use this, when operating on a Set, that cannot hold nil.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   357
     Answer the argument, aCollection (sigh)."
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   358
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   359
    aCollection do:[:eachElement |
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   360
        eachElement notNil ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   361
            self add:eachElement
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   362
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   363
    ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   364
    ^ aCollection
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   365
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   366
    "
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   367
     #(1 2 3 4) asSet addAllNonNilElements:#(5 nil 6 7 8)
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   368
    "
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   369
!
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   370
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
remove:oldObject ifAbsent:exceptionBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   372
    "remove oldObject from the collection and return it.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   373
     If it was not in the collection return the value of exceptionBlock.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   374
     Notice, that the returned object could be non-identical to the argument
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   375
     (although it will always be equal).
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   376
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   377
     WARNING: do not remove elements while iterating over the receiver.
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   378
	      See #saveRemove: to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   379
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   380
    |index next objectRemoved|
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
3265
18d96c910653 handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents: 3261
diff changeset
   382
    oldObject isNil ifTrue:[^ exceptionBlock value].
18d96c910653 handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents: 3261
diff changeset
   383
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   384
    index := self find:oldObject ifAbsent:0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   385
    index == 0 ifTrue:[^ exceptionBlock value].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   386
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   387
    objectRemoved := keyArray basicAt:index.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   388
    keyArray basicAt:index put:nil.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   389
    tally := tally - 1.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   390
    tally == 0 ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   391
	keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). 
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   392
    ] ifFalse:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   393
	index == keyArray basicSize ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   394
	    next := 1
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   395
	] ifFalse:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   396
	    next := index + 1.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   397
	].
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   398
	(keyArray basicAt:next) notNil ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   399
	    keyArray basicAt:index put:DeletedEntry.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   400
	].
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   401
	self emptyCheck
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
    ].
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   403
    ^ objectRemoved
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   404
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   405
    "Modified: / 16.11.2001 / 10:14:24 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   408
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   409
    "remove all elements from the receiver. Returns the receiver."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   410
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   411
    self setTally:7.
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   412
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   413
    "Modified: 12.4.1996 / 13:35:06 / cg"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   414
!
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   415
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   416
saveRemove:oldObject 
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   417
    "remove the element, oldObject from the collection.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   418
     Return the element 
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   419
     (could be non-identical to oldObject, since I hash on equality, not on identity).
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   420
     If it was not in the collection return nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   421
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   422
     In contrast to #remove:, this does not resize the underlying collection
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   423
     and therefore does NOT rehash & change the elements order.
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   424
     Therefore this can be used while enumerating the receiver,
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   425
     which is not possible if #remove: is used.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   426
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   427
     WARNING: since no resizing is done, the physical amount of memory used
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   428
              by the container remains the same, although the logical size shrinks.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   429
              You may want to manually resize the receiver using #emptyCheck.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   430
              (after the loop)"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   431
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   432
    |index "{ Class:SmallInteger }"
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   433
     next  "{ Class:SmallInteger }"
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   434
     removedObject|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   435
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   436
    oldObject isNil ifTrue:[^ nil].
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   437
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   438
    index := self find:oldObject ifAbsent:0.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   439
    index == 0 ifTrue:[^ nil].
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   440
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   441
    removedObject := keyArray basicAt:index.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   442
    keyArray basicAt:index put:nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   443
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   444
    tally := tally - 1.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   445
    tally ~~ 0 ifTrue:[
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   446
        index == keyArray basicSize ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   447
            next := 1
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   448
        ] ifFalse:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   449
            next := index + 1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   450
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   451
        (keyArray basicAt:next) notNil ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   452
            keyArray basicAt:index put:DeletedEntry
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   453
        ].
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   454
    ].
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   455
    ^ removedObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   456
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   457
    "does NOT work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   458
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   459
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   460
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   461
        s := Set new.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   462
        s add:1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   463
        s add:2.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   464
        s add:3.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   465
        s add:4.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   466
        s add:5.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   467
        s add:6.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   468
        s add:7.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   469
        s add:8.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   470
        s add:9.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   471
        s do:[:v |
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   472
            v odd ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   473
                s remove:v 
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   474
            ]
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   475
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   476
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   477
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   478
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   479
    "DOES work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   480
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   481
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   482
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   483
        s := Set new.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   484
        s add:1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   485
        s add:2.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   486
        s add:3.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   487
        s add:4.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   488
        s add:5.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   489
        s add:6.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   490
        s add:7.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   491
        s add:8.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   492
        s add:9.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   493
        s do:[:v |
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   494
            v odd ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   495
                s saveRemove:v 
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   496
            ]
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   497
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   498
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   499
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   500
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   501
    "Created: / 1.3.1996 / 21:14:26 / cg"
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   502
    "Modified: / 16.11.2001 / 10:22:59 / cg"
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   503
!
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   504
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   505
testAndAdd:anObject
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   506
    "add the argument, anObject to the receiver.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   507
     Answer true, if the element did already exist in the collection,
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   508
     false otherwise.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   509
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   510
     WARNING: do not add elements while iterating over the receiver.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   511
              Iterate over a copy to do this."
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   512
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   513
    |index "{ Class: SmallInteger }"|
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   514
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   515
    anObject isNil ifTrue:[
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   516
        ^ self invalidElementError.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   517
    ].
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   518
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   519
    index := self findKeyOrNil:anObject.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   520
    (keyArray basicAt:index) isNil ifTrue:[
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   521
        "/ not already there
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   522
        keyArray basicAt:index put:anObject.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   523
        tally := tally + 1.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   524
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   525
        self fullCheck.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   526
        ^ false.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   527
    ].
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   528
    ^ true.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   529
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   530
    "Modified: 30.1.1997 / 14:58:08 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   531
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   532
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   533
!Set methodsFor:'binary storage'!
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   534
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   535
readBinaryContentsFrom: stream manager: manager
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   536
    "must rehash after reload"
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   537
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   538
    super readBinaryContentsFrom: stream manager: manager.
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   539
    self rehash
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   540
! !
11052
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   541
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   542
!Set methodsFor:'comparing'!
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   543
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   544
= aCollection
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   545
    "return true, if the argument is a Set containing the same elements
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   546
     as I do"
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   547
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   548
    aCollection species == self species ifFalse:[^ false].
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   549
    aCollection size == self size ifFalse:[^ false].
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   550
    "/ same number of elements; since I am a Set, all of
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   551
    "/ of my elements must be in the other collection ...
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
   552
    ^ self conform:[:element | (aCollection includes:element)]
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   553
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   554
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   555
     #(1 2 3 4 5) asSet = #(2 3 4 5 1) asSet
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   556
     #(1 2 3 4 5) asSet = #(2 3 4 5 1.0) asSet 
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   557
     #(1 2 3 4 5) asSet = #(2 3 4 5 'one') asSet 
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   558
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   559
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   560
     |d1 d2|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   561
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   562
     d1 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   563
     d2 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   564
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   565
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   566
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   567
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   568
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   569
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   570
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   571
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   572
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   573
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   574
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   575
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   576
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   577
     d1 = d2     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   578
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   579
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   580
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   581
     |d1 d2|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   582
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   583
     d1 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   584
     d2 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   585
     d1 at:1 put:'uno'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   586
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   587
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   588
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   589
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   590
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   591
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   592
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   593
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   594
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   595
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   596
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   597
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   598
     d1 = d2     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   599
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   600
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
   601
    "Modified: / 13-10-2006 / 12:58:43 / cg"
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   602
!
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   603
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   604
hash
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   605
    "return a hash key for the receiver"
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   606
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   607
    "this hash is stupid - but for larger collections, the hashing
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   608
     time can become much bigger than the time lost in added probing.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   609
     Time will show ...
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   610
     Notice & warning:
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   611
	if the #= method is ever changed to compare non-dictionaries equal,
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   612
	the code below must be changed to assert that the same hash-value is 
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   613
	still returned.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   614
	(which may be hard to acomplish)
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   615
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   616
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   617
    |mySize|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   618
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   619
    mySize := self size.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   620
    mySize == 0 ifTrue:[^ 0].
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   621
    ^ ((self first) hash times:mySize) bitAnd:16r3FFFFFFF
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   622
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   623
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   624
     |d|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   625
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   626
     d := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   627
     d at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   628
     d at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   629
     d at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   630
     d at:'two' put:2.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   631
     d hash     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   632
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   633
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   634
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   635
     |d|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   636
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   637
     d := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   638
     d at:1 put:'uno'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   639
     d at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   640
     d at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   641
     d at:'two' put:2.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   642
     d hash     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   643
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   644
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   645
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   646
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   647
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   648
! !
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   649
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   650
!Set methodsFor:'copying'!
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   651
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   652
postCopy
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   653
    "have to copy the keyArray too"
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   654
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   655
    keyArray := keyArray shallowCopy
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   656
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   657
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   658
!Set methodsFor:'enumerating'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   659
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   660
do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   661
    "perform the block for all members in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   662
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   663
     WARNING: do not add/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
   664
	      Iterate over a copy to do this."
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   665
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   666
    |sz "{ Class: SmallInteger }"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   667
     element|
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   668
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   669
    sz := keyArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   670
    1 to:sz do:[:index |
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   671
	element := keyArray at: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
   672
	(element notNil and:[element ~~ DeletedEntry]) 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
   673
	    aBlock value:element
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
   674
	]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   675
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   676
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   677
    "Modified: 1.3.1996 / 21:41:13 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   678
! !
61
claus
parents: 50
diff changeset
   679
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   680
!Set methodsFor:'inspecting'!
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   681
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   682
inspectorClass
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   683
    "redefined to use SetInspector
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   684
     (instead of the default Inspector)."
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   685
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   686
    ^ SetInspectorView
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
   687
! !
969
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   688
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   689
!Set methodsFor:'obsolete set operations'!
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   690
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   691
+ aCollection
8606
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   692
    "Kept for backward compatibility. 
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   693
     Use #union: instead, to isolate arithmethic and set operations"
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   694
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   695
    <resource: #obsolete>
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   696
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   697
    ^ self union:aCollection.
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   698
!
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   699
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   700
- aCollection
8606
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   701
    "Kept for backward compatibility. 
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   702
     Use #\ instead, to isolate arithmethic and set operations"
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   703
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   704
    <resource: #obsolete>
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   705
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   706
    ^ self \ aCollection
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   707
! !
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   708
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   709
!Set methodsFor:'private'!
2
claus
parents: 1
diff changeset
   710
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   711
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   712
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
   713
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
   714
     return the value of evaluating aBlock."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   715
61
claus
parents: 50
diff changeset
   716
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   717
     length "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   718
     startIndex probe|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   719
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   720
    length := keyArray basicSize.
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
   721
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
   722
"/
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
   723
"/  length < 10 ifTrue:[
362
claus
parents: 359
diff changeset
   724
"/      "assuming, that for small collections the overhead of hashing
claus
parents: 359
diff changeset
   725
"/       is larger ... maybe that proves wrong 
claus
parents: 359
diff changeset
   726
"/       (if overhead of comparing is higher)"
claus
parents: 359
diff changeset
   727
"/      ^ keyArray indexOf:key ifAbsent:aBlock
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
   728
"/  ].
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   729
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   730
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   731
a27a279701f8 Initial revision
claus
parents:
diff changeset
   732
    [true] whileTrue:[
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   733
        probe := (keyArray basicAt:index).
13279
10d204f50776 changed: #find:ifAbsent:
sr
parents: 12963
diff changeset
   734
        (probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index].
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   735
        (self slotIsEmpty:probe) ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   736
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   737
        index == length ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   738
            index := 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   739
        ] ifFalse:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   740
            index := index + 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   741
        ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   742
        index == startIndex ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   743
    ]
13279
10d204f50776 changed: #find:ifAbsent:
sr
parents: 12963
diff changeset
   744
10d204f50776 changed: #find:ifAbsent:
sr
parents: 12963
diff changeset
   745
    "Modified: / 03-02-2011 / 13:53:18 / sr"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   746
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   747
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   748
findKeyOrNil:key
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
   749
    "Look for the key in the receiver.  
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   750
     If it is found, return the index of the first unused slot. 
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
   751
     Grow the receiver, if key was not found, and no unused slots were present"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   752
61
claus
parents: 50
diff changeset
   753
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   754
     length "{ Class:SmallInteger }"
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
   755
     startIndex probe 
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   756
     delIndex|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   757
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   758
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   759
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   760
a27a279701f8 Initial revision
claus
parents:
diff changeset
   761
    [true] whileTrue:[
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   762
        probe := keyArray basicAt:index.
13311
53dece2ccaa1 changed: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 13279
diff changeset
   763
        (probe notNil and:[probe ~~ DeletedEntry and:[key = probe]]) ifTrue:[^ index].
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   764
        (self slotIsEmpty:probe) ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   765
            delIndex isNil ifTrue:[^ index].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   766
            keyArray basicAt:delIndex put:nil.
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   767
            ^ delIndex
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   768
        ].
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
   769
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   770
        probe == DeletedEntry ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   771
            delIndex isNil ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   772
                delIndex := index
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   773
            ]
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   774
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   775
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   776
        index == length ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   777
            index := 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   778
        ] ifFalse:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   779
            index := index + 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   780
        ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   781
        index == startIndex ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   782
            delIndex notNil ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   783
                keyArray basicAt:delIndex put:nil.
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   784
                ^ delIndex
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   785
            ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   786
            ^ self grow findKeyOrNil:key
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   787
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   788
    ]
1450
18c2a1da8be2 first look for nil-entry in findKeyOrNil: - saves a compare in the good case, when adding new elements
Claus Gittinger <cg@exept.de>
parents: 1319
diff changeset
   789
13311
53dece2ccaa1 changed: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 13279
diff changeset
   790
    "Modified: / 27-02-2011 / 15:30:42 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   791
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   792
a27a279701f8 Initial revision
claus
parents:
diff changeset
   793
findNil:key
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   794
    "Look for the next slot usable for key.  
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   795
     WARNING:
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   796
	This method assumes that key is not already in the receiver 
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   797
	AND that keyArray does not have previously removed entries 
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   798
	AND that there is an empty slot.
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   799
     To be used ONLY while growing/rehashing to enter elements into a fresh
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   800
     collection - if any of the above conditions is not met, the method
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   801
     loops forever."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   802
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   803
    |index  "{ Class:SmallInteger }"
61
claus
parents: 50
diff changeset
   804
     length "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   805
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   806
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   807
    index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   808
5772
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   809
"/    index := keyArray identityIndexOf:nil startingAt:index.
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   810
"/    index == 0 ifTrue:[
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   811
"/        index := keyArray identityIndexOf:nil startingAt:1 endingAt:index.
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   812
"/    ].
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   813
    "/ that code is completely inlined by stc ...
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
   814
    [(keyArray basicAt:index) notNil] whileTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   815
	index == length ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   816
	    index := 1
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   817
	] ifFalse:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   818
	    index := index + 1
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   819
	].
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   820
	"notice: no check for no nil found - we must find one since
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   821
	 this is only called after growing"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   822
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   823
    ^ index
1055
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   824
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   825
    "Modified: 21.3.1997 / 10:32:58 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   826
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   827
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   828
hashFor:aKey
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   829
    "return the arguments hash value.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   830
     Redefined in subclasses, which use a different comparison (i.e. identity compare)"
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   831
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   832
    ^ aKey hash
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   833
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   834
    "Created: 19.3.1997 / 15:03:03 / cg"
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   835
!
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   836
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   837
initialIndexForKey:aKey
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   838
    "return an initial index given a key."
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   839
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   840
    |index  "{ Class:SmallInteger }"
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   841
     length "{ Class:SmallInteger }"|
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   842
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   843
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   844
    index := (self hashFor:aKey) bitAnd:16r3FFFFFFF.
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   845
    index < 16r1FFFFFFF ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   846
	index := index * 2
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   847
    ].
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   848
    index := index \\ length + 1.
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   849
    ^ index.
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   850
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   851
    "Modified: 1.3.1997 / 01:01:13 / cg"
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   852
    "Created: 19.3.1997 / 15:02:41 / cg"
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   853
!
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
   854
5845
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   855
invalidElementError
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   856
    "this is send when a nil is used with a Set.
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   857
     For now this is just an info-Message. Will become an error sometimes."
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   858
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   859
    "/ self elementBoundError.
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   860
9086
cd770483c45d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
   861
    InfoPrinting ifTrue:[
cd770483c45d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8831
diff changeset
   862
        'Set [warning]: nil is not a valid element - will be an error in later versions' errorPrintCR.
11472
d30ca682c65f showWhereWeCameFrom moved
Claus Gittinger <cg@exept.de>
parents: 11052
diff changeset
   863
        Context showWhereWeCameFrom.
6540
cf1a6a1a8645 better invalidElementError message
Claus Gittinger <cg@exept.de>
parents: 6519
diff changeset
   864
    ].
5845
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   865
!
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
   866
3261
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   867
keyArray
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   868
    ^ keyArray
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   869
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   870
    "Created: / 2.2.1998 / 14:50:55 / cg"
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   871
!
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
   872
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   873
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   874
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   875
     Extracted to make life of weak subclasses easier ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   876
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   877
    ^ Array basicNew:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   878
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   879
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   880
rehash
61
claus
parents: 50
diff changeset
   881
    "rehash is done by re-adding all elements to a new empty set.
claus
parents: 50
diff changeset
   882
     Rehash is needed after a binaryRead, for example."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   883
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   884
    |element oldKeyArray newKeyArray newIdx
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   885
     n "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   886
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   887
    oldKeyArray := keyArray.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   888
    n := oldKeyArray size.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   889
    keyArray := newKeyArray := self keyContainerOfSize:n.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   890
2
claus
parents: 1
diff changeset
   891
    1 to:n do:[:index |
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   892
	element := oldKeyArray at:index.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   893
	(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   894
	    "cannot be already there"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   895
	    newIdx := self findNil:element.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   896
	    newKeyArray basicAt:newIdx put:element
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   897
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   898
    ]
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   899
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   900
    "Modified: / 30.10.1997 / 16:04:46 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   901
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   902
a27a279701f8 Initial revision
claus
parents:
diff changeset
   903
rehashFrom:startIndex
61
claus
parents: 50
diff changeset
   904
    "rehash elements starting at index - after a remove.
claus
parents: 50
diff changeset
   905
     Notice: due to the new implementation of remove, 
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   906
	     this is no longer needed"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   907
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   908
    |element i "{ Class:SmallInteger }"
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   909
     length
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   910
     index "{ Class:SmallInteger }" |
a27a279701f8 Initial revision
claus
parents:
diff changeset
   911
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   912
    length := keyArray basicSize.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   913
    index := startIndex.
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   914
    element := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   915
    [element notNil] whileTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   916
	i := self findNil:element.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   917
	i == index ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   918
	    ^ self
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   919
	].
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   920
	keyArray basicAt:i put:element.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   921
	keyArray basicAt:index put:nil.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   922
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   923
	index == length ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   924
	    index := 1
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   925
	] ifFalse:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   926
	    index := index + 1.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   927
	].
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   928
	element := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   929
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   930
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   931
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   932
setTally:count
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   933
    "initialize the contents array (for at least count slots) 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   934
     and set tally to zero.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   935
     The size is increased to the next prime for better hashing behavior."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   936
3726
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   937
    |n|
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   938
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   939
    n := self class goodSizeFrom:count.
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   940
    n == keyArray size ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   941
	keyArray atAllPut:nil.
3726
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   942
    ] ifFalse:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   943
	keyArray := self keyContainerOfSize:n. 
3726
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   944
    ].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   945
    tally := 0
3726
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   946
cf3eafa152a4 try to avoid allocation of a new keyArray in #setTally:
Claus Gittinger <cg@exept.de>
parents: 3265
diff changeset
   947
    "Modified: / 4.8.1998 / 22:07:25 / cg"
6233
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   948
!
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   949
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   950
slotIsEmpty:aSlotValue
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   951
    "only redefined in weak subclasses, since they treat a 0-value
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   952
     as being empty"
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   953
0a79cbd07543 *** empty log message ***
martin
parents: 6191
diff changeset
   954
    ^ aSlotValue isNil
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   955
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   956
7260
edfa8d6a6046 method category rename
Claus Gittinger <cg@exept.de>
parents: 7169
diff changeset
   957
!Set methodsFor:'private-grow & shrink'!
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   958
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   959
emptyCheck
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   960
    "check if the receiver has become too empty (after a remove)
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   961
     and shrink if it makes sense.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   962
     Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   963
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   964
    |sz      "{Class: SmallInteger}"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   965
     newSize "{Class: SmallInteger}" |
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   966
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   967
    sz := keyArray basicSize.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   968
    sz > 56 ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   969
	"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   970
	 shrink if too empty
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   971
	"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   972
	tally < (sz // 8) ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   973
	    newSize := sz // 4.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   974
	    self grow:newSize
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   975
	]
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   976
    ]
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   977
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   978
    "Modified: 19.3.1997 / 16:02:55 / cg"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   979
!
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   980
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   981
fullCheck
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   982
    "check if collection is full (after an add); grow if so.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   983
     Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   984
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   985
    |sz "{Class: SmallInteger}" |
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   986
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   987
    "
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   988
     grow if filled more than 75% 
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   989
    "
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   990
    sz := keyArray basicSize.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   991
    tally > (sz * 3 // 4) ifTrue:[
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   992
       self grow
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   993
    ]
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   994
!
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   995
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   996
grow
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   997
    "change the number of element slots of the collection to a useful
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   998
     new size"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   999
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1000
    self grow:(keyArray basicSize * 2)
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1001
!
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1002
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1003
grow:newSize
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1004
    "change the number of element slots of the collection - to do this,
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1005
     we have to rehash (which is done by re-adding all elements to a new
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1006
     empty set)."
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1007
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1008
    |elem oldKeyArray newKeyArray deletedEntry
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1009
     containerSize oldSize "{ Class:SmallInteger }"|
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1010
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1011
    oldKeyArray := keyArray.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1012
    oldSize := oldKeyArray size.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1013
    containerSize := (self class goodSizeFrom:newSize).
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1014
    containerSize == oldSize ifTrue:[^ self].
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1015
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1016
    keyArray := newKeyArray := self keyContainerOfSize:containerSize. 
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1017
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1018
    deletedEntry := DeletedEntry.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1019
    1 to:oldSize do:[:srcIndex |
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1020
	elem := oldKeyArray basicAt:srcIndex.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1021
	(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1022
	    "cannot be already there"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1023
	    newKeyArray basicAt:(self findNil:elem) put:elem
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1024
	].
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1025
    ].
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1026
! !
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1027
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1028
!Set methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1029
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1030
collisionCount
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1031
    "return the number of key collisions in the set.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1032
     There is a collision if two keys hash to the same value."
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1033
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1034
    |count|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1035
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1036
    count := 0.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1037
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1038
    keyArray do:[:eachKey|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1039
        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1040
            count := count + (self collisionsFor:eachKey).
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1041
        ]
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1042
    ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1043
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1044
    ^ count
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1045
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1046
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1047
collisionsFor:key
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1048
    "Return the number of searches - 1 required for key"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1049
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1050
    |index  "{ Class:SmallInteger }"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1051
     length "{ Class:SmallInteger }"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1052
     hash "{ Class:SmallInteger }"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1053
     step "{ Class:SmallInteger }"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1054
     startIndex probe count|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1055
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1056
    length := keyArray basicSize.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1057
    hash := (self hashFor:key) bitAnd:16r3FFFFFFF.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1058
    hash < 16r1FFFFFFF ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1059
        hash := hash * 2
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1060
    ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1061
    startIndex := index := hash \\ length + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1062
    step:= hash // length + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1063
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1064
    count := 0.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1065
    [true] whileTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1066
        probe := (keyArray basicAt:index).
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1067
        (probe notNil and:[key = probe]) ifTrue:[^ count].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1068
        (self slotIsEmpty:probe) ifTrue:[self error:'non existing key'].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1069
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1070
        index == length ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1071
            index := 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1072
        ] ifFalse:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1073
            index := index + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1074
        ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1075
        count := count + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1076
        index == startIndex ifTrue:[self error:'non existing key'].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1077
    ]
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1078
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1079
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1080
maxChainLength
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1081
    "return the number of the maximum chain length in the set.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1082
     This is the worst case overhead when accessing a key."
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1083
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1084
    |chainLength key|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1085
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1086
    chainLength := 0.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1087
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1088
    keyArray do:[:eachKey| |t|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1089
        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1090
            t := self collisionsFor:eachKey.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1091
            chainLength < t ifTrue:[chainLength := t. key := eachKey].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1092
        ]
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1093
    ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1094
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1095
    ^ (key -> chainLength)
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1096
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1097
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1098
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1099
    "return the number of set elements"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1100
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1101
    ^ tally
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1102
! !
5471
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
  1103
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1104
!Set methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1105
362
claus
parents: 359
diff changeset
  1106
capacity 
claus
parents: 359
diff changeset
  1107
    "return the number of elements, that the receiver is
claus
parents: 359
diff changeset
  1108
     prepared to take.
claus
parents: 359
diff changeset
  1109
     Not used by the system; added for ST-80 compatibility."
claus
parents: 359
diff changeset
  1110
claus
parents: 359
diff changeset
  1111
    ^ keyArray size
claus
parents: 359
diff changeset
  1112
!
claus
parents: 359
diff changeset
  1113
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1114
includes:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1115
    "return true if the argument anObject is in the receiver"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1116
5568
826390eef742 includes:nil - allowed, but send out a warning
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
  1117
    anObject isNil ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1118
	"even if adding nil to a set is forbidden, asking for it should be ok!!"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1119
	"self invalidElementError."
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1120
	^ false 
5568
826390eef742 includes:nil - allowed, but send out a warning
Claus Gittinger <cg@exept.de>
parents: 5557
diff changeset
  1121
    ].
362
claus
parents: 359
diff changeset
  1122
    ^ (self find:anObject ifAbsent:0) ~~ 0
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1123
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1124
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1125
isEmpty
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1126
    "return true if the receiver is empty"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1127
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1128
    ^ tally == 0
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1129
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1130
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1131
isFixedSize
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1132
    "return true if the receiver cannot grow - this will vanish once
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1133
     Arrays and Strings learn how to grow ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1134
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1135
    ^ false
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1136
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1137
2383
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1138
notEmpty
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1139
    "return true if the receiver is not empty"
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1140
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1141
    ^ tally ~~ 0
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1142
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1143
    "Created: 12.2.1997 / 12:39:02 / cg"
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1144
!
8b2957f5ec90 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2328
diff changeset
  1145
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1146
occurrencesOf:anObject
5048
b1e10ce753fd comment
Claus Gittinger <cg@exept.de>
parents: 4795
diff changeset
  1147
    "return the number of occurrences of anObject in the receiver.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
  1148
     As I am a Set, this can only return 0 or 1."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1149
5845
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
  1150
    anObject isNil ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1151
	"even if adding nil to a set is forbidden, asking for it should be ok!!"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1152
	"self invalidElementError."
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1153
	^ 0.
5845
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
  1154
    ].
64a385f14970 Use #invalidElementError to warn about illegal use of nil in Sets
Stefan Vogel <sv@exept.de>
parents: 5834
diff changeset
  1155
362
claus
parents: 359
diff changeset
  1156
    (self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1157
    ^ 1
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
  1158
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
  1159
    "Modified: / 16.11.2001 / 10:30:14 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1160
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1161
4795
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1162
sameContentsAs:aCollection
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1163
    "answer true, if all the elements in self and aCollection
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1164
     are common. This is not defined as #=, since we cannot redefine #hash
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1165
     for aCollection."
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1166
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1167
    aCollection size ~~ self size ifTrue:[
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
  1168
        ^ false
4795
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1169
    ].
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1170
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
  1171
    ^ aCollection conform:[:e | (self includes:e)]
4795
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1172
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1173
    "
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1174
      #(1 2 3) asSet sameContentsAs: #(1 2 3)
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1175
      #(1 2 3 4) asSet sameContentsAs: #(1 2 3)
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1176
      #(1 2 3) asSet sameContentsAs: #(1 2 3 3)
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1177
      #(1 2 3 'aa') asSet sameContentsAs: #(1 2 3 'aa')
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1178
      #(1 2 3 'aa') asIdentitySet sameContentsAs: #(1 2 3 'aa')
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1179
      #(1 2 3 #aa) asIdentitySet sameContentsAs: #(1 2 3 #aa)
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1180
    "
4bdb6488d5b2 New method #sameContentsAs:
Stefan Vogel <sv@exept.de>
parents: 4017
diff changeset
  1181
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
  1182
    "Modified: / 13-10-2006 / 12:59:01 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1183
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1184
8394
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1185
!Set methodsFor:'visiting'!
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1186
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1187
acceptVisitor:aVisitor with:aParameter
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1188
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1189
    ^ aVisitor visitSet:self with:aParameter
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1190
! !
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1191
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1192
!Set::EmptySlot class methodsFor:'instance creation'!
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1193
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1194
basicNew
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1195
    TheOneAndOnlyInstance isNil ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1196
	TheOneAndOnlyInstance := super basicNew
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1197
    ].
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1198
    ^ TheOneAndOnlyInstance
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1199
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1200
! !
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1201
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
  1202
!Set class methodsFor:'documentation'!
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1203
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1204
version
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
  1205
    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.110 2012-02-22 12:55:54 stefan Exp $'
12342
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1206
!
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1207
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1208
version_CVS
14019
9b08cdb0db50 changed: #copyWithoutAll:
Stefan Vogel <sv@exept.de>
parents: 14017
diff changeset
  1209
    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.110 2012-02-22 12:55:54 stefan Exp $'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1210
! !
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
  1211
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1212
Set initialize!