1300
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
3845
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
1300
|
5 |
ArrayedCollection variableSubclass:#LazyArray
|
|
6 |
instanceVariableNames:'valueGenerator'
|
3306
|
7 |
classVariableNames:'UncomputedValueSingleton'
|
1300
|
8 |
poolDictionaries:''
|
|
9 |
category:'Collections-Arrayed'
|
|
10 |
!
|
|
11 |
|
3306
|
12 |
Object subclass:#UncomputedValue
|
|
13 |
instanceVariableNames:''
|
|
14 |
classVariableNames:''
|
|
15 |
poolDictionaries:''
|
|
16 |
privateIn:LazyArray
|
|
17 |
!
|
|
18 |
|
1300
|
19 |
!LazyArray class methodsFor:'documentation'!
|
|
20 |
|
|
21 |
documentation
|
|
22 |
"
|
4144
|
23 |
An Array which computes its values lazily (on demand) and remembers them.
|
|
24 |
Useful if it is relatively expensive to compute an element,
|
|
25 |
and it may be needed again later.
|
1300
|
26 |
|
|
27 |
[author:]
|
|
28 |
Claus Gittinger (cg@alan)
|
|
29 |
|
|
30 |
[see also:]
|
|
31 |
Lazy
|
|
32 |
"
|
|
33 |
!
|
|
34 |
|
|
35 |
examples
|
|
36 |
"
|
|
37 |
[exBegin]
|
|
38 |
|squares|
|
|
39 |
|
|
40 |
squares := LazyArray new:100.
|
|
41 |
squares valueGenerator:[:index | index squared].
|
|
42 |
|
|
43 |
squares at:50.
|
|
44 |
squares inspect.
|
|
45 |
[exEnd]
|
|
46 |
"
|
|
47 |
! !
|
|
48 |
|
|
49 |
!LazyArray class methodsFor:'initialization'!
|
|
50 |
|
|
51 |
initialize
|
3306
|
52 |
UncomputedValueSingleton isNil ifTrue:[
|
|
53 |
UncomputedValueSingleton := UncomputedValue new.
|
1300
|
54 |
]
|
|
55 |
|
|
56 |
"
|
|
57 |
self initialize
|
|
58 |
"
|
|
59 |
! !
|
|
60 |
|
|
61 |
!LazyArray class methodsFor:'instance creation'!
|
|
62 |
|
|
63 |
new:size
|
|
64 |
^ (super new:size) atAllPut:UncomputedValue
|
|
65 |
! !
|
|
66 |
|
|
67 |
!LazyArray methodsFor:'accessing'!
|
|
68 |
|
|
69 |
at:index
|
|
70 |
|val|
|
|
71 |
|
|
72 |
val := super at:index.
|
|
73 |
val == UncomputedValue ifTrue:[
|
|
74 |
val := valueGenerator value:index.
|
|
75 |
self at:index put:val.
|
|
76 |
].
|
|
77 |
^ val.
|
|
78 |
!
|
|
79 |
|
|
80 |
valueGenerator:aBlock
|
|
81 |
valueGenerator := aBlock
|
|
82 |
! !
|
|
83 |
|
|
84 |
!LazyArray class methodsFor:'documentation'!
|
|
85 |
|
|
86 |
version
|
3845
|
87 |
^ '$Header$'
|
1300
|
88 |
! !
|
|
89 |
|
3306
|
90 |
|
1300
|
91 |
LazyArray initialize!
|