Set.st
author Claus Gittinger <cg@exept.de>
Wed, 29 Jan 1997 22:05:45 +0100
changeset 2314 39ba656af0bb
parent 2272 9942d9c853c3
child 2323 a1fb9151a986
permissions -rw-r--r--
comment
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
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Collection subclass:#Set
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    14
	instanceVariableNames:'tally keyArray'
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    15
	classVariableNames:'DeletedEntry'
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    16
	poolDictionaries:''
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
    17
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
2272
9942d9c853c3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
    20
Object subclass:#EmptySlot
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    21
	instanceVariableNames:''
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    22
	classVariableNames:''
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    23
	poolDictionaries:''
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    24
	privateIn:Set
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    25
!
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    26
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    27
!Set class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 COPYRIGHT (c) 1991 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
    32
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    33
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    41
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    42
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    43
documentation
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    44
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    45
    a Set is a collection where each element occurs at most once.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    46
    The inclusion test is done using = for comparison; 
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    47
    see IdentitySet for sets using identity compare.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    48
    Sets use hashing for fast access, this access is considerably faster,
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    49
    if a good hash-number is returned by the elements.
362
claus
parents: 359
diff changeset
    50
claus
parents: 359
diff changeset
    51
    Notice that the default hash (Object>>hash) is not perfect; due to
claus
parents: 359
diff changeset
    52
    the implementation of hash-keys in ST/X, increased hash collisions
claus
parents: 359
diff changeset
    53
    are to be expected for large sets (say: > 20000 element). 
claus
parents: 359
diff changeset
    54
    If your objects are heavyly used in sets or dictionaries, and you need
claus
parents: 359
diff changeset
    55
    big collections, your instances may provide a better hash values.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    56
345
claus
parents: 324
diff changeset
    57
    Performance hints: 
657
449935f15b9e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    58
      If only symbols or smallIntegers are used as keys, 
362
claus
parents: 359
diff changeset
    59
      use an instance of IdentitySet for slightly better performance, 
claus
parents: 359
diff changeset
    60
      since both hashing and comparison is faster.
345
claus
parents: 324
diff changeset
    61
claus
parents: 324
diff changeset
    62
      If you have a rough idea how big the set is going to grow,
claus
parents: 324
diff changeset
    63
      create it using #new: instead of #new. Even if the size given is a
claus
parents: 324
diff changeset
    64
      poor guess (say half of the real size), there is some 20-30% performance
claus
parents: 324
diff changeset
    65
      win to expect, since many resizing operations of the set are avoided.
claus
parents: 324
diff changeset
    66
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    67
    Examples:
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    68
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    69
        |s|
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    70
        s := Set new.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    71
        s add:'hello'.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    72
        s add:'world'.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    73
        s add:#foo.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    74
        s add:1.2345678.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    75
        s add:'hello'.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
    76
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    77
        s printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    78
        's size -> ' print. s size printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    79
        '(s includes:''hello'') -> ' print. (s includes:'hello') printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    80
        '(s includes:#foo)    -> ' print. (s includes:#foo) printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    81
        '(s includes:''foo'')   -> ' print. (s includes:'foo') printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    82
        '(s includes:#bar)    -> ' print. (s includes:#bar) printNL.
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    83
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    84
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    85
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    86
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    87
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    89
!Set class methodsFor:'initialization'!
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    90
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    91
initialize
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    92
    "initialize the Set class"
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    93
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    94
    DeletedEntry isNil ifTrue:[
2272
9942d9c853c3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2262
diff changeset
    95
        DeletedEntry := EmptySlot new
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    96
    ].
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    97
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
    98
    "Set initialize"
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
    99
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   100
    "Modified: 24.1.1997 / 21:09:00 / cg"
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   101
! !
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   102
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   103
!Set class methodsFor:'instance creation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
    "return a new empty Set"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
a27a279701f8 Initial revision
claus
parents:
diff changeset
   108
    ^ self new:7
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   110
a27a279701f8 Initial revision
claus
parents:
diff changeset
   111
new:anInteger
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
    "return a new empty Set with space for anInteger elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   114
    "
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   115
     make it somewhat bigger; hashing works better if fill grade is
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   116
     below 10% (make it 75% here ..)
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   117
    "
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   118
    ^ self basicNew setTally:(anInteger * 4 // 3)
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   119
! !
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   120
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   121
!Set class methodsFor:'queries'!
252
   122
   123
goodSizeFrom:arg 
   124
    "return a good array size for the given argument.
   125
     Returns the next prime after arg, since prime sizes are good for hashing."
   126
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   127
    |n|
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   128
252
   129
    arg <= 11 ifTrue:[^ 11].
   130
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   131
    n := arg * 3 // 2.
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   132
252
   133
    "
   134
     mhmh - this returns good numbers for collections with up-to about
   135
     500k elements; if you have bigger ones, add some more primes here ...
   136
    "
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   137
    n <= 524288 ifTrue:[
252
   138
	   "2  4  8  16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288"
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   139
	^ #(11 11 11 17 37 67 131 257 521 1031 2053 4099 8209 16411 32771 65537 131101 262147 524309) at:(n highBit)
252
   140
    ].
   141
    "
   142
     make it odd - at least
   143
    "
302
1f76060d58a4 *** empty log message ***
claus
parents: 252
diff changeset
   144
    ^ n bitOr:1
252
   145
! !
   146
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
!Set methodsFor:'accessing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   148
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   149
at:index
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
    "report an error: at: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   151
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   152
    ^ self errorNotKeyed
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   154
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   155
at:index put:anObject
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   156
    "report an error: at:put: is not allowed for Sets"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   157
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
    ^ self errorNotKeyed
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   159
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   160
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   161
!Set methodsFor:'adding & removing'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   162
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   163
add:anObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   164
    "add the argument, anObject to the receiver.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   165
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   166
     WARNING: do not add elements while iterating over the receiver.
1126
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 1056
diff changeset
   167
	      Iterate over a copy to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   168
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
    |index "{ Class: SmallInteger }"|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   170
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   171
    anObject notNil ifTrue:[
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
   172
	index := self findKeyOrNil:anObject.
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
   173
	(keyArray basicAt:index) isNil 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
   174
	    keyArray basicAt:index put:anObject.
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
   175
	    tally := tally + 1.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   176
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
   177
	    self fullCheck.
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
   178
	]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   179
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   180
    ^ anObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   181
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   182
    "Modified: 1.3.1996 / 21:24:26 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   183
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   184
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   185
remove:oldObject ifAbsent:exceptionBlock
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   186
    "remove oldObject from the collection and return it.
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   187
     If it was not in the collection return the value of exceptionBlock.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   188
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   189
     WARNING: do not remove elements while iterating over the receiver.
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   190
              See #saveRemove: to do this."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   191
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   192
    |index next|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   193
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   194
    index := self find:oldObject ifAbsent:0.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   195
    index == 0 ifTrue:[^ exceptionBlock value].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   196
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   197
    keyArray basicAt:index put:nil.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   198
    tally := tally - 1.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   199
    tally == 0 ifTrue:[
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   200
        keyArray := self keyContainerOfSize:(self class goodSizeFrom:0). 
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   201
    ] ifFalse:[
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   202
        index == keyArray basicSize ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   203
            next := 1
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   204
        ] ifFalse:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   205
            next := index + 1.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   206
        ].
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   207
        (keyArray basicAt:next) notNil ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   208
            keyArray basicAt:index put:DeletedEntry.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   209
        ].
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   210
        self emptyCheck
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   211
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   212
    ^ oldObject
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   213
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   214
    "Modified: 29.1.1997 / 21:34:33 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   215
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   216
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   217
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   218
    "remove all elements from the receiver. Returns the receiver."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   219
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   220
    self setTally:7.
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   221
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1144
diff changeset
   222
    "Modified: 12.4.1996 / 13:35:06 / cg"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   223
!
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   224
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   225
saveRemove:oldObject 
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   226
    "remove the element, oldObject from the collection.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   227
     Return the element.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   228
     If it was not in the collection return nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   229
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   230
     In contrast to #remove:, this does not resize the underlying collection
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   231
     and therefore does NOT rehash & change the elements order.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   232
     Therefor this can be used while enumerating the receiver,
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   233
     which is not possible if #remove: is used.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   234
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   235
     WARNING: since no resizing is done, the physical amount of memory used
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   236
              by the container remains the same, although the logical size shrinks.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   237
              You may want to manually resize the receiver using #emptyCheck.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   238
              (after the loop)"
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   239
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   240
    |index "{ Class:SmallInteger }"
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   241
     next  "{ Class:SmallInteger }"|
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   242
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   243
    oldObject isNil ifTrue:[^ nil].
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   244
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   245
    index := self find:oldObject ifAbsent:0.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   246
    index == 0 ifTrue:[^ nil].
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   247
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   248
    keyArray basicAt:index put:nil.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   249
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   250
    tally := tally - 1.
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   251
    tally ~~ 0 ifTrue:[
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   252
        index == keyArray basicSize ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   253
            next := 1
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   254
        ] ifFalse:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   255
            next := index + 1.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   256
        ].
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   257
        (keyArray basicAt:next) notNil ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   258
            keyArray basicAt:index put:DeletedEntry
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   259
        ].
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   260
    ].
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   261
    ^ oldObject
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   262
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   263
    "does NOT work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   264
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   265
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   266
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   267
        s := Set new.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   268
        s add:1.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   269
        s add:2.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   270
        s add:3.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   271
        s add:4.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   272
        s add:5.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   273
        s add:6.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   274
        s add:7.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   275
        s add:8.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   276
        s add:9.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   277
        s do:[:v |
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   278
            v odd ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   279
                s remove:v 
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   280
            ]
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   281
        ].
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   282
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   283
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   284
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   285
    "DOES work:
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   286
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   287
        |s|
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   288
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   289
        s := Set new.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   290
        s add:1.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   291
        s add:2.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   292
        s add:3.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   293
        s add:4.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   294
        s add:5.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   295
        s add:6.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   296
        s add:7.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   297
        s add:8.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   298
        s add:9.
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   299
        s do:[:v |
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   300
            v odd ifTrue:[
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   301
                s saveRemove:v 
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   302
            ]
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   303
        ].
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   304
        s inspect
1052
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   305
    "
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   306
7d43187f7e11 added saveRemove: & commentaries
Claus Gittinger <cg@exept.de>
parents: 969
diff changeset
   307
    "Created: 1.3.1996 / 21:14:26 / cg"
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   308
    "Modified: 29.1.1997 / 21:35:55 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   309
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   311
!Set methodsFor:'binary storage'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   313
readBinaryContentsFrom: stream manager: manager
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   314
    "must rehash after reload"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   315
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   316
    super readBinaryContentsFrom: stream manager: manager.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   317
    self rehash
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   319
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   320
!Set methodsFor:'copying'!
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   321
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   322
postCopy
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   323
    "have to copy the keyArray too"
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   324
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   325
    keyArray := keyArray shallowCopy
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   326
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   327
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
!Set methodsFor:'enumerating'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   329
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
do:aBlock
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   331
    "perform the block for all members in the collection.
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   332
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   333
     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
   334
	      Iterate over a copy to do this."
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   335
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   336
    |sz "{ Class: SmallInteger }"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   337
     element|
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   338
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
    sz := keyArray size.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
    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
   341
	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
   342
	(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
   343
	    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
   344
	]
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
    ]
1056
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   346
991ed2a9318b commentary
Claus Gittinger <cg@exept.de>
parents: 1055
diff changeset
   347
    "Modified: 1.3.1996 / 21:41:13 / cg"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
! !
61
claus
parents: 50
diff changeset
   349
969
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   350
!Set methodsFor:'inspecting'!
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   351
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   352
inspectorClass
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   353
    "redefined to use SetInspector
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   354
     (instead of the default Inspector)."
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   355
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   356
    ^ SetInspectorView
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   357
! !
6c285e945e8f Use SetInspectorView.
Stefan Vogel <sv@exept.de>
parents: 657
diff changeset
   358
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   359
!Set methodsFor:'private'!
2
claus
parents: 1
diff changeset
   360
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   361
emptyCheck
252
   362
    "check if the receiver has become too empty (after a remove)
324
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   363
     and shrink if it makes sense.
290cfb34ec93 *** empty log message ***
claus
parents: 302
diff changeset
   364
     Definition of 'too empty' is 'filled less than 12.5% (i.e. 1/8th)'"
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   365
61
claus
parents: 50
diff changeset
   366
    |sz      "{Class: SmallInteger}"
claus
parents: 50
diff changeset
   367
     newSize "{Class: SmallInteger}" |
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   368
61
claus
parents: 50
diff changeset
   369
    sz := keyArray basicSize.
1643
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   370
    sz > 56 ifTrue:[
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   371
        "
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   372
         shrink if too empty
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   373
        "
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   374
        tally < (sz // 8) ifTrue:[
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   375
            newSize := sz // 7.
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   376
            self grow:newSize
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   377
        ]
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   378
    ]
1643
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   379
577e35f5a046 oops - shrinking of Set could create a fully populated Set,
Claus Gittinger <cg@exept.de>
parents: 1450
diff changeset
   380
    "Modified: 4.9.1996 / 14:39:53 / cg"
41
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   381
!
a14247b04d03 *** empty log message ***
claus
parents: 13
diff changeset
   382
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   383
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
   384
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
   385
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
   386
     return the value of evaluating aBlock."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   387
61
claus
parents: 50
diff changeset
   388
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   389
     length "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   390
     startIndex probe|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   391
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   392
    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
   393
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
   394
"/
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
   395
"/  length < 10 ifTrue:[
362
claus
parents: 359
diff changeset
   396
"/      "assuming, that for small collections the overhead of hashing
claus
parents: 359
diff changeset
   397
"/       is larger ... maybe that proves wrong 
claus
parents: 359
diff changeset
   398
"/       (if overhead of comparing is higher)"
claus
parents: 359
diff changeset
   399
"/      ^ 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
   400
"/  ].
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   401
362
claus
parents: 359
diff changeset
   402
    index := key hash.
1975
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   403
    index < 16r1FFFFFFF ifTrue:[
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   404
        index := index * 2
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   405
    ].
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   406
    index := index \\ length + 1.
362
claus
parents: 359
diff changeset
   407
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   408
a27a279701f8 Initial revision
claus
parents:
diff changeset
   409
    [true] whileTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   410
	probe := (keyArray basicAt:index).
362
claus
parents: 359
diff changeset
   411
	probe isNil ifTrue:[^ aBlock value].
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   412
	key = probe ifTrue:[^ index].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   413
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   414
	index == length ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   415
	    index := 1
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   416
	] ifFalse:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   417
	    index := index + 1
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   418
	].
362
claus
parents: 359
diff changeset
   419
	index == startIndex ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   420
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   421
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   422
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   423
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
   424
    "Look for the key in the receiver.  
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
   425
     If it is found, return return the index of the first unused slot. 
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
   426
     Grow the receiver, if key was not found, and no unused slots were present"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   427
61
claus
parents: 50
diff changeset
   428
    |index  "{ Class:SmallInteger }"
claus
parents: 50
diff changeset
   429
     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
   430
     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
   431
     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
   432
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
   433
    delIndex := 0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   434
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   435
    length := keyArray basicSize.
362
claus
parents: 359
diff changeset
   436
    index := key hash.
1975
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   437
    index < 16r1FFFFFFF ifTrue:[
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   438
        index := index * 2
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   439
    ].
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   440
    index := index \\ length + 1.
362
claus
parents: 359
diff changeset
   441
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   442
a27a279701f8 Initial revision
claus
parents:
diff changeset
   443
    [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
   444
        probe := keyArray basicAt: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
   445
        probe isNil 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
   446
            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
   447
            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
   448
            ^ 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
   449
        ].
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
   450
        key = probe ifTrue:[^ 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
   451
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
   452
        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
   453
            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
   454
                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
   455
            ]
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
   456
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   457
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
   458
        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
   459
            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
   460
        ] 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
   461
            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
   462
        ].
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
   463
        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
   464
            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
   465
                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
   466
                ^ 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
   467
            ].
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
   468
            ^ 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
   469
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   470
    ]
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
   471
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
   472
    "Modified: 4.6.1996 / 11:15:16 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   473
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   474
a27a279701f8 Initial revision
claus
parents:
diff changeset
   475
findNil:key
a27a279701f8 Initial revision
claus
parents:
diff changeset
   476
    "Look for the next slot usable for key.  This method assumes that
1055
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   477
     key is not already in the receiver and that keyArray does not have
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   478
     previously removed entries.
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   479
     To be used only while growing/rehashing to enter elements into a fresh
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   480
     collection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   481
1319
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   482
    |startIndex
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   483
     index  "{ Class:SmallInteger }"
61
claus
parents: 50
diff changeset
   484
     length "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   485
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   486
    length := keyArray basicSize.
362
claus
parents: 359
diff changeset
   487
    index := key hash.
1975
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   488
    index < 16r1FFFFFFF ifTrue:[
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   489
        index := index * 2
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   490
    ].
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   491
    index := index \\ length + 1.
1319
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   492
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   493
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   494
    [(keyArray basicAt:index) notNil] whileTrue:[
1319
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   495
        index == length ifTrue:[
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   496
            index := 1
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   497
        ] ifFalse:[
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   498
            index := index + 1
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   499
        ].
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   500
        index == startIndex ifTrue:[
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   501
            "notice: should not be reached
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   502
             - we must find one since this is only to be 
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   503
               called after growing"
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   504
            ^ 0
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   505
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   507
    ^ index
1055
d3784a235a2e commentary
Claus Gittinger <cg@exept.de>
parents: 1052
diff changeset
   508
1319
296bcdecf9e6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   509
    "Modified: 30.4.1996 / 14:15:20 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   510
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   511
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   512
fullCheck
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
    "check if collection is full (after an add); grow if so.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   514
     Definition of 'full' is currently: 'filled more than 75% (i.e. 3/4th)'"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   515
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   516
    |sz "{Class: SmallInteger}" |
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   517
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   518
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   519
     grow if filled more than 75% 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   520
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   521
    sz := keyArray basicSize.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   522
    tally > (sz * 3 // 4) ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   523
       self grow
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   524
    ]
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   525
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   526
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   527
grow
a27a279701f8 Initial revision
claus
parents:
diff changeset
   528
    "change the number of element slots of the collection to a useful
a27a279701f8 Initial revision
claus
parents:
diff changeset
   529
     new size"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   530
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   531
    self grow:(keyArray basicSize * 2)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   532
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   533
a27a279701f8 Initial revision
claus
parents:
diff changeset
   534
grow:newSize
a27a279701f8 Initial revision
claus
parents:
diff changeset
   535
    "change the number of element slots of the collection - to do this,
a27a279701f8 Initial revision
claus
parents:
diff changeset
   536
     we have to rehash (which is done by re-adding all elements to a new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   537
     empty set)."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   538
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   539
    |elem oldKeyArray newKeyArray deletedEntry
252
   540
     containerSize oldSize "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   541
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   542
    oldKeyArray := keyArray.
252
   543
    oldSize := oldKeyArray size.
   544
    containerSize := (self class goodSizeFrom:newSize).
   545
    containerSize == oldSize ifTrue:[^ self].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   546
252
   547
    keyArray := newKeyArray := self keyContainerOfSize:containerSize. 
   548
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   549
    deletedEntry := DeletedEntry.
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   550
    1 to:oldSize do:[:srcIndex |
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   551
	elem := oldKeyArray basicAt:srcIndex.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   552
	(elem notNil and:[elem ~~ deletedEntry]) ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   553
	    "cannot be already there"
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   554
	    newKeyArray basicAt:(self findNil:elem) put:elem
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   555
	].
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   556
    ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   557
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   558
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   559
initialIndexFor:hashKey boundedBy:length
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   560
    "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
   561
     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
   562
     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
   563
     improves performance, by avoiding an extra message send ...)"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   564
1972
f9bfcad48b64 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1643
diff changeset
   565
    ^ (hashKey * 2 \\ length) + 1.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   566
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   567
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   568
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   569
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   570
     Extracted to make life of weak subclasses easier ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   571
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   572
    ^ Array basicNew:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   573
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   574
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   575
rehash
61
claus
parents: 50
diff changeset
   576
    "rehash is done by re-adding all elements to a new empty set.
claus
parents: 50
diff changeset
   577
     Rehash is needed after a binaryRead, for example."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   578
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   579
    |element oldKeyArray newKeyArray
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   580
     n "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   581
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   582
    oldKeyArray := keyArray.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   583
    n := oldKeyArray size.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   584
    keyArray := newKeyArray := self keyContainerOfSize:n.
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   585
2
claus
parents: 1
diff changeset
   586
    1 to:n do:[:index |
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   587
	element := oldKeyArray at:index.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   588
	(element notNil and:[element ~~ DeletedEntry]) ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   589
	    "cannot be already there"
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   590
	    newKeyArray basicAt:(self findNil:element) put:element
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   591
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   592
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   593
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   594
a27a279701f8 Initial revision
claus
parents:
diff changeset
   595
rehashFrom:startIndex
61
claus
parents: 50
diff changeset
   596
    "rehash elements starting at index - after a remove.
claus
parents: 50
diff changeset
   597
     Notice: due to the new implementation of remove, 
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   598
	     this is no longer needed"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   599
3
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   600
    |element i "{ Class:SmallInteger }"
24d81bf47225 *** empty log message ***
claus
parents: 2
diff changeset
   601
     length
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   602
     index "{ Class:SmallInteger }" |
a27a279701f8 Initial revision
claus
parents:
diff changeset
   603
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   604
    length := keyArray basicSize.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   605
    index := startIndex.
13
62303f84ff5f *** empty log message ***
claus
parents: 5
diff changeset
   606
    element := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   607
    [element notNil] whileTrue:[
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   608
	i := self findNil:element.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   609
	i == index ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   610
	    ^ self
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   611
	].
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   612
	keyArray basicAt:i put:element.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   613
	keyArray basicAt:index put:nil.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   614
159
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   615
	index == length ifTrue:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   616
	    index := 1
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   617
	] ifFalse:[
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   618
	    index := index + 1.
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   619
	].
514c749165c3 *** empty log message ***
claus
parents: 119
diff changeset
   620
	element := keyArray basicAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   621
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
   622
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   623
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   624
setTally:count
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   625
    "initialize the contents array (for at least count slots) 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   626
     and set tally to zero.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   627
     The size is increased to the next prime for better hashing behavior."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   628
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   629
    keyArray := self keyContainerOfSize:(self class goodSizeFrom:count). 
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   630
    tally := 0
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   631
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   632
a27a279701f8 Initial revision
claus
parents:
diff changeset
   633
!Set methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   634
362
claus
parents: 359
diff changeset
   635
capacity 
claus
parents: 359
diff changeset
   636
    "return the number of elements, that the receiver is
claus
parents: 359
diff changeset
   637
     prepared to take.
claus
parents: 359
diff changeset
   638
     Not used by the system; added for ST-80 compatibility."
claus
parents: 359
diff changeset
   639
claus
parents: 359
diff changeset
   640
    ^ keyArray size
claus
parents: 359
diff changeset
   641
!
claus
parents: 359
diff changeset
   642
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   643
includes:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   644
    "return true if the argument anObject is in the receiver"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   645
362
claus
parents: 359
diff changeset
   646
    ^ (self find:anObject ifAbsent:0) ~~ 0
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   647
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   648
a27a279701f8 Initial revision
claus
parents:
diff changeset
   649
isEmpty
a27a279701f8 Initial revision
claus
parents:
diff changeset
   650
    "return true if the receiver is empty"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   651
a27a279701f8 Initial revision
claus
parents:
diff changeset
   652
    ^ tally == 0
a27a279701f8 Initial revision
claus
parents:
diff changeset
   653
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   654
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   655
isFixedSize
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   656
    "return true if the receiver cannot grow - this will vanish once
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   657
     Arrays and Strings learn how to grow ..."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   658
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   659
    ^ false
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   660
!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   661
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   662
occurrencesOf:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   663
    "return the number of occurrences of anObject in the receiver"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   664
362
claus
parents: 359
diff changeset
   665
    (self find:anObject ifAbsent:0) == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   666
    ^ 1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   667
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   668
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   669
size
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   670
    "return the number of set elements"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   671
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   672
    ^ tally
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   673
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   674
2259
685b96fb41b6 a better deletedClass
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   675
!Set class methodsFor:'documentation'!
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   676
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   677
version
2314
39ba656af0bb comment
Claus Gittinger <cg@exept.de>
parents: 2272
diff changeset
   678
    ^ '$Header: /cvs/stx/stx/libbasic/Set.st,v 1.44 1997-01-29 21:05:45 cg Exp $'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   679
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   680
Set initialize!