IdentitySet.st
author Claus Gittinger <cg@exept.de>
Tue, 12 Nov 1996 16:13:57 +0100
changeset 1975 c94a8e0b0251
parent 1972 f9bfcad48b64
child 2460 1d7caef5f5ed
permissions -rw-r--r--
avoid largeIntegers

"
 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 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.
"
!

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.

    [author:]
        Claus Gittinger
"
! !

!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.
"/  ].
"/

    index := key identityHash.
    index < 16r1FFFFFFF ifTrue:[
        index := index * 2
    ].
    index := index \\ length + 1.
    startIndex := index.

    [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 return the index of the first unused slot. 
     Grow the receiver, if key was not found, and no unused slots were present"

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

    delIndex := 0.

    length := keyArray basicSize.
    index := key identityHash.
    index < 16r1FFFFFFF ifTrue:[
        index := index * 2
    ].

    index := index \\ length + 1.
    startIndex := index.

    [true] whileTrue:[
	probe := keyArray basicAt:index.
	key == probe ifTrue:[^ index].
	probe isNil ifTrue:[
	    delIndex == 0 ifTrue:[^ index].
	    keyArray basicAt:delIndex put:nil.
	    ^ delIndex
	].

	probe == DeletedEntry ifTrue:[
	    delIndex == 0 ifTrue:[
		delIndex := index
	    ]
	].

	index == length ifTrue:[
	    index := 1
	] ifFalse:[
	    index := index + 1
	].
	index == startIndex ifTrue:[
	    delIndex ~~ 0 ifTrue:[
		keyArray basicAt:delIndex put:nil.
		^ delIndex
	    ].
	    ^ self grow findKeyOrNil:key
	].
    ]

    "Modified: 26.3.1996 / 20:00:42 / cg"
!

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.
    index < 16r1FFFFFFF ifTrue:[
        index := index * 2
    ].
    index := index \\ 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

! !

!IdentitySet class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.20 1996-11-12 15:13:55 cg Exp $'
! !