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