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