author | Claus Gittinger <cg@exept.de> |
Mon, 07 Jun 2004 11:02:59 +0200 | |
changeset 8374 | a5d76d47cc70 |
parent 7823 | 4a31fa10c6eb |
child 8394 | da194de43766 |
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 |
|
8374 | 218 |
!Interval methodsFor:'converting-reindexed'! |
219 |
||
220 |
from:startIndex |
|
221 |
"return a new collection representing the receivers elements starting at startIndex." |
|
222 |
||
223 |
step == 1 ifTrue:[ |
|
224 |
^ start+startIndex-1 to:stop |
|
225 |
]. |
|
226 |
"could be more intelligent here" |
|
227 |
^ super from:startIndex |
|
228 |
||
229 |
" |
|
230 |
(1 to:100) from:2 |
|
231 |
||
232 |
(1 to:20 by:2) from:2 |
|
233 |
(1 to:20 by:2) asArray from:2 |
|
234 |
((1 to:20 by:2) from:2) asArray |
|
235 |
" |
|
236 |
! |
|
237 |
||
238 |
to:endIndex |
|
239 |
"return a new collection representing the receivers elements upTo and including endIndex." |
|
240 |
||
241 |
^ start to:(endIndex min:stop) by:step |
|
242 |
||
243 |
" |
|
244 |
(1 to:100) to:50 |
|
245 |
(1 to:100 by:2) to:50 |
|
246 |
" |
|
247 |
! ! |
|
248 |
||
6747 | 249 |
!Interval methodsFor:'encoding'! |
250 |
||
251 |
encodeOn:anEncoder with:aParameter |
|
252 |
"this is special. Some encoders want to encode this as a sequenceable collection, |
|
253 |
some want to encode a less expensive representation" |
|
254 |
||
255 |
anEncoder encodeInterval:self with:aParameter |
|
256 |
! ! |
|
257 |
||
606 | 258 |
!Interval methodsFor:'enumerating'! |
39 | 259 |
|
606 | 260 |
collect:aBlock |
261 |
"evaluate the argument, aBlock for every element in the collection |
|
262 |
and return a collection of the results - Redefined since SeqColl |
|
263 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 264 |
|
606 | 265 |
|newCollection| |
44 | 266 |
|
606 | 267 |
newCollection := self species new:(self size). |
268 |
self do:[:each | |
|
269 |
newCollection add:(aBlock value:each) |
|
44 | 270 |
]. |
606 | 271 |
^ newCollection |
44 | 272 |
|
269 | 273 |
" |
606 | 274 |
(1 to:20) collect:[:i | i*i] |
269 | 275 |
" |
44 | 276 |
! |
277 |
||
1 | 278 |
do:aBlock |
2 | 279 |
"evaluate the argument, aBlock for every element in the |
269 | 280 |
receiver-interval. |
281 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
282 |
slow for intervals." |
|
2 | 283 |
|
3986 | 284 |
|aValue iter| |
1 | 285 |
|
286 |
aValue := start. |
|
3986 | 287 |
aValue isInteger ifTrue:[ |
288 |
step < 0 ifTrue:[ |
|
289 |
[stop <= aValue] whileTrue:[ |
|
290 |
aBlock value:aValue. |
|
291 |
aValue := aValue + step |
|
292 |
] |
|
293 |
] ifFalse:[ |
|
294 |
[stop >= aValue] whileTrue:[ |
|
295 |
aBlock value:aValue. |
|
296 |
aValue := aValue + step |
|
297 |
] |
|
298 |
] |
|
1 | 299 |
] ifFalse:[ |
3986 | 300 |
"/ the code below avoids rounding errors |
301 |
"/ to accumulate if floats are enumerated. |
|
302 |
iter := 1. |
|
303 |
step < 0 ifTrue:[ |
|
304 |
[stop <= aValue] whileTrue:[ |
|
305 |
aBlock value:aValue. |
|
306 |
aValue := start + (iter * step). |
|
307 |
iter := iter + 1. |
|
308 |
] |
|
309 |
] ifFalse:[ |
|
310 |
[stop >= aValue] whileTrue:[ |
|
311 |
aBlock value:aValue. |
|
312 |
aValue := start + (iter * step). |
|
313 |
iter := iter + 1. |
|
314 |
] |
|
315 |
] |
|
1 | 316 |
] |
3986 | 317 |
|
318 |
" |
|
319 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
320 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
321 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
6532 | 322 |
$a to:$z do:[:v | Transcript showCR:v] |
3986 | 323 |
" |
1 | 324 |
! |
325 |
||
326 |
select:aBlock |
|
327 |
"evaluate the argument, aBlock for every element in the collection |
|
328 |
and return a collection of all elements for which the block return |
|
269 | 329 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 330 |
slow for intervals." |
331 |
||
332 |
|newColl| |
|
333 |
||
54 | 334 |
newColl := self species new:(self size). |
1 | 335 |
self do:[:each | |
216 | 336 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 337 |
]. |
338 |
^ newColl |
|
269 | 339 |
|
340 |
" |
|
341 |
(1 to:20) select:[:i | i even] |
|
342 |
" |
|
606 | 343 |
! ! |
344 |
||
345 |
!Interval methodsFor:'printing & storing'! |
|
346 |
||
6709 | 347 |
displayString |
348 |
^ self printString |
|
349 |
||
350 |
" |
|
351 |
(1 to:10) |
|
352 |
(1 to:10 by:2) |
|
353 |
" |
|
354 |
! |
|
355 |
||
606 | 356 |
printOn:aStream |
357 |
"append a printed representation to aStream" |
|
358 |
||
359 |
start printOn:aStream. |
|
360 |
aStream nextPutAll:' to:'. |
|
361 |
stop printOn:aStream. |
|
362 |
step ~= 1 ifTrue:[ |
|
363 |
aStream nextPutAll:' by:'. |
|
364 |
step printOn:aStream. |
|
365 |
]. |
|
366 |
||
367 |
" |
|
368 |
(1 to:10) printOn:Transcript |
|
369 |
(1 to:10 by:2) printOn:Transcript |
|
370 |
(1 to:10) printString |
|
371 |
" |
|
1 | 372 |
! |
373 |
||
606 | 374 |
storeOn:aStream |
375 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 376 |
|
606 | 377 |
aStream nextPut:$(. |
378 |
self printOn:aStream. |
|
379 |
aStream nextPut:$). |
|
269 | 380 |
|
381 |
" |
|
606 | 382 |
(1 to:10) storeOn:Transcript |
383 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 384 |
" |
1 | 385 |
! ! |
606 | 386 |
|
387 |
!Interval methodsFor:'private'! |
|
388 |
||
389 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
390 |
"set start, stop and step components" |
|
391 |
||
392 |
start := startInteger. |
|
393 |
stop := stopInteger. |
|
394 |
step := stepInteger |
|
395 |
! |
|
396 |
||
397 |
species |
|
398 |
"return the type of collection to be returned by collect, select etc." |
|
399 |
||
400 |
^ OrderedCollection |
|
401 |
! ! |
|
402 |
||
5557 | 403 |
!Interval methodsFor:'queries'! |
404 |
||
7823 | 405 |
max |
406 |
"return the maximum value in the receiver collection, |
|
407 |
redefined, since this can be easily computed. |
|
408 |
Raises an error, if the receiver is empty." |
|
409 |
||
410 |
step == 1 ifTrue:[stop >= start ifTrue:[^ stop]]. |
|
411 |
step < 0 ifTrue:[stop <= start ifTrue:[^ start]]. |
|
412 |
^ super max |
|
413 |
||
414 |
" |
|
415 |
(0 to:15) max |
|
416 |
(0 to:15 by:2) max |
|
417 |
(0 to:15 by:8) max |
|
418 |
(15 to:0) max -> error |
|
419 |
(15 to:0 by:4) max -> error |
|
420 |
(-1 to:-15 by:-1) max |
|
421 |
(-1 to:-15 by:-4) max |
|
422 |
(-1 to:15 by:-1) max -> error |
|
423 |
" |
|
424 |
! |
|
425 |
||
426 |
min |
|
427 |
"return the minimum value in the receiver collection, |
|
428 |
redefined, since this can be easily computed. |
|
429 |
Raises an error, if the receiver is empty." |
|
430 |
||
431 |
step == -1 ifTrue:[stop <= start ifTrue:[^ stop]]. |
|
432 |
step > 0 ifTrue:[stop >= start ifTrue:[^ start]]. |
|
433 |
^ super min |
|
434 |
||
435 |
" |
|
436 |
(0 to:15) min |
|
437 |
(0 to:15 by:2) min |
|
438 |
(0 to:15 by:8) min |
|
439 |
(15 to:0) min -> error |
|
440 |
(15 to:0 by:4) min -> error |
|
441 |
(-1 to:-15 by:-1) min |
|
442 |
(-1 to:-15 by:-4) min |
|
443 |
(-1 to:15 by:-1) min -> error |
|
444 |
" |
|
445 |
! |
|
446 |
||
447 |
minMax |
|
448 |
"return the minimum and maximum values in the receiver collection |
|
449 |
as a two element array. |
|
450 |
Raises an error, if the receiver is empty." |
|
451 |
||
452 |
step == -1 ifTrue:[ |
|
453 |
stop <= start ifTrue:[ |
|
454 |
^ Array with:stop with:start |
|
455 |
] |
|
456 |
]. |
|
457 |
step == 1 ifTrue:[ |
|
458 |
stop >= start ifTrue:[ |
|
459 |
^ Array with:start with:stop |
|
460 |
] |
|
461 |
]. |
|
462 |
^ super minMax |
|
463 |
||
464 |
" |
|
465 |
(0 to:15) minMax |
|
466 |
(0 to:15 by:2) minMax |
|
467 |
(0 to:15 by:8) minMax |
|
468 |
(15 to:0) minMax -> error |
|
469 |
(15 to:0 by:4) minMax -> error |
|
470 |
(-1 to:-15 by:-1) minMax |
|
471 |
(-1 to:-15 by:-4) minMax |
|
472 |
(-1 to:15 by:-1) minMax -> error |
|
473 |
" |
|
474 |
! |
|
475 |
||
5557 | 476 |
size |
477 |
"return the number of elements in the collection" |
|
478 |
||
479 |
(step < 0) ifTrue:[ |
|
480 |
(start < stop) ifTrue:[ |
|
481 |
^ 0 |
|
482 |
]. |
|
483 |
^ stop - start // step + 1 |
|
484 |
]. |
|
485 |
(stop < start) ifTrue:[ |
|
486 |
^ 0 |
|
487 |
]. |
|
488 |
^ stop - start // step + 1 |
|
489 |
! ! |
|
490 |
||
6671 | 491 |
!Interval methodsFor:'testing'! |
492 |
||
6712 | 493 |
includes:anElement |
494 |
|rest| |
|
495 |
stop >= start ifTrue:[ |
|
496 |
(anElement between:start and:stop) ifFalse:[^ false]. |
|
497 |
] ifFalse:[ |
|
498 |
(anElement between:stop and:start) ifFalse:[^ false]. |
|
499 |
]. |
|
500 |
rest := (anElement - start) rem:step. |
|
501 |
^ rest = 0 |
|
502 |
||
503 |
" |
|
504 |
(1 to:15) includes:0 |
|
505 |
(1 to:15) includes:16 |
|
506 |
(1 to:15) includes:1 |
|
507 |
(1 to:15) includes:15 |
|
508 |
(1 to:15) includes:5 |
|
509 |
(1 to:15) includes:14 |
|
510 |
(1 to:15) includes:4.0 |
|
511 |
(1 to:15) includes:4.4 |
|
512 |
||
513 |
(1 to:15 by:3) includes:0 |
|
514 |
(1 to:15 by:3) includes:16 |
|
515 |
(1 to:15 by:3) includes:1 |
|
516 |
(1 to:15 by:3) includes:15 |
|
517 |
(1 to:15 by:3) includes:5 |
|
518 |
(1 to:15 by:3) includes:4 |
|
519 |
(1 to:15 by:3) includes:13 |
|
520 |
(1 to:15 by:3) includes:14 |
|
521 |
(1 to:15 by:3) includes:4.0 |
|
522 |
(1 to:15 by:3) includes:4.4 |
|
523 |
||
524 |
(10 to:-10 by:-3) includes:11 |
|
525 |
(10 to:-10 by:-3) includes:10 |
|
526 |
(10 to:-10 by:-3) includes:9 |
|
527 |
(10 to:-10 by:-3) includes:8 |
|
528 |
(10 to:-10 by:-3) includes:7 |
|
529 |
(10 to:-10 by:-3) includes:4 |
|
530 |
(10 to:-10 by:-3) includes:0 |
|
531 |
(10 to:-10 by:-3) includes:-1 |
|
532 |
(10 to:-10 by:-3) includes:-2 |
|
533 |
(10 to:-10 by:-3) includes:-8 |
|
534 |
(10 to:-10 by:-3) includes:-9 |
|
535 |
(10 to:-10 by:-3) includes:-10 |
|
536 |
(10 to:-10 by:-3) includes:-11 |
|
537 |
(10 to:-10 by:-3) includes:-2.4 |
|
538 |
||
539 |
(-10 to:-20 by:-2) includes:-16 |
|
540 |
(-10 to:-20 by:-2) includes:-20 |
|
541 |
(-10 to:-20 by:-2) includes:-23 |
|
542 |
(-10 to:-20 by:-2) includes:-24 |
|
543 |
||
544 |
" |
|
6671 | 545 |
! ! |
546 |
||
629 | 547 |
!Interval class methodsFor:'documentation'! |
548 |
||
549 |
version |
|
8374 | 550 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.40 2004-06-07 09:02:59 cg Exp $' |
629 | 551 |
! ! |