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