author | Jan Vrany <jan.vrany@labware.com> |
Wed, 23 Jun 2021 12:50:05 +0100 | |
branch | jv |
changeset 25430 | acd92449dc30 |
parent 21024 | 8734987eb5c7 |
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 |
|
14128 | 303 |
|elementValue mySize newCollection| |
44 | 304 |
|
14128 | 305 |
elementValue := start. |
16709 | 306 |
newCollection := self species newWithSize:(mySize := self size). |
14128 | 307 |
1 to: mySize do:[:i | |
308 |
newCollection at:i put:(aBlock value:elementValue). |
|
309 |
elementValue := elementValue + step. |
|
44 | 310 |
]. |
14128 | 311 |
^ newCollection |
44 | 312 |
|
269 | 313 |
" |
14128 | 314 |
(1 to:20 by:2) collect:[:i | i*i] |
269 | 315 |
" |
11270 | 316 |
|
14129 | 317 |
"Modified: / 04-05-2012 / 13:03:56 / cg" |
44 | 318 |
! |
319 |
||
1 | 320 |
do:aBlock |
11270 | 321 |
"evaluate the argument, aBlock for every element in the receiver-interval. |
322 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
2 | 323 |
|
3986 | 324 |
|aValue iter| |
1 | 325 |
|
326 |
aValue := start. |
|
3986 | 327 |
aValue isInteger ifTrue:[ |
328 |
step < 0 ifTrue:[ |
|
329 |
[stop <= aValue] whileTrue:[ |
|
330 |
aBlock value:aValue. |
|
331 |
aValue := aValue + step |
|
332 |
] |
|
333 |
] ifFalse:[ |
|
334 |
[stop >= aValue] whileTrue:[ |
|
335 |
aBlock value:aValue. |
|
336 |
aValue := aValue + step |
|
337 |
] |
|
338 |
] |
|
1 | 339 |
] ifFalse:[ |
3986 | 340 |
"/ the code below avoids rounding errors |
341 |
"/ to accumulate if floats are enumerated. |
|
342 |
iter := 1. |
|
343 |
step < 0 ifTrue:[ |
|
344 |
[stop <= aValue] whileTrue:[ |
|
345 |
aBlock value:aValue. |
|
346 |
aValue := start + (iter * step). |
|
347 |
iter := iter + 1. |
|
348 |
] |
|
349 |
] ifFalse:[ |
|
350 |
[stop >= aValue] whileTrue:[ |
|
351 |
aBlock value:aValue. |
|
352 |
aValue := start + (iter * step). |
|
353 |
iter := iter + 1. |
|
354 |
] |
|
355 |
] |
|
1 | 356 |
] |
3986 | 357 |
|
358 |
" |
|
359 |
1e7 to:1e7+1 by:0.25 do:[:v | Transcript showCR:v] |
|
360 |
1.0 to:2.0 by:0.25 do:[:v | Transcript showCR:v] |
|
361 |
2.0 to:1.0 by:-0.25 do:[:v | Transcript showCR:v] |
|
6532 | 362 |
$a to:$z do:[:v | Transcript showCR:v] |
3986 | 363 |
" |
11270 | 364 |
|
365 |
"Modified: / 22-10-2008 / 12:47:30 / cg" |
|
366 |
! |
|
367 |
||
368 |
reverseDo:aBlock |
|
369 |
"evaluate the argument, aBlock for every element in the receiver-interval in |
|
370 |
reverse order. |
|
371 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
372 |
||
373 |
^ self reversed do:aBlock |
|
374 |
||
375 |
" |
|
376 |
(1 to:10) do:[:el | Transcript showCR:el ]. |
|
377 |
(1 to:10) reverseDo:[:el | Transcript showCR:el ]. |
|
378 |
" |
|
379 |
||
380 |
"Modified: / 22-10-2008 / 12:49:28 / cg" |
|
1 | 381 |
! |
382 |
||
383 |
select:aBlock |
|
384 |
"evaluate the argument, aBlock for every element in the collection |
|
11270 | 385 |
and return a collection of all elements for which the block return true. |
386 |
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals." |
|
1 | 387 |
|
388 |
|newColl| |
|
389 |
||
54 | 390 |
newColl := self species new:(self size). |
1 | 391 |
self do:[:each | |
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
392 |
(aBlock value:each) ifTrue:[newColl add:each] |
1 | 393 |
]. |
394 |
^ newColl |
|
269 | 395 |
|
396 |
" |
|
397 |
(1 to:20) select:[:i | i even] |
|
398 |
" |
|
11270 | 399 |
|
400 |
"Modified: / 22-10-2008 / 12:47:41 / cg" |
|
606 | 401 |
! ! |
402 |
||
16709 | 403 |
|
606 | 404 |
!Interval methodsFor:'printing & storing'! |
405 |
||
13074
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
406 |
displayOn:aGCOrStream |
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
407 |
|
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
408 |
"/ 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
|
409 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
16746 | 410 |
(aGCOrStream isStream) ifTrue:[ |
13074
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
411 |
self printOn:aGCOrStream. |
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
412 |
^ self. |
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
413 |
]. |
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
414 |
^ super displayOn:aGCOrStream |
6709 | 415 |
|
416 |
" |
|
417 |
(1 to:10) |
|
418 |
(1 to:10 by:2) |
|
419 |
" |
|
420 |
! |
|
421 |
||
606 | 422 |
printOn:aStream |
423 |
"append a printed representation to aStream" |
|
424 |
||
425 |
start printOn:aStream. |
|
426 |
aStream nextPutAll:' to:'. |
|
427 |
stop printOn:aStream. |
|
428 |
step ~= 1 ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
429 |
aStream nextPutAll:' by:'. |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
430 |
step printOn:aStream. |
606 | 431 |
]. |
432 |
||
433 |
" |
|
434 |
(1 to:10) printOn:Transcript |
|
435 |
(1 to:10 by:2) printOn:Transcript |
|
436 |
(1 to:10) printString |
|
437 |
" |
|
1 | 438 |
! |
439 |
||
606 | 440 |
storeOn:aStream |
441 |
"store a representation which can reconstruct the receiver to aStream" |
|
1 | 442 |
|
606 | 443 |
aStream nextPut:$(. |
444 |
self printOn:aStream. |
|
445 |
aStream nextPut:$). |
|
269 | 446 |
|
447 |
" |
|
606 | 448 |
(1 to:10) storeOn:Transcript |
449 |
(1 to:10 by:2) storeOn:Transcript |
|
269 | 450 |
" |
1 | 451 |
! ! |
606 | 452 |
|
453 |
!Interval methodsFor:'private'! |
|
454 |
||
455 |
setFrom:startInteger to:stopInteger by:stepInteger |
|
456 |
"set start, stop and step components" |
|
457 |
||
458 |
start := startInteger. |
|
459 |
stop := stopInteger. |
|
460 |
step := stepInteger |
|
461 |
! |
|
462 |
||
463 |
species |
|
464 |
"return the type of collection to be returned by collect, select etc." |
|
465 |
||
466 |
^ OrderedCollection |
|
467 |
! ! |
|
468 |
||
5557 | 469 |
!Interval methodsFor:'queries'! |
470 |
||
7823 | 471 |
max |
472 |
"return the maximum value in the receiver collection, |
|
473 |
redefined, since this can be easily computed. |
|
474 |
Raises an error, if the receiver is empty." |
|
475 |
||
476 |
step == 1 ifTrue:[stop >= start ifTrue:[^ stop]]. |
|
477 |
step < 0 ifTrue:[stop <= start ifTrue:[^ start]]. |
|
478 |
^ super max |
|
479 |
||
480 |
" |
|
481 |
(0 to:15) max |
|
482 |
(0 to:15 by:2) max |
|
483 |
(0 to:15 by:8) max |
|
484 |
(15 to:0) max -> error |
|
485 |
(15 to:0 by:4) max -> error |
|
486 |
(-1 to:-15 by:-1) max |
|
487 |
(-1 to:-15 by:-4) max |
|
488 |
(-1 to:15 by:-1) max -> error |
|
489 |
" |
|
490 |
! |
|
491 |
||
492 |
min |
|
493 |
"return the minimum value in the receiver collection, |
|
494 |
redefined, since this can be easily computed. |
|
495 |
Raises an error, if the receiver is empty." |
|
496 |
||
497 |
step == -1 ifTrue:[stop <= start ifTrue:[^ stop]]. |
|
498 |
step > 0 ifTrue:[stop >= start ifTrue:[^ start]]. |
|
499 |
^ super min |
|
500 |
||
501 |
" |
|
502 |
(0 to:15) min |
|
503 |
(0 to:15 by:2) min |
|
504 |
(0 to:15 by:8) min |
|
505 |
(15 to:0) min -> error |
|
506 |
(15 to:0 by:4) min -> error |
|
507 |
(-1 to:-15 by:-1) min |
|
508 |
(-1 to:-15 by:-4) min |
|
509 |
(-1 to:15 by:-1) min -> error |
|
510 |
" |
|
511 |
! |
|
512 |
||
513 |
minMax |
|
514 |
"return the minimum and maximum values in the receiver collection |
|
515 |
as a two element array. |
|
516 |
Raises an error, if the receiver is empty." |
|
517 |
||
518 |
step == -1 ifTrue:[ |
|
519 |
stop <= start ifTrue:[ |
|
520 |
^ Array with:stop with:start |
|
521 |
] |
|
522 |
]. |
|
523 |
step == 1 ifTrue:[ |
|
524 |
stop >= start ifTrue:[ |
|
525 |
^ Array with:start with:stop |
|
526 |
] |
|
527 |
]. |
|
528 |
^ super minMax |
|
529 |
||
530 |
" |
|
531 |
(0 to:15) minMax |
|
532 |
(0 to:15 by:2) minMax |
|
533 |
(0 to:15 by:8) minMax |
|
534 |
(15 to:0) minMax -> error |
|
535 |
(15 to:0 by:4) minMax -> error |
|
536 |
(-1 to:-15 by:-1) minMax |
|
537 |
(-1 to:-15 by:-4) minMax |
|
538 |
(-1 to:15 by:-1) minMax -> error |
|
539 |
" |
|
540 |
! |
|
541 |
||
5557 | 542 |
size |
543 |
"return the number of elements in the collection" |
|
544 |
||
545 |
(step < 0) ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
546 |
(start < stop) ifTrue:[ |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
547 |
^ 0 |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
548 |
]. |
18510 | 549 |
^ (start - stop) // step abs + 1 |
5557 | 550 |
]. |
551 |
(stop < start) ifTrue:[ |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
552 |
^ 0 |
5557 | 553 |
]. |
18510 | 554 |
^ (stop - start) // step + 1 |
5557 | 555 |
! ! |
556 |
||
11902 | 557 |
!Interval methodsFor:'set operations'! |
558 |
||
559 |
intersect:aCollection |
|
560 |
"return a new interval containing all elements of the receiver, |
|
561 |
which are also contained in the argument collection" |
|
562 |
||
11903 | 563 |
"/ could be much more intelligent here... |
11902 | 564 |
aCollection class == Interval ifTrue:[ |
565 |
aCollection step = step ifTrue:[ |
|
566 |
step = 1 ifTrue:[ |
|
567 |
(self includes:aCollection start) ifTrue:[ |
|
568 |
^ (start max:(aCollection start)) to:(stop min:(aCollection stop)) |
|
569 |
]. |
|
570 |
(self includes:aCollection stop) ifTrue:[ |
|
571 |
^ (start max:(aCollection start)) to:(stop min:(aCollection stop)) |
|
572 |
] |
|
573 |
] |
|
574 |
]. |
|
575 |
]. |
|
576 |
||
577 |
^ super intersect:aCollection |
|
578 |
||
579 |
" |
|
580 |
(1 to:10) intersect:(4 to:20) |
|
581 |
(1 to:10) intersect:(11 to:20) |
|
582 |
(1 to:10) intersect:(10 to:20) |
|
583 |
(4 to:20) intersect:(1 to:10) |
|
11903 | 584 |
(4 to:20) intersect:(1 to:10 by:2) |
11902 | 585 |
" |
586 |
! ! |
|
587 |
||
11266 | 588 |
!Interval methodsFor:'sorting & reordering'! |
589 |
||
590 |
reversed |
|
591 |
"return a copy with elements in reverse order" |
|
592 |
||
593 |
"this can be tricky, if stepping from start does not reach stop exactly. |
|
594 |
So what is the reverse of: (2 to:5 by: 2) ? |
|
595 |
I think, that the correct semantic is to behave transparent to the type |
|
596 |
of collection and generate the same elements as another collection would. |
|
597 |
In other words, the same as (1 to:5 by: 2) asOrderedCollection reversed |
|
598 |
would. That means, we get: #(4 2). |
|
599 |
This also means, that (2 to:5 by: 2) reversed reversed does not return the |
|
600 |
original, but another interval which generates the same elements !! (2 to:4 by: 2)" |
|
601 |
||
602 |
|rest| |
|
603 |
||
604 |
step isInteger ifTrue:[ |
|
605 |
rest := ((stop - step) rem: step). |
|
606 |
^ (stop-rest) to:start by:step negated |
|
607 |
]. |
|
608 |
^ super reversed |
|
609 |
||
610 |
||
611 |
" |
|
612 |
(1 to:4) reversed asOrderedCollection |
|
613 |
(1 to:4) reversed reversed asOrderedCollection |
|
614 |
(1 to:4) asOrderedCollection reversed |
|
615 |
(1 to:4) asOrderedCollection reversed reversed |
|
616 |
||
617 |
(2 to:5 by: 2) asOrderedCollection |
|
618 |
(2 to:5 by: 2) asOrderedCollection reversed |
|
619 |
(2 to:5 by: 2) asOrderedCollection reversed reversed |
|
620 |
(2 to:5 by: 2) reversed asOrderedCollection |
|
621 |
(2 to:5 by: 2) reversed reversed asOrderedCollection |
|
622 |
||
623 |
(1 to:2 by: 0.3) asOrderedCollection |
|
624 |
(1 to:2 by: 0.3) asOrderedCollection reversed |
|
625 |
(1 to:2 by: 0.3) asOrderedCollection reversed reversed |
|
626 |
(1 to:2 by: 0.3) reversed asOrderedCollection |
|
627 |
(1 to:2 by: 0.3) reversed reversed asOrderedCollection |
|
628 |
" |
|
629 |
! ! |
|
630 |
||
6671 | 631 |
!Interval methodsFor:'testing'! |
632 |
||
6712 | 633 |
includes:anElement |
11902 | 634 |
"return true if anElement is in the interval (Numeric compare using =)" |
635 |
||
6712 | 636 |
|rest| |
11902 | 637 |
|
6712 | 638 |
stop >= start ifTrue:[ |
639 |
(anElement between:start and:stop) ifFalse:[^ false]. |
|
640 |
] ifFalse:[ |
|
641 |
(anElement between:stop and:start) ifFalse:[^ false]. |
|
642 |
]. |
|
643 |
rest := (anElement - start) rem:step. |
|
644 |
^ rest = 0 |
|
645 |
||
646 |
" |
|
647 |
(1 to:15) includes:0 |
|
648 |
(1 to:15) includes:16 |
|
649 |
(1 to:15) includes:1 |
|
650 |
(1 to:15) includes:15 |
|
651 |
(1 to:15) includes:5 |
|
652 |
(1 to:15) includes:14 |
|
11902 | 653 |
(1 to:15) includes:4 |
6712 | 654 |
(1 to:15) includes:4.0 |
655 |
(1 to:15) includes:4.4 |
|
656 |
||
657 |
(1 to:15 by:3) includes:0 |
|
658 |
(1 to:15 by:3) includes:16 |
|
659 |
(1 to:15 by:3) includes:1 |
|
660 |
(1 to:15 by:3) includes:15 |
|
661 |
(1 to:15 by:3) includes:5 |
|
662 |
(1 to:15 by:3) includes:4 |
|
663 |
(1 to:15 by:3) includes:13 |
|
664 |
(1 to:15 by:3) includes:14 |
|
665 |
(1 to:15 by:3) includes:4.0 |
|
666 |
(1 to:15 by:3) includes:4.4 |
|
667 |
||
668 |
(10 to:-10 by:-3) includes:11 |
|
669 |
(10 to:-10 by:-3) includes:10 |
|
670 |
(10 to:-10 by:-3) includes:9 |
|
671 |
(10 to:-10 by:-3) includes:8 |
|
672 |
(10 to:-10 by:-3) includes:7 |
|
673 |
(10 to:-10 by:-3) includes:4 |
|
674 |
(10 to:-10 by:-3) includes:0 |
|
675 |
(10 to:-10 by:-3) includes:-1 |
|
676 |
(10 to:-10 by:-3) includes:-2 |
|
677 |
(10 to:-10 by:-3) includes:-8 |
|
678 |
(10 to:-10 by:-3) includes:-9 |
|
679 |
(10 to:-10 by:-3) includes:-10 |
|
680 |
(10 to:-10 by:-3) includes:-11 |
|
681 |
(10 to:-10 by:-3) includes:-2.4 |
|
682 |
||
683 |
(-10 to:-20 by:-2) includes:-16 |
|
684 |
(-10 to:-20 by:-2) includes:-20 |
|
685 |
(-10 to:-20 by:-2) includes:-23 |
|
686 |
(-10 to:-20 by:-2) includes:-24 |
|
687 |
||
688 |
" |
|
6671 | 689 |
! ! |
690 |
||
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
691 |
!Interval methodsFor:'visiting'! |
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 |
acceptVisitor:aVisitor with:aParameter |
16722 | 694 |
"dispatch for visitor pattern; send #visitInterval:with: to aVisitor. |
695 |
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
|
696 |
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
|
697 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
698 |
^ aVisitor visitInterval:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
699 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8374
diff
changeset
|
700 |
|
629 | 701 |
!Interval class methodsFor:'documentation'! |
702 |
||
703 |
version |
|
18510 | 704 |
^ '$Header$' |
13074
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
705 |
! |
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
706 |
|
cbbd50931549
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
11903
diff
changeset
|
707 |
version_CVS |
18510 | 708 |
^ '$Header$' |
629 | 709 |
! ! |
16709 | 710 |