author | Claus Gittinger <cg@exept.de> |
Tue, 24 Oct 2000 16:31:26 +0200 | |
changeset 5672 | bd82b2b8c930 |
parent 5557 | f5f8d236027c |
child 6132 | 615c4fe0f449 |
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 |
||
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 |
||
90 |
^ self new fromLiteralArrayEncoding:anArray.! ! |
|
91 |
||
606 | 92 |
!Interval methodsFor:'accessing'! |
1 | 93 |
|
606 | 94 |
at:index |
95 |
"return (i.e. compute) the index'th element" |
|
2 | 96 |
|
606 | 97 |
(index between:1 and:self size) ifTrue:[ |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
98 |
^ start + (step * (index - 1)) |
606 | 99 |
]. |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
100 |
^ self subscriptBoundsError:index |
606 | 101 |
! |
1 | 102 |
|
606 | 103 |
at:index put:anObject |
1998 | 104 |
"{ Pragma: +optSpace }" |
105 |
||
606 | 106 |
"catch at:put: message - intervals cannot store elements" |
107 |
||
108 |
self error:'you cannot store into an interval' |
|
109 |
! |
|
1 | 110 |
|
111 |
first |
|
112 |
"return the first element of the collection" |
|
113 |
||
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
114 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
115 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
116 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
117 |
]. |
1 | 118 |
^ start |
119 |
! |
|
120 |
||
328 | 121 |
increment |
339 | 122 |
"alias for #step; for ST-80 compatibility" |
328 | 123 |
|
124 |
^ step |
|
125 |
! |
|
126 |
||
606 | 127 |
last |
128 |
"return the last element of the collection" |
|
1 | 129 |
|
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
130 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
131 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
132 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
133 |
]. |
606 | 134 |
^ stop |
1 | 135 |
! |
136 |
||
606 | 137 |
start |
138 |
"return the first number of the range" |
|
2 | 139 |
|
606 | 140 |
^ start |
141 |
! |
|
142 |
||
143 |
start:aNumber |
|
144 |
"set the first number of the range" |
|
145 |
||
146 |
start := aNumber |
|
1 | 147 |
! |
148 |
||
606 | 149 |
step |
150 |
"return the step increment of the range. |
|
151 |
OBSOLETE: |
|
152 |
Please use #increment for ST-80 compatibility." |
|
153 |
||
154 |
^ step |
|
155 |
! |
|
156 |
||
157 |
step:aNumber |
|
158 |
"set the step increment of the range" |
|
2 | 159 |
|
606 | 160 |
step := aNumber |
161 |
! |
|
162 |
||
163 |
stop |
|
164 |
"return the end number of the range" |
|
165 |
||
166 |
^ stop |
|
167 |
! |
|
168 |
||
169 |
stop:aNumber |
|
170 |
"set the end number of the range" |
|
171 |
||
172 |
stop := aNumber |
|
1 | 173 |
! ! |
174 |
||
4155 | 175 |
!Interval methodsFor:'adding & removing'! |
1 | 176 |
|
177 |
add:newObject |
|
1998 | 178 |
"{ Pragma: +optSpace }" |
179 |
||
2 | 180 |
"catch add message - intervals cannot add elements" |
181 |
||
1 | 182 |
self error:'elements cannot be added to an interval' |
183 |
! |
|
184 |
||
185 |
remove:anObject |
|
1998 | 186 |
"{ Pragma: +optSpace }" |
187 |
||
2 | 188 |
"catch remove message - intervals cannot remove elements" |
189 |
||
1 | 190 |
self error:'elements cannot be removed from an interval' |
191 |
! ! |
|
192 |
||
5672 | 193 |
!Interval methodsFor:'converting'! |
194 |
||
195 |
fromLiteralArrayEncoding:encoding |
|
196 |
"read my values from an encoding. |
|
197 |
The encoding is supposed to be either of the form: |
|
198 |
(#Interval start stop step) |
|
199 |
This is the reverse operation to #literalArrayEncoding." |
|
200 |
||
201 |
start := (encoding at:2). |
|
202 |
stop := (encoding at:3). |
|
203 |
step := (encoding at:4). |
|
204 |
||
205 |
" |
|
206 |
Interval new fromLiteralArrayEncoding:((1 to:10) literalArrayEncoding) |
|
207 |
Interval new fromLiteralArrayEncoding:((1 to:10 by:2) literalArrayEncoding) |
|
208 |
Interval decodeFromLiteralArray:((1 to:10 by:2) literalArrayEncoding) |
|
209 |
"! |
|
210 |
||
211 |
literalArrayEncoding |
|
212 |
"encode myself as an array literal, from which a copy of the receiver |
|
213 |
can be reconstructed with #decodeAsLiteralArray." |
|
214 |
||
215 |
^ Array |
|
216 |
with:self class name asSymbol |
|
217 |
with:start |
|
218 |
with:stop |
|
219 |
with:step |
|
220 |
||
221 |
" |
|
222 |
(1 to:10) literalArrayEncoding |
|
223 |
(1 to:10 by:2) literalArrayEncoding |
|
224 |
"! ! |
|
225 |
||
606 | 226 |
!Interval methodsFor:'enumerating'! |
39 | 227 |
|
606 | 228 |
collect:aBlock |
229 |
"evaluate the argument, aBlock for every element in the collection |
|
230 |
and return a collection of the results - Redefined since SeqColl |
|
231 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 232 |
|
606 | 233 |
|newCollection| |
44 | 234 |
|
606 | 235 |
newCollection := self species new:(self size). |
236 |
self do:[:each | |
|
237 |
newCollection add:(aBlock value:each) |
|
44 | 238 |
]. |
606 | 239 |
^ newCollection |
44 | 240 |
|
269 | 241 |
" |
606 | 242 |
(1 to:20) collect:[:i | i*i] |
269 | 243 |
" |
44 | 244 |
! |
245 |
||
1 | 246 |
do:aBlock |
2 | 247 |
"evaluate the argument, aBlock for every element in the |
269 | 248 |
receiver-interval. |
249 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
250 |
slow for intervals." |
|
2 | 251 |
|
3986 | 252 |
|aValue iter| |
1 | 253 |
|
254 |
aValue := start. |
|
3986 | 255 |
aValue isInteger ifTrue:[ |
256 |
step < 0 ifTrue:[ |
|
257 |
[stop <= aValue] whileTrue:[ |
|
258 |
aBlock value:aValue. |
|
259 |
aValue := aValue + step |
|
260 |
] |
|
261 |
] ifFalse:[ |
|
262 |
[stop >= aValue] whileTrue:[ |
|
263 |
aBlock value:aValue. |
|
264 |
aValue := aValue + step |
|
265 |
] |
|
266 |
] |
|
1 | 267 |
] ifFalse:[ |
3986 | 268 |
"/ the code below avoids rounding errors |
269 |
"/ to accumulate if floats are enumerated. |
|
270 |
iter := 1. |
|
271 |
step < 0 ifTrue:[ |
|
272 |
[stop <= aValue] whileTrue:[ |
|
273 |
aBlock value:aValue. |
|
274 |
aValue := start + (iter * step). |
|
275 |
iter := iter + 1. |
|
276 |
] |
|
277 |
] ifFalse:[ |
|
278 |
[stop >= aValue] whileTrue:[ |
|
279 |
aBlock value:aValue. |
|
280 |
aValue := start + (iter * step). |
|
281 |
iter := iter + 1. |
|
282 |
] |
|
283 |
] |
|
1 | 284 |
] |
3986 | 285 |
|
286 |
" |
|
287 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
288 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
289 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
290 |
" |
|
1 | 291 |
! |
292 |
||
293 |
select:aBlock |
|
294 |
"evaluate the argument, aBlock for every element in the collection |
|
295 |
and return a collection of all elements for which the block return |
|
269 | 296 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 297 |
slow for intervals." |
298 |
||
299 |
|newColl| |
|
300 |
||
54 | 301 |
newColl := self species new:(self size). |
1 | 302 |
self do:[:each | |
216 | 303 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 304 |
]. |
305 |
^ newColl |
|
269 | 306 |
|
307 |
" |
|
308 |
(1 to:20) select:[:i | i even] |
|
309 |
" |
|
606 | 310 |
! ! |
311 |
||
312 |
!Interval methodsFor:'printing & storing'! |
|
313 |
||
314 |
printOn:aStream |
|
315 |
"append a printed representation to aStream" |
|
316 |
||
317 |
start printOn:aStream. |
|
318 |
aStream nextPutAll:' to:'. |
|
319 |
stop printOn:aStream. |
|
320 |
step ~= 1 ifTrue:[ |
|
321 |
aStream nextPutAll:' by:'. |
|
322 |
step printOn:aStream. |
|
323 |
]. |
|
324 |
||
325 |
" |
|
326 |
(1 to:10) printOn:Transcript |
|
327 |
(1 to:10 by:2) printOn:Transcript |
|
328 |
(1 to:10) printString |
|
329 |
" |
|
1 | 330 |
! |
331 |
||
606 | 332 |
storeOn:aStream |
333 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 334 |
|
606 | 335 |
aStream nextPut:$(. |
336 |
self printOn:aStream. |
|
337 |
aStream nextPut:$). |
|
269 | 338 |
|
339 |
" |
|
606 | 340 |
(1 to:10) storeOn:Transcript |
341 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 342 |
" |
1 | 343 |
! ! |
606 | 344 |
|
345 |
!Interval methodsFor:'private'! |
|
346 |
||
347 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
348 |
"set start, stop and step components" |
|
349 |
||
350 |
start := startInteger. |
|
351 |
stop := stopInteger. |
|
352 |
step := stepInteger |
|
353 |
! |
|
354 |
||
355 |
species |
|
356 |
"return the type of collection to be returned by collect, select etc." |
|
357 |
||
358 |
^ OrderedCollection |
|
359 |
! ! |
|
360 |
||
5557 | 361 |
!Interval methodsFor:'queries'! |
362 |
||
363 |
size |
|
364 |
"return the number of elements in the collection" |
|
365 |
||
366 |
(step < 0) ifTrue:[ |
|
367 |
(start < stop) ifTrue:[ |
|
368 |
^ 0 |
|
369 |
]. |
|
370 |
^ stop - start // step + 1 |
|
371 |
]. |
|
372 |
(stop < start) ifTrue:[ |
|
373 |
^ 0 |
|
374 |
]. |
|
375 |
^ stop - start // step + 1 |
|
376 |
! ! |
|
377 |
||
629 | 378 |
!Interval class methodsFor:'documentation'! |
379 |
||
380 |
version |
|
5672 | 381 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.28 2000-10-24 14:31:26 cg Exp $' |
629 | 382 |
! ! |