4328
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2003 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
1300
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
3845
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
1300
|
16 |
ArrayedCollection variableSubclass:#LazyArray
|
|
17 |
instanceVariableNames:'valueGenerator'
|
3306
|
18 |
classVariableNames:'UncomputedValueSingleton'
|
1300
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Arrayed'
|
|
21 |
!
|
|
22 |
|
3306
|
23 |
Object subclass:#UncomputedValue
|
|
24 |
instanceVariableNames:''
|
|
25 |
classVariableNames:''
|
|
26 |
poolDictionaries:''
|
|
27 |
privateIn:LazyArray
|
|
28 |
!
|
|
29 |
|
1300
|
30 |
!LazyArray class methodsFor:'documentation'!
|
|
31 |
|
4328
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 2003 by Claus Gittinger
|
|
35 |
All Rights Reserved
|
|
36 |
|
|
37 |
This software is furnished under a license and may be used
|
|
38 |
only in accordance with the terms of that license and with the
|
|
39 |
inclusion of the above copyright notice. This software may not
|
|
40 |
be provided or otherwise made available to, or used by, any
|
|
41 |
other person. No title to or ownership of the software is
|
|
42 |
hereby transferred.
|
|
43 |
"
|
|
44 |
!
|
|
45 |
|
1300
|
46 |
documentation
|
|
47 |
"
|
4144
|
48 |
An Array which computes its values lazily (on demand) and remembers them.
|
|
49 |
Useful if it is relatively expensive to compute an element,
|
|
50 |
and it may be needed again later.
|
1300
|
51 |
|
|
52 |
[author:]
|
|
53 |
Claus Gittinger (cg@alan)
|
|
54 |
|
|
55 |
[see also:]
|
|
56 |
Lazy
|
|
57 |
"
|
|
58 |
!
|
|
59 |
|
|
60 |
examples
|
|
61 |
"
|
|
62 |
[exBegin]
|
|
63 |
|squares|
|
|
64 |
|
|
65 |
squares := LazyArray new:100.
|
|
66 |
squares valueGenerator:[:index | index squared].
|
|
67 |
|
|
68 |
squares at:50.
|
|
69 |
squares inspect.
|
|
70 |
[exEnd]
|
|
71 |
"
|
|
72 |
! !
|
|
73 |
|
|
74 |
!LazyArray class methodsFor:'initialization'!
|
|
75 |
|
|
76 |
initialize
|
3306
|
77 |
UncomputedValueSingleton isNil ifTrue:[
|
|
78 |
UncomputedValueSingleton := UncomputedValue new.
|
1300
|
79 |
]
|
|
80 |
|
|
81 |
"
|
|
82 |
self initialize
|
|
83 |
"
|
|
84 |
! !
|
|
85 |
|
|
86 |
!LazyArray class methodsFor:'instance creation'!
|
|
87 |
|
|
88 |
new:size
|
|
89 |
^ (super new:size) atAllPut:UncomputedValue
|
|
90 |
! !
|
|
91 |
|
|
92 |
!LazyArray methodsFor:'accessing'!
|
|
93 |
|
|
94 |
at:index
|
|
95 |
|val|
|
|
96 |
|
|
97 |
val := super at:index.
|
|
98 |
val == UncomputedValue ifTrue:[
|
|
99 |
val := valueGenerator value:index.
|
|
100 |
self at:index put:val.
|
|
101 |
].
|
|
102 |
^ val.
|
|
103 |
!
|
|
104 |
|
|
105 |
valueGenerator:aBlock
|
|
106 |
valueGenerator := aBlock
|
|
107 |
! !
|
|
108 |
|
|
109 |
!LazyArray class methodsFor:'documentation'!
|
|
110 |
|
|
111 |
version
|
3845
|
112 |
^ '$Header$'
|
1300
|
113 |
! !
|
|
114 |
|
3306
|
115 |
|
1300
|
116 |
LazyArray initialize!
|