IdDict.st
author claus
Fri, 25 Feb 1994 14:00:53 +0100
changeset 56 be0ed17e6f85
parent 39 bcf183a31bbb
child 77 6c38ca59927f
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1992 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.
"

Dictionary subclass:#IdentityDictionary
       instanceVariableNames:''
       classVariableNames:''
       poolDictionaries:''
       category:'Collections-Unordered'
!

IdentityDictionary comment:'

COPYRIGHT (c) 1992 by Claus Gittinger
              All Rights Reserved

same as a Dictionary but key must be identical - not just equal.
Since compare is on identity keys, hashing is also done via
identityHash instead of hash.

$Header: /cvs/stx/stx/libbasic/Attic/IdDict.st,v 1.6 1994-01-09 21:17:23 claus Exp $

written jul 92 by claus
'!

!IdentityDictionary methodsFor:'accessing'!

keyAtValue:aValue ifAbsent:exceptionBlock
    "return the key whose value equals the argument, the value of the 
     exceptionBlock if none is found.
     This is a slow access, since there is no fast reverse mapping"

    keyArray keysAndValuesDo:[:index :aKey |
        (aKey notNil and:[aKey ~~ DeletedEntry]) ifTrue:[
            (valueArray at:index) == aValue ifTrue:[^ aKey].
        ]
    ].
    ^ exceptionBlock value
! !

!IdentityDictionary methodsFor:'private'!

findKeyOrNil:key
    "Look for the key in the receiver.  If it is found, return
     the index of the association 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"

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

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

    index := startIndex.
    [true] whileTrue:[
        probe := keyArray basicAt:index.
        probe == DeletedEntry ifTrue:[
            keyArray basicAt:index put:nil.
            ^ index
        ].
        (probe isNil or: [probe == key]) ifTrue:[^ 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

! 

find:key ifAbsent:aBlock
    "Look for the key in the receiver.  If it is found, return
     the index of the association containing the key, otherwise
     return the value of evaluating aBlock."

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

    length := keyArray basicSize.
    length < 10 ifTrue:[
        "assuming, that for small dictionaries the overhead of hashing
         is large ... maybe that proves wrong (if overhead of comparing
         is high)"
        ^ keyArray identityIndexOf:key ifAbsent:aBlock.
    ].

    startIndex := key identityHash \\ length + 1.

    index := startIndex.
    [true] whileTrue:[
        probe := keyArray basicAt:index.
        probe == key ifTrue:[^ index].

        index == length ifTrue:[
            index := 1
        ] ifFalse:[
            index := index + 1
        ].
        (probe isNil or:[index == startIndex]) ifTrue:[^ aBlock value]
    ]
! !