author | Claus Gittinger <cg@exept.de> |
Thu, 19 Feb 2004 15:13:00 +0100 | |
changeset 7970 | 2ae69eb663eb |
parent 7823 | 4a31fa10c6eb |
child 8374 | a5d76d47cc70 |
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 |
|
6132 | 15 |
ReadOnlySequenceableCollection 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 |
||
5672 | 83 |
!Interval class methodsFor:'converting'! |
84 |
||
85 |
decodeFromLiteralArray:anArray |
|
86 |
"create & return a new instance from information encoded in anArray. |
|
87 |
Re-redefined, since the implementation in SeqColl creates instances with an initial |
|
88 |
size, which is not allowed for intevals." |
|
89 |
||
6132 | 90 |
^ self new fromLiteralArrayEncoding:anArray. |
91 |
! ! |
|
5672 | 92 |
|
606 | 93 |
!Interval methodsFor:'accessing'! |
1 | 94 |
|
606 | 95 |
at:index |
96 |
"return (i.e. compute) the index'th element" |
|
2 | 97 |
|
606 | 98 |
(index between:1 and:self size) ifTrue:[ |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
99 |
^ start + (step * (index - 1)) |
606 | 100 |
]. |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
101 |
^ self subscriptBoundsError:index |
606 | 102 |
! |
1 | 103 |
|
104 |
first |
|
105 |
"return the first element of the collection" |
|
106 |
||
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
107 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
108 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
109 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
110 |
]. |
1 | 111 |
^ start |
112 |
! |
|
113 |
||
328 | 114 |
increment |
339 | 115 |
"alias for #step; for ST-80 compatibility" |
328 | 116 |
|
117 |
^ step |
|
118 |
! |
|
119 |
||
606 | 120 |
last |
121 |
"return the last element of the collection" |
|
1 | 122 |
|
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
123 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
124 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
125 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
126 |
]. |
606 | 127 |
^ stop |
1 | 128 |
! |
129 |
||
606 | 130 |
start |
131 |
"return the first number of the range" |
|
2 | 132 |
|
606 | 133 |
^ start |
134 |
! |
|
135 |
||
136 |
start:aNumber |
|
137 |
"set the first number of the range" |
|
138 |
||
139 |
start := aNumber |
|
1 | 140 |
! |
141 |
||
606 | 142 |
step |
143 |
"return the step increment of the range. |
|
144 |
OBSOLETE: |
|
145 |
Please use #increment for ST-80 compatibility." |
|
146 |
||
147 |
^ step |
|
148 |
! |
|
149 |
||
150 |
step:aNumber |
|
151 |
"set the step increment of the range" |
|
2 | 152 |
|
606 | 153 |
step := aNumber |
154 |
! |
|
155 |
||
156 |
stop |
|
157 |
"return the end number of the range" |
|
158 |
||
159 |
^ stop |
|
160 |
! |
|
161 |
||
162 |
stop:aNumber |
|
163 |
"set the end number of the range" |
|
164 |
||
165 |
stop := aNumber |
|
1 | 166 |
! ! |
167 |
||
6745 | 168 |
!Interval methodsFor:'comparing'! |
169 |
||
170 |
= anInterval |
|
171 |
anInterval class == self class ifTrue:[ |
|
172 |
^ start = anInterval start |
|
173 |
and:[ stop = anInterval stop |
|
174 |
and:[ step = anInterval step]] |
|
175 |
]. |
|
176 |
^ super = anInterval |
|
177 |
! |
|
178 |
||
179 |
hash |
|
6748
604366d06326
hash: must be same as SeqColl hash; inherit
Claus Gittinger <cg@exept.de>
parents:
6747
diff
changeset
|
180 |
^ super hash "/ do not redefine: must generate same hash as SeqColl. |
6745 | 181 |
! ! |
182 |
||
5672 | 183 |
!Interval methodsFor:'converting'! |
184 |
||
185 |
fromLiteralArrayEncoding:encoding |
|
186 |
"read my values from an encoding. |
|
187 |
The encoding is supposed to be either of the form: |
|
188 |
(#Interval start stop step) |
|
189 |
This is the reverse operation to #literalArrayEncoding." |
|
190 |
||
191 |
start := (encoding at:2). |
|
192 |
stop := (encoding at:3). |
|
193 |
step := (encoding at:4). |
|
194 |
||
195 |
" |
|
196 |
Interval new fromLiteralArrayEncoding:((1 to:10) literalArrayEncoding) |
|
197 |
Interval new fromLiteralArrayEncoding:((1 to:10 by:2) literalArrayEncoding) |
|
198 |
Interval decodeFromLiteralArray:((1 to:10 by:2) literalArrayEncoding) |
|
6132 | 199 |
" |
200 |
! |
|
5672 | 201 |
|
202 |
literalArrayEncoding |
|
203 |
"encode myself as an array literal, from which a copy of the receiver |
|
204 |
can be reconstructed with #decodeAsLiteralArray." |
|
205 |
||
206 |
^ Array |
|
207 |
with:self class name asSymbol |
|
208 |
with:start |
|
209 |
with:stop |
|
210 |
with:step |
|
211 |
||
212 |
" |
|
213 |
(1 to:10) literalArrayEncoding |
|
214 |
(1 to:10 by:2) literalArrayEncoding |
|
6132 | 215 |
" |
216 |
! ! |
|
5672 | 217 |
|
6747 | 218 |
!Interval methodsFor:'encoding'! |
219 |
||
220 |
encodeOn:anEncoder with:aParameter |
|
221 |
"this is special. Some encoders want to encode this as a sequenceable collection, |
|
222 |
some want to encode a less expensive representation" |
|
223 |
||
224 |
anEncoder encodeInterval:self with:aParameter |
|
225 |
! ! |
|
226 |
||
606 | 227 |
!Interval methodsFor:'enumerating'! |
39 | 228 |
|
606 | 229 |
collect:aBlock |
230 |
"evaluate the argument, aBlock for every element in the collection |
|
231 |
and return a collection of the results - Redefined since SeqColl |
|
232 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 233 |
|
606 | 234 |
|newCollection| |
44 | 235 |
|
606 | 236 |
newCollection := self species new:(self size). |
237 |
self do:[:each | |
|
238 |
newCollection add:(aBlock value:each) |
|
44 | 239 |
]. |
606 | 240 |
^ newCollection |
44 | 241 |
|
269 | 242 |
" |
606 | 243 |
(1 to:20) collect:[:i | i*i] |
269 | 244 |
" |
44 | 245 |
! |
246 |
||
1 | 247 |
do:aBlock |
2 | 248 |
"evaluate the argument, aBlock for every element in the |
269 | 249 |
receiver-interval. |
250 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
251 |
slow for intervals." |
|
2 | 252 |
|
3986 | 253 |
|aValue iter| |
1 | 254 |
|
255 |
aValue := start. |
|
3986 | 256 |
aValue isInteger ifTrue:[ |
257 |
step < 0 ifTrue:[ |
|
258 |
[stop <= aValue] whileTrue:[ |
|
259 |
aBlock value:aValue. |
|
260 |
aValue := aValue + step |
|
261 |
] |
|
262 |
] ifFalse:[ |
|
263 |
[stop >= aValue] whileTrue:[ |
|
264 |
aBlock value:aValue. |
|
265 |
aValue := aValue + step |
|
266 |
] |
|
267 |
] |
|
1 | 268 |
] ifFalse:[ |
3986 | 269 |
"/ the code below avoids rounding errors |
270 |
"/ to accumulate if floats are enumerated. |
|
271 |
iter := 1. |
|
272 |
step < 0 ifTrue:[ |
|
273 |
[stop <= aValue] whileTrue:[ |
|
274 |
aBlock value:aValue. |
|
275 |
aValue := start + (iter * step). |
|
276 |
iter := iter + 1. |
|
277 |
] |
|
278 |
] ifFalse:[ |
|
279 |
[stop >= aValue] whileTrue:[ |
|
280 |
aBlock value:aValue. |
|
281 |
aValue := start + (iter * step). |
|
282 |
iter := iter + 1. |
|
283 |
] |
|
284 |
] |
|
1 | 285 |
] |
3986 | 286 |
|
287 |
" |
|
288 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
289 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
290 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
6532 | 291 |
$a to:$z do:[:v | Transcript showCR:v] |
3986 | 292 |
" |
1 | 293 |
! |
294 |
||
295 |
select:aBlock |
|
296 |
"evaluate the argument, aBlock for every element in the collection |
|
297 |
and return a collection of all elements for which the block return |
|
269 | 298 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 299 |
slow for intervals." |
300 |
||
301 |
|newColl| |
|
302 |
||
54 | 303 |
newColl := self species new:(self size). |
1 | 304 |
self do:[:each | |
216 | 305 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 306 |
]. |
307 |
^ newColl |
|
269 | 308 |
|
309 |
" |
|
310 |
(1 to:20) select:[:i | i even] |
|
311 |
" |
|
606 | 312 |
! ! |
313 |
||
314 |
!Interval methodsFor:'printing & storing'! |
|
315 |
||
6709 | 316 |
displayString |
317 |
^ self printString |
|
318 |
||
319 |
" |
|
320 |
(1 to:10) |
|
321 |
(1 to:10 by:2) |
|
322 |
" |
|
323 |
! |
|
324 |
||
606 | 325 |
printOn:aStream |
326 |
"append a printed representation to aStream" |
|
327 |
||
328 |
start printOn:aStream. |
|
329 |
aStream nextPutAll:' to:'. |
|
330 |
stop printOn:aStream. |
|
331 |
step ~= 1 ifTrue:[ |
|
332 |
aStream nextPutAll:' by:'. |
|
333 |
step printOn:aStream. |
|
334 |
]. |
|
335 |
||
336 |
" |
|
337 |
(1 to:10) printOn:Transcript |
|
338 |
(1 to:10 by:2) printOn:Transcript |
|
339 |
(1 to:10) printString |
|
340 |
" |
|
1 | 341 |
! |
342 |
||
606 | 343 |
storeOn:aStream |
344 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 345 |
|
606 | 346 |
aStream nextPut:$(. |
347 |
self printOn:aStream. |
|
348 |
aStream nextPut:$). |
|
269 | 349 |
|
350 |
" |
|
606 | 351 |
(1 to:10) storeOn:Transcript |
352 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 353 |
" |
1 | 354 |
! ! |
606 | 355 |
|
356 |
!Interval methodsFor:'private'! |
|
357 |
||
358 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
359 |
"set start, stop and step components" |
|
360 |
||
361 |
start := startInteger. |
|
362 |
stop := stopInteger. |
|
363 |
step := stepInteger |
|
364 |
! |
|
365 |
||
366 |
species |
|
367 |
"return the type of collection to be returned by collect, select etc." |
|
368 |
||
369 |
^ OrderedCollection |
|
370 |
! ! |
|
371 |
||
5557 | 372 |
!Interval methodsFor:'queries'! |
373 |
||
7823 | 374 |
max |
375 |
"return the maximum value in the receiver collection, |
|
376 |
redefined, since this can be easily computed. |
|
377 |
Raises an error, if the receiver is empty." |
|
378 |
||
379 |
step == 1 ifTrue:[stop >= start ifTrue:[^ stop]]. |
|
380 |
step < 0 ifTrue:[stop <= start ifTrue:[^ start]]. |
|
381 |
^ super max |
|
382 |
||
383 |
" |
|
384 |
(0 to:15) max |
|
385 |
(0 to:15 by:2) max |
|
386 |
(0 to:15 by:8) max |
|
387 |
(15 to:0) max -> error |
|
388 |
(15 to:0 by:4) max -> error |
|
389 |
(-1 to:-15 by:-1) max |
|
390 |
(-1 to:-15 by:-4) max |
|
391 |
(-1 to:15 by:-1) max -> error |
|
392 |
" |
|
393 |
! |
|
394 |
||
395 |
min |
|
396 |
"return the minimum value in the receiver collection, |
|
397 |
redefined, since this can be easily computed. |
|
398 |
Raises an error, if the receiver is empty." |
|
399 |
||
400 |
step == -1 ifTrue:[stop <= start ifTrue:[^ stop]]. |
|
401 |
step > 0 ifTrue:[stop >= start ifTrue:[^ start]]. |
|
402 |
^ super min |
|
403 |
||
404 |
" |
|
405 |
(0 to:15) min |
|
406 |
(0 to:15 by:2) min |
|
407 |
(0 to:15 by:8) min |
|
408 |
(15 to:0) min -> error |
|
409 |
(15 to:0 by:4) min -> error |
|
410 |
(-1 to:-15 by:-1) min |
|
411 |
(-1 to:-15 by:-4) min |
|
412 |
(-1 to:15 by:-1) min -> error |
|
413 |
" |
|
414 |
! |
|
415 |
||
416 |
minMax |
|
417 |
"return the minimum and maximum values in the receiver collection |
|
418 |
as a two element array. |
|
419 |
Raises an error, if the receiver is empty." |
|
420 |
||
421 |
step == -1 ifTrue:[ |
|
422 |
stop <= start ifTrue:[ |
|
423 |
^ Array with:stop with:start |
|
424 |
] |
|
425 |
]. |
|
426 |
step == 1 ifTrue:[ |
|
427 |
stop >= start ifTrue:[ |
|
428 |
^ Array with:start with:stop |
|
429 |
] |
|
430 |
]. |
|
431 |
^ super minMax |
|
432 |
||
433 |
" |
|
434 |
(0 to:15) minMax |
|
435 |
(0 to:15 by:2) minMax |
|
436 |
(0 to:15 by:8) minMax |
|
437 |
(15 to:0) minMax -> error |
|
438 |
(15 to:0 by:4) minMax -> error |
|
439 |
(-1 to:-15 by:-1) minMax |
|
440 |
(-1 to:-15 by:-4) minMax |
|
441 |
(-1 to:15 by:-1) minMax -> error |
|
442 |
" |
|
443 |
! |
|
444 |
||
5557 | 445 |
size |
446 |
"return the number of elements in the collection" |
|
447 |
||
448 |
(step < 0) ifTrue:[ |
|
449 |
(start < stop) ifTrue:[ |
|
450 |
^ 0 |
|
451 |
]. |
|
452 |
^ stop - start // step + 1 |
|
453 |
]. |
|
454 |
(stop < start) ifTrue:[ |
|
455 |
^ 0 |
|
456 |
]. |
|
457 |
^ stop - start // step + 1 |
|
458 |
! ! |
|
459 |
||
6671 | 460 |
!Interval methodsFor:'testing'! |
461 |
||
6712 | 462 |
includes:anElement |
463 |
|rest| |
|
464 |
stop >= start ifTrue:[ |
|
465 |
(anElement between:start and:stop) ifFalse:[^ false]. |
|
466 |
] ifFalse:[ |
|
467 |
(anElement between:stop and:start) ifFalse:[^ false]. |
|
468 |
]. |
|
469 |
rest := (anElement - start) rem:step. |
|
470 |
^ rest = 0 |
|
471 |
||
472 |
" |
|
473 |
(1 to:15) includes:0 |
|
474 |
(1 to:15) includes:16 |
|
475 |
(1 to:15) includes:1 |
|
476 |
(1 to:15) includes:15 |
|
477 |
(1 to:15) includes:5 |
|
478 |
(1 to:15) includes:14 |
|
479 |
(1 to:15) includes:4.0 |
|
480 |
(1 to:15) includes:4.4 |
|
481 |
||
482 |
(1 to:15 by:3) includes:0 |
|
483 |
(1 to:15 by:3) includes:16 |
|
484 |
(1 to:15 by:3) includes:1 |
|
485 |
(1 to:15 by:3) includes:15 |
|
486 |
(1 to:15 by:3) includes:5 |
|
487 |
(1 to:15 by:3) includes:4 |
|
488 |
(1 to:15 by:3) includes:13 |
|
489 |
(1 to:15 by:3) includes:14 |
|
490 |
(1 to:15 by:3) includes:4.0 |
|
491 |
(1 to:15 by:3) includes:4.4 |
|
492 |
||
493 |
(10 to:-10 by:-3) includes:11 |
|
494 |
(10 to:-10 by:-3) includes:10 |
|
495 |
(10 to:-10 by:-3) includes:9 |
|
496 |
(10 to:-10 by:-3) includes:8 |
|
497 |
(10 to:-10 by:-3) includes:7 |
|
498 |
(10 to:-10 by:-3) includes:4 |
|
499 |
(10 to:-10 by:-3) includes:0 |
|
500 |
(10 to:-10 by:-3) includes:-1 |
|
501 |
(10 to:-10 by:-3) includes:-2 |
|
502 |
(10 to:-10 by:-3) includes:-8 |
|
503 |
(10 to:-10 by:-3) includes:-9 |
|
504 |
(10 to:-10 by:-3) includes:-10 |
|
505 |
(10 to:-10 by:-3) includes:-11 |
|
506 |
(10 to:-10 by:-3) includes:-2.4 |
|
507 |
||
508 |
(-10 to:-20 by:-2) includes:-16 |
|
509 |
(-10 to:-20 by:-2) includes:-20 |
|
510 |
(-10 to:-20 by:-2) includes:-23 |
|
511 |
(-10 to:-20 by:-2) includes:-24 |
|
512 |
||
513 |
" |
|
6671 | 514 |
! ! |
515 |
||
629 | 516 |
!Interval class methodsFor:'documentation'! |
517 |
||
518 |
version |
|
7823 | 519 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.39 2004-01-07 12:51:18 cg Exp $' |
629 | 520 |
! ! |