MethodDictionary.st
author Stefan Vogel <sv@exept.de>
Sat, 07 Aug 2004 20:47:18 +0200
changeset 8467 f25892c63a56
parent 5557 f5f8d236027c
child 9444 c7134e0aa248
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1995 by eXept Software AG
              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' }"

Collection variableSubclass:#MethodDictionary
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Kernel-Methods'
!

!MethodDictionary class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by eXept Software AG
              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
"
    Instances of MethodDictionary store selector/method associations
    in classes. Conceptionally, they behave like IdentityDictionaries, but are
    implemented using a single array (instead of Dictionary, which uses
    two arrays to store keys and values separately).
    Also, they do not use hashing, since due to caching in the VM, hashing
    does not make too much of a difference in speed, but complicates the 
    VM implementation.

    [author:]
        Stefan Vogel

    [see also:]
        Dictionary
        IdentityDictionary
        Behavior Class
        Method 
        Symbol
"
! !

!MethodDictionary class methodsFor:'instance creation'!

new:sz 
    "create and return a new methodDictionary holding sz
     key->value associations"

    ^ self basicNew:(sz * 2)
!

withAll:aDictionary
    "create a MethodDictionary from another Dictionary"

    |newDict i "{ Class: SmallInteger }" |

    newDict := self new:aDictionary size.
    i := 1.
    aDictionary keysAndValuesDo:[:key :value |
        newDict basicAt:i   put:key.
        newDict basicAt:i+1 put:value.
        i := i+2.
    ].
    ^ newDict

    "
        |d|

        d := Dictionary withKeys:#(a b c d e) andValues:#(1 2 3 4 5).
        MethodDictionary withAll:d.
    "

    "Created: 12.6.1996 / 13:46:43 / stefan"
    "Modified: 12.6.1996 / 13:56:36 / stefan"
    "Modified: 3.7.1996 / 11:05:55 / cg"
!

withKeys:keys andValues:values
    "create a MethodDictionary from a key (selector) array and value (method) array"

    |inst 
     sz "{ Class: SmallInteger }"
     idx "{ Class: SmallInteger }" |

    sz := keys size.
    inst := self new:sz.
    idx := 2.
    1 to:sz do:[:i |
        inst basicAt:(idx-1)  put:(keys   at:i).
        inst basicAt:(idx)    put:(values at:i).
	idx := idx + 2.
    ].
    ^ inst

    "Created: 12.6.1996 / 13:46:43 / stefan"
    "Modified: 3.7.1996 / 11:05:34 / cg"
! !

!MethodDictionary class methodsFor:'binary storage'!

binaryFullDefinitionFrom:stream manager:manager
   |size "{ Class: SmallInteger }" 
    inst|

   size := manager nextObject.
   inst := self new:size.
   1 to:size*2 by:2 do:[:i|
        inst basicAt:i put:manager nextObject.          "/ get selector
        inst basicAt:(i + 1) put:(Method binaryFullDefinitionFrom:stream manager:manager).
   ].
   ^ inst

    "Created: 7.6.1996 / 13:37:22 / stefan"
    "Modified: 7.6.1996 / 13:52:08 / stefan"
    "Modified: 3.7.1996 / 11:06:34 / cg"
! !

!MethodDictionary class methodsFor:'queries'!

isBuiltInClass
    "this class is known by the run-time-system"

    ^ self == MethodDictionary
! !

!MethodDictionary methodsFor:'accessing'!

associationAt:key 
    "return an association consisting of aKey and the element indexed 
     by aKey - 
     report an error, if no element is stored under aKey."

    ^ Association key:key value:(self at:key)
!

at:key 
    "return the value for a given key, which is supposed to be a symbol"

    |sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) == key ifTrue:[^ self basicAt:(i + 1)]
    ].
    ^ self errorKeyNotFound:key

    "Modified: 7.6.1996 / 15:53:28 / stefan"
!

at:key ifAbsent:exceptionBlock
    "return the element indexed by aKey - 
     return result of exceptionBlock if no element is stored under aKey"

    |sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) == key ifTrue:[^ self basicAt:(i + 1)]
    ].
    ^ exceptionBlock value
!

at:key put:value 
    "set the value for a given key, which is supposed to be a symbol.
     In contrast to dictionaries, we allow adding elements only, if there is an
     empty slot (nil key) present."

    |slot sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        slot := self basicAt:i.
        (slot == key) ifTrue:[
            ^ self basicAt:(i + 1) put:value
        ].
        slot isNil ifTrue:[
            self basicAt:i put:key.
            ^ self basicAt:(i + 1) put:value
        ].
    ].
    ^ self errorKeyNotFound:key

    "Modified: 7.6.1996 / 09:39:04 / stefan"
    "Modified: 23.1.1997 / 13:59:29 / cg"
!

at:key putOrAppend:value 
    "set the value for a given key, which is supposed to be a symbol.
     In contrast to dictionaries, we allow adding elements only, if there is an
     empty slot (nil key) present.
     Otherwise a new MethodDictionary is created & returned"

    |slot emptySlot newDict sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        slot := self basicAt:i.
        (slot == key) ifTrue:[
            self basicAt:(i + 1) put:value .
            ^ self.
        ].
        slot isNil ifTrue:[
            emptySlot := i.
        ]
    ].

    emptySlot notNil ifTrue:[
        self basicAt:emptySlot       put:key.
        self basicAt:(emptySlot + 1) put:value.
        ^ self.
    ].

    "/ not enough room for new entry, copy to new dictionary
    newDict := self class new:sz//2+1.

"/ cannot do this ...
"/    newDict replaceFrom:1 to:sz with:self startingAt:1.
"/ must use basicAt
    1 to:sz do:[:i |
        newDict basicAt:i put:(self basicAt:i).
    ].

    newDict basicAt:(sz+1) put:key.
    newDict basicAt:(sz+2) put:value.
    ^ newDict.

    "Created: 7.6.1996 / 15:01:54 / stefan"
    "Modified: 7.6.1996 / 17:32:40 / stefan"
    "Modified: 23.1.1997 / 14:00:03 / cg"
!

keyAtValue:value ifAbsent:exceptionBlock
    "return the first key with value - 
     return result of exceptionBlock if no key can be found"

    |sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    2 to:sz by:2 do:[:i |
        (self basicAt:i) == value ifTrue:[^ self basicAt:(i - 1)]
    ].
    ^ exceptionBlock value

    "Created: 7.6.1996 / 14:53:57 / stefan"
!

removeKey:key 
    "remove key from dictionary. 
     We actually do not remove it, but set it to nil."

    ^ self removeKey:key ifAbsent:[self errorKeyNotFound:key].

    "Created: 7.6.1996 / 15:58:50 / stefan"
!

removeKey:key ifAbsent:failBlock
    "remove key from dictionary. 
     We actually do not remove it, but set it to nil."

    |value sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) == key ifTrue:[
           value := self basicAt:(i + 1).
           self basicAt:i put:nil. self basicAt:(i+1) put:nil.
           ^ value
        ]
    ].

    ^ failBlock value.

    "Created: 7.6.1996 / 15:57:56 / stefan"
    "Modified: 23.1.1997 / 14:36:24 / cg"
!

removeKeyAndCompress:key
    "remove key from dictionary. 
     A new, compressed MethodDictionary will be returned,
     or nil, if key is not present."

    |newDict dstIndex sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) == key ifTrue:[
            dstIndex := i
        ]
    ].

    dstIndex isNil ifTrue:[^ self].

    sz := self basicSize.
    newDict := self class new:(sz//2-1).
    dstIndex := 1.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) ~~ key ifTrue:[
           newDict basicAt:dstIndex   put:(self basicAt:i).
           newDict basicAt:dstIndex+1 put:(self basicAt:i+1).
           dstIndex := dstIndex + 2.
        ]
    ].
    dstIndex > sz ifTrue:[
        ^ nil
    ].
    ^ newDict

    "Created: 7.6.1996 / 15:57:56 / stefan"
    "Modified: 7.6.1996 / 16:47:02 / stefan"
    "Modified: 12.2.1997 / 19:47:23 / cg"
! !

!MethodDictionary methodsFor:'binary storage'!

storeFullBinaryDefinitionOn:stream manager:manager
    "store the complete description (i.e. including methods)"

    self size storeBinaryOn:stream manager:manager.
    self keysAndValuesDo:[:sel :mthd|
        sel storeBinaryOn:stream manager:manager.
        mthd makeRealMethod storeFullBinaryDefinitionOn:stream manager:manager.
    ].

    "Created: 7.6.1996 / 12:53:00 / stefan"
! !

!MethodDictionary methodsFor:'enumerating'!

collect:aBlock
    "for each element in the receiver, evaluate the argument, aBlock
     and return a Bag with the results."

    |sz "{ Class: SmallInteger }"
     newCollection key value|

    newCollection := Bag new.
    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        key := self basicAt:i. value := self basicAt:i+1.
        key notNil ifTrue:[
            newCollection add:(aBlock value:value)
        ]
    ].
    ^ newCollection

    "Created: 24.6.1996 / 17:41:41 / cg"
    "Modified: 23.1.1997 / 13:58:24 / cg"
!

do:aBlock
    "evaluate the 1 arg block aBlock for each value (i.e. each Method)"

    |sz "{ Class: SmallInteger }"
     key value|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        key := self basicAt:i. value := self basicAt:i+1.
        key notNil ifTrue:[
            aBlock value:value.
        ]
    ].

    "Created: 7.6.1996 / 09:25:23 / stefan"
    "Modified: 7.6.1996 / 13:47:37 / stefan"
    "Modified: 23.1.1997 / 13:57:49 / cg"
!

findFirstKey:aBlock
    "find and return the first key, for which evaluation of the argument, aBlock
     returns true; return nil if none is detected."

    self keysDo:[:key |
        (aBlock value:key) ifTrue:[^ key].
    ].
    ^ nil

    "Created: 8.10.1996 / 22:01:31 / cg"
    "Modified: 8.10.1996 / 22:02:03 / cg"
!

keysAndValuesDo:aBlock
    "evaluate the 2 arg block aBlock for each key (i.e. each selector)
     and each value (i.e. each method)"

    |key value sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        key := self basicAt:i. value := self basicAt:(i+1).
        (key notNil) ifTrue:[
            value notNil ifTrue:[
                aBlock value:key value:value.
            ]
        ]
    ].

    "Created: / 7.6.1996 / 09:27:42 / stefan"
    "Modified: / 3.2.1998 / 00:48:47 / cg"
!

keysDo:aBlock
    "evaluate the 1 arg block aBlock for each key (i.e. each selector)"

    |key sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        key := self basicAt:i.
        (key notNil) ifTrue:[
            aBlock value:key.
        ]
    ].

    "Created: 7.6.1996 / 09:26:34 / stefan"
! !

!MethodDictionary methodsFor:'inspecting'!

inspectorClass
    "redefined to use DictionaryInspector
     (instead of the default Inspector)."

    ^ DictionaryInspectorView

    "Created: 12.6.1996 / 12:29:13 / stefan"
! !

!MethodDictionary methodsFor:'private'!

compressed
    "compress - return either the myself or a new, compressed MethodDictionary"

    |newDict tally key mySize
     dstIndex "{ Class: SmallInteger }" 
     sz       "{ Class: SmallInteger }" |

    sz := self basicSize.
    mySize := sz // 2.
    tally := 0.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) notNil ifTrue:[
            tally := tally + 1
        ]
    ].

    tally == mySize ifTrue:[^ self].

    newDict := self species new:tally.
    dstIndex := 1.
    1 to:sz by:2 do:[:i |
        key := self basicAt:i.
        key notNil ifTrue:[
           newDict basicAt:dstIndex   put:key.
           newDict basicAt:dstIndex+1 put:(self basicAt:i+1).
           dstIndex := dstIndex + 2.
        ]
    ].
    ^ newDict

    "Modified: / 05-08-2004 / 20:05:44 / stefan"
! !

!MethodDictionary methodsFor:'queries'!

includesKey:key
    "return true, if there is an entry stored under key
     (i.e. there is a method for that selector)"

    |sz "{ Class: SmallInteger }"|

    sz := self basicSize.
    1 to:sz by:2 do:[:i |
        (self basicAt:i) == key ifTrue:[^ true].
    ].

    ^ false

    "Created: 30.7.1997 / 11:01:37 / cg"
!

keys
    "return a collection containing all keys of the receiver"

    |sz "{ Class: SmallInteger }"
     keys key|

    sz := self basicSize.
    keys := OrderedCollection new:(sz // 2).

    1 to:sz by:2 do:[:index | 
        key := self basicAt:index.
        key notNil ifTrue:[
            keys add:key
        ]
    ].
    ^ keys

    "
     self methodDictionary keys 
    "

    "Modified: 23.1.1997 / 13:56:13 / cg"
!

size
    "return the number of elements (associations) in the receiver"

    ^ self basicSize // 2
!

values
    "return a collection containing all values of the receiver"

    |sz "{ Class: SmallInteger }"
     values key|

    sz := self basicSize.
    values := OrderedCollection new:(sz // 2).

    1 to:sz by:2 do:[:index | 
        key := self basicAt:index.
        key notNil ifTrue:[
            values add:(self basicAt:index+1)
        ]
    ].
    ^ values

    "
     self methodDictionary values
    "

    "Modified: 23.1.1997 / 13:55:17 / cg"
! !

!MethodDictionary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/MethodDictionary.st,v 1.23 2004-08-07 18:47:18 stefan Exp $'
! !