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