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