1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
Intervals represent a collection (or range) of numeric values specified by
|
|
26 |
a startValue, an endValue and a step. The elements are computed, not stored.
|
|
27 |
For example, the interval (1 to:5) containes the elements (1 2 3 4 5) and
|
|
28 |
(1 to:6 by:2) contains (1 3 5).
|
|
29 |
|
54
|
30 |
$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.7 1994-02-25 12:58:54 claus Exp $
|
1
|
31 |
|
|
32 |
written summer 89 by claus
|
|
33 |
'!
|
|
34 |
|
|
35 |
!Interval class methodsFor:'instance creation'!
|
|
36 |
|
|
37 |
from:start to:stop
|
|
38 |
"return a new interval with elements from start
|
|
39 |
to stop by 1"
|
|
40 |
|
2
|
41 |
^ self new setFrom:start to:stop by:1
|
1
|
42 |
!
|
|
43 |
|
|
44 |
from:start to:stop by:step
|
|
45 |
"return a new interval with elements from start
|
|
46 |
to stop by step"
|
|
47 |
|
2
|
48 |
^ self new setFrom:start to:stop by:step
|
1
|
49 |
! !
|
|
50 |
|
|
51 |
!Interval methodsFor:'private'!
|
|
52 |
|
|
53 |
setFrom:startInteger to:stopInteger by:stepInteger
|
2
|
54 |
"set start, stop and step components"
|
|
55 |
|
1
|
56 |
start := startInteger.
|
|
57 |
stop := stopInteger.
|
|
58 |
step := stepInteger
|
|
59 |
! !
|
|
60 |
|
|
61 |
!Interval methodsFor:'accessing'!
|
|
62 |
|
|
63 |
first
|
|
64 |
"return the first element of the collection"
|
|
65 |
|
|
66 |
^ start
|
|
67 |
!
|
|
68 |
|
2
|
69 |
last
|
|
70 |
"return the last element of the collection"
|
|
71 |
|
|
72 |
^ stop
|
|
73 |
!
|
|
74 |
|
1
|
75 |
start
|
|
76 |
"return the first number of the range"
|
|
77 |
|
|
78 |
^ start
|
|
79 |
!
|
|
80 |
|
|
81 |
start:aNumber
|
|
82 |
"set the first number of the range"
|
|
83 |
|
|
84 |
start := aNumber
|
|
85 |
!
|
|
86 |
|
|
87 |
stop
|
|
88 |
"return the end number of the range"
|
|
89 |
|
|
90 |
^ stop
|
|
91 |
!
|
|
92 |
|
|
93 |
stop:aNumber
|
|
94 |
"set the end number of the range"
|
|
95 |
|
|
96 |
stop := aNumber
|
|
97 |
!
|
|
98 |
|
|
99 |
step
|
|
100 |
"return the step increment of the range"
|
|
101 |
|
|
102 |
^ step
|
|
103 |
!
|
|
104 |
|
|
105 |
step:aNumber
|
|
106 |
"set the step increment of the range"
|
|
107 |
|
|
108 |
step := aNumber
|
|
109 |
!
|
|
110 |
|
|
111 |
size
|
|
112 |
"return the number of elements in the collection"
|
|
113 |
|
|
114 |
(step < 0) ifTrue:[
|
|
115 |
(start < stop) ifTrue:[
|
|
116 |
^ 0
|
|
117 |
].
|
|
118 |
^ stop - start // step + 1
|
|
119 |
].
|
|
120 |
(stop < start) ifTrue:[
|
|
121 |
^ 0
|
|
122 |
].
|
|
123 |
^ stop - start // step + 1
|
|
124 |
!
|
|
125 |
|
|
126 |
at:index
|
2
|
127 |
"return (i.e. compute) the index'th element"
|
|
128 |
|
1
|
129 |
(index between:1 and:self size) ifTrue:[
|
|
130 |
^ start + (step * (index - 1))
|
|
131 |
].
|
|
132 |
self errorSubscriptBounds:index
|
|
133 |
!
|
|
134 |
|
|
135 |
at:index put:anObject
|
2
|
136 |
"catch at:put: message - intervals cannot store elements"
|
|
137 |
|
1
|
138 |
self error:'you cannot store into an interval'
|
|
139 |
! !
|
|
140 |
|
|
141 |
!Interval methodsFor:'adding/removing elements'!
|
|
142 |
|
|
143 |
add:newObject
|
2
|
144 |
"catch add message - intervals cannot add elements"
|
|
145 |
|
1
|
146 |
self error:'elements cannot be added to an interval'
|
|
147 |
!
|
|
148 |
|
|
149 |
remove:anObject
|
2
|
150 |
"catch remove message - intervals cannot remove elements"
|
|
151 |
|
1
|
152 |
self error:'elements cannot be removed from an interval'
|
|
153 |
! !
|
|
154 |
|
|
155 |
!Interval methodsFor:'private'!
|
|
156 |
|
|
157 |
species
|
39
|
158 |
"return the type of collection to be returned by collect, select etc."
|
|
159 |
|
1
|
160 |
^ OrderedCollection
|
|
161 |
! !
|
|
162 |
|
2
|
163 |
!Interval methodsFor:'printing & storing'!
|
|
164 |
|
|
165 |
displayString
|
54
|
166 |
^ 'Interval(' , self printString , ')'
|
2
|
167 |
!
|
|
168 |
|
44
|
169 |
printOn:aStream
|
|
170 |
"append a printed representation to aStream"
|
|
171 |
|
|
172 |
start printOn:aStream.
|
|
173 |
aStream nextPutAll:' to:'.
|
|
174 |
stop printOn:aStream.
|
|
175 |
step ~= 1 ifTrue:[
|
|
176 |
aStream nextPutAll:' by:'.
|
|
177 |
step printOn:aStream.
|
|
178 |
].
|
|
179 |
|
|
180 |
"(1 to:10) printOn:Transcript"
|
54
|
181 |
"(1 to:10 by:2) printOn:Transcript"
|
|
182 |
"(1 to:10) printString"
|
44
|
183 |
!
|
|
184 |
|
2
|
185 |
storeOn:aStream
|
44
|
186 |
"store a representation which can reconstruct the receiver to aStream"
|
|
187 |
|
|
188 |
aStream nextPut:$(.
|
54
|
189 |
self printOn:aStream.
|
44
|
190 |
aStream nextPut:$).
|
2
|
191 |
|
|
192 |
"(1 to:10) storeOn:Transcript"
|
|
193 |
"(1 to:10 by:2) storeOn:Transcript"
|
|
194 |
! !
|
|
195 |
|
1
|
196 |
!Interval methodsFor:'enumeration'!
|
|
197 |
|
|
198 |
do:aBlock
|
2
|
199 |
"evaluate the argument, aBlock for every element in the
|
|
200 |
receiver-interval"
|
|
201 |
|
1
|
202 |
|aValue|
|
|
203 |
|
|
204 |
aValue := start.
|
|
205 |
step < 0 ifTrue:[
|
|
206 |
[stop <= aValue] whileTrue:[
|
|
207 |
aBlock value:aValue.
|
|
208 |
aValue := aValue + step
|
|
209 |
]
|
|
210 |
] ifFalse:[
|
|
211 |
[stop >= aValue] whileTrue:[
|
|
212 |
aBlock value:aValue.
|
|
213 |
aValue := aValue + step
|
|
214 |
]
|
|
215 |
]
|
|
216 |
!
|
|
217 |
|
|
218 |
select:aBlock
|
|
219 |
"evaluate the argument, aBlock for every element in the collection
|
|
220 |
and return a collection of all elements for which the block return
|
|
221 |
true. redefined since SeqColl accesses the receiver with at:, which is
|
|
222 |
slow for intervals."
|
|
223 |
|
|
224 |
|newColl|
|
|
225 |
|
54
|
226 |
newColl := self species new:(self size).
|
1
|
227 |
self do:[:each |
|
|
228 |
(aBlock value:each) ifTrue:[newColl add:each]
|
|
229 |
].
|
|
230 |
^ newColl
|
|
231 |
!
|
|
232 |
|
|
233 |
collect:aBlock
|
|
234 |
"evaluate the argument, aBlock for every element in the collection
|
|
235 |
and return a collection of the results - redefined since SeqColl
|
|
236 |
accesses the receiver via at:, which is slow for intervals"
|
|
237 |
|
|
238 |
|newCollection|
|
|
239 |
|
|
240 |
newCollection := self species new:(self size).
|
|
241 |
self do:[:each |
|
|
242 |
newCollection add:(aBlock value:each)
|
|
243 |
].
|
|
244 |
^ newCollection
|
|
245 |
! !
|