3737
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2000 by eXept Software AG
|
|
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 |
"
|
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
|
16 |
ReadOnlySequenceableCollection subclass:#GeometricSeries
|
|
17 |
instanceVariableNames:'start stop factor'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Sequenceable'
|
|
21 |
!
|
|
22 |
|
|
23 |
!GeometricSeries class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2000 by eXept Software AG
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
Much like intervals (which have a constant difference between elements),
|
|
42 |
these have a constant factor between them.
|
|
43 |
GeometricSeries represent a collection (or range) of values specified by
|
|
44 |
a startValue, an endValue and a *factor*.
|
|
45 |
Like with intervals, the elements are computed, not stored.
|
4453
|
46 |
For example,
|
|
47 |
the GeometricSeries (1 to:100 byFactor:2) contains the elements (1 2 4 8 16 32 64).
|
|
48 |
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
|
49 |
|
|
50 |
examples:
|
|
51 |
|
|
52 |
(1 to:100 byFactor:2) do:[:i | Transcript showCR:i]
|
|
53 |
(1 to:100 byFactor:1.1) do:[:i | Transcript showCR:i]
|
|
54 |
(1 to:100 byFactor:2) asArray
|
|
55 |
(1 to:128 byFactor:2) asArray
|
|
56 |
(128 to:2 byFactor:(1/2)) asArray
|
|
57 |
|
3739
|
58 |
(1 to:128 byFactor:2) sum
|
|
59 |
(1 to:128 byFactor:2) asArray sum
|
|
60 |
|
3737
|
61 |
[author:]
|
|
62 |
Claus Gittinger
|
|
63 |
"
|
|
64 |
! !
|
|
65 |
|
|
66 |
!GeometricSeries class methodsFor:'instance creation'!
|
|
67 |
|
|
68 |
from:start to:stop byFactor:factor
|
|
69 |
"return a new geometric series with elements from start
|
|
70 |
to stop by a factor"
|
|
71 |
|
|
72 |
^ self new setFrom:start to:stop byFactor:factor
|
|
73 |
|
|
74 |
|
|
75 |
! !
|
|
76 |
|
|
77 |
!GeometricSeries methodsFor:'enumerating'!
|
|
78 |
|
|
79 |
do:aBlock
|
3739
|
80 |
"evaluate the argument, aBlock for every element in the receiver.
|
|
81 |
Redefined since SeqColl accesses the receiver with at:,
|
|
82 |
which is slow for intervals."
|
3737
|
83 |
|
|
84 |
|aValue iter|
|
|
85 |
|
|
86 |
aValue := start.
|
3739
|
87 |
(aValue isLimitedPrecisionReal or:[factor isLimitedPrecisionReal]) ifFalse:[
|
|
88 |
factor < 1 ifTrue:[
|
|
89 |
[stop <= aValue] whileTrue:[
|
|
90 |
aBlock value:aValue.
|
|
91 |
aValue := aValue * factor
|
|
92 |
]
|
|
93 |
] ifFalse:[
|
|
94 |
[stop >= aValue] whileTrue:[
|
|
95 |
aBlock value:aValue.
|
|
96 |
aValue := aValue * factor
|
|
97 |
]
|
|
98 |
]
|
|
99 |
] ifTrue:[
|
|
100 |
"/ the code below avoids rounding errors
|
|
101 |
"/ to accumulate if floats are enumerated.
|
|
102 |
iter := 1.
|
|
103 |
factor < 1 ifTrue:[
|
|
104 |
[stop <= aValue] whileTrue:[
|
|
105 |
aBlock value:aValue.
|
|
106 |
aValue := start + (factor raisedTo:iter).
|
|
107 |
iter := iter + 1.
|
|
108 |
]
|
|
109 |
] ifFalse:[
|
|
110 |
[stop >= aValue] whileTrue:[
|
|
111 |
aBlock value:aValue.
|
|
112 |
aValue := start + (factor raisedTo:iter).
|
|
113 |
iter := iter + 1.
|
|
114 |
]
|
|
115 |
]
|
3737
|
116 |
]
|
|
117 |
|
|
118 |
"
|
|
119 |
(1 to:128 byFactor:2) do:[:v | Transcript showCR:v]
|
|
120 |
(16 to:1 byFactor:(1/2)) do:[:v | Transcript showCR:v]
|
|
121 |
(1.0 to:128.0 byFactor:2) do:[:v | Transcript showCR:v]
|
|
122 |
|
|
123 |
(GeometricSeries from:1 to:128 byFactor:2) do:[:v | Transcript showCR:v]
|
|
124 |
"
|
|
125 |
!
|
|
126 |
|
|
127 |
size
|
|
128 |
|cnt|
|
|
129 |
|
|
130 |
cnt := 0.
|
|
131 |
self do:[:each | cnt := cnt + 1].
|
|
132 |
^ cnt
|
|
133 |
|
|
134 |
"Created: / 31.10.2001 / 15:06:31 / cg"
|
|
135 |
! !
|
|
136 |
|
|
137 |
!GeometricSeries methodsFor:'printing & storing'!
|
|
138 |
|
|
139 |
printOn:aStream
|
|
140 |
"append a printed representation to aStream"
|
|
141 |
|
|
142 |
start printOn:aStream.
|
|
143 |
aStream nextPutAll:' to:'.
|
|
144 |
stop printOn:aStream.
|
|
145 |
aStream nextPutAll:' byFactor:'.
|
|
146 |
factor printOn:aStream.
|
|
147 |
|
|
148 |
!
|
|
149 |
|
|
150 |
storeOn:aStream
|
|
151 |
"store a representation which can reconstruct the receiver to aStream"
|
|
152 |
|
|
153 |
aStream nextPut:$(.
|
|
154 |
self printOn:aStream.
|
|
155 |
aStream nextPut:$).
|
|
156 |
|
|
157 |
|
|
158 |
! !
|
|
159 |
|
|
160 |
!GeometricSeries methodsFor:'private'!
|
|
161 |
|
|
162 |
setFrom:startVal to:stopVal byFactor:factorVal
|
|
163 |
"set start, stop and factor components"
|
|
164 |
|
|
165 |
start := startVal.
|
|
166 |
stop := stopVal.
|
|
167 |
factor := factorVal
|
|
168 |
|
|
169 |
|
|
170 |
!
|
|
171 |
|
|
172 |
species
|
|
173 |
"return the type of collection to be returned by collect, select etc."
|
|
174 |
|
|
175 |
^ OrderedCollection
|
|
176 |
|
|
177 |
! !
|
|
178 |
|
|
179 |
!GeometricSeries class methodsFor:'documentation'!
|
|
180 |
|
|
181 |
version
|
|
182 |
^ '$Header$'
|
|
183 |
!
|
|
184 |
|
|
185 |
version_CVS
|
|
186 |
^ '$Header$'
|
|
187 |
! !
|
|
188 |
|