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