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