IdSet.st
author Claus Gittinger <cg@exept.de>
Fri, 08 Nov 1996 16:27:05 +0100
changeset 1938 e1a56370a833
parent 1290 15ba3221b89b
child 1972 f9bfcad48b64
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
a27a279701f8 Initial revision
claus
parents:
diff changeset
    48
!IdentitySet methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    49
a27a279701f8 Initial revision
claus
parents:
diff changeset
    50
find:key ifAbsent:aBlock
a27a279701f8 Initial revision
claus
parents:
diff changeset
    51
    "Look for the key in the receiver.  If it is found, return
a27a279701f8 Initial revision
claus
parents:
diff changeset
    52
     the index of the slot containing the key, otherwise
a27a279701f8 Initial revision
claus
parents:
diff changeset
    53
     return the value of evaluating aBlock.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    54
     Redefined to compare for identity instead of equality"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    55
a27a279701f8 Initial revision
claus
parents:
diff changeset
    56
    |index      "{ Class:SmallInteger }"
3
24d81bf47225 *** empty log message ***
claus
parents: 1
diff changeset
    57
     length startIndex probe |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    58
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    59
    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
    60
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
    61
"/
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
    62
"/  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
    63
"/	"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
    64
"/	 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
    65
"/	^ 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
    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
"/
10
claus
parents: 3
diff changeset
    68
362
claus
parents: 250
diff changeset
    69
    index := key identityHash.
claus
parents: 250
diff changeset
    70
    index := index \\ length + 1.
claus
parents: 250
diff changeset
    71
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    72
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
    [true] whileTrue:[
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    74
	probe := (keyArray basicAt:index).
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    75
	probe == key ifTrue:[^ index].
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    76
	probe isNil ifTrue:[^ aBlock value].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    78
	index == length ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    79
	    index := 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    80
	] ifFalse:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    81
	    index := index + 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    82
	].
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    83
	index == startIndex ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    84
	    ^ aBlock value
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
    85
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
    ]
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
    89
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
    90
    "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
    91
     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
    92
     Grow the receiver, if key was not found, and no unused slots were present"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    93
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
    94
    |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
    95
     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
    96
     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
    97
     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
    98
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
    delIndex := 0.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   101
    length := keyArray basicSize.
362
claus
parents: 250
diff changeset
   102
    index := key identityHash.
claus
parents: 250
diff changeset
   103
    index := index \\ length + 1.
claus
parents: 250
diff changeset
   104
    startIndex := index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
    [true] whileTrue:[
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   107
	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
   108
	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
   109
	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
   110
	    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
   111
	    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
   112
	    ^ 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
   113
	].
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
   114
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   115
	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
   116
	    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
   117
		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
   118
	    ]
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   119
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   120
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   121
	index == length ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   122
	    index := 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   123
	] ifFalse:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   124
	    index := index + 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   125
	].
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
   126
	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
   127
	    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
   128
		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
   129
		^ 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
   130
	    ].
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   131
	    ^ 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
   132
	].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
    ]
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
   134
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
   135
    "Modified: 26.3.1996 / 20:00:42 / cg"
10
claus
parents: 3
diff changeset
   136
!
claus
parents: 3
diff changeset
   137
claus
parents: 3
diff changeset
   138
findNil:key
claus
parents: 3
diff changeset
   139
    "Look for the next slot usable for key.  This method assumes that
claus
parents: 3
diff changeset
   140
     key is not already in the receiver - used only while growing/rehashing"
claus
parents: 3
diff changeset
   141
claus
parents: 3
diff changeset
   142
    |index  "{ Class:SmallInteger }"
claus
parents: 3
diff changeset
   143
     length|
claus
parents: 3
diff changeset
   144
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   145
    length := keyArray basicSize.
362
claus
parents: 250
diff changeset
   146
    index := key identityHash.
claus
parents: 250
diff changeset
   147
    index := index \\ length + 1.
10
claus
parents: 3
diff changeset
   148
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   149
    [(keyArray basicAt:index) notNil] whileTrue:[
250
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   150
	index == length ifTrue:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   151
	    index := 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   152
	] ifFalse:[
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   153
	    index := index + 1
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   154
	].
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   155
	"notice: no check for no nil found - we must find one since
a5deb61ffdac *** empty log message ***
claus
parents: 92
diff changeset
   156
	 this is only called after growing"
10
claus
parents: 3
diff changeset
   157
    ].
claus
parents: 3
diff changeset
   158
    ^ index
claus
parents: 3
diff changeset
   159
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   160
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   161
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   162
!IdentitySet class methodsFor:'documentation'!
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   163
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   164
version
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1126
diff changeset
   165
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/IdSet.st,v 1.18 1996-04-25 16:18:39 cg Exp $'
634
e15218d09420 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   166
! !