5058
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
3737
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2000 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
ReadOnlySequenceableCollection subclass:#GeometricSeries
|
|
19 |
instanceVariableNames:'start stop factor'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Sequenceable'
|
|
23 |
!
|
|
24 |
|
|
25 |
!GeometricSeries class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2000 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
Much like intervals (which have a constant difference between elements),
|
|
44 |
these have a constant factor between them.
|
|
45 |
GeometricSeries represent a collection (or range) of values specified by
|
|
46 |
a startValue, an endValue and a *factor*.
|
|
47 |
Like with intervals, the elements are computed, not stored.
|
4453
|
48 |
For example,
|
|
49 |
the GeometricSeries (1 to:100 byFactor:2) contains the elements (1 2 4 8 16 32 64).
|
|
50 |
and GeometricSeries (1 to:(1/100) byFactor:(1/2)) contains the elements (1 1/2 1/4 1/8 1/16 1/32 1/64).
|
3737
|
51 |
|
|
52 |
examples:
|
|
53 |
|
|
54 |
(1 to:100 byFactor:2) do:[:i | Transcript showCR:i]
|
|
55 |
(1 to:100 byFactor:1.1) do:[:i | Transcript showCR:i]
|
|
56 |
(1 to:100 byFactor:2) asArray
|
|
57 |
(1 to:128 byFactor:2) asArray
|
|
58 |
(128 to:2 byFactor:(1/2)) asArray
|
|
59 |
|
3739
|
60 |
(1 to:128 byFactor:2) sum
|
|
61 |
(1 to:128 byFactor:2) asArray sum
|
|
62 |
|
3737
|
63 |
[author:]
|
|
64 |
Claus Gittinger
|
|
65 |
"
|
|
66 |
! !
|
|
67 |
|
|
68 |
!GeometricSeries class methodsFor:'instance creation'!
|
|
69 |
|
|
70 |
from:start to:stop byFactor:factor
|
|
71 |
"return a new geometric series with elements from start
|
|
72 |
to stop by a factor"
|
|
73 |
|
|
74 |
^ self new setFrom:start to:stop byFactor:factor
|
|
75 |
|
|
76 |
|
|
77 |
! !
|
|
78 |
|
5058
|
79 |
!GeometricSeries methodsFor:'accessing'!
|
|
80 |
|
|
81 |
at:index
|
|
82 |
"return (i.e. compute) the index'th element"
|
|
83 |
|
|
84 |
(index between:1 and:self size) ifTrue:[
|
|
85 |
^ start * (factor raisedToInteger: (index - 1))
|
|
86 |
].
|
|
87 |
^ self subscriptBoundsError:index
|
|
88 |
|
|
89 |
"
|
|
90 |
(1 to:128 byFactor:2) do:[:v | Transcript showCR:v]
|
|
91 |
(1 to:128 byFactor:2) at:1.
|
|
92 |
(1 to:128 byFactor:2) at:2.
|
|
93 |
(1 to:128 byFactor:2) at:3.
|
5079
|
94 |
(1 to:128 byFactor:2) last.
|
|
95 |
(1 to:100 byFactor:2) last.
|
5058
|
96 |
|
|
97 |
(16 to:1 byFactor:(1/2)) do:[:v | Transcript showCR:v]
|
|
98 |
(16 to:1 byFactor:(1/2)) at:1.
|
|
99 |
(16 to:1 byFactor:(1/2)) at:2.
|
|
100 |
(16 to:1 byFactor:(1/2)) at:3.
|
|
101 |
(16 to:1 byFactor:(1/2)) last.
|
5079
|
102 |
(16 to:3 byFactor:(1/2)) last.
|
5058
|
103 |
"
|
|
104 |
|
|
105 |
"Created: / 22-07-2019 / 14:49:12 / Claus Gittinger"
|
5079
|
106 |
!
|
|
107 |
|
|
108 |
first
|
|
109 |
^ start
|
|
110 |
|
|
111 |
"
|
|
112 |
(1 to:100 byFactor:2) first
|
|
113 |
(10 to:100 byFactor:3) first
|
|
114 |
(100 to:10 byFactor:1/3) first
|
|
115 |
"
|
|
116 |
|
|
117 |
"Created: / 31.10.2001 / 15:06:31 / cg"
|
|
118 |
!
|
|
119 |
|
|
120 |
last
|
|
121 |
|n last|
|
|
122 |
|
|
123 |
n := (stop/start) log:factor.
|
|
124 |
n := n truncated asInteger.
|
|
125 |
^ start * (factor raisedToInteger:n).
|
|
126 |
|
|
127 |
"
|
|
128 |
(1 to:100 byFactor:2) last
|
|
129 |
(1 to:100 byFactor:3) last
|
|
130 |
(10 to:100 byFactor:3) last
|
|
131 |
(100 to:10 byFactor:1/3) last
|
|
132 |
(100 to:1 byFactor:1/2) last
|
|
133 |
|
|
134 |
(1 to:100 byFactor:2.0) last
|
|
135 |
(1 to:64.0 byFactor:2.0) last
|
|
136 |
(1 to:100 byFactor:3.0) last
|
|
137 |
(10 to:100.0 byFactor:3) last
|
|
138 |
(100 to:10.0 byFactor:0.3) last
|
|
139 |
(100 to:3 byFactor:1/2) last
|
|
140 |
(100 to:3 byFactor:0.5) last
|
|
141 |
"
|
|
142 |
|
|
143 |
"Created: / 31.10.2001 / 15:06:31 / cg"
|
5058
|
144 |
! !
|
|
145 |
|
3737
|
146 |
!GeometricSeries methodsFor:'enumerating'!
|
|
147 |
|
|
148 |
do:aBlock
|
3739
|
149 |
"evaluate the argument, aBlock for every element in the receiver.
|
|
150 |
Redefined since SeqColl accesses the receiver with at:,
|
|
151 |
which is slow for intervals."
|
3737
|
152 |
|
|
153 |
|aValue iter|
|
|
154 |
|
|
155 |
aValue := start.
|
3739
|
156 |
(aValue isLimitedPrecisionReal or:[factor isLimitedPrecisionReal]) ifFalse:[
|
|
157 |
factor < 1 ifTrue:[
|
|
158 |
[stop <= aValue] whileTrue:[
|
|
159 |
aBlock value:aValue.
|
|
160 |
aValue := aValue * factor
|
|
161 |
]
|
|
162 |
] ifFalse:[
|
|
163 |
[stop >= aValue] whileTrue:[
|
|
164 |
aBlock value:aValue.
|
|
165 |
aValue := aValue * factor
|
|
166 |
]
|
|
167 |
]
|
|
168 |
] ifTrue:[
|
5079
|
169 |
"/ the code below tries to avoid rounding errors
|
3739
|
170 |
"/ to accumulate if floats are enumerated.
|
|
171 |
iter := 1.
|
|
172 |
factor < 1 ifTrue:[
|
|
173 |
[stop <= aValue] whileTrue:[
|
|
174 |
aBlock value:aValue.
|
5079
|
175 |
aValue := start * (factor raisedTo:iter).
|
3739
|
176 |
iter := iter + 1.
|
|
177 |
]
|
|
178 |
] ifFalse:[
|
|
179 |
[stop >= aValue] whileTrue:[
|
|
180 |
aBlock value:aValue.
|
5079
|
181 |
aValue := start * (factor raisedTo:iter).
|
3739
|
182 |
iter := iter + 1.
|
|
183 |
]
|
|
184 |
]
|
3737
|
185 |
]
|
|
186 |
|
|
187 |
"
|
|
188 |
(1 to:128 byFactor:2) do:[:v | Transcript showCR:v]
|
|
189 |
(16 to:1 byFactor:(1/2)) do:[:v | Transcript showCR:v]
|
|
190 |
(1.0 to:128.0 byFactor:2) do:[:v | Transcript showCR:v]
|
|
191 |
|
|
192 |
(GeometricSeries from:1 to:128 byFactor:2) do:[:v | Transcript showCR:v]
|
|
193 |
"
|
|
194 |
! !
|
|
195 |
|
|
196 |
!GeometricSeries methodsFor:'printing & storing'!
|
|
197 |
|
|
198 |
printOn:aStream
|
|
199 |
"append a printed representation to aStream"
|
|
200 |
|
|
201 |
start printOn:aStream.
|
|
202 |
aStream nextPutAll:' to:'.
|
|
203 |
stop printOn:aStream.
|
|
204 |
aStream nextPutAll:' byFactor:'.
|
|
205 |
factor printOn:aStream.
|
|
206 |
|
|
207 |
!
|
|
208 |
|
|
209 |
storeOn:aStream
|
|
210 |
"store a representation which can reconstruct the receiver to aStream"
|
|
211 |
|
|
212 |
aStream nextPut:$(.
|
|
213 |
self printOn:aStream.
|
|
214 |
aStream nextPut:$).
|
|
215 |
|
|
216 |
|
|
217 |
! !
|
|
218 |
|
|
219 |
!GeometricSeries methodsFor:'private'!
|
|
220 |
|
|
221 |
setFrom:startVal to:stopVal byFactor:factorVal
|
|
222 |
"set start, stop and factor components"
|
|
223 |
|
|
224 |
start := startVal.
|
|
225 |
stop := stopVal.
|
|
226 |
factor := factorVal
|
|
227 |
|
|
228 |
|
|
229 |
!
|
|
230 |
|
|
231 |
species
|
|
232 |
"return the type of collection to be returned by collect, select etc."
|
|
233 |
|
|
234 |
^ OrderedCollection
|
|
235 |
|
|
236 |
! !
|
|
237 |
|
5079
|
238 |
!GeometricSeries methodsFor:'queries'!
|
|
239 |
|
|
240 |
size
|
|
241 |
|n last|
|
|
242 |
|
|
243 |
n := (stop/start) log:factor.
|
|
244 |
n := n truncated asInteger.
|
|
245 |
last := start * (factor raisedToInteger:n).
|
|
246 |
^ n + 1
|
|
247 |
|
|
248 |
"/ cnt := 0.
|
|
249 |
"/ self do:[:each | cnt := cnt + 1].
|
|
250 |
"/ ^ cnt
|
|
251 |
|
|
252 |
"
|
|
253 |
(1 to:100 byFactor:2) size
|
|
254 |
(1 to:100 byFactor:3) size
|
|
255 |
(10 to:100 byFactor:3) size
|
|
256 |
(100 to:10 byFactor:1/3) size
|
|
257 |
(100 to:1 byFactor:1/2) size
|
|
258 |
|
|
259 |
(1 to:100 byFactor:2.0) size
|
|
260 |
(1 to:64.0 byFactor:2.0) size
|
|
261 |
(1 to:100 byFactor:3.0) size
|
|
262 |
(10 to:100.0 byFactor:3) size
|
|
263 |
(100 to:10.0 byFactor:0.3) size
|
|
264 |
(100 to:3 byFactor:1/2) size
|
|
265 |
(100 to:3 byFactor:0.5) size
|
|
266 |
"
|
|
267 |
|
|
268 |
"Created: / 31.10.2001 / 15:06:31 / cg"
|
|
269 |
! !
|
|
270 |
|
3737
|
271 |
!GeometricSeries class methodsFor:'documentation'!
|
|
272 |
|
|
273 |
version
|
|
274 |
^ '$Header$'
|
|
275 |
!
|
|
276 |
|
|
277 |
version_CVS
|
|
278 |
^ '$Header$'
|
|
279 |
! !
|
|
280 |
|