author | Claus Gittinger <cg@exept.de> |
Sat, 08 May 1999 15:25:13 +0200 | |
changeset 4155 | 80c7fc5fe7b2 |
parent 3986 | 2ad4ebb5ebbb |
child 5521 | 7cad30b0ade9 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
216 | 3 |
All Rights Reserved |
1 | 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 |
||
13 |
SequenceableCollection subclass:#Interval |
|
1290 | 14 |
instanceVariableNames:'start stop step' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Sequenceable' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!Interval class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
216 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
Intervals represent a collection (or range) of numeric values specified by |
|
269 | 39 |
a startValue, an endValue and a step. |
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
629
diff
changeset
|
40 |
The interesting thing is that the elements are computed, not stored. |
88 | 41 |
For example, the interval (1 to:5) containes the elements (1 2 3 4 5) and |
42 |
(1 to:6 by:2) contains (1 3 5). |
|
339 | 43 |
|
44 |
examples: |
|
45 |
||
1998 | 46 |
(1 to:10) do:[:i | Transcript showCR:i] |
339 | 47 |
|
48 |
notice, that this is semantically equivalent to: |
|
49 |
||
1998 | 50 |
1 to:10 do:[:i | Transcript showCR:i] |
339 | 51 |
|
52 |
however, the second is preferred, since loops using to:do: are |
|
53 |
much faster and do not create temporary garbage objects. |
|
54 |
Therefore, Intervals are generally NOT used for this kind of loops. |
|
55 |
||
1998 | 56 |
(1 to:10) asArray |
339 | 57 |
|
1998 | 58 |
(1 to:10 by:2) asOrderedCollection |
1290 | 59 |
|
60 |
[author:] |
|
1998 | 61 |
Claus Gittinger |
88 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
65 |
!Interval class methodsFor:'instance creation'! |
|
66 |
||
67 |
from:start to:stop |
|
68 |
"return a new interval with elements from start |
|
69 |
to stop by 1" |
|
70 |
||
2 | 71 |
^ self new setFrom:start to:stop by:1 |
1 | 72 |
! |
73 |
||
74 |
from:start to:stop by:step |
|
75 |
"return a new interval with elements from start |
|
76 |
to stop by step" |
|
77 |
||
2 | 78 |
^ self new setFrom:start to:stop by:step |
1 | 79 |
! ! |
80 |
||
606 | 81 |
!Interval methodsFor:'accessing'! |
1 | 82 |
|
606 | 83 |
at:index |
84 |
"return (i.e. compute) the index'th element" |
|
2 | 85 |
|
606 | 86 |
(index between:1 and:self size) ifTrue:[ |
87 |
^ start + (step * (index - 1)) |
|
88 |
]. |
|
89 |
self errorSubscriptBounds:index |
|
90 |
! |
|
1 | 91 |
|
606 | 92 |
at:index put:anObject |
1998 | 93 |
"{ Pragma: +optSpace }" |
94 |
||
606 | 95 |
"catch at:put: message - intervals cannot store elements" |
96 |
||
97 |
self error:'you cannot store into an interval' |
|
98 |
! |
|
1 | 99 |
|
100 |
first |
|
101 |
"return the first element of the collection" |
|
102 |
||
103 |
^ start |
|
104 |
! |
|
105 |
||
328 | 106 |
increment |
339 | 107 |
"alias for #step; for ST-80 compatibility" |
328 | 108 |
|
109 |
^ step |
|
110 |
! |
|
111 |
||
606 | 112 |
last |
113 |
"return the last element of the collection" |
|
1 | 114 |
|
606 | 115 |
^ stop |
1 | 116 |
! |
117 |
||
118 |
size |
|
119 |
"return the number of elements in the collection" |
|
120 |
||
121 |
(step < 0) ifTrue:[ |
|
216 | 122 |
(start < stop) ifTrue:[ |
123 |
^ 0 |
|
124 |
]. |
|
125 |
^ stop - start // step + 1 |
|
1 | 126 |
]. |
127 |
(stop < start) ifTrue:[ |
|
216 | 128 |
^ 0 |
1 | 129 |
]. |
130 |
^ stop - start // step + 1 |
|
131 |
! |
|
132 |
||
606 | 133 |
start |
134 |
"return the first number of the range" |
|
2 | 135 |
|
606 | 136 |
^ start |
137 |
! |
|
138 |
||
139 |
start:aNumber |
|
140 |
"set the first number of the range" |
|
141 |
||
142 |
start := aNumber |
|
1 | 143 |
! |
144 |
||
606 | 145 |
step |
146 |
"return the step increment of the range. |
|
147 |
OBSOLETE: |
|
148 |
Please use #increment for ST-80 compatibility." |
|
149 |
||
150 |
^ step |
|
151 |
! |
|
152 |
||
153 |
step:aNumber |
|
154 |
"set the step increment of the range" |
|
2 | 155 |
|
606 | 156 |
step := aNumber |
157 |
! |
|
158 |
||
159 |
stop |
|
160 |
"return the end number of the range" |
|
161 |
||
162 |
^ stop |
|
163 |
! |
|
164 |
||
165 |
stop:aNumber |
|
166 |
"set the end number of the range" |
|
167 |
||
168 |
stop := aNumber |
|
1 | 169 |
! ! |
170 |
||
4155 | 171 |
!Interval methodsFor:'adding & removing'! |
1 | 172 |
|
173 |
add:newObject |
|
1998 | 174 |
"{ Pragma: +optSpace }" |
175 |
||
2 | 176 |
"catch add message - intervals cannot add elements" |
177 |
||
1 | 178 |
self error:'elements cannot be added to an interval' |
179 |
! |
|
180 |
||
181 |
remove:anObject |
|
1998 | 182 |
"{ Pragma: +optSpace }" |
183 |
||
2 | 184 |
"catch remove message - intervals cannot remove elements" |
185 |
||
1 | 186 |
self error:'elements cannot be removed from an interval' |
187 |
! ! |
|
188 |
||
606 | 189 |
!Interval methodsFor:'enumerating'! |
39 | 190 |
|
606 | 191 |
collect:aBlock |
192 |
"evaluate the argument, aBlock for every element in the collection |
|
193 |
and return a collection of the results - Redefined since SeqColl |
|
194 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 195 |
|
606 | 196 |
|newCollection| |
44 | 197 |
|
606 | 198 |
newCollection := self species new:(self size). |
199 |
self do:[:each | |
|
200 |
newCollection add:(aBlock value:each) |
|
44 | 201 |
]. |
606 | 202 |
^ newCollection |
44 | 203 |
|
269 | 204 |
" |
606 | 205 |
(1 to:20) collect:[:i | i*i] |
269 | 206 |
" |
44 | 207 |
! |
208 |
||
1 | 209 |
do:aBlock |
2 | 210 |
"evaluate the argument, aBlock for every element in the |
269 | 211 |
receiver-interval. |
212 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
213 |
slow for intervals." |
|
2 | 214 |
|
3986 | 215 |
|aValue iter| |
1 | 216 |
|
217 |
aValue := start. |
|
3986 | 218 |
aValue isInteger ifTrue:[ |
219 |
step < 0 ifTrue:[ |
|
220 |
[stop <= aValue] whileTrue:[ |
|
221 |
aBlock value:aValue. |
|
222 |
aValue := aValue + step |
|
223 |
] |
|
224 |
] ifFalse:[ |
|
225 |
[stop >= aValue] whileTrue:[ |
|
226 |
aBlock value:aValue. |
|
227 |
aValue := aValue + step |
|
228 |
] |
|
229 |
] |
|
1 | 230 |
] ifFalse:[ |
3986 | 231 |
"/ the code below avoids rounding errors |
232 |
"/ to accumulate if floats are enumerated. |
|
233 |
iter := 1. |
|
234 |
step < 0 ifTrue:[ |
|
235 |
[stop <= aValue] whileTrue:[ |
|
236 |
aBlock value:aValue. |
|
237 |
aValue := start + (iter * step). |
|
238 |
iter := iter + 1. |
|
239 |
] |
|
240 |
] ifFalse:[ |
|
241 |
[stop >= aValue] whileTrue:[ |
|
242 |
aBlock value:aValue. |
|
243 |
aValue := start + (iter * step). |
|
244 |
iter := iter + 1. |
|
245 |
] |
|
246 |
] |
|
1 | 247 |
] |
3986 | 248 |
|
249 |
" |
|
250 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
251 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
252 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
253 |
" |
|
1 | 254 |
! |
255 |
||
256 |
select:aBlock |
|
257 |
"evaluate the argument, aBlock for every element in the collection |
|
258 |
and return a collection of all elements for which the block return |
|
269 | 259 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 260 |
slow for intervals." |
261 |
||
262 |
|newColl| |
|
263 |
||
54 | 264 |
newColl := self species new:(self size). |
1 | 265 |
self do:[:each | |
216 | 266 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 267 |
]. |
268 |
^ newColl |
|
269 | 269 |
|
270 |
" |
|
271 |
(1 to:20) select:[:i | i even] |
|
272 |
" |
|
606 | 273 |
! ! |
274 |
||
275 |
!Interval methodsFor:'printing & storing'! |
|
276 |
||
277 |
printOn:aStream |
|
278 |
"append a printed representation to aStream" |
|
279 |
||
280 |
start printOn:aStream. |
|
281 |
aStream nextPutAll:' to:'. |
|
282 |
stop printOn:aStream. |
|
283 |
step ~= 1 ifTrue:[ |
|
284 |
aStream nextPutAll:' by:'. |
|
285 |
step printOn:aStream. |
|
286 |
]. |
|
287 |
||
288 |
" |
|
289 |
(1 to:10) printOn:Transcript |
|
290 |
(1 to:10 by:2) printOn:Transcript |
|
291 |
(1 to:10) printString |
|
292 |
" |
|
1 | 293 |
! |
294 |
||
606 | 295 |
storeOn:aStream |
296 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 297 |
|
606 | 298 |
aStream nextPut:$(. |
299 |
self printOn:aStream. |
|
300 |
aStream nextPut:$). |
|
269 | 301 |
|
302 |
" |
|
606 | 303 |
(1 to:10) storeOn:Transcript |
304 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 305 |
" |
1 | 306 |
! ! |
606 | 307 |
|
308 |
!Interval methodsFor:'private'! |
|
309 |
||
310 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
311 |
"set start, stop and step components" |
|
312 |
||
313 |
start := startInteger. |
|
314 |
stop := stopInteger. |
|
315 |
step := stepInteger |
|
316 |
! |
|
317 |
||
318 |
species |
|
319 |
"return the type of collection to be returned by collect, select etc." |
|
320 |
||
321 |
^ OrderedCollection |
|
322 |
! ! |
|
323 |
||
629 | 324 |
!Interval class methodsFor:'documentation'! |
325 |
||
326 |
version |
|
4155 | 327 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.25 1999-05-08 13:25:13 cg Exp $' |
629 | 328 |
! ! |