CacheDictionary.st
author claus
Sun, 09 Jan 1994 22:12:52 +0100
changeset 13 f089ff744cd1
parent 2 07d9ee98e092
child 31 e223f3cf2995
permissions -rw-r--r--
*** empty log message ***

"
 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 comment:'

COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

a CacheDictionary is a Dictionary which will not grow - i.e. keep
only a limited number of elements.

$Header: /cvs/stx/stx/libbasic2/CacheDictionary.st,v 1.4 1994-01-09 21:12:52 claus Exp $

written jan 93 by claus
'!

!CacheDictionary methodsFor:'private'!

emptyCheck
    "redefined - dont shrink"

    ^ self
!

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

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

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

    index := startIndex.
    [true] whileTrue:[
        probe := keyArray basicAt:index.
        (probe == nil or: [probe = key]) ifTrue:[^ index].

        (freeIndex isNil and:[probe == DeletedElement]) 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
            ].

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

    ]
! !