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