|
Class: VirtualArrayWithCache
Object
|
+--Collection
|
+--SequenceableCollection
|
+--VirtualArray
|
+--VirtualArrayWithCache
|
+--VirtualReadOnlyTextFileContents
- Package:
- stx:libbasic2
- Category:
- Collections-Arrayed
- Version:
- rev:
1.6
date: 2023/11/15 07:22:11
- user: cg
- file: VirtualArrayWithCache.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
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).
Similar to a LazyArray, however, this only remembers a number of elements in
an LRU fashion, whereas a LazyArray remembers all.
[instance variables:]
[class variables:]
copyrightCOPYRIGHT (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.
defaults
-
defaultLRUCacheSize
-
collection protocol
-
at: index
-
return the element at index.
The value is computed by the generator and remembered in the lruCache
initialization
-
lruCache: aCacheDictionaryOrNil
-
useful to setup a cache with a different size
Usage example(s):
|v|
v := VirtualArrayWithCache new:10000.
v generator:[:idx | idx factorial].
v lruCache:(CacheDictionary new:10).
v inspect.
|
|factorialLines|
factorialLines := VirtualArrayWithCache new:10000.
factorialLines generator:[:index | index factorial printString].
factorialLines at:1000.
factorialLines at:1000.
factorialLines at:10000.
|
|top textView factorialLines generator|
generator :=
[
|lastN lastComputed|
[:n |
lastN == (n-1) ifTrue:[
Transcript showCR:'fast'.
lastComputed := lastComputed * n
] ifFalse:[
Transcript showCR:'slow'.
lastComputed := n factorial
].
lastN := n.
lastComputed
].
] value.
factorialLines := VirtualArrayWithCache new:10000.
factorialLines generator:[:index | e'{index}: {generator value:index}'].
top := StandardSystemView new.
top label:'Factorials computed as you scroll'.
textView := HVScrollableView for:TextView in:top.
textView origin:0@0 corner:1.0@1.0.
textView list:factorialLines expandTabs:false
scanForNonStrings:false includesNonStrings:false.
textView checkedLinesForWidthOfContentsComputation:0.
textView setWidthOfContents:1000.
top open.
|
|