VirtualArrayWithCache.st
author Stefan Vogel <sv@exept.de>
Wed, 08 Apr 2020 19:02:35 +0200
changeset 5473 de911f462862
parent 5388 534a32d00c8f
permissions -rw-r--r--
*** empty log message ***

"{ Encoding: utf8 }"

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

"{ NameSpace: Smalltalk }"

VirtualArray subclass:#VirtualArrayWithCache
	instanceVariableNames:'lruCache'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Arrayed'
!

!VirtualArrayWithCache class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2018 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
"
    like a VirtualArray, but caches the results of the last few accesses.
    This might behave better, if it is expensive to compute the elements,
    and they are often accessed repeatedly (as when showing in a listView).

    [author:]
        Claus Gittinger

    [instance variables:]

    [class variables:]

    [see also:]

"
!

examples
"
                                                                [exBegin]
    |factorialLines|

    factorialLines := VirtualArrayWithCache new:10000.
    factorialLines generator:[:index | index factorial printString].
    factorialLines at:1000.
    factorialLines at:1000.
    factorialLines at:10000.
    
                                                                [exEnd]
"
! !

!VirtualArrayWithCache methodsFor:'collection protocol'!

at:index
    lruCache isNil ifTrue:[
        lruCache := CacheDictionary new:100.
    ].
    ^ lruCache at:index ifAbsentPut:[generator value:index]

    "Created: / 01-03-2019 / 20:35:45 / Claus Gittinger"
! !

!VirtualArrayWithCache class methodsFor:'documentation'!

version_CVS
    ^ '$Header$'
! !