IdentitySet.st
author claus
Mon, 08 May 1995 05:31:14 +0200
changeset 339 e8658d38abfb
parent 250 a5deb61ffdac
child 362 4131e87e79ec
permissions -rw-r--r--
.

"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Set subclass:#IdentitySet
	 instanceVariableNames:''
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Collections-Unordered'
!

IdentitySet comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.10 1995-02-15 10:24:26 claus Exp $
'!

!IdentitySet class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.10 1995-02-15 10:24:26 claus Exp $
"
!

documentation
"
    same as a Set but compares elements using == 
    (i.e. they must be identical - not just equal in structure).
    Since compare is on identity (using ==), hashing is also done via
    #identityHash instead of #hash.
"
! !

!IdentitySet methodsFor:'private'!

find:key ifAbsent:aBlock
    "Look for the key in the receiver.  If it is found, return
     the index of the slot containing the key, otherwise
     return the value of evaluating aBlock.
     Redefined to compare for identity instead of equality"

    |index      "{ Class:SmallInteger }"
     length startIndex probe |

    length := keyArray basicSize.
    length < 10 ifTrue:[
	"assuming, that for small sets the overhead of hashing
	 is large ..."
	^ keyArray identityIndexOf:key ifAbsent:aBlock.
    ].

    startIndex := key identityHash \\ length + 1.

    index := startIndex.
    [true] whileTrue:[
	probe := (keyArray basicAt:index).
	probe == key ifTrue:[^ index].
	probe isNil ifTrue:[^ aBlock value].

	index == length ifTrue:[
	    index := 1
	] ifFalse:[
	    index := index + 1
	].
	index == startIndex ifTrue:[
	    ^ aBlock value
	]
    ]
!

findKeyOrNil:key
    "Look for the key in the receiver.  If it is found, return
     the index of the slot containing the key, otherwise
     return the index of the first unused slot. Grow the receiver,
     if key was not found, and no unused slots where present.
     Redefined to compare for identity instead of equality"

    |index      "{ Class:SmallInteger }"
     length startIndex probe |

    length := keyArray basicSize.
    startIndex := key identityHash \\ length + 1.

    index := startIndex.
    [true] whileTrue:[
	probe := keyArray basicAt:index.
	(probe isNil or: [key == probe]) ifTrue:[^ index].
	probe == DeletedEntry ifTrue:[
	    keyArray basicAt:index put:nil.
	    ^ index
	].

	index == length ifTrue:[
	    index := 1
	] ifFalse:[
	    index := index + 1
	].
	index == startIndex ifTrue: [
	    ^ self grow findKeyOrNil:key
	]
    ]
!

findNil:key
    "Look for the next slot usable for key.  This method assumes that
     key is not already in the receiver - used only while growing/rehashing"

    |index  "{ Class:SmallInteger }"
     length|

    length := keyArray basicSize.
    index := key identityHash \\ length + 1.

    [(keyArray basicAt:index) notNil] whileTrue:[
	index == length ifTrue:[
	    index := 1
	] ifFalse:[
	    index := index + 1
	].
	"notice: no check for no nil found - we must find one since
	 this is only called after growing"
    ].
    ^ index

! !