author | Claus Gittinger <cg@exept.de> |
Sat, 16 Dec 1995 14:04:33 +0100 | |
changeset 778 | afbb3ebde874 |
parent 759 | 908363ce8a32 |
child 1290 | 15ba3221b89b |
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 |
|
606 | 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 |
||
46 |
(1 to:10) do:[:i | Transcript showCr:i] |
|
47 |
||
48 |
notice, that this is semantically equivalent to: |
|
49 |
||
50 |
1 to:10 do:[:i | Transcript showCr:i] |
|
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 |
||
56 |
(1 to:10) asArray |
|
57 |
||
58 |
(1 to:10 by:2) asOrderedCollection |
|
88 | 59 |
" |
60 |
! ! |
|
1 | 61 |
|
62 |
!Interval class methodsFor:'instance creation'! |
|
63 |
||
64 |
from:start to:stop |
|
65 |
"return a new interval with elements from start |
|
66 |
to stop by 1" |
|
67 |
||
2 | 68 |
^ self new setFrom:start to:stop by:1 |
1 | 69 |
! |
70 |
||
71 |
from:start to:stop by:step |
|
72 |
"return a new interval with elements from start |
|
73 |
to stop by step" |
|
74 |
||
2 | 75 |
^ self new setFrom:start to:stop by:step |
1 | 76 |
! ! |
77 |
||
606 | 78 |
!Interval methodsFor:'accessing'! |
1 | 79 |
|
606 | 80 |
at:index |
81 |
"return (i.e. compute) the index'th element" |
|
2 | 82 |
|
606 | 83 |
(index between:1 and:self size) ifTrue:[ |
84 |
^ start + (step * (index - 1)) |
|
85 |
]. |
|
86 |
self errorSubscriptBounds:index |
|
87 |
! |
|
1 | 88 |
|
606 | 89 |
at:index put:anObject |
90 |
"catch at:put: message - intervals cannot store elements" |
|
91 |
||
92 |
self error:'you cannot store into an interval' |
|
93 |
! |
|
1 | 94 |
|
95 |
first |
|
96 |
"return the first element of the collection" |
|
97 |
||
98 |
^ start |
|
99 |
! |
|
100 |
||
328 | 101 |
increment |
339 | 102 |
"alias for #step; for ST-80 compatibility" |
328 | 103 |
|
104 |
^ step |
|
105 |
! |
|
106 |
||
606 | 107 |
last |
108 |
"return the last element of the collection" |
|
1 | 109 |
|
606 | 110 |
^ stop |
1 | 111 |
! |
112 |
||
113 |
size |
|
114 |
"return the number of elements in the collection" |
|
115 |
||
116 |
(step < 0) ifTrue:[ |
|
216 | 117 |
(start < stop) ifTrue:[ |
118 |
^ 0 |
|
119 |
]. |
|
120 |
^ stop - start // step + 1 |
|
1 | 121 |
]. |
122 |
(stop < start) ifTrue:[ |
|
216 | 123 |
^ 0 |
1 | 124 |
]. |
125 |
^ stop - start // step + 1 |
|
126 |
! |
|
127 |
||
606 | 128 |
start |
129 |
"return the first number of the range" |
|
2 | 130 |
|
606 | 131 |
^ start |
132 |
! |
|
133 |
||
134 |
start:aNumber |
|
135 |
"set the first number of the range" |
|
136 |
||
137 |
start := aNumber |
|
1 | 138 |
! |
139 |
||
606 | 140 |
step |
141 |
"return the step increment of the range. |
|
142 |
OBSOLETE: |
|
143 |
Please use #increment for ST-80 compatibility." |
|
144 |
||
145 |
^ step |
|
146 |
! |
|
147 |
||
148 |
step:aNumber |
|
149 |
"set the step increment of the range" |
|
2 | 150 |
|
606 | 151 |
step := aNumber |
152 |
! |
|
153 |
||
154 |
stop |
|
155 |
"return the end number of the range" |
|
156 |
||
157 |
^ stop |
|
158 |
! |
|
159 |
||
160 |
stop:aNumber |
|
161 |
"set the end number of the range" |
|
162 |
||
163 |
stop := aNumber |
|
1 | 164 |
! ! |
165 |
||
166 |
!Interval methodsFor:'adding/removing elements'! |
|
167 |
||
168 |
add:newObject |
|
2 | 169 |
"catch add message - intervals cannot add elements" |
170 |
||
1 | 171 |
self error:'elements cannot be added to an interval' |
172 |
! |
|
173 |
||
174 |
remove:anObject |
|
2 | 175 |
"catch remove message - intervals cannot remove elements" |
176 |
||
1 | 177 |
self error:'elements cannot be removed from an interval' |
178 |
! ! |
|
179 |
||
606 | 180 |
!Interval methodsFor:'enumerating'! |
39 | 181 |
|
606 | 182 |
collect:aBlock |
183 |
"evaluate the argument, aBlock for every element in the collection |
|
184 |
and return a collection of the results - Redefined since SeqColl |
|
185 |
accesses the receiver via at:, which is slow for intervals" |
|
2 | 186 |
|
606 | 187 |
|newCollection| |
44 | 188 |
|
606 | 189 |
newCollection := self species new:(self size). |
190 |
self do:[:each | |
|
191 |
newCollection add:(aBlock value:each) |
|
44 | 192 |
]. |
606 | 193 |
^ newCollection |
44 | 194 |
|
269 | 195 |
" |
606 | 196 |
(1 to:20) collect:[:i | i*i] |
269 | 197 |
" |
44 | 198 |
! |
199 |
||
1 | 200 |
do:aBlock |
2 | 201 |
"evaluate the argument, aBlock for every element in the |
269 | 202 |
receiver-interval. |
203 |
Redefined since SeqColl accesses the receiver with at:, which is |
|
204 |
slow for intervals." |
|
2 | 205 |
|
1 | 206 |
|aValue| |
207 |
||
208 |
aValue := start. |
|
209 |
step < 0 ifTrue:[ |
|
216 | 210 |
[stop <= aValue] whileTrue:[ |
211 |
aBlock value:aValue. |
|
212 |
aValue := aValue + step |
|
213 |
] |
|
1 | 214 |
] ifFalse:[ |
216 | 215 |
[stop >= aValue] whileTrue:[ |
216 |
aBlock value:aValue. |
|
217 |
aValue := aValue + step |
|
218 |
] |
|
1 | 219 |
] |
220 |
! |
|
221 |
||
222 |
select:aBlock |
|
223 |
"evaluate the argument, aBlock for every element in the collection |
|
224 |
and return a collection of all elements for which the block return |
|
269 | 225 |
true. Redefined since SeqColl accesses the receiver with at:, which is |
1 | 226 |
slow for intervals." |
227 |
||
228 |
|newColl| |
|
229 |
||
54 | 230 |
newColl := self species new:(self size). |
1 | 231 |
self do:[:each | |
216 | 232 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 233 |
]. |
234 |
^ newColl |
|
269 | 235 |
|
236 |
" |
|
237 |
(1 to:20) select:[:i | i even] |
|
238 |
" |
|
606 | 239 |
! ! |
240 |
||
241 |
!Interval methodsFor:'printing & storing'! |
|
242 |
||
243 |
printOn:aStream |
|
244 |
"append a printed representation to aStream" |
|
245 |
||
246 |
start printOn:aStream. |
|
247 |
aStream nextPutAll:' to:'. |
|
248 |
stop printOn:aStream. |
|
249 |
step ~= 1 ifTrue:[ |
|
250 |
aStream nextPutAll:' by:'. |
|
251 |
step printOn:aStream. |
|
252 |
]. |
|
253 |
||
254 |
" |
|
255 |
(1 to:10) printOn:Transcript |
|
256 |
(1 to:10 by:2) printOn:Transcript |
|
257 |
(1 to:10) printString |
|
258 |
" |
|
1 | 259 |
! |
260 |
||
606 | 261 |
storeOn:aStream |
262 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 263 |
|
606 | 264 |
aStream nextPut:$(. |
265 |
self printOn:aStream. |
|
266 |
aStream nextPut:$). |
|
269 | 267 |
|
268 |
" |
|
606 | 269 |
(1 to:10) storeOn:Transcript |
270 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 271 |
" |
1 | 272 |
! ! |
606 | 273 |
|
274 |
!Interval methodsFor:'private'! |
|
275 |
||
276 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
277 |
"set start, stop and step components" |
|
278 |
||
279 |
start := startInteger. |
|
280 |
stop := stopInteger. |
|
281 |
step := stepInteger |
|
282 |
! |
|
283 |
||
284 |
species |
|
285 |
"return the type of collection to be returned by collect, select etc." |
|
286 |
||
287 |
^ OrderedCollection |
|
288 |
! ! |
|
289 |
||
629 | 290 |
!Interval class methodsFor:'documentation'! |
291 |
||
292 |
version |
|
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
629
diff
changeset
|
293 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.20 1995-12-15 12:47:33 cg Exp $' |
629 | 294 |
! ! |