PluggableDictionary.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Jun 2016 18:09:58 +0200
changeset 3934 3fc6968232c1
parent 3178 5d24b4312240
child 4021 12a732f6d443
permissions -rw-r--r--
class: exept_expecco_plugin class definition

"
 COPYRIGHT (c) 2014 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:libbasic2' }"

Dictionary subclass:#PluggableDictionary
	instanceVariableNames:'hashFunction compareFunction'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Unordered'
!

!PluggableDictionary class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2014 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
"
    a dictionary where the hash- and compare functions can be provided externally.

    [author:]
        Claus Gittinger
"
!

examples
"
    |s|

    s := PluggableDictionary
        hashWith:[:k | k size] 
        compareWith:[:a :b | a notNil and:[b notNil and:[a asLowercase = b asLowercase]]].
    s at:'hello' put:123.
    s at:'world' put:222.
    s at:'abc' put:333.
    s at:'Hello'.
    s at:'heLLo'.
    s at:'ABC'.
    s at:'WORLD'.
    s size.
    s includesKey:'heLlo'.
    s includesKey:'wOrLd'.
    s includesKey:'wOrLds'.
"
! !

!PluggableDictionary class methodsFor:'instance creation'!

hashWith:hashFunctionArg compareWith:compareFunctionArg
    ^ self new hashWith:hashFunctionArg compareWith:compareFunctionArg
! !

!PluggableDictionary methodsFor:'accessing'!

hashWith:hashFunctionArg compareWith:compareFunctionArg 
    hashFunction := hashFunctionArg.
    compareFunction := compareFunctionArg.
! !

!PluggableDictionary methodsFor:'private'!

compareSame:element1 with:element2
    "compare two elements for being the same. 
     Here, return the value from compareFunction"

    ^ compareFunction value:element1 value:element2
!

emptyCollectionForKeys
    "return an empty collection used for keys.
     Here, an IdentitySet is returned.
     Made a separate method to allow redefinition for different kind of
     containers in subclasses."

    ^ PluggableSet new:(self size) hashWith:hashFunction compareWith:compareFunction
!

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.
     Redefined - since we inherit this code from Set-Dictionary
     (one of the seldom cases, where I could make use of multiple inheritance
     and inherit from IdentitySet ... sigh)"

    |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 := index := self initialIndexForKey:key.

    [true] whileTrue:[
        probe := keyArray basicAt:index.
        probe notNil ifTrue:[
            (compareFunction value:probe value: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.
        probe notNil ifTrue:[
            (compareFunction value:probe value:key) 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
        ].
    ]
!

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

    ^ hashFunction value:aKey
! !

!PluggableDictionary methodsFor:'testing'!

occurrencesOf:anObject
    "count & return how often anObject is stored in the dictionary.
     This counts values - not keys.
     Redefined to use #== (identity compare), NOT equality compare."

    |cnt|

    anObject isNil ifTrue:[^ super occurrencesOf:anObject].

    cnt := 0.
    valueArray do:[:element |
       (compareFunction value:element value:anObject) ifTrue:[cnt := cnt + 1]
    ].
    ^ cnt
! !

!PluggableDictionary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.2 2014-02-18 21:21:01 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/PluggableDictionary.st,v 1.2 2014-02-18 21:21:01 cg Exp $'
! !