4819
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
VirtualArray subclass:#VirtualArrayWithCache
|
|
6 |
instanceVariableNames:'lruCache'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Collections-Abstract'
|
|
10 |
!
|
|
11 |
|
|
12 |
!VirtualArrayWithCache class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
|
16 |
like a VirtualArray, but caches the results of the last few accesses.
|
|
17 |
This might behave better, if it is expensive to compute the elements,
|
|
18 |
and they are often accessed repeatedly (as when showing in a listView).
|
|
19 |
|
|
20 |
[author:]
|
|
21 |
Claus Gittinger
|
|
22 |
|
|
23 |
[instance variables:]
|
|
24 |
|
|
25 |
[class variables:]
|
|
26 |
|
|
27 |
[see also:]
|
|
28 |
|
|
29 |
"
|
|
30 |
!
|
|
31 |
|
|
32 |
examples
|
|
33 |
"
|
|
34 |
[exBegin]
|
|
35 |
|factorialLines|
|
|
36 |
|
|
37 |
factorialLines := VirtualArrayWithCache new:10000.
|
|
38 |
factorialLines generator:[:index | index factorial printString].
|
|
39 |
factorialLines at:1000.
|
|
40 |
factorialLines at:1000.
|
|
41 |
factorialLines at:10000.
|
|
42 |
|
|
43 |
[exEnd]
|
|
44 |
"
|
|
45 |
! !
|
|
46 |
|
|
47 |
!VirtualArrayWithCache methodsFor:'collection protocol'!
|
|
48 |
|
|
49 |
at:index
|
|
50 |
lruCache isNil ifTrue:[
|
|
51 |
lruCache := CacheDictionary new:100.
|
|
52 |
].
|
|
53 |
^ lruCache at:index ifAbsentPut:[generator value:index]
|
|
54 |
|
|
55 |
"Created: / 01-03-2019 / 20:35:45 / Claus Gittinger"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!VirtualArrayWithCache class methodsFor:'documentation'!
|
|
59 |
|
|
60 |
version_CVS
|
|
61 |
^ '$Header$'
|
|
62 |
! !
|
|
63 |
|