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