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