IdentitySet.st
author Claus Gittinger <cg@exept.de>
Tue, 03 Feb 2004 16:45:01 +0100
changeset 7866 6624a55c7dd0
parent 7310 8865e8d343cd
child 10091 ad0bf1206ce0
permissions -rw-r--r--
resourceDirectory

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

"{ Package: 'stx:libbasic' }"

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:'Compatibility-Squeak'!

copyWithout:anElement
    ^ self select:[:each | each ~~ anElement]

    "
     #(1 2 3 4 5 6 7) asSet copyWithout:5
    "
!

copyWithoutAll:aCollection
    ^ self select:[:each | (aCollection includesIdentical:each)]

    "
     #(1 2 3 4 5 6 7) asSet copyWithoutAll:#(3 5 7 9)
    "
! !

!IdentitySet methodsFor:'adding & removing'!

removeIdentical:anObject ifAbsent:exceptionBlock
    ^ super remove:anObject ifAbsent:exceptionBlock
! !

!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 "{ Class:SmallInteger }"
     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 := index := self initialIndexForKey:key.

    [true] whileTrue:[
        probe := keyArray basicAt:index.
        probe == key ifTrue:[^ index].        "<<<< == is different from inherited"
        (self slotIsEmpty:probe) 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.
    startIndex := index := self initialIndexForKey:key.

    [true] whileTrue:[
        probe := keyArray basicAt:index.
        key == probe ifTrue:[^ index].
        (self slotIsEmpty:probe) 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"
!

hashFor:aKey
    "return an initial index given a key."

    ^ aKey identityHash

    "Created: 19.3.1997 / 15:18:59 / cg"
! !

!IdentitySet methodsFor:'testing'!

identicalContentsAs:aCollection
    "return true if the receiver and aCollection represent collections
     with identical contents (but not caring for order)."

    aCollection size == self size ifFalse:[^ false].
    aCollection do:[:eachElement |
        (self includesIdentical:eachElement) ifFalse:[^ false]
    ].
    ^ true

    "
     |col|

     col := #('aaa' 'bbb' 'ccc' 'ddd').
     col identicalContentsAs:(col asIdentitySet).  
     col identicalContentsAs:(col copy asIdentitySet).  
     col identicalContentsAs:(col deepCopy asIdentitySet).  
   "
!

includesIdentical:anObject
    "for identitySet, the #includes: test already tests for identity"

    ^ self includes:anObject

    "Created: / 11.12.1998 / 20:01:12 / cg"
! !

!IdentitySet class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/IdentitySet.st,v 1.29 2003-05-19 18:36:05 cg Exp $'
! !