IdDict.st
author claus
Wed, 13 Oct 1993 01:19:00 +0100
changeset 3 24d81bf47225
parent 1 a27a279701f8
child 10 4f1f9a91e406
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.3 1993-10-13 00:16:10 claus Exp $

written jul 92 by claus
'!

!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 isNil or: [probe == key]) ifTrue:[^ index].

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

findKey: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)"
        index := keyArray identityIndexOf:key.
        index == 0 ifTrue:[
            ^ aBlock value
        ].
        ^ index
    ].

    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]
    ]
! !