Set.st
author Stefan Vogel <sv@exept.de>
Tue, 28 Apr 2020 16:22:26 +0200
changeset 25375 b784fc06a5eb
parent 24619 37f1256b4281
permissions -rw-r--r--
#REFACTORING by stefan class: KeyedCollection class added: #newWithCapacity:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24619
37f1256b4281 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24180
diff changeset
     1
"{ Encoding: utf8 }"
37f1256b4281 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24180
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1991 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5471
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
    14
"{ Package: 'stx:libbasic' }"
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
    15
17263
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
    16
"{ NameSpace: Smalltalk }"
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
Collection subclass:#Set
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    19
	instanceVariableNames:'tally keyArray'
14314
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    20
	classVariableNames:'DeletedEntry NilEntry'
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    21
	poolDictionaries:''
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    22
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
2272
9942d9c853c3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
    25
Object subclass:#EmptySlot
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    26
	instanceVariableNames:''
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
    27
	classVariableNames:'TheOneAndOnlyInstance'
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    28
	poolDictionaries:''
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    29
	privateIn:Set
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    30
!
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    31
14314
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    32
Object subclass:#NilKey
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    33
	instanceVariableNames:''
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    34
	classVariableNames:'TheOneAndOnlyInstance'
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    35
	poolDictionaries:''
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    36
	privateIn:Set
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    37
!
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
    38
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    39
!Set class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    42
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    43
 COPYRIGHT (c) 1991 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
    44
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    45
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    46
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    47
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    48
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    49
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    50
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    51
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    52
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    53
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    54
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    55
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    56
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    57
    a Set is a collection where each element occurs at most once.
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    58
    The inclusion test is done using equality (=) for comparison; 
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    59
    see IdentitySet for sets using identity compare.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    60
    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
    61
    and therefore:
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    62
        (Set with:3.0) includes:3
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    63
    will return true (since 3.0 and 3 are equal).
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    64
    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
    65
    identical.
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
    66
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    67
    Sets use hashing for fast access, this access is considerably faster,
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    68
    if a good hash-number is returned by the elements.
362
claus
parents: 359
diff changeset
    69
claus
parents: 359
diff changeset
    70
    Notice that the default hash (Object>>hash) is not perfect; due to
claus
parents: 359
diff changeset
    71
    the implementation of hash-keys in ST/X, increased hash collisions
claus
parents: 359
diff changeset
    72
    are to be expected for large sets (say: > 20000 element). 
claus
parents: 359
diff changeset
    73
    If your objects are heavyly used in sets or dictionaries, and you need
claus
parents: 359
diff changeset
    74
    big collections, your instances may provide a better hash values.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    75
345
claus
parents: 324
diff changeset
    76
    Performance hints: 
657
449935f15b9e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    77
      If only symbols or smallIntegers are used as keys, 
362
claus
parents: 359
diff changeset
    78
      use an instance of IdentitySet for slightly better performance, 
claus
parents: 359
diff changeset
    79
      since both hashing and comparison is faster.
345
claus
parents: 324
diff changeset
    80
claus
parents: 324
diff changeset
    81
      If you have a rough idea how big the set is going to grow,
claus
parents: 324
diff changeset
    82
      create it using #new: instead of #new. Even if the size given is a
claus
parents: 324
diff changeset
    83
      poor guess (say half of the real size), there is some 20-30% performance
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    84
      win to expect, since many resizing operations of the set are avoided
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    85
      (resizing is expensive, as the set does a rehash).
345
claus
parents: 324
diff changeset
    86
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    87
    Examples:
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    88
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    89
        |s|
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    90
        s := Set new.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    91
        s add:'hello'.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    92
        s add:'world'.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    93
        s add:#foo.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    94
        s add:1.2345678.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    95
        s add:'hello'.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    96
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    97
        s printCR.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    98
        's size -> ' print. s size printCR.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
    99
        '(s includes:''hello'') -> ' print. (s includes:'hello') printCR.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
   100
        '(s includes:#foo)    -> ' print. (s includes:#foo) printCR.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
   101
        '(s includes:''foo'')   -> ' print. (s includes:'foo') printCR.
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
   102
        '(s includes:#bar)    -> ' print. (s includes:#bar) printCR.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   103
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   104
    [author:]
14300
5b7ebcd0d2e0 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14134
diff changeset
   105
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   106
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
   107
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   109
!Set class methodsFor:'initialization'!
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   110
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   111
initialize
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   112
    "initialize the Set class"
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   113
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   114
    DeletedEntry isNil ifTrue:[
14314
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
   115
        DeletedEntry := EmptySlot new.
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
   116
        NilEntry := NilKey new.
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   117
    ].
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   118
14314
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
   119
    "
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
   120
        Set initialize
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
   121
    "
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   122
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   123
    "Modified: 24.1.1997 / 21:09:00 / cg"
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   124
! !
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   125
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   126
!Set class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   128
decodeFromLiteralArray:anArray
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   129
    "create & return a new instance from information encoded in anArray."
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   130
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   131
    |set 
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   132
     sz "{ Class: SmallInteger }"|
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   133
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   134
    sz := anArray size.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   135
    set := self new:sz-1.
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   136
    2 to:sz do:[:idx| set add:(anArray at:idx) decodeAsLiteralArray].
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   137
    ^ set
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   138
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   139
    "
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   140
     (Set with:1234
8831
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   141
          with:(1 @ 2)
918f291920c2 Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents: 8606
diff changeset
   142
          with:'hello'
6083
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   143
     ) literalArrayEncoding decodeAsLiteralArray    
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   144
    "
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   145
!
98a14d57e673 added: literalArrayEncoding
tm
parents: 5845
diff changeset
   146
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
    "return a new empty Set"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
23330
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   150
    ^ self new:5
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   151
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   152
    "Modified: / 11-09-2018 / 17:58:42 / Stefan Vogel"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   153
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
new:anInteger
a27a279701f8 Initial revision
claus
parents:
diff changeset
   156
    "return a new empty Set with space for anInteger elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   158
    "
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   159
     make it somewhat bigger; hashing works better if fill grade is
23854
62302a0ed7ad #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 23845
diff changeset
   160
     below 90% (make it 75% here ..)
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   161
    "
23330
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   162
    ^ self basicNew initializeForCapacity:(anInteger * 4 // 3)+1
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   163
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   164
    "Modified: / 11-09-2018 / 17:58:51 / Stefan Vogel"
23854
62302a0ed7ad #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 23845
diff changeset
   165
    "Modified (comment): / 08-03-2019 / 09:35:39 / Claus Gittinger"
22307
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   166
!
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   167
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   168
newWithCapacity:size
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   169
    "return a new empty Collection with capacity for n elements."
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   170
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   171
    ^ self new:size
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   172
ec05bef825fb #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 21659
diff changeset
   173
    "Created: / 10-10-2017 / 17:58:47 / stefan"
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   174
! !
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   175
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   176
!Set class methodsFor:'queries'!
252
   177
   178
goodSizeFrom:arg 
   179
    "return a good array size for the given argument.
   180
     Returns the next prime after arg, since prime sizes are good for hashing."
   181
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   182
    |n|
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   183
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   184
    "/ arg <= 11 ifTrue:[^ 11].
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   185
    "/ n := arg * 3 // 2.
252
   186
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   187
    arg <= 7 ifTrue:[^ 7].
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   188
    n := arg.
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   189
252
   190
    "
   191
     mhmh - this returns good numbers for collections with up-to about
   192
     500k elements; if you have bigger ones, add some more primes here ...
   193
    "
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   194
    n <= 524288 ifTrue:[
9318
42d7dac12b73 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9310
diff changeset
   195
           "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
   196
        ^ #(7  7 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit)
252
   197
    ].
2461
2d65e1845502 minimum goodSize reduced to 7.
Claus Gittinger <cg@exept.de>
parents: 2459
diff changeset
   198
9318
42d7dac12b73 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9310
diff changeset
   199
    ^ n nextPrime
252
   200
! !
   201
3048
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   202
!Set class methodsFor:'utilities'!
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   203
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   204
rehashAllSubInstances
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   205
    "rehash all sets & dictionaries.
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   206
     Useful utility if hash/identityHash method was changed
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   207
     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
   208
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   209
     self allSubInstances do:[:s | s rehash]
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   210
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   211
    "
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   212
     Set rehashAllSubInstances
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   213
    "
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   214
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   215
    "Created: / 24.10.1997 / 23:13:44 / cg"
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   216
! !
88c3a9c669e8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2475
diff changeset
   217
23330
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   218
7268
4a2b7acd6913 method category rename
Claus Gittinger <cg@exept.de>
parents: 7260
diff changeset
   219
!Set methodsFor:'Compatibility-ST80'!
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   220
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   221
initialIndexFor:hashKey boundedBy:length
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   222
    "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
   223
     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
   224
     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
   225
     improves performance, by avoiding an extra message send ...)"
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   226
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   227
    |index "{ Class:SmallInteger }"|
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   228
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   229
    index := hashKey bitAnd:16r3FFFFFFF.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   230
    index < 16r1FFFFFFF ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
   231
	index := index * 2
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   232
    ].
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   233
    index := index \\ length + 1.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   234
    ^ index.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   235
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   236
    "Modified: 1.3.1997 / 01:01:13 / cg"
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   237
! !
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
   238
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   239
!Set methodsFor:'Compatibility-Squeak'!
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
like:anObject
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   242
    "Answer an object in the receiver that is equal to anObject,
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   243
     nil if no such object is found. 
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   244
     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
   245
     if the two object compare equal)"
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   246
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   247
    ^ self elementAt:anObject ifAbsent:[ nil ]
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
    "
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   250
     (Set withAll:#(10.0 20.0 30.0 40.0)) like:20
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   251
    "
7311
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   252
! !
6878116b0a27 +copyWithout/copyWithoutAll:
Claus Gittinger <cg@exept.de>
parents: 7268
diff changeset
   253
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   254
!Set methodsFor:'accessing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   255
12734
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   256
addFirst:anObject
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   257
    "add the argument, anObject to the receiver.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   258
     If the receiver is ordered, the new element will be added at the beginning.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   259
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   260
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   261
    ^ self shouldNotImplement
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   262
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   263
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   264
at:index
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
    "report an error: at: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   267
    ^ self errorNotKeyed
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
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   270
at:index put:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   271
    "report an error: at:put: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   272
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   273
    ^ self errorNotKeyed
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   274
!
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   275
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   276
elementAt:anObject
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   277
    "return the element, if contained in the set.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   278
     If there is none, report an error.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   279
     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
   280
     non-identitysets, to find an existing element, for a 
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   281
     given equal (but not identical) object.
12342
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
   282
     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
   283
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   284
    ^ self elementAt:anObject ifAbsent:[^ self errorNotFound].
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
    "Modified: 20.3.1997 / 20:35:24 / cg"
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
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   289
elementAt:anObject ifAbsent:exceptionBlock
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   290
    "return the element, if contained in the set.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   291
     If there is none, return the result from evaluating exceptionBlock.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   292
     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
   293
     non-identitysets, to find an existing element, for a 
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   294
     given equal (but not identical) object.
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   295
     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
   296
     goody."
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   297
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   298
    |index "{ Class: SmallInteger }"  ret|
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   299
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   300
    index := self find:(anObject ? NilEntry)ifAbsent:0.
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   301
    index == 0 ifTrue:[
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   302
        ^ exceptionBlock value
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   303
    ].
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   304
    ret := keyArray basicAt:index.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   305
    ret == NilEntry ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   306
        ret := nil.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   307
    ].
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   308
    ^ ret.
2469
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   309
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   310
    "Created: 20.3.1997 / 20:34:07 / cg"
ecca86d50852 added #elementAt: / #elementAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2461
diff changeset
   311
    "Modified: 20.3.1997 / 20:35:49 / cg"
12734
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   312
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   313
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   314
removeLast
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   315
    "remove the last element from the receiver.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   316
     Return the removed element.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   317
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   318
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   319
    ^ self shouldNotImplement
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   320
!
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   321
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   322
reverseDo:aBlock
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   323
    "evaluate the argument, aBlock for each element in reverse order.
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   324
     An error is raised here - it does not make sense for unordered collections"
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   325
Claus Gittinger <cg@exept.de>
parents: 12342
diff changeset
   326
    ^ self shouldNotImplement
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
! !
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
!Set methodsFor:'adding & removing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   331
add:keyArg
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   332
    "add the argument, anObject to the receiver.
14895
1112f18b10d1 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 14656
diff changeset
   333
     Return the added element.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   334
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   335
     WARNING: do not add elements while iterating over the receiver.
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   336
              Iterate over a copy to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   338
    |key index "{ Class: SmallInteger }"|
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   340
    keyArg isNil ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   341
        key := NilEntry.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   342
    ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   343
        key := keyArg.
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   344
    ].
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   346
    index := self findKeyOrNil:key.
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   347
    (keyArray basicAt:index) isNil ifTrue:[
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   348
        "/ not already there
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   349
        keyArray basicAt:index put:key.
10868
0f72216e1c1d *** empty log message ***
sr
parents: 10588
diff changeset
   350
        tally := tally + 1.
5335
fae7e0698241 added a warning: adding nil will soon be an error.
Claus Gittinger <cg@exept.de>
parents: 5298
diff changeset
   351
17263
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
   352
        self possiblyGrow.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   353
    ].
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   354
    ^ keyArg
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   355
2328
0fd1d715e5a9 oops - the last one was not good
Claus Gittinger <cg@exept.de>
parents: 2323
diff changeset
   356
    "Modified: 30.1.1997 / 14:58:08 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   357
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   358
23189
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   359
addOrReplace:anObject
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   360
    "add the argument, anObject to the receiver.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   361
     If it is already included, replace it by anObject.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   362
     Return nil, if anObject was not present in the receiver, 
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   363
     otherwise the element that has been replaced.
24619
37f1256b4281 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24180
diff changeset
   364
     Notice: this sounds like a useless operation, but because sets compare by equality, 
37f1256b4281 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24180
diff changeset
   365
             the old object may be replaced by another object when equeal, but not identical.
37f1256b4281 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 24180
diff changeset
   366
             For example, a string by a symbol or a float by an integer...
23189
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   367
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   368
     WARNING: do not add elements while iterating over the receiver.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   369
              Iterate over a copy to do this."
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   370
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   371
    |key index "{ Class: SmallInteger }" existingKey|
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   372
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   373
    anObject isNil ifTrue:[
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   374
        key := NilEntry.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   375
    ] ifFalse:[
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   376
        key := anObject.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   377
    ].
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   378
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   379
    index := self findKeyOrNil:key.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   380
    existingKey := keyArray basicAt:index.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   381
    keyArray basicAt:index put:key.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   382
    existingKey isNil ifTrue:[
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   383
        "/ not already there
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   384
        tally := tally + 1.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   385
        self possiblyGrow.
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   386
    ].
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   387
    ^ existingKey
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   388
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   389
    "
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   390
        Note that 1 is replaced by 1.0:
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   391
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   392
        self new 
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   393
                addOrReplace:1;
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   394
                addOrReplace:2;
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   395
                addOrReplace:nil;
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   396
                addOrReplace:1.0;
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   397
                yourself
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   398
    "
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   399
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   400
    "Created: / 03-07-2018 / 19:13:58 / Stefan Vogel"
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   401
    "Modified (comment): / 03-07-2018 / 23:38:13 / Stefan Vogel"
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   402
!
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
   403
17410
9c4592a7b540 class: Set
Stefan Vogel <sv@exept.de>
parents: 17263
diff changeset
   404
clearContents
22571
06c8f9ab1a8c #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22444
diff changeset
   405
    "remove all elements from the receiver, but do not resize the underlying keyArray.
21348
6040a106c11f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20356
diff changeset
   406
     Returns the receiver.
6040a106c11f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20356
diff changeset
   407
     Similar to removeAll, but might behave better, 
6040a106c11f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20356
diff changeset
   408
     if the receiver is to be filled again afterwards."
17410
9c4592a7b540 class: Set
Stefan Vogel <sv@exept.de>
parents: 17263
diff changeset
   409
9c4592a7b540 class: Set
Stefan Vogel <sv@exept.de>
parents: 17263
diff changeset
   410
    keyArray atAllPut:nil.
17413
8048855efc47 class: Set
Stefan Vogel <sv@exept.de>
parents: 17410
diff changeset
   411
    tally := 0.
21348
6040a106c11f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20356
diff changeset
   412
6040a106c11f #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20356
diff changeset
   413
    "Modified (comment): / 06-02-2017 / 12:55:05 / cg"
17410
9c4592a7b540 class: Set
Stefan Vogel <sv@exept.de>
parents: 17263
diff changeset
   414
!
9c4592a7b540 class: Set
Stefan Vogel <sv@exept.de>
parents: 17263
diff changeset
   415
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   416
remove:oldObjectArg ifAbsent:exceptionBlock
19080
0f75504d1ce4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18466
diff changeset
   417
    "remove the first occurrence of oldObject from the collection and return it.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   418
     If it was not in the collection return the value of exceptionBlock.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   419
     Notice, that the returned object could be non-identical to the argument
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   420
     (although it will always be equal).
1052
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
     WARNING: do not remove elements while iterating over the receiver.
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   423
              See #saveRemove: to do this."
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   424
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   425
    |oldObject index next removedObject|
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   427
    oldObjectArg isNil ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   428
        oldObject := NilEntry.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   429
    ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   430
        oldObject := oldObjectArg.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   431
    ].
3265
18d96c910653 handle attempt to remove nil.
Claus Gittinger <cg@exept.de>
parents: 3261
diff changeset
   432
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   433
    index := self find:oldObject ifAbsent:0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   434
    index == 0 ifTrue:[^ exceptionBlock value].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   436
    removedObject := keyArray basicAt:index.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   437
    keyArray basicAt:index put:nil.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   438
    tally := tally - 1.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   439
    tally == 0 ifTrue:[
22951
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
   440
        self possiblyShrinkToZero
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   441
    ] ifFalse:[
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   442
        index == keyArray basicSize ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   443
            next := 1
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   444
        ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   445
            next := index + 1.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   446
        ].
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   447
        (keyArray basicAt:next) notNil ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   448
            keyArray basicAt:index put:DeletedEntry.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   449
        ].
17263
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
   450
        self possiblyShrink
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   451
    ].
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   452
    removedObject == NilEntry ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   453
        removedObject := nil.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   454
    ].
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   455
    ^ removedObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   456
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   457
    "Modified: / 16.11.2001 / 10:14:24 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   458
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   459
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   460
removeAll
21350
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   461
    "remove all elements from the receiver and resize (shrink) the underlying container. 
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   462
     Returns the receiver.
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   463
     Similar to clearContents, which but might behave better, 
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   464
     if the receiver is to be filled again afterwards."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   465
18331
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
   466
    self initializeForCapacity:7.
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   467
21350
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   468
    "Modified: / 12-04-1996 / 13:35:06 / cg"
a71d9cb8ee31 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 21348
diff changeset
   469
    "Modified (comment): / 06-02-2017 / 12:56:14 / cg"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   470
!
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   471
15806
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   472
removeIdentical:oldObjectArg ifAbsent:exceptionBlock
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   473
    "remove oldObject from the collection and return it.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   474
     If it was not in the collection return the value of exceptionBlock.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   475
     Uses identity compare (==) to search for an occurrence.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   476
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   477
     WARNING: do not remove elements while iterating over the receiver.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   478
              See #saveRemove: to do this."
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   479
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   480
    |oldObject index next|
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   481
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   482
    oldObjectArg isNil ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   483
        oldObject := NilEntry.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   484
    ] ifFalse:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   485
        oldObject := oldObjectArg.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   486
    ].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   487
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   488
    "first a quick check. 
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   489
     There is a high possibility that objects, which are
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   490
     equal are also identical"
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   491
    index := self find:oldObject ifAbsent:0.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   492
    index ~~ 0 ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   493
        oldObject ~~ (keyArray basicAt:index) ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   494
            index := 0.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   495
        ]
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   496
    ].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   497
    index == 0 ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   498
        "have to go the long and hard path..."
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   499
        index := self findIdentical:oldObject ifAbsent:0.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   500
        index == 0 ifTrue:[^ exceptionBlock value].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   501
    ].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   502
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   503
    keyArray basicAt:index put:nil.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   504
    tally := tally - 1.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   505
    tally == 0 ifTrue:[
22951
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
   506
        self possiblyShrinkToZero
15806
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   507
    ] ifFalse:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   508
        index == keyArray basicSize ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   509
            next := 1
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   510
        ] ifFalse:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   511
            next := index + 1.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   512
        ].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   513
        (keyArray basicAt:next) notNil ifTrue:[
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   514
            keyArray basicAt:index put:DeletedEntry.
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   515
        ].
17263
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
   516
        self possiblyShrink
15806
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   517
    ].
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   518
    ^ oldObjectArg
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   519
!
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   520
23207
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   521
replaceAll:oldObject with:newObject
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   522
    "replace all oldObjects by newObject in the receiver."
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   523
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   524
    self 
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   525
        remove:oldObject ifAbsent:[];
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   526
        addOrReplace:newObject.
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   527
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   528
    "Created: / 13-07-2018 / 12:04:31 / Stefan Vogel"
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   529
!
37d3c6400f95 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 23189
diff changeset
   530
16515
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   531
safeRemove:oldObject 
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   532
    "remove the element, oldObject from the collection.
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   533
     Return the element 
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   534
     (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
   535
     If it was not in the collection return nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   536
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   537
     In contrast to #remove:, this does not resize the underlying collection
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   538
     and therefore does NOT rehash & change the elements order.
14319
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   539
     Therefor this can be used while enumerating the receiver,
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   540
     which is not possible if #remove: is used.
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   541
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   542
     WARNING: since no resizing is done, the physical amount of memory used
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   543
              by the container remains the same, although the logical size shrinks.
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   544
              You may want to manually resize the receiver using #emptyCheck.
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   545
              (after the loop)"
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   546
16515
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   547
    ^ self safeRemove:oldObject ifAbsent:[].
14319
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   548
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   549
    "Created: / 16.11.2001 / 10:23:48 / cg"
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   550
    "Modified: / 16.11.2001 / 10:24:03 / cg"
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   551
!
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   552
16515
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   553
safeRemove:oldObjectArg ifAbsent:exceptionValueProvider
14319
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   554
    "remove the element, oldObject from the collection.
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   555
     Return the element 
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   556
     (could be non-identical to oldObject, since I hash on equality, not on identity).
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   557
     If it was not in the collection return the value of exceptionValueProvider.
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   558
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   559
     In contrast to #remove:, this does not resize the underlying collection
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   560
     and therefore does NOT rehash & change the elements order.
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   561
     Therefore this can be used while enumerating the receiver,
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   562
     which is not possible if #remove: is used.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   563
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   564
     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
   565
              by the container remains the same, although the logical size shrinks.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   566
              You may want to manually resize the receiver using #emptyCheck.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   567
              (after the loop)"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   568
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   569
    |oldObject
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   570
     index "{ Class:SmallInteger }"
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   571
     next  "{ Class:SmallInteger }"
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   572
     removedObject|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   573
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   574
    oldObjectArg isNil ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   575
        oldObject := NilEntry.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   576
    ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   577
        oldObject := oldObjectArg.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   578
    ].
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   579
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   580
    index := self find:oldObject ifAbsent:0.
14319
9edc09830441 added: #saveRemove:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 14315
diff changeset
   581
    index == 0 ifTrue:[^ exceptionValueProvider value].
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   582
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   583
    removedObject := keyArray basicAt:index.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   584
    keyArray basicAt:index put:nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   585
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   586
    tally := tally - 1.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   587
    tally ~~ 0 ifTrue:[
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   588
        index == keyArray basicSize ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   589
            next := 1
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   590
        ] ifFalse:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   591
            next := index + 1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   592
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   593
        (keyArray basicAt:next) notNil ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   594
            keyArray basicAt:index put:DeletedEntry
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   595
        ].
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   596
    ].
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   597
    removedObject == NilEntry ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   598
        removedObject := nil.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   599
    ].
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   600
    ^ removedObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   601
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   602
    "does NOT work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   603
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   604
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   605
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   606
        s := Set new.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   607
        s add:1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   608
        s add:2.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   609
        s add:3.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   610
        s add:4.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   611
        s add:5.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   612
        s add:6.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   613
        s add:7.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   614
        s add:8.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   615
        s add:9.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   616
        s do:[:v |
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   617
            v odd ifTrue:[
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   618
                s remove:v 
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   619
            ]
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   620
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   621
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   622
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   623
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   624
    "DOES work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   625
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   626
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   627
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   628
        s := Set new.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   629
        s add:1.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   630
        s add:2.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   631
        s add:3.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   632
        s add:4.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   633
        s add:5.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   634
        s add:6.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   635
        s add:7.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   636
        s add:8.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   637
        s add:9.
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   638
        s do:[:v |
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   639
            v odd ifTrue:[
16515
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   640
                s safeRemove:v 
12963
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   641
            ]
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   642
        ].
8ff7dc77ce1d added: #addAllNonNilElements:
Stefan Vogel <sv@exept.de>
parents: 12734
diff changeset
   643
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   644
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   645
6191
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   646
    "Created: / 1.3.1996 / 21:14:26 / cg"
28ac2bdae9e9 comments;
Claus Gittinger <cg@exept.de>
parents: 6127
diff changeset
   647
    "Modified: / 16.11.2001 / 10:22:59 / cg"
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   648
!
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   649
16515
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   650
saveRemove:oldObject 
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   651
    <resource: #obsolete>
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   652
    "bad spelling - kept for backward compatibility (2014-06-04)"
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   653
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   654
    ^ self safeRemove:oldObject.
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   655
!
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   656
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   657
saveRemove:oldObjectArg ifAbsent:exceptionValueProvider
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   658
    <resource: #obsolete>
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   659
    "bad spelling - kept for backward compatibility (2014-06-04)"
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   660
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   661
    ^ self safeRemove:oldObjectArg ifAbsent:exceptionValueProvider.
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   662
!
6641d9cd92a6 class: Set
Stefan Vogel <sv@exept.de>
parents: 16241
diff changeset
   663
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   664
testAndAdd:keyArg
21652
927a47057848 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21553
diff changeset
   665
    "Test, if the element is present in the receiver.
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   666
     Answer true, if the element did already exist in the collection,
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   667
     false otherwise.
21652
927a47057848 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21553
diff changeset
   668
     If the element does not exist, add it to the collection.
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   669
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   670
     WARNING: do not add elements while iterating over the receiver.
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   671
              Iterate over a copy to do this."
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   672
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   673
    |key index "{ Class: SmallInteger }"|
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   674
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   675
    keyArg isNil ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   676
        key := NilEntry.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   677
    ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   678
        key := keyArg.
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   679
    ].
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   680
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   681
    index := self findKeyOrNil:key.
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   682
    (keyArray basicAt:index) notNil ifTrue:[
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   683
        ^ true.
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   684
    ].
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   685
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   686
    "/ not already there
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   687
    keyArray basicAt:index put:key.
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   688
    tally := tally + 1.
10588
7043b70ff150 New: #testAndAdd:
Stefan Vogel <sv@exept.de>
parents: 10090
diff changeset
   689
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   690
    self possiblyGrow.
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   691
    ^ false.
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   692
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   693
    "Modified: / 30-01-1997 / 14:58:08 / cg"
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   694
    "Modified: / 16-02-2017 / 13:47:10 / stefan"
21652
927a47057848 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21553
diff changeset
   695
    "Modified (comment): / 16-03-2017 / 16:46:04 / stefan"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   696
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   697
11052
0c44bf26275c comment
Claus Gittinger <cg@exept.de>
parents: 10868
diff changeset
   698
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   699
!Set methodsFor:'comparing'!
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   700
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   701
= anObject
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   702
    "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
   703
     as I do"
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   704
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   705
    anObject species ~~ self species ifTrue:[^ false].
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   706
    anObject size ~~ self size ifTrue:[^ false].
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   707
    "/ same number of elements; since I am a Set, all of
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   708
    "/ of my elements must be in the other collection ...
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   709
    self do:[:eachElement | (anObject includes:eachElement) ifFalse:[^ false]].
14134
72811a1ed607 changed:
Stefan Vogel <sv@exept.de>
parents: 14019
diff changeset
   710
    ^ true.
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   711
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   712
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   713
     #(1 2 3 4 5) asSet = #(2 3 4 5 1) asSet
18221
5299a2514ed2 class: Set
Stefan Vogel <sv@exept.de>
parents: 17413
diff changeset
   714
     #(nil 1 2 3 4 5) asSet = #(2 3 4 5 1) asSet
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   715
     #(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
   716
     #(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
   717
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   718
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   719
     |d1 d2|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   720
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   721
     d1 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   722
     d2 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   723
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   724
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   725
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   726
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   727
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   728
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   729
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   730
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   731
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   732
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   733
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   734
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   735
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   736
     d1 = d2     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   737
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   738
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   739
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   740
     |d1 d2|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   741
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   742
     d1 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   743
     d2 := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   744
     d1 at:1 put:'uno'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   745
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   746
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   747
     d1 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   748
     d1 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   749
     d1 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   750
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   751
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   752
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   753
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   754
     d2 at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   755
     d2 at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   756
     d2 at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   757
     d1 = d2     
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   758
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   759
10090
af5b9428e8ae code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents: 9933
diff changeset
   760
    "Modified: / 13-10-2006 / 12:58:43 / cg"
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   761
    "Modified (comment): / 16-02-2017 / 14:09:32 / stefan"
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   762
!
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   763
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   764
hash
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   765
    "return a hash key for the receiver"
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   766
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   767
    "this hash is stupid - but for larger collections, the hashing
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   768
     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
   769
     Time will show ...
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   770
     Notice & warning:
14134
72811a1ed607 changed:
Stefan Vogel <sv@exept.de>
parents: 14019
diff changeset
   771
        if the #= method is ever changed to compare non-dictionaries equal,
22444
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   772
        the code below must be changed to assert that the same hash-value is
14134
72811a1ed607 changed:
Stefan Vogel <sv@exept.de>
parents: 14019
diff changeset
   773
        still returned.
22444
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   774
        (which may be hard to accomplish)
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   775
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   776
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   777
    |mySize|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   778
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   779
    mySize := self size.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   780
    mySize == 0 ifTrue:[^ 0].
14134
72811a1ed607 changed:
Stefan Vogel <sv@exept.de>
parents: 14019
diff changeset
   781
    ^ (self anElement hash times:mySize) bitAnd:16r3FFFFFFF
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   782
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   783
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   784
     |d|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   785
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   786
     d := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   787
     d at:1 put:'one'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   788
     d at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   789
     d at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   790
     d at:'two' put:2.
22444
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   791
     d hash
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   792
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   793
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   794
    "
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   795
     |d|
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   796
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   797
     d := Dictionary new.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   798
     d at:1 put:'uno'.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   799
     d at:'one' put:1.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   800
     d at:2 put:#two.
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   801
     d at:'two' put:2.
22444
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   802
     d hash
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   803
    "
22444
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   804
48b6391b2e6b #DOCUMENTATION by mawalch
mawalch
parents: 22307
diff changeset
   805
    "Modified (comment): / 17-01-2018 / 15:18:56 / mawalch"
5280
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   806
! !
ad806c9b992e added #= & #hash; EXPERIMENTAL
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   807
15000
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   808
!Set methodsFor:'converting'!
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   809
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   810
asNewSet
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   811
    "make sure to return a unique new set"
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   812
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   813
    "could be an instance of a subclass..."
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   814
    self class == Set ifTrue:[
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   815
        ^ self copy
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   816
    ].
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   817
    ^ self asSet
15000
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   818
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   819
    " 
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   820
        |s|
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   821
        s := #(1 2 3 4) asSet.
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   822
        self assert:(s ~~ s asNewSet).
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   823
        self assert:(s = s asNewSet).
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   824
     "
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   825
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
   826
    "Modified: / 16-02-2017 / 14:10:43 / stefan"
15036
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   827
!
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   828
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   829
asSet 
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   830
    "return the receiver as a Set"
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   831
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   832
    "could be an instance of a subclass..."
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   833
    self class == Set ifTrue:[
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   834
        ^ self
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   835
    ].
f86ace435ad0 class: Set
Stefan Vogel <sv@exept.de>
parents: 15000
diff changeset
   836
    ^ super asSet
15000
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   837
! !
4d10fba9aed4 class: Set
Stefan Vogel <sv@exept.de>
parents: 14895
diff changeset
   838
24180
58ef875ce1b8 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23919
diff changeset
   839
!Set methodsFor:'copying-private'!
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   840
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   841
postCopy
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   842
    "have to copy the keyArray too"
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   843
21384
d0a0cd26d471 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21350
diff changeset
   844
    <modifier: #super> "must be called if redefined"
d0a0cd26d471 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21350
diff changeset
   845
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   846
    keyArray := keyArray shallowCopy
21384
d0a0cd26d471 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21350
diff changeset
   847
d0a0cd26d471 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21350
diff changeset
   848
    "Modified: / 08-02-2017 / 00:16:45 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   849
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   850
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   851
!Set methodsFor:'enumerating'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   852
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   853
do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   854
    "perform the block for all members in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   855
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   856
     WARNING: do not add/remove elements while iterating over the receiver.
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   857
              Iterate over a copy to do this."
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   858
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   859
    |sz "{ Class: SmallInteger }"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   860
     element|
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   861
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   862
    sz := keyArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   863
    1 to:sz do:[:index |
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   864
        element := keyArray at:index.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   865
        (element notNil and:[element ~~ DeletedEntry]) ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   866
            element == NilEntry ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   867
                element := nil.
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   868
            ].
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   869
            aBlock value:element
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
   870
        ]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   871
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   872
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   873
    "Modified: 1.3.1996 / 21:41:13 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   874
! !
61
claus
parents: 50
diff changeset
   875
969
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   876
23330
0bec0f8e3df9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 23207
diff changeset
   877
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   878
!Set methodsFor:'obsolete set operations'!
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   879
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   880
+ aCollection
8606
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   881
    "Kept for backward compatibility. 
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   882
     Use #union: instead, to isolate arithmethic and set operations"
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   883
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   884
    <resource: #obsolete>
17202
c35f2b3fb28d class: Set
sr
parents: 17201
diff changeset
   885
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   886
    ^ self union:aCollection.
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   887
!
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   888
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   889
- aCollection
8606
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   890
    "Kept for backward compatibility. 
6612bbac2d71 comments
Claus Gittinger <cg@exept.de>
parents: 8499
diff changeset
   891
     Use #\ instead, to isolate arithmethic and set operations"
7169
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   892
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   893
    <resource: #obsolete>
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   894
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   895
    ^ self \ aCollection
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   896
! !
74576ccad3c0 Clean up set operations
Stefan Vogel <sv@exept.de>
parents: 7167
diff changeset
   897
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   898
!Set methodsFor:'private'!
2
claus
parents: 1
diff changeset
   899
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   900
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   901
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
   902
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
   903
     return the value of evaluating aBlock."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   904
61
claus
parents: 50
diff changeset
   905
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   906
     length "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   907
     startIndex probe|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   908
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   909
    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
   910
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
   911
"/
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
   912
"/  length < 10 ifTrue:[
362
claus
parents: 359
diff changeset
   913
"/      "assuming, that for small collections the overhead of hashing
claus
parents: 359
diff changeset
   914
"/       is larger ... maybe that proves wrong 
claus
parents: 359
diff changeset
   915
"/       (if overhead of comparing is higher)"
claus
parents: 359
diff changeset
   916
"/      ^ 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
   917
"/  ].
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   918
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   919
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   920
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
   921
    [
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   922
        probe := keyArray basicAt:index.
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   923
        probe isNil ifTrue:[^ aBlock value].
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   924
        (probe ~~ DeletedEntry and:[key = probe]) ifTrue:[^ index].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   925
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   926
        index == length ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   927
            index := 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   928
        ] ifFalse:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   929
            index := index + 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   930
        ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   931
        index == startIndex ifTrue:[^ aBlock value].
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
   932
    ] loop.
13279
10d204f50776 changed: #find:ifAbsent:
sr
parents: 12963
diff changeset
   933
10d204f50776 changed: #find:ifAbsent:
sr
parents: 12963
diff changeset
   934
    "Modified: / 03-02-2011 / 13:53:18 / sr"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   935
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   936
15806
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   937
findIdentical:key ifAbsent:aBlock
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   938
    "Look for the key in the receiver.  If it is found, return
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   939
     the index of the slot containing the key, otherwise
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   940
     return the value of evaluating aBlock."
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   941
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   942
    ^ keyArray identityIndexOf:key ifAbsent:aBlock
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   943
!
62bc6058e34f class: Set
Stefan Vogel <sv@exept.de>
parents: 15036
diff changeset
   944
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   945
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
   946
    "Look for the key in the receiver.  
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
   947
     If it is found, return the index of the first unused slot. 
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   948
     Grow the receiver, if key was not found, and no unused slots were present
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   949
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   950
     Warning: an empty slot MUST be filled by the sender - it is only to be sent
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   951
              by at:put: / add: - like methods."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   952
61
claus
parents: 50
diff changeset
   953
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   954
     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
   955
     startIndex probe 
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   956
     delIndex "{ Class:SmallInteger }"|
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   957
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   958
    delIndex := 0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   959
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   960
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
   961
    startIndex := index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   962
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
   963
    [
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   964
        probe := keyArray basicAt:index.
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   965
        probe isNil ifTrue:[
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   966
            delIndex == 0 ifTrue:[^ index].
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   967
            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
   968
            ^ delIndex
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   969
        ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   970
        probe == DeletedEntry ifTrue:[
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   971
            delIndex == 0 ifTrue:[
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   972
                delIndex := index
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   973
            ]
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   974
        ] ifFalse:[
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   975
            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
   976
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   977
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   978
        index == length ifTrue:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   979
            index := 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   980
        ] ifFalse:[
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   981
            index := index + 1
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   982
        ].
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   983
        index == startIndex ifTrue:[
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
   984
            delIndex ~~ 0 ifTrue:[
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   985
                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
   986
                ^ delIndex
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   987
            ].
20038
8837589d612a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19872
diff changeset
   988
            self grow.
8837589d612a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19872
diff changeset
   989
            length := keyArray basicSize.
8837589d612a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19872
diff changeset
   990
            startIndex := index := self initialIndexForKey:key.
7654
41441d702cfc avoid need for elements to know how to compare against nil.
Claus Gittinger <cg@exept.de>
parents: 7598
diff changeset
   991
        ].
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
   992
    ] loop.
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
   993
13311
53dece2ccaa1 changed: #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 13279
diff changeset
   994
    "Modified: / 27-02-2011 / 15:30:42 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   995
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   996
20236
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
   997
findKeyOrNilOrDeletedEntry:key
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
   998
    "Look for the key in the receiver.  
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
   999
     If it is found, return the index of the first unused slot. 
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1000
     Grow the receiver, if key was not found, and no unused slots were present.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1001
     The answer is the index into the keyArray where the (keyArray at:index)
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1002
     may contain:
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1003
        nil             -   an empty slot
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1004
        DeletedEntry    -   an empty slot, but preceeded and followed by non-empty
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1005
                            slots with keys hashing to the same value (hash collisions)
23919
8ff052582b22 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 23854
diff changeset
  1006
        key             -   key is already present in the slot."
20236
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1007
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1008
    |index  "{ Class:SmallInteger }"
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1009
     length "{ Class:SmallInteger }"
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1010
     startIndex probe 
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1011
     delIndex "{ Class:SmallInteger }"|
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1012
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1013
    delIndex := 0.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1014
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1015
    length := keyArray basicSize.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1016
    startIndex := index := self initialIndexForKey:key.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1017
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1018
    [
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1019
        probe := keyArray basicAt:index.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1020
        probe isNil ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1021
            delIndex == 0 ifTrue:[^ index].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1022
            ^ delIndex
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1023
        ].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1024
        probe == DeletedEntry ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1025
            delIndex == 0 ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1026
                delIndex := index
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1027
            ]
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1028
        ] ifFalse:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1029
            key = probe ifTrue:[^ index]
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1030
        ].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1031
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1032
        index == length ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1033
            index := 1
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1034
        ] ifFalse:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1035
            index := index + 1
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1036
        ].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1037
        index == startIndex ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1038
            delIndex ~~ 0 ifTrue:[
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1039
                ^ delIndex
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1040
            ].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1041
            self grow.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1042
            length := keyArray basicSize.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1043
            startIndex := index := self initialIndexForKey:key.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1044
        ].
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1045
    ] loop.
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1046
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1047
    "Modified: / 27-02-2011 / 15:30:42 / cg"
23919
8ff052582b22 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 23854
diff changeset
  1048
    "Modified (comment): / 15-03-2019 / 15:15:31 / Stefan Vogel"
20236
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1049
!
b99be28d9875 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 20038
diff changeset
  1050
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1051
findNil:key
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
  1052
    "Look for the next slot usable for key.  
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
  1053
     WARNING:
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1054
        This method assumes that key is not already in the receiver 
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1055
        AND that keyArray does not have previously removed entries 
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1056
        AND that there is an empty slot.
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
  1057
     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
  1058
     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
  1059
     loops forever."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1060
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
  1061
    |index  "{ Class:SmallInteger }"
61
claus
parents: 50
diff changeset
  1062
     length "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1063
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
  1064
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
  1065
    index := self initialIndexForKey:key.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1066
5772
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1067
"/    index := keyArray identityIndexOf:nil startingAt:index.
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1068
"/    index == 0 ifTrue:[
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1069
"/        index := keyArray identityIndexOf:nil startingAt:1 endingAt:index.
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1070
"/    ].
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1071
    "/ that code is completely inlined by stc ...
e45edba144f1 nope - not better.
Claus Gittinger <cg@exept.de>
parents: 5771
diff changeset
  1072
    [(keyArray basicAt:index) notNil] whileTrue:[
14315
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1073
        index == length ifTrue:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1074
            index := 1
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1075
        ] ifFalse:[
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1076
            index := index + 1
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1077
        ].
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1078
        "notice: no check for no nil found - we must find one since
f75e7698ccfe Support for nil as element in a set
Stefan Vogel <sv@exept.de>
parents: 14314
diff changeset
  1079
         this is only called after growing"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1080
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1081
    ^ index
1055
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
  1082
2475
dffcb8b97178 use #hashFor: in #findNil:
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
  1083
    "Modified: 21.3.1997 / 10:32:58 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1084
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1085
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1086
hashFor:aKey
6991
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
  1087
    "return the arguments hash value.
e95313d7c780 comment and category change only
Claus Gittinger <cg@exept.de>
parents: 6917
diff changeset
  1088
     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
  1089
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1090
    ^ aKey hash
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1091
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1092
    "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
  1093
!
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1094
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1095
initialIndexForKey:aKey
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1096
    "return an initial index given a key."
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1097
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1098
    |index  "{ Class:SmallInteger }"
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1099
     length "{ Class:SmallInteger }"|
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1100
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1101
    length := keyArray basicSize.
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
  1102
    index := (self hashFor:aKey) bitAnd:16r3FFFFFFF.
14656
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1103
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1104
    "multiply by a large prime to spread the keys over the whole range.
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1105
     This causes maxChainLength to reduce from about 2000 to 6 for an
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1106
     IdentitySet of 9000 Smalltalk classes"
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1107
    index := index times:31415821.
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1108
    index := index \\ length + 1.
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1109
    ^ index.
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1110
14656
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1111
    "
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1112
        Smalltalk allClasses maxChainLength
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1113
    "
f5cab05e380f class: Set
Stefan Vogel <sv@exept.de>
parents: 14654
diff changeset
  1114
2459
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1115
    "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
  1116
    "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
  1117
!
0a38e8602aac findNil: no need to check for wraparound;
Claus Gittinger <cg@exept.de>
parents: 2427
diff changeset
  1118
18331
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1119
initializeForCapacity:minSize
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1120
    "initialize the contents array (for at least minSize slots) 
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1121
     and set tally to zero.
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1122
     The size is increased to the next prime for better hashing behavior."
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1123
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1124
    |n|
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1125
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1126
    n := self class goodSizeFrom:minSize.
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1127
    n == keyArray size ifTrue:[
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1128
        keyArray atAllPut:nil.
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1129
    ] ifFalse:[
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1130
        keyArray := self keyContainerOfSize:n. 
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1131
    ].
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1132
    tally := 0
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1133
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1134
    "Modified: / 4.8.1998 / 22:07:25 / cg"
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1135
!
521e3c40ea23 class: Set
Claus Gittinger <cg@exept.de>
parents: 18221
diff changeset
  1136
3261
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
  1137
keyArray
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
  1138
    ^ keyArray
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
  1139
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
  1140
    "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
  1141
!
7e4e57c041df added access to my (private) keyArray instVar (sorry)
Claus Gittinger <cg@exept.de>
parents: 3082
diff changeset
  1142
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1143
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1144
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1145
     Extracted to make life of weak subclasses easier ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1146
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1147
    ^ Array basicNew:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1148
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1149
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1150
rehash
61
claus
parents: 50
diff changeset
  1151
    "rehash is done by re-adding all elements to a new empty set.
claus
parents: 50
diff changeset
  1152
     Rehash is needed after a binaryRead, for example."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1153
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1154
    |element oldKeyArray newKeyArray newIdx
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
  1155
     n "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1156
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
  1157
    oldKeyArray := keyArray.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
  1158
    n := oldKeyArray size.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
  1159
    keyArray := newKeyArray := self keyContainerOfSize:n.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
  1160
2
claus
parents: 1
diff changeset
  1161
    1 to:n do:[:index |
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1162
	element := oldKeyArray at:index.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1163
	(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1164
	    "cannot be already there"
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1165
	    newIdx := self findNil:element.
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1166
	    newKeyArray basicAt:newIdx put:element
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1167
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1168
    ]
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1169
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1170
    "Modified: / 30.10.1997 / 16:04:46 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1171
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1172
7260
edfa8d6a6046 method category rename
Claus Gittinger <cg@exept.de>
parents: 7169
diff changeset
  1173
!Set methodsFor:'private-grow & shrink'!
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1174
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1175
grow
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1176
    "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
  1177
     new size"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1178
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1179
    self grow:(keyArray basicSize * 2)
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1180
!
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1181
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1182
grow:newSize
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1183
    "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
  1184
     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
  1185
     empty set)."
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1186
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1187
    |elem oldKeyArray newKeyArray deletedEntry
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1188
     containerSize oldSize "{ Class:SmallInteger }"|
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1189
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1190
    oldKeyArray := keyArray.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1191
    oldSize := oldKeyArray size.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1192
    containerSize := (self class goodSizeFrom:newSize).
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1193
    containerSize == oldSize ifTrue:[^ self].
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1194
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1195
    keyArray := newKeyArray := self keyContainerOfSize:containerSize. 
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1196
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1197
    deletedEntry := DeletedEntry.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1198
    1 to:oldSize do:[:srcIndex |
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1199
	elem := oldKeyArray basicAt:srcIndex.
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1200
	(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1201
	    "cannot be already there"
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1202
	    newKeyArray basicAt:(self findNil:elem) put:elem
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1203
	].
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1204
    ].
17263
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1205
!
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1206
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1207
possiblyGrow
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1208
    "check if collection is full (after an add); grow if so.
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1209
     Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'"
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1210
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1211
    |sz "{Class: SmallInteger}" |
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1212
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1213
    "
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1214
     grow if filled more than 75% 
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1215
    "
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1216
    sz := keyArray basicSize.
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1217
    tally > (sz * 3 // 4) ifTrue:[
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1218
       self grow
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1219
    ]
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1220
!
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1221
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1222
possiblyShrink
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1223
    "check if the receiver has become too empty (after a remove)
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1224
     and shrink if it makes sense.
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1225
     Definition of 'too empty' is: 'filled less than 12.5% (i.e. 1/8th)'"
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1226
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1227
    |sz      "{Class: SmallInteger}"
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1228
     newSize "{Class: SmallInteger}" |
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1229
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1230
    sz := keyArray basicSize.
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1231
    sz > 56 ifTrue:[
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1232
        "
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1233
         shrink if too empty
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1234
        "
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1235
        tally < (sz // 8) ifTrue:[
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1236
            newSize := sz // 4.
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1237
            self grow:newSize
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1238
        ]
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1239
    ]
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1240
65d015f98f43 renamed the private emptyCheck of Set & Dictionary
Claus Gittinger <cg@exept.de>
parents: 17202
diff changeset
  1241
    "Modified: 19.3.1997 / 16:02:55 / cg"
22951
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1242
!
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1243
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1244
possiblyShrinkToZero
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1245
    "the tally went down to zero.
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1246
     throw away any big container and start anew"
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1247
9886016582bd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22571
diff changeset
  1248
    keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). 
3082
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1249
! !
d09ee5fdcc7b checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3048
diff changeset
  1250
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1251
!Set methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1252
23694
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1253
capacity 
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1254
    "return the number of elements, that the receiver is prepared to take w.o. resizing.
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1255
     Notice, that Sets do automatically resize as required, 
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1256
     so knowing the capacity is of no real use."
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1257
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1258
    ^ keyArray size
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1259
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1260
    "Modified (comment): / 17-03-2017 / 11:50:16 / stefan"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1261
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1262
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1263
collisionCount
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1264
    "return the number of key collisions in the set.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1265
     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
  1266
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1267
    |count|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1268
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1269
    count := 0.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1270
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1271
    keyArray do:[:eachKey|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1272
        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1273
            count := count + (self collisionsFor:eachKey).
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1274
        ]
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1275
    ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1276
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1277
    ^ count
14654
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1278
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1279
    "
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1280
      self allSubInstances 
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1281
          collect:[:each| each collisionCount -> each] 
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1282
          thenSelect:[:each| each key > 0]
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1283
    "
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1284
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1285
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1286
collisionsFor:key
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1287
    "Return the number of searches - 1 required for key"
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1288
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1289
    |index  "{ Class:SmallInteger }"
14654
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1290
     length "{ Class:SmallInteger }" startIndex probe count|
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1291
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1292
    length := keyArray basicSize.
14654
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1293
    startIndex := index := self initialIndexForKey:key.
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1294
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1295
    count := 0.
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
  1296
    [
14654
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1297
        probe := keyArray basicAt:index.
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1298
        (probe notNil and:[key = probe]) ifTrue:[^ count].
19542
0cff447dfffa #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19080
diff changeset
  1299
        probe isNil ifTrue:[self error:'non existing key'].
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1300
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1301
        index == length ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1302
            index := 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1303
        ] ifFalse:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1304
            index := index + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1305
        ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1306
        count := count + 1.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1307
        index == startIndex ifTrue:[self error:'non existing key'].
16241
7f73a12c32e1 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 15815
diff changeset
  1308
    ] loop.
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1309
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1310
23694
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1311
includes:anObject
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1312
    "return true if the argument anObject is in the receiver"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1313
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1314
    tally == 0 ifTrue:[^ false]. "/ quick reject if empty
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1315
    ^ (self find:(anObject ? NilEntry) ifAbsent:0) ~~ 0
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1316
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1317
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1318
isEmpty
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1319
    "return true if the receiver is empty"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1320
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1321
    ^ tally == 0
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1322
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1323
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1324
isEmptyOrNil
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1325
    "return true if the receiver is empty"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1326
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1327
    ^ tally == 0
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1328
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1329
    "Created: / 23-02-2017 / 15:51:50 / stefan"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1330
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1331
8499
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1332
maxChainLength
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1333
    "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
  1334
     This is the worst case overhead when accessing a key."
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1335
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1336
    |chainLength key|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1337
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1338
    chainLength := 0.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1339
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1340
    keyArray do:[:eachKey| |t|
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1341
        (eachKey notNil and:[eachKey ~~ DeletedEntry]) ifTrue:[
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1342
            t := self collisionsFor:eachKey.
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1343
            chainLength < t ifTrue:[chainLength := t. key := eachKey].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1344
        ]
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1345
    ].
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1346
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1347
    ^ (key -> chainLength)
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1348
!
de9cfb15379a Add methods for diagnosis
Stefan Vogel <sv@exept.de>
parents: 8394
diff changeset
  1349
23694
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1350
notEmpty
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1351
    "return true if the receiver is not empty"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1352
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1353
    ^ tally ~~ 0
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1354
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1355
    "Created: 12.2.1997 / 12:39:02 / cg"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1356
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1357
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1358
notEmptyOrNil
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1359
    "return true if the receiver is not empty"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1360
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1361
    ^ tally ~~ 0
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1362
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1363
    "Created: / 23-02-2017 / 15:51:58 / stefan"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1364
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1365
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1366
occurrencesOf:anObject
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1367
    "return the number of occurrences of anObject in the receiver.
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1368
     As I am a Set, this can only return 0 or 1."
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1369
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1370
    tally == 0 ifTrue:[^ 0]. 
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1371
    (self find:(anObject ? NilEntry) ifAbsent:0) == 0 ifTrue:[^ 0].
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1372
    ^ 1
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1373
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1374
    "Modified: / 16.11.2001 / 10:30:14 / cg"
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1375
!
e4a55e9a2735 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23330
diff changeset
  1376
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1377
size
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1378
    "return the number of set elements"
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1379
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1380
    ^ tally
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5471
diff changeset
  1381
! !
5471
a57eeb01c5ab General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents: 5335
diff changeset
  1382
23189
15ea23cfead5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 22951
diff changeset
  1383
16810
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1384
!Set methodsFor:'searching'!
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1385
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1386
findFirst:aBlock ifNone:exceptionValue
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1387
    "find the index of the first element, for which evaluation of the argument, aBlock returns true; 
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1388
     return its index or the value from exceptionValue if none detected.
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1389
     This is much like #detect:ifNone:, however, here an INDEX is returned,
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1390
     while #detect:ifNone: returns the element.
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1391
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1392
     Sets do not have indices."
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1393
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1394
    ^ self shouldNotImplement
21457
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1395
!
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1396
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1397
findLast:aBlock ifNone:exceptionValue
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1398
    "Sets do not have indices."
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1399
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1400
    ^ self shouldNotImplement
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1401
3be1776f3774 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 21384
diff changeset
  1402
    "Created: / 16-02-2017 / 13:54:53 / stefan"
16810
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1403
! !
f6b1aa12bd5f class: Set
Stefan Vogel <sv@exept.de>
parents: 16717
diff changeset
  1404
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1405
!Set methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1406
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1407
isFixedSize
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1408
    "return true if the receiver cannot grow - this will vanish once
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1409
     Arrays and Strings learn how to grow ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1410
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1411
    ^ false
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1412
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1413
18466
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1414
isOrdered
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1415
    "return true, if the receiver's elements are ordered.
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1416
     Redefined to return false here, because the order of keys (and values in dictionaries)
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1417
     may change due to rehashing, when elements are added/removed"
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1418
0359b064feba added isOrdered query
Claus Gittinger <cg@exept.de>
parents: 18331
diff changeset
  1419
    ^ false
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1420
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1421
8394
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1422
!Set methodsFor:'visiting'!
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1423
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1424
acceptVisitor:aVisitor with:aParameter
16717
ce1544557168 comment/format only
Claus Gittinger <cg@exept.de>
parents: 16515
diff changeset
  1425
    "dispatch for visitor pattern; send #visitSet:with: to aVisitor."
8394
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1426
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1427
    ^ aVisitor visitSet:self with:aParameter
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1428
! !
da194de43766 Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents: 7654
diff changeset
  1429
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1430
!Set::EmptySlot class methodsFor:'instance creation'!
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1431
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1432
basicNew
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1433
    TheOneAndOnlyInstance isNil ifTrue:[
6994
0f08c1d8c2ac Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6991
diff changeset
  1434
	TheOneAndOnlyInstance := super basicNew
4017
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1435
    ].
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1436
    ^ TheOneAndOnlyInstance
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1437
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1438
! !
cdf103ce2e30 there can be only one EmptySlot
Claus Gittinger <cg@exept.de>
parents: 3726
diff changeset
  1439
14314
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1440
!Set::NilKey class methodsFor:'instance creation'!
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1441
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1442
basicNew
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1443
    TheOneAndOnlyInstance isNil ifTrue:[
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1444
        TheOneAndOnlyInstance := super basicNew
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1445
    ].
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1446
    ^ TheOneAndOnlyInstance
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1447
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1448
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1449
! !
e2d4d97f9a2c class definition
Stefan Vogel <sv@exept.de>
parents: 14300
diff changeset
  1450
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
  1451
!Set class methodsFor:'documentation'!
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1452
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1453
version
19080
0f75504d1ce4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18466
diff changeset
  1454
    ^ '$Header$'
12342
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1455
!
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1456
10c0bdcf6c37 added: #like:
Claus Gittinger <cg@exept.de>
parents: 11472
diff changeset
  1457
version_CVS
19080
0f75504d1ce4 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18466
diff changeset
  1458
    ^ '$Header$'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
  1459
! !
6917
979bfb9267e3 refactored to invoke #hash/#identityHash only from one place
Claus Gittinger <cg@exept.de>
parents: 6577
diff changeset
  1460
14654
32c189acecb6 class: Set
Stefan Vogel <sv@exept.de>
parents: 14319
diff changeset
  1461
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1462
Set initialize!