author | Claus Gittinger <cg@exept.de> |
Mon, 15 Dec 2008 12:48:15 +0100 | |
changeset 11409 | 82874ed12ab4 |
parent 11270 | a616ba9b7f81 |
child 11477 | 2a46e08433d3 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
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 |
" |
|
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
13 |
|
6132 | 14 |
ReadOnlySequenceableCollection subclass:#Interval |
8940
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
15 |
instanceVariableNames:'start stop step' |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
16 |
classVariableNames:'' |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
17 |
poolDictionaries:'' |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
18 |
category:'Collections-Sequenceable' |
1 | 19 |
! |
20 |
||
88 | 21 |
!Interval class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
Intervals represent a collection (or range) of numeric values specified by |
|
269 | 40 |
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
|
41 |
The interesting thing is that the elements are computed, not stored. |
11409 | 42 |
However, protocol-wise, intervals behave like any other (read-only) sequenceable collection. |
43 |
||
88 | 44 |
For example, the interval (1 to:5) containes the elements (1 2 3 4 5) and |
45 |
(1 to:6 by:2) contains (1 3 5). |
|
339 | 46 |
|
47 |
examples: |
|
48 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
49 |
(1 to:10) do:[:i | Transcript showCR:i] |
339 | 50 |
|
51 |
notice, that this is semantically equivalent to: |
|
52 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
53 |
1 to:10 do:[:i | Transcript showCR:i] |
339 | 54 |
|
55 |
however, the second is preferred, since loops using to:do: are |
|
56 |
much faster and do not create temporary garbage objects. |
|
57 |
Therefore, Intervals are generally NOT used for this kind of loops. |
|
58 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
59 |
(1 to:10) asArray |
339 | 60 |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
61 |
(1 to:10 by:2) asOrderedCollection |
1290 | 62 |
|
63 |
[author:] |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
64 |
Claus Gittinger |
88 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
68 |
!Interval class methodsFor:'instance creation'! |
|
69 |
||
70 |
from:start to:stop |
|
11409 | 71 |
"return a new interval with elements from start to stop by 1" |
1 | 72 |
|
2 | 73 |
^ self new setFrom:start to:stop by:1 |
1 | 74 |
! |
75 |
||
76 |
from:start to:stop by:step |
|
11409 | 77 |
"return a new interval with elements from start to stop by step" |
1 | 78 |
|
2 | 79 |
^ self new setFrom:start to:stop by:step |
1 | 80 |
! ! |
81 |
||
5672 | 82 |
!Interval class methodsFor:'converting'! |
83 |
||
84 |
decodeFromLiteralArray:anArray |
|
85 |
"create & return a new instance from information encoded in anArray. |
|
9411 | 86 |
Re-redefined, since the implementation in SequencableCollection creates instances with an initial |
87 |
size, which is not allowed for intervals." |
|
5672 | 88 |
|
6132 | 89 |
^ self new fromLiteralArrayEncoding:anArray. |
90 |
! ! |
|
5672 | 91 |
|
606 | 92 |
!Interval methodsFor:'accessing'! |
1 | 93 |
|
606 | 94 |
at:index |
95 |
"return (i.e. compute) the index'th element" |
|
2 | 96 |
|
606 | 97 |
(index between:1 and:self size) ifTrue:[ |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
98 |
^ start + (step * (index - 1)) |
606 | 99 |
]. |
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
100 |
^ self subscriptBoundsError:index |
606 | 101 |
! |
1 | 102 |
|
103 |
first |
|
104 |
"return the first element of the collection" |
|
105 |
||
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
106 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
107 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
108 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
109 |
]. |
1 | 110 |
^ start |
111 |
! |
|
112 |
||
328 | 113 |
increment |
339 | 114 |
"alias for #step; for ST-80 compatibility" |
328 | 115 |
|
116 |
^ step |
|
117 |
! |
|
118 |
||
606 | 119 |
last |
120 |
"return the last element of the collection" |
|
1 | 121 |
|
5521
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
122 |
(((step < 0) and:[start < stop]) |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
123 |
or:[(step > 0) and:[stop < start]]) ifTrue:[ |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
124 |
^ self emptyCollectionError |
7cad30b0ade9
fixed empty collection error handling
Claus Gittinger <cg@exept.de>
parents:
4155
diff
changeset
|
125 |
]. |
606 | 126 |
^ stop |
1 | 127 |
! |
128 |
||
606 | 129 |
start |
130 |
"return the first number of the range" |
|
2 | 131 |
|
606 | 132 |
^ start |
133 |
! |
|
134 |
||
135 |
start:aNumber |
|
136 |
"set the first number of the range" |
|
137 |
||
138 |
start := aNumber |
|
1 | 139 |
! |
140 |
||
606 | 141 |
step |
142 |
"return the step increment of the range. |
|
143 |
OBSOLETE: |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
144 |
Please use #increment for ST-80 compatibility." |
606 | 145 |
|
146 |
^ step |
|
147 |
! |
|
148 |
||
149 |
step:aNumber |
|
150 |
"set the step increment of the range" |
|
2 | 151 |
|
606 | 152 |
step := aNumber |
153 |
! |
|
154 |
||
155 |
stop |
|
156 |
"return the end number of the range" |
|
157 |
||
158 |
^ stop |
|
159 |
! |
|
160 |
||
161 |
stop:aNumber |
|
162 |
"set the end number of the range" |
|
163 |
||
164 |
stop := aNumber |
|
1 | 165 |
! ! |
166 |
||
6745 | 167 |
!Interval methodsFor:'comparing'! |
168 |
||
169 |
= anInterval |
|
170 |
anInterval class == self class ifTrue:[ |
|
171 |
^ start = anInterval start |
|
172 |
and:[ stop = anInterval stop |
|
173 |
and:[ step = anInterval step]] |
|
174 |
]. |
|
175 |
^ super = anInterval |
|
176 |
! |
|
177 |
||
178 |
hash |
|
6748
604366d06326
hash: must be same as SeqColl hash; inherit
Claus Gittinger <cg@exept.de>
parents:
6747
diff
changeset
|
179 |
^ super hash "/ do not redefine: must generate same hash as SeqColl. |
6745 | 180 |
! ! |
181 |
||
5672 | 182 |
!Interval methodsFor:'converting'! |
183 |
||
184 |
fromLiteralArrayEncoding:encoding |
|
185 |
"read my values from an encoding. |
|
186 |
The encoding is supposed to be either of the form: |
|
187 |
(#Interval start stop step) |
|
188 |
This is the reverse operation to #literalArrayEncoding." |
|
189 |
||
190 |
start := (encoding at:2). |
|
191 |
stop := (encoding at:3). |
|
10794
b37d77576d77
allow for missing step in literalArrayEncoding
Michael Beyl <mb@exept.de>
parents:
9411
diff
changeset
|
192 |
(encoding size > 3) ifTrue:[ |
b37d77576d77
allow for missing step in literalArrayEncoding
Michael Beyl <mb@exept.de>
parents:
9411
diff
changeset
|
193 |
step := (encoding at:4). |
b37d77576d77
allow for missing step in literalArrayEncoding
Michael Beyl <mb@exept.de>
parents:
9411
diff
changeset
|
194 |
] ifFalse:[ |
b37d77576d77
allow for missing step in literalArrayEncoding
Michael Beyl <mb@exept.de>
parents:
9411
diff
changeset
|
195 |
step := 1. |
b37d77576d77
allow for missing step in literalArrayEncoding
Michael Beyl <mb@exept.de>
parents:
9411
diff
changeset
|
196 |
] |
5672 | 197 |
|
198 |
" |
|
199 |
Interval new fromLiteralArrayEncoding:((1 to:10) literalArrayEncoding) |
|
200 |
Interval new fromLiteralArrayEncoding:((1 to:10 by:2) literalArrayEncoding) |
|
201 |
Interval decodeFromLiteralArray:((1 to:10 by:2) literalArrayEncoding) |
|
6132 | 202 |
" |
203 |
! |
|
5672 | 204 |
|
205 |
literalArrayEncoding |
|
206 |
"encode myself as an array literal, from which a copy of the receiver |
|
207 |
can be reconstructed with #decodeAsLiteralArray." |
|
208 |
||
209 |
^ Array |
|
9411 | 210 |
with:self class name |
5672 | 211 |
with:start |
212 |
with:stop |
|
213 |
with:step |
|
214 |
||
215 |
" |
|
216 |
(1 to:10) literalArrayEncoding |
|
217 |
(1 to:10 by:2) literalArrayEncoding |
|
6132 | 218 |
" |
219 |
! ! |
|
5672 | 220 |
|
8374 | 221 |
!Interval methodsFor:'converting-reindexed'! |
222 |
||
223 |
from:startIndex |
|
224 |
"return a new collection representing the receivers elements starting at startIndex." |
|
225 |
||
226 |
step == 1 ifTrue:[ |
|
227 |
^ start+startIndex-1 to:stop |
|
228 |
]. |
|
229 |
"could be more intelligent here" |
|
230 |
^ super from:startIndex |
|
231 |
||
232 |
" |
|
233 |
(1 to:100) from:2 |
|
234 |
||
235 |
(1 to:20 by:2) from:2 |
|
236 |
(1 to:20 by:2) asArray from:2 |
|
237 |
((1 to:20 by:2) from:2) asArray |
|
238 |
" |
|
239 |
! |
|
240 |
||
241 |
to:endIndex |
|
242 |
"return a new collection representing the receivers elements upTo and including endIndex." |
|
243 |
||
244 |
^ start to:(endIndex min:stop) by:step |
|
245 |
||
246 |
" |
|
247 |
(1 to:100) to:50 |
|
248 |
(1 to:100 by:2) to:50 |
|
249 |
" |
|
250 |
! ! |
|
251 |
||
606 | 252 |
!Interval methodsFor:'enumerating'! |
39 | 253 |
|
606 | 254 |
collect:aBlock |
255 |
"evaluate the argument, aBlock for every element in the collection |
|
11270 | 256 |
and return a collection of the results. |
257 |
Redefined since SeqColl accesses the receiver via at:, which is slow for intervals" |
|
2 | 258 |
|
606 | 259 |
|newCollection| |
44 | 260 |
|
8940
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
261 |
(start == 1 and:[step == 1]) ifTrue:[ |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
262 |
newCollection := Array new:(self size). |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
263 |
self do:[:each | |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
264 |
newCollection at:each put:(aBlock value:each) |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
265 |
]. |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
266 |
] ifFalse:[ |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
267 |
newCollection := self species new:(self size). |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
268 |
self do:[:each | |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
269 |
newCollection add:(aBlock value:each) |
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
270 |
]. |
44 | 271 |
]. |
606 | 272 |
^ newCollection |
44 | 273 |
|
269 | 274 |
" |
8940
b05bb4dd96b3
collect returns an Array if possible
Claus Gittinger <cg@exept.de>
parents:
8394
diff
changeset
|
275 |
(1 to:20) collect:[:i | i*i] |
269 | 276 |
" |
11270 | 277 |
|
278 |
"Modified: / 22-10-2008 / 12:47:52 / cg" |
|
44 | 279 |
! |
280 |
||
1 | 281 |
do:aBlock |
11270 | 282 |
"evaluate the argument, aBlock for every element in the receiver-interval. |
283 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
2 | 284 |
|
3986 | 285 |
|aValue iter| |
1 | 286 |
|
287 |
aValue := start. |
|
3986 | 288 |
aValue isInteger ifTrue:[ |
289 |
step < 0 ifTrue:[ |
|
290 |
[stop <= aValue] whileTrue:[ |
|
291 |
aBlock value:aValue. |
|
292 |
aValue := aValue + step |
|
293 |
] |
|
294 |
] ifFalse:[ |
|
295 |
[stop >= aValue] whileTrue:[ |
|
296 |
aBlock value:aValue. |
|
297 |
aValue := aValue + step |
|
298 |
] |
|
299 |
] |
|
1 | 300 |
] ifFalse:[ |
3986 | 301 |
"/ the code below avoids rounding errors |
302 |
"/ to accumulate if floats are enumerated. |
|
303 |
iter := 1. |
|
304 |
step < 0 ifTrue:[ |
|
305 |
[stop <= aValue] whileTrue:[ |
|
306 |
aBlock value:aValue. |
|
307 |
aValue := start + (iter * step). |
|
308 |
iter := iter + 1. |
|
309 |
] |
|
310 |
] ifFalse:[ |
|
311 |
[stop >= aValue] whileTrue:[ |
|
312 |
aBlock value:aValue. |
|
313 |
aValue := start + (iter * step). |
|
314 |
iter := iter + 1. |
|
315 |
] |
|
316 |
] |
|
1 | 317 |
] |
3986 | 318 |
|
319 |
" |
|
320 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
321 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
322 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
6532 | 323 |
$a to:$z do:[:v | Transcript showCR:v] |
3986 | 324 |
" |
11270 | 325 |
|
326 |
"Modified: / 22-10-2008 / 12:47:30 / cg" |
|
327 |
! |
|
328 |
||
329 |
reverseDo:aBlock |
|
330 |
"evaluate the argument, aBlock for every element in the receiver-interval in |
|
331 |
reverse order. |
|
332 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
333 |
||
334 |
^ self reversed do:aBlock |
|
335 |
||
336 |
" |
|
337 |
(1 to:10) do:[:el | Transcript showCR:el ]. |
|
338 |
(1 to:10) reverseDo:[:el | Transcript showCR:el ]. |
|
339 |
" |
|
340 |
||
341 |
"Modified: / 22-10-2008 / 12:49:28 / cg" |
|
1 | 342 |
! |
343 |
||
344 |
select:aBlock |
|
345 |
"evaluate the argument, aBlock for every element in the collection |
|
11270 | 346 |
and return a collection of all elements for which the block return true. |
347 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
1 | 348 |
|
349 |
|newColl| |
|
350 |
||
54 | 351 |
newColl := self species new:(self size). |
1 | 352 |
self do:[:each | |
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
353 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 354 |
]. |
355 |
^ newColl |
|
269 | 356 |
|
357 |
" |
|
358 |
(1 to:20) select:[:i | i even] |
|
359 |
" |
|
11270 | 360 |
|
361 |
"Modified: / 22-10-2008 / 12:47:41 / cg" |
|
606 | 362 |
! ! |
363 |
||
364 |
!Interval methodsFor:'printing & storing'! |
|
365 |
||
6709 | 366 |
displayString |
367 |
^ self printString |
|
368 |
||
369 |
" |
|
370 |
(1 to:10) |
|
371 |
(1 to:10 by:2) |
|
372 |
" |
|
373 |
! |
|
374 |
||
606 | 375 |
printOn:aStream |
376 |
"append a printed representation to aStream" |
|
377 |
||
378 |
start printOn:aStream. |
|
379 |
aStream nextPutAll:' to:'. |
|
380 |
stop printOn:aStream. |
|
381 |
step ~= 1 ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
382 |
aStream nextPutAll:' by:'. |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
383 |
step printOn:aStream. |
606 | 384 |
]. |
385 |
||
386 |
" |
|
387 |
(1 to:10) printOn:Transcript |
|
388 |
(1 to:10 by:2) printOn:Transcript |
|
389 |
(1 to:10) printString |
|
390 |
" |
|
1 | 391 |
! |
392 |
||
606 | 393 |
storeOn:aStream |
394 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 395 |
|
606 | 396 |
aStream nextPut:$(. |
397 |
self printOn:aStream. |
|
398 |
aStream nextPut:$). |
|
269 | 399 |
|
400 |
" |
|
606 | 401 |
(1 to:10) storeOn:Transcript |
402 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 403 |
" |
1 | 404 |
! ! |
606 | 405 |
|
406 |
!Interval methodsFor:'private'! |
|
407 |
||
408 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
409 |
"set start, stop and step components" |
|
410 |
||
411 |
start := startInteger. |
|
412 |
stop := stopInteger. |
|
413 |
step := stepInteger |
|
414 |
! |
|
415 |
||
416 |
species |
|
417 |
"return the type of collection to be returned by collect, select etc." |
|
418 |
||
419 |
^ OrderedCollection |
|
420 |
! ! |
|
421 |
||
5557 | 422 |
!Interval methodsFor:'queries'! |
423 |
||
7823 | 424 |
max |
425 |
"return the maximum value in the receiver collection, |
|
426 |
redefined, since this can be easily computed. |
|
427 |
Raises an error, if the receiver is empty." |
|
428 |
||
429 |
step == 1 ifTrue:[stop >= start ifTrue:[^ stop]]. |
|
430 |
step < 0 ifTrue:[stop <= start ifTrue:[^ start]]. |
|
431 |
^ super max |
|
432 |
||
433 |
" |
|
434 |
(0 to:15) max |
|
435 |
(0 to:15 by:2) max |
|
436 |
(0 to:15 by:8) max |
|
437 |
(15 to:0) max -> error |
|
438 |
(15 to:0 by:4) max -> error |
|
439 |
(-1 to:-15 by:-1) max |
|
440 |
(-1 to:-15 by:-4) max |
|
441 |
(-1 to:15 by:-1) max -> error |
|
442 |
" |
|
443 |
! |
|
444 |
||
445 |
min |
|
446 |
"return the minimum value in the receiver collection, |
|
447 |
redefined, since this can be easily computed. |
|
448 |
Raises an error, if the receiver is empty." |
|
449 |
||
450 |
step == -1 ifTrue:[stop <= start ifTrue:[^ stop]]. |
|
451 |
step > 0 ifTrue:[stop >= start ifTrue:[^ start]]. |
|
452 |
^ super min |
|
453 |
||
454 |
" |
|
455 |
(0 to:15) min |
|
456 |
(0 to:15 by:2) min |
|
457 |
(0 to:15 by:8) min |
|
458 |
(15 to:0) min -> error |
|
459 |
(15 to:0 by:4) min -> error |
|
460 |
(-1 to:-15 by:-1) min |
|
461 |
(-1 to:-15 by:-4) min |
|
462 |
(-1 to:15 by:-1) min -> error |
|
463 |
" |
|
464 |
! |
|
465 |
||
466 |
minMax |
|
467 |
"return the minimum and maximum values in the receiver collection |
|
468 |
as a two element array. |
|
469 |
Raises an error, if the receiver is empty." |
|
470 |
||
471 |
step == -1 ifTrue:[ |
|
472 |
stop <= start ifTrue:[ |
|
473 |
^ Array with:stop with:start |
|
474 |
] |
|
475 |
]. |
|
476 |
step == 1 ifTrue:[ |
|
477 |
stop >= start ifTrue:[ |
|
478 |
^ Array with:start with:stop |
|
479 |
] |
|
480 |
]. |
|
481 |
^ super minMax |
|
482 |
||
483 |
" |
|
484 |
(0 to:15) minMax |
|
485 |
(0 to:15 by:2) minMax |
|
486 |
(0 to:15 by:8) minMax |
|
487 |
(15 to:0) minMax -> error |
|
488 |
(15 to:0 by:4) minMax -> error |
|
489 |
(-1 to:-15 by:-1) minMax |
|
490 |
(-1 to:-15 by:-4) minMax |
|
491 |
(-1 to:15 by:-1) minMax -> error |
|
492 |
" |
|
493 |
! |
|
494 |
||
5557 | 495 |
size |
496 |
"return the number of elements in the collection" |
|
497 |
||
498 |
(step < 0) ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
499 |
(start < stop) ifTrue:[ |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
500 |
^ 0 |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
501 |
]. |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
502 |
^ stop - start // step + 1 |
5557 | 503 |
]. |
504 |
(stop < start) ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
505 |
^ 0 |
5557 | 506 |
]. |
507 |
^ stop - start // step + 1 |
|
508 |
! ! |
|
509 |
||
11266 | 510 |
!Interval methodsFor:'sorting & reordering'! |
511 |
||
512 |
reversed |
|
513 |
"return a copy with elements in reverse order" |
|
514 |
||
515 |
"this can be tricky, if stepping from start does not reach stop exactly. |
|
516 |
So what is the reverse of: (2 to:5 by: 2) ? |
|
517 |
I think, that the correct semantic is to behave transparent to the type |
|
518 |
of collection and generate the same elements as another collection would. |
|
519 |
In other words, the same as (1 to:5 by: 2) asOrderedCollection reversed |
|
520 |
would. That means, we get: #(4 2). |
|
521 |
This also means, that (2 to:5 by: 2) reversed reversed does not return the |
|
522 |
original, but another interval which generates the same elements !! (2 to:4 by: 2)" |
|
523 |
||
524 |
|rest| |
|
525 |
||
526 |
step isInteger ifTrue:[ |
|
527 |
rest := ((stop - step) rem: step). |
|
528 |
^ (stop-rest) to:start by:step negated |
|
529 |
]. |
|
530 |
^ super reversed |
|
531 |
||
532 |
||
533 |
" |
|
534 |
(1 to:4) reversed asOrderedCollection |
|
535 |
(1 to:4) reversed reversed asOrderedCollection |
|
536 |
(1 to:4) asOrderedCollection reversed |
|
537 |
(1 to:4) asOrderedCollection reversed reversed |
|
538 |
||
539 |
(2 to:5 by: 2) asOrderedCollection |
|
540 |
(2 to:5 by: 2) asOrderedCollection reversed |
|
541 |
(2 to:5 by: 2) asOrderedCollection reversed reversed |
|
542 |
(2 to:5 by: 2) reversed asOrderedCollection |
|
543 |
(2 to:5 by: 2) reversed reversed asOrderedCollection |
|
544 |
||
545 |
(1 to:2 by: 0.3) asOrderedCollection |
|
546 |
(1 to:2 by: 0.3) asOrderedCollection reversed |
|
547 |
(1 to:2 by: 0.3) asOrderedCollection reversed reversed |
|
548 |
(1 to:2 by: 0.3) reversed asOrderedCollection |
|
549 |
(1 to:2 by: 0.3) reversed reversed asOrderedCollection |
|
550 |
" |
|
551 |
! ! |
|
552 |
||
6671 | 553 |
!Interval methodsFor:'testing'! |
554 |
||
6712 | 555 |
includes:anElement |
556 |
|rest| |
|
557 |
stop >= start ifTrue:[ |
|
558 |
(anElement between:start and:stop) ifFalse:[^ false]. |
|
559 |
] ifFalse:[ |
|
560 |
(anElement between:stop and:start) ifFalse:[^ false]. |
|
561 |
]. |
|
562 |
rest := (anElement - start) rem:step. |
|
563 |
^ rest = 0 |
|
564 |
||
565 |
" |
|
566 |
(1 to:15) includes:0 |
|
567 |
(1 to:15) includes:16 |
|
568 |
(1 to:15) includes:1 |
|
569 |
(1 to:15) includes:15 |
|
570 |
(1 to:15) includes:5 |
|
571 |
(1 to:15) includes:14 |
|
572 |
(1 to:15) includes:4.0 |
|
573 |
(1 to:15) includes:4.4 |
|
574 |
||
575 |
(1 to:15 by:3) includes:0 |
|
576 |
(1 to:15 by:3) includes:16 |
|
577 |
(1 to:15 by:3) includes:1 |
|
578 |
(1 to:15 by:3) includes:15 |
|
579 |
(1 to:15 by:3) includes:5 |
|
580 |
(1 to:15 by:3) includes:4 |
|
581 |
(1 to:15 by:3) includes:13 |
|
582 |
(1 to:15 by:3) includes:14 |
|
583 |
(1 to:15 by:3) includes:4.0 |
|
584 |
(1 to:15 by:3) includes:4.4 |
|
585 |
||
586 |
(10 to:-10 by:-3) includes:11 |
|
587 |
(10 to:-10 by:-3) includes:10 |
|
588 |
(10 to:-10 by:-3) includes:9 |
|
589 |
(10 to:-10 by:-3) includes:8 |
|
590 |
(10 to:-10 by:-3) includes:7 |
|
591 |
(10 to:-10 by:-3) includes:4 |
|
592 |
(10 to:-10 by:-3) includes:0 |
|
593 |
(10 to:-10 by:-3) includes:-1 |
|
594 |
(10 to:-10 by:-3) includes:-2 |
|
595 |
(10 to:-10 by:-3) includes:-8 |
|
596 |
(10 to:-10 by:-3) includes:-9 |
|
597 |
(10 to:-10 by:-3) includes:-10 |
|
598 |
(10 to:-10 by:-3) includes:-11 |
|
599 |
(10 to:-10 by:-3) includes:-2.4 |
|
600 |
||
601 |
(-10 to:-20 by:-2) includes:-16 |
|
602 |
(-10 to:-20 by:-2) includes:-20 |
|
603 |
(-10 to:-20 by:-2) includes:-23 |
|
604 |
(-10 to:-20 by:-2) includes:-24 |
|
605 |
||
606 |
" |
|
6671 | 607 |
! ! |
608 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
609 |
!Interval methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
610 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
611 |
acceptVisitor:aVisitor with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
612 |
"this is special. Some encoders want to encode this as a sequenceable collection, |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
613 |
some want to encode a less expensive representation" |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
614 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
615 |
^ aVisitor visitInterval:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
616 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
617 |
|
629 | 618 |
!Interval class methodsFor:'documentation'! |
619 |
||
620 |
version |
|
11409 | 621 |
^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.47 2008-12-15 11:48:15 cg Exp $' |
629 | 622 |
! ! |