author | Jan Vrany <jan.vrany@labware.com> |
Wed, 03 Feb 2021 11:52:30 +0000 | |
branch | jv |
changeset 25410 | c91ea94445d1 |
parent 21246 | 958aa6e341d4 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
213 | 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 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
13 |
|
18396 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
Object subclass:#Magnitude |
1226 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Magnitude-General' |
|
1 | 21 |
! |
22 |
||
88 | 23 |
!Magnitude class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
213 | 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 |
This is an abstract class definining common methods for |
|
21154 | 42 |
Objects which can be compared by a kind of less-than relation. |
1295 | 43 |
|
44 |
[author:] |
|
45 |
Claus Gittinger |
|
88 | 46 |
" |
47 |
! ! |
|
1 | 48 |
|
8892 | 49 |
!Magnitude class methodsFor:'queries'! |
50 |
||
51 |
isAbstract |
|
11226 | 52 |
"Return if this class is an abstract class. |
53 |
True is returned for Magnitude here; false for subclasses. |
|
19456 | 54 |
Abstract subclasses must redefine this again." |
11226 | 55 |
|
8892 | 56 |
^ self == Magnitude |
57 |
! ! |
|
58 |
||
10600 | 59 |
!Magnitude methodsFor:'Compatibility-Squeak'! |
60 |
||
61 |
min:aMin max:aMax |
|
62 |
"similar to clampBetween:and:" |
|
63 |
||
64 |
^ (self min: aMin) max: aMax |
|
65 |
||
66 |
"Created: / 06-06-2007 / 11:00:22 / cg" |
|
67 |
! ! |
|
68 |
||
1 | 69 |
!Magnitude methodsFor:'comparing'! |
70 |
||
699 | 71 |
< aMagnitude |
1 | 72 |
"Compare the receiver with the argument and return true if the |
699 | 73 |
receiver is less than the argument. Otherwise return false." |
74 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
75 |
^ self subclassResponsibility |
699 | 76 |
! |
1 | 77 |
|
699 | 78 |
<= aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
79 |
"return true, if the argument is greater or equal than the receiver" |
699 | 80 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
81 |
^ (aMagnitude < self) not |
1 | 82 |
! |
83 |
||
84 |
= aMagnitude |
|
85 |
"Compare the receiver with the argument and return true if the |
|
86 |
receiver is equal to the argument. Otherwise return false." |
|
87 |
||
88 |
^ self subclassResponsibility |
|
89 |
! |
|
90 |
||
699 | 91 |
> aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
92 |
"return true, if the argument is less than the receiver" |
1 | 93 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
94 |
^ aMagnitude < self |
1 | 95 |
! |
96 |
||
97 |
>= aMagnitude |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
98 |
"return true, if the argument is less or equal than the receiver" |
1 | 99 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
100 |
^ (self < aMagnitude) not |
213 | 101 |
! |
102 |
||
1226 | 103 |
clampBetween:min and:max |
104 |
"return the receiver if its between min .. max, |
|
105 |
or min if its less than min, or max of its greater than max. |
|
106 |
This is only a lazy-typers helper for: ((something min:max) max:min)" |
|
107 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
108 |
(max < self) ifTrue:[^ max]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
109 |
(self < min) ifTrue:[^ min]. |
1226 | 110 |
^ self |
111 |
||
112 |
" |
|
113 |
1 clampBetween:2 and:5 |
|
114 |
3 clampBetween:2 and:5 |
|
115 |
6 clampBetween:2 and:5 |
|
116 |
" |
|
117 |
||
118 |
"Modified: 19.4.1996 / 14:58:12 / cg" |
|
119 |
! |
|
120 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
121 |
compare:arg ifLess:lessBlock ifEqual:equalBlock ifGreater:greaterBlock |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
122 |
"three-way compare - thanks to Self for this idea. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
123 |
Can be redefined in subclasses to do it with a single comparison if |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
124 |
comparison is expensive." |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
125 |
|
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
126 |
self < arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
127 |
^ lessBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
128 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
129 |
self = arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
130 |
^ equalBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
131 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
132 |
^ greaterBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
133 |
! |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
134 |
|
19006 | 135 |
compareWith:arg |
136 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
137 |
greater, 0 if equal and -1 if less than the argument." |
|
138 |
||
139 |
self < arg ifTrue:[ |
|
140 |
^ -1 |
|
141 |
]. |
|
142 |
self = arg ifTrue:[ |
|
143 |
^ 0 |
|
144 |
]. |
|
145 |
^ 1 |
|
146 |
||
147 |
" |
|
148 |
100 compareWith: 101 |
|
149 |
" |
|
150 |
! |
|
151 |
||
16275 | 152 |
hash |
153 |
"instances, for which #= answers true must answer the same hash" |
|
154 |
||
155 |
^ self subclassResponsibility |
|
156 |
! |
|
157 |
||
699 | 158 |
max:aMagnitude |
159 |
"return the receiver or the argument, whichever has greater magnitude" |
|
160 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
161 |
(aMagnitude < self) ifTrue:[^ self]. |
699 | 162 |
^ aMagnitude |
163 |
||
164 |
" |
|
165 |
1 max: 2 |
|
166 |
1 max: 2.0 |
|
167 |
2.0 max: 1.0 |
|
168 |
2.0 max: 2 |
|
169 |
" |
|
170 |
! |
|
171 |
||
213 | 172 |
min:aMagnitude |
173 |
"return the receiver or the argument, whichever has lesser magnitude" |
|
174 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
175 |
(self < aMagnitude) ifTrue:[^ self]. |
213 | 176 |
^ aMagnitude |
177 |
||
178 |
" |
|
179 |
1 min: 2 |
|
180 |
1 min: 2.0 |
|
181 |
2.0 min: 1.0 |
|
182 |
2.0 min: 2 |
|
183 |
" |
|
1 | 184 |
! ! |
185 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
186 |
!Magnitude methodsFor:'iteration'! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
187 |
|
11602 | 188 |
downTo:stop by:step do:aBlock |
189 |
"For each element of the interval from the receiver down to the argument stop, |
|
190 |
decrementing by step, evaluate aBlock, passing the number as argument." |
|
191 |
||
192 |
^ self to:stop by:step negated do:aBlock |
|
193 |
||
194 |
" |
|
195 |
10 downTo:1 by:2 do:[:i | Transcript showCR:i]. |
|
196 |
" |
|
197 |
! |
|
198 |
||
6073 | 199 |
downTo:stop do:aBlock |
200 |
"For each element of the interval from the receiver down to the argument stop, |
|
201 |
evaluate aBlock, passing the number as argument." |
|
202 |
||
203 |
^ self to:stop by:-1 do:aBlock |
|
204 |
||
205 |
" |
|
206 |
10 downTo:1 do:[:i | Transcript showCR:i]. |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
207 |
$d downTo:$a do:[:i | Transcript showCR:i]. |
6073 | 208 |
" |
209 |
! |
|
210 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
211 |
to:stop by:incr do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
212 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
213 |
by step, evaluate aBlock passing the element as argument. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
214 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
215 |
Not all Magnitudes do implement #negative and #+ however, |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
216 |
so should this method go to ArithmethicValue? |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
217 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
218 |
Only use #<, to speed up things (for subclasses only defining #<)" |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
219 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
220 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
221 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
222 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
223 |
"using #negative would be more portable, but stc does only inline #< but not #negative (yet)" |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
224 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
225 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
226 |
[tmp < stop] whileFalse:[ |
6073 | 227 |
aBlock value:tmp. |
228 |
tmp := tmp+incr |
|
229 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
230 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
231 |
[stop < tmp] whileFalse:[ |
6073 | 232 |
aBlock value:tmp. |
233 |
tmp := tmp+incr |
|
234 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
235 |
] |
6073 | 236 |
|
237 |
" |
|
238 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
239 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
240 |
$a to:$z by:2 do:[:i | Transcript showCR:i]. |
6073 | 241 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
242 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
243 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
244 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
245 |
to:stop by:incr doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
246 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
247 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
248 |
Pass a break argument, to allow for premature exit of the loop." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
249 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
250 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
251 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
252 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
253 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
254 |
"using #negative would be more portable, but stc does only inline #< but not #negative (yet)" |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
255 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
256 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
257 |
[tmp < stop] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
258 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
259 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
260 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
261 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
262 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
263 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
264 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
265 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
266 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
267 |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
268 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
269 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
270 |
1 to:100 by:5 doWithBreak:[:index :break | |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
271 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
272 |
index > 50 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
273 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
274 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
275 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
276 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
277 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
278 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
279 |
to:stop by:incr doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
280 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
281 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
282 |
Pass a break argument, to allow for premature exit of the loop." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
283 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
284 |
|tmp exit| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
285 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
286 |
exit := [:exitValue | ^ exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
287 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
288 |
"using #negative would be more portable, but stc does only inline #< but not #negative (yet)" |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
289 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
290 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
291 |
[tmp < stop] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
292 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
293 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
294 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
295 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
296 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
297 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
298 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
299 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
300 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
301 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
302 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
303 |
1 to:100 by:5 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
304 |
Transcript showCR:index printString. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
305 |
index > 50 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
306 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
307 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
308 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
309 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
310 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
311 |
|
11837 | 312 |
to:stop count:aBlock |
313 |
"same as (self to:stop) count:aBlock" |
|
314 |
||
315 |
|cnt| |
|
316 |
||
317 |
cnt := 0. |
|
318 |
self to:stop do:[:i | |
|
319 |
(aBlock value:i) ifTrue:[ cnt := cnt + 1 ]. |
|
320 |
]. |
|
321 |
^ cnt |
|
322 |
||
323 |
" |
|
324 |
1 to:10 count:[:n | n even] |
|
325 |
" |
|
326 |
! |
|
327 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
328 |
to:stop do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
329 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
330 |
evaluate aBlock, passing the number as argument." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
331 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
332 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
333 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
334 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
335 |
[stop < tmp] whileFalse:[ |
6073 | 336 |
aBlock value:tmp. |
337 |
tmp := tmp+1 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
338 |
] |
6073 | 339 |
|
340 |
" |
|
341 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
342 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
343 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
|
344 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
345 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
346 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
347 |
to:stop doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
348 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
349 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
350 |
Pass a break argument, to allow for premature exit of the loop." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
351 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
352 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
353 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
354 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
355 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
356 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
357 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
358 |
tmp := tmp+1 |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
359 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
360 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
361 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
362 |
1 to:10 doWithBreak:[:index :break | |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
363 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
364 |
index > 5 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
365 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
366 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
367 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
368 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
369 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
370 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
371 |
to:stop doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
372 |
"For each element of the interval from the receiver up to the argument stop, |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
373 |
evaluate aBlock, passing the number as argument. |
15374 | 374 |
Passes an additional exit object, which can be used to leave |
375 |
the loop early, by sending it a #value: message. |
|
376 |
Returns nil or the value passed to the exit>>value: message. |
|
377 |
||
378 |
Notice, that this is different to a return statement in the block, |
|
379 |
which returns from the enclosed method, NOT only from the block." |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
380 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
381 |
|exit tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
382 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
383 |
exit := [:exitValue | ^exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
384 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
385 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
386 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
387 |
tmp := tmp+1 |
15374 | 388 |
]. |
389 |
^ nil |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
390 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
391 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
392 |
1 to:10 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
393 |
index == 5 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
394 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
395 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
396 |
Transcript showCR:index. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
397 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
398 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
399 |
! ! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
400 |
|
1 | 401 |
!Magnitude methodsFor:'testing'! |
402 |
||
403 |
between:min and:max |
|
18396 | 404 |
"return true if the receiver greater than or equal to the argument min |
405 |
and less than or equal to the argument max" |
|
1 | 406 |
|
407 |
(self < min) ifTrue:[^ false]. |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
408 |
(max < self) ifTrue:[^ false]. |
1 | 409 |
^ true |
56 | 410 |
|
213 | 411 |
" |
412 |
1.2 between:1 and:2 |
|
413 |
(3/2) between:1 and:2 |
|
414 |
(3/2) between:0 and:1 |
|
415 |
" |
|
1 | 416 |
! |
417 |
||
418 |
in:anInterval |
|
419 |
"return whether the receiver is within the interval bounds" |
|
420 |
||
421 |
(self < anInterval start) ifTrue:[^ false]. |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
422 |
(anInterval stop < self) ifTrue:[^ false]. |
1 | 423 |
^ true |
56 | 424 |
|
213 | 425 |
" |
426 |
1.2 in:(1 to: 2) |
|
427 |
(3/2) in:(1 to: 2) |
|
428 |
(3/2) in:(0 to: 1) |
|
429 |
" |
|
1 | 430 |
! ! |
699 | 431 |
|
432 |
!Magnitude class methodsFor:'documentation'! |
|
433 |
||
434 |
version |
|
19005 | 435 |
^ '$Header$' |
19006 | 436 |
! |
437 |
||
438 |
version_CVS |
|
439 |
^ '$Header$' |
|
699 | 440 |
! ! |
15374 | 441 |