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