CacheDictionary.st
author claus
Thu, 10 Aug 1995 15:19:05 +0200
changeset 84 d401ce0001dc
parent 80 989ae65d4675
child 85 df13b436b54e
permissions -rw-r--r--
.

"
 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

$Header: /cvs/stx/stx/libbasic2/CacheDictionary.st,v 1.8 1995-08-10 13:17:40 claus Exp $
'!

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

version
"
$Header: /cvs/stx/stx/libbasic2/CacheDictionary.st,v 1.8 1995-08-10 13:17:40 claus Exp $
$Revision: 1.8 $
"
!

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

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

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

    ]
! !