author | Claus Gittinger <cg@exept.de> |
Sat, 17 Nov 2001 11:10:08 +0100 | |
changeset 6211 | 69f320c220d5 |
parent 6132 | 615c4fe0f449 |
child 6532 | e6bd5b495eee |
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 |
||
5672 | 168 |
!Interval methodsFor:'converting'! |
169 |
||
170 |
fromLiteralArrayEncoding:encoding |
|
171 |
"read my values from an encoding. |
|
172 |
The encoding is supposed to be either of the form: |
|
173 |
(#Interval start stop step) |
|
174 |
This is the reverse operation to #literalArrayEncoding." |
|
175 |
||
176 |
start := (encoding at:2). |
|
177 |
stop := (encoding at:3). |
|
178 |
step := (encoding at:4). |
|
179 |
||
180 |
" |
|
181 |
Interval new fromLiteralArrayEncoding:((1 to:10) literalArrayEncoding) |
|
182 |
Interval new fromLiteralArrayEncoding:((1 to:10 by:2) literalArrayEncoding) |
|
183 |
Interval decodeFromLiteralArray:((1 to:10 by:2) literalArrayEncoding) |
|
6132 | 184 |
" |
185 |
! |
|
5672 | 186 |
|
187 |
literalArrayEncoding |
|
188 |
"encode myself as an array literal, from which a copy of the receiver |
|
189 |
can be reconstructed with #decodeAsLiteralArray." |
|
190 |
||
191 |
^ Array |
|
192 |
with:self class name asSymbol |
|
193 |
with:start |
|
194 |
with:stop |
|
195 |
with:step |
|
196 |
||
197 |
" |
|
198 |
(1 to:10) literalArrayEncoding |
|
199 |
(1 to:10 by:2) literalArrayEncoding |
|
6132 | 200 |
" |
201 |
! ! |
|
5672 | 202 |
|
606 | 203 |
!Interval methodsFor:'enumerating'! |
39 | 204 |
|
606 | 205 |
collect:aBlock |
206 |
"evaluate the argument, aBlock for every element in the collection |
|
207 |
and return a collection of the results - Redefined since SeqColl |
|
208 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 209 |
|
606 | 210 |
|newCollection| |
44 | 211 |
|
606 | 212 |
newCollection := self species new:(self size). |
213 |
self do:[:each | |
|
214 |
newCollection add:(aBlock value:each) |
|
44 | 215 |
]. |
606 | 216 |
^ newCollection |
44 | 217 |
|
269 | 218 |
" |
606 | 219 |
(1 to:20) collect:[:i | i*i] |
269 | 220 |
" |
44 | 221 |
! |
222 |
||
1 | 223 |
do:aBlock |
2 | 224 |
"evaluate the argument, aBlock for every element in the |
269 | 225 |
receiver-interval. |
226 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
227 |
slow for intervals." |
|
2 | 228 |
|
3986 | 229 |
|aValue iter| |
1 | 230 |
|
231 |
aValue := start. |
|
3986 | 232 |
aValue isInteger ifTrue:[ |
233 |
step < 0 ifTrue:[ |
|
234 |
[stop <= aValue] whileTrue:[ |
|
235 |
aBlock value:aValue. |
|
236 |
aValue := aValue + step |
|
237 |
] |
|
238 |
] ifFalse:[ |
|
239 |
[stop >= aValue] whileTrue:[ |
|
240 |
aBlock value:aValue. |
|
241 |
aValue := aValue + step |
|
242 |
] |
|
243 |
] |
|
1 | 244 |
] ifFalse:[ |
3986 | 245 |
"/ the code below avoids rounding errors |
246 |
"/ to accumulate if floats are enumerated. |
|
247 |
iter := 1. |
|
248 |
step < 0 ifTrue:[ |
|
249 |
[stop <= aValue] whileTrue:[ |
|
250 |
aBlock value:aValue. |
|
251 |
aValue := start + (iter * step). |
|
252 |
iter := iter + 1. |
|
253 |
] |
|
254 |
] ifFalse:[ |
|
255 |
[stop >= aValue] whileTrue:[ |
|
256 |
aBlock value:aValue. |
|
257 |
aValue := start + (iter * step). |
|
258 |
iter := iter + 1. |
|
259 |
] |
|
260 |
] |
|
1 | 261 |
] |
3986 | 262 |
|
263 |
" |
|
264 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
265 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
266 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
267 |
" |
|
1 | 268 |
! |
269 |
||
270 |
select:aBlock |
|
271 |
"evaluate the argument, aBlock for every element in the collection |
|
272 |
and return a collection of all elements for which the block return |
|
269 | 273 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 274 |
slow for intervals." |
275 |
||
276 |
|newColl| |
|
277 |
||
54 | 278 |
newColl := self species new:(self size). |
1 | 279 |
self do:[:each | |
216 | 280 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 281 |
]. |
282 |
^ newColl |
|
269 | 283 |
|
284 |
" |
|
285 |
(1 to:20) select:[:i | i even] |
|
286 |
" |
|
606 | 287 |
! ! |
288 |
||
289 |
!Interval methodsFor:'printing & storing'! |
|
290 |
||
291 |
printOn:aStream |
|
292 |
"append a printed representation to aStream" |
|
293 |
||
294 |
start printOn:aStream. |
|
295 |
aStream nextPutAll:' to:'. |
|
296 |
stop printOn:aStream. |
|
297 |
step ~= 1 ifTrue:[ |
|
298 |
aStream nextPutAll:' by:'. |
|
299 |
step printOn:aStream. |
|
300 |
]. |
|
301 |
||
302 |
" |
|
303 |
(1 to:10) printOn:Transcript |
|
304 |
(1 to:10 by:2) printOn:Transcript |
|
305 |
(1 to:10) printString |
|
306 |
" |
|
1 | 307 |
! |
308 |
||
606 | 309 |
storeOn:aStream |
310 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 311 |
|
606 | 312 |
aStream nextPut:$(. |
313 |
self printOn:aStream. |
|
314 |
aStream nextPut:$). |
|
269 | 315 |
|
316 |
" |
|
606 | 317 |
(1 to:10) storeOn:Transcript |
318 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 319 |
" |
1 | 320 |
! ! |
606 | 321 |
|
322 |
!Interval methodsFor:'private'! |
|
323 |
||
324 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
325 |
"set start, stop and step components" |
|
326 |
||
327 |
start := startInteger. |
|
328 |
stop := stopInteger. |
|
329 |
step := stepInteger |
|
330 |
! |
|
331 |
||
332 |
species |
|
333 |
"return the type of collection to be returned by collect, select etc." |
|
334 |
||
335 |
^ OrderedCollection |
|
336 |
! ! |
|
337 |
||
5557 | 338 |
!Interval methodsFor:'queries'! |
339 |
||
340 |
size |
|
341 |
"return the number of elements in the collection" |
|
342 |
||
343 |
(step < 0) ifTrue:[ |
|
344 |
(start < stop) ifTrue:[ |
|
345 |
^ 0 |
|
346 |
]. |
|
347 |
^ stop - start // step + 1 |
|
348 |
]. |
|
349 |
(stop < start) ifTrue:[ |
|
350 |
^ 0 |
|
351 |
]. |
|
352 |
^ stop - start // step + 1 |
|
353 |
! ! |
|
354 |
||
629 | 355 |
!Interval class methodsFor:'documentation'! |
356 |
||
357 |
version |
|
6132 | 358 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.29 2001-10-31 14:57:48 cg Exp $' |
629 | 359 |
! ! |