IdSet.st
author Claus Gittinger <cg@exept.de>
Thu, 02 Sep 1999 23:44:11 +0200
changeset 4670 21eec331e1e9
parent 4625 1ebb8edfbd0d
permissions -rw-r--r--
checkin from browser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
Set subclass:#IdentitySet
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    14
	instanceVariableNames:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    15
	classVariableNames:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    16
	poolDictionaries:''
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    17
	category:'Collections-Unordered'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    20
!IdentitySet class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    21
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    22
copyright
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    24
 COPYRIGHT (c) 1993 by Claus Gittinger
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    25
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    27
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    28
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    31
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    32
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    34
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    35
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
documentation
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
    same as a Set but compares elements using == 
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
    (i.e. they must be identical - not just equal in structure).
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    40
    Since compare is on identity (using ==), hashing is also done via
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    41
    #identityHash instead of #hash.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    42
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    43
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
    44
        Claus Gittinger
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    45
"
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    46
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    47
4625
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    48
!IdentitySet methodsFor:'adding & removing'!
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    49
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    50
removeIdentical:anObject ifAbsent:exceptionBlock
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    51
    ^ super remove:anObject ifAbsent:exceptionBlock
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    52
! !
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
    53
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    54
!IdentitySet methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    55
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
    57
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
    58
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
    59
     return the value of evaluating aBlock.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    60
     Redefined to compare for identity instead of equality"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
a27a279701f8 Initial revision
claus
parents:
diff changeset
    62
    |index      "{ Class:SmallInteger }"
3
24d81bf47225 *** empty log message ***
claus
parents: 1
diff changeset
    63
     length startIndex probe |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    64
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    65
    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: 634
diff changeset
    66
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: 634
diff changeset
    67
"/
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: 634
diff changeset
    68
"/  length < 10 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: 634
diff changeset
    69
"/	"assuming, that for small sets the overhead of hashing
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: 634
diff changeset
    70
"/	 is large ..."
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: 634
diff changeset
    71
"/	^ keyArray identityIndexOf:key ifAbsent:aBlock.
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: 634
diff changeset
    72
"/  ].
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: 634
diff changeset
    73
"/
10
claus
parents: 3
diff changeset
    74
362
claus
parents: 250
diff changeset
    75
    index := key identityHash.
1975
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
    76
    index < 16r1FFFFFFF ifTrue:[
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
    77
        index := index * 2
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
    78
    ].
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
    79
    index := index \\ length + 1.
362
claus
parents: 250
diff changeset
    80
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    81
a27a279701f8 Initial revision
claus
parents:
diff changeset
    82
    [true] whileTrue:[
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    83
	probe := (keyArray basicAt:index).
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    84
	probe == key ifTrue:[^ index].
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    85
	probe isNil ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    87
	index == length ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    88
	    index := 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    89
	] ifFalse:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    90
	    index := index + 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    91
	].
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    92
	index == startIndex ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    93
	    ^ aBlock value
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    94
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    95
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
    96
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    97
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    98
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: 634
diff changeset
    99
    "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: 634
diff changeset
   100
     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: 634
diff changeset
   101
     Grow the receiver, if key was not found, and no unused slots were present"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
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: 634
diff changeset
   103
    |index  "{ 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: 634
diff changeset
   104
     length "{ 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: 634
diff changeset
   105
     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: 634
diff changeset
   106
     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: 634
diff changeset
   107
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: 634
diff changeset
   108
    delIndex := 0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   110
    length := keyArray basicSize.
362
claus
parents: 250
diff changeset
   111
    index := key identityHash.
1975
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   112
    index < 16r1FFFFFFF ifTrue:[
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   113
        index := index * 2
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   114
    ].
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   115
c94a8e0b0251 avoid largeIntegers
Claus Gittinger <cg@exept.de>
parents: 1972
diff changeset
   116
    index := index \\ length + 1.
362
claus
parents: 250
diff changeset
   117
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   118
a27a279701f8 Initial revision
claus
parents:
diff changeset
   119
    [true] whileTrue:[
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   120
	probe := keyArray basicAt: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: 634
diff changeset
   121
	key == probe ifTrue:[^ 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: 634
diff changeset
   122
	probe 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: 634
diff changeset
   123
	    delIndex == 0 ifTrue:[^ 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: 634
diff changeset
   124
	    keyArray basicAt:delIndex put:nil.
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: 634
diff changeset
   125
	    ^ delIndex
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: 634
diff changeset
   126
	].
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: 634
diff changeset
   127
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   128
	probe == DeletedEntry 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: 634
diff changeset
   129
	    delIndex == 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   130
		delIndex := 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: 634
diff changeset
   131
	    ]
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   132
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   134
	index == length ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   135
	    index := 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   136
	] ifFalse:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   137
	    index := index + 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   138
	].
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: 634
diff changeset
   139
	index == startIndex 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: 634
diff changeset
   140
	    delIndex ~~ 0 ifTrue:[
497de696dff0 OOPS - could add elements twice after a remove (shame on me: how could that go unnoticed for so long ...)
Claus Gittinger <cg@exept.de>
parents: 634
diff changeset
   141
		keyArray basicAt:delIndex put:nil.
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: 634
diff changeset
   142
		^ delIndex
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: 634
diff changeset
   143
	    ].
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   144
	    ^ self grow 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: 634
diff changeset
   145
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   146
    ]
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: 634
diff changeset
   147
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: 634
diff changeset
   148
    "Modified: 26.3.1996 / 20:00:42 / cg"
10
claus
parents: 3
diff changeset
   149
!
claus
parents: 3
diff changeset
   150
2460
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   151
hashFor:aKey
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   152
    "return an initial index given a key."
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   153
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   154
    ^ aKey identityHash
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   155
1d7caef5f5ed added #hashFor:
Claus Gittinger <cg@exept.de>
parents: 1975
diff changeset
   156
    "Created: 19.3.1997 / 15:18:59 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   157
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   158
3944
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   159
!IdentitySet methodsFor:'testing'!
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   160
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   161
includesIdentical:anObject
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   162
    "for identitySet, the #includes: test already tests for identity"
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   163
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   164
    ^ self includes:anObject
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   165
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   166
    "Created: / 11.12.1998 / 20:01:12 / cg"
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   167
! !
541580143566 #includesIdentical: is the same as #includes:
Claus Gittinger <cg@exept.de>
parents: 2474
diff changeset
   168
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   169
!IdentitySet class methodsFor:'documentation'!
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   170
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   171
version
4625
1ebb8edfbd0d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3944
diff changeset
   172
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/IdSet.st,v 1.24 1999-08-24 10:49:24 cg Exp $'
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   173
! !