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