initial checkin
authorClaus Gittinger <cg@exept.de>
Fri, 01 Mar 2019 20:43:23 +0100
changeset 4819 2dba3c4a24b6
parent 4818 17979c7fad6b
child 4820 88e85e9e301f
initial checkin
VirtualArrayWithCache.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VirtualArrayWithCache.st	Fri Mar 01 20:43:23 2019 +0100
@@ -0,0 +1,63 @@
+"{ Package: 'stx:libbasic2' }"
+
+"{ NameSpace: Smalltalk }"
+
+VirtualArray subclass:#VirtualArrayWithCache
+	instanceVariableNames:'lruCache'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Collections-Abstract'
+!
+
+!VirtualArrayWithCache class methodsFor:'documentation'!
+
+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$'
+! !
+