CacheDictionary.st
author Claus Gittinger <cg@exept.de>
Tue, 24 Jun 1997 19:42:44 +0200
changeset 538 9cffa21b8dd4
parent 499 d3814cbd1f90
child 910 af28d492d1eb
permissions -rw-r--r--
added example

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

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

!CacheDictionary 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
"
    a CacheDictionary is a Dictionary which will not grow - i.e. keep
    only a limited number of elements.
    It can be used as a cache, for keeping recently used objects alive.

    [author:]
        Claus Gittinger
"
!

examples
"
    |d|

    d := CacheDictionary new:16.
    1 to:20 do:[:i |
	d at:i printString put:i.
    ].
    21 to:40 do:[:i |
	d at:i printString put:i.
    ].
    d inspect
"
! !

!CacheDictionary methodsFor:'private'!

emptyCheck
    "redefined - never shrink"

    ^ self

    "Modified: 30.1.1997 / 15:17:12 / cg"
!

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. If no empty slot
     is available, make one empty (but never grow)."

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

    length := keyArray basicSize.
    index := key hash.
    index < 16r1FFFFFFF ifTrue:[
        index := index * 2
    ].
    index := index \\ length + 1.
    startIndex := index.

    [true] whileTrue:[
        probe := keyArray basicAt:index.
        probe = key ifTrue:[^ index].
        probe isNil ifTrue:[
            freeIndex isNil ifTrue:[^ index].
            keyArray basicAt:freeIndex put:nil.
            valueArray basicAt:freeIndex put:nil.
            ^ freeIndex
        ].

        (freeIndex isNil and:[probe == DeletedEntry]) ifTrue:[
            freeIndex := index
        ].

        index := index + 1.
        index > length ifTrue:[
            index := 1.
        ].
        index == startIndex ifTrue:[
            "/ mhmh - actually, a kind of round-robin would be better
            freeIndex isNil ifTrue:[
                freeIndex := startIndex
            ].

            keyArray basicAt:freeIndex put:nil.
            valueArray basicAt:freeIndex put:nil.
            tally := tally - 1.
            ^ freeIndex
        ].

    ]

    "Modified: 1.3.1997 / 00:59:55 / cg"
!

fullCheck
    "redefined - never grow"

    ^ self

    "Modified: 30.1.1997 / 15:17:18 / cg"
! !

!CacheDictionary class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/CacheDictionary.st,v 1.18 1997-06-24 17:42:44 cg Exp $'
! !