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