author | Claus Gittinger <cg@exept.de> |
Mon, 10 Aug 2009 15:30:08 +0200 | |
changeset 11837 | 27fb4984e97b |
parent 11602 | b7c26da0a6c3 |
child 15374 | 13a5c9dae49b |
child 18011 | deb0c3355881 |
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 |
|
1 | 14 |
Object subclass:#Magnitude |
1226 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Magnitude-General' |
|
1 | 19 |
! |
20 |
||
88 | 21 |
!Magnitude class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
213 | 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 |
This is an abstract class definining common methods for |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
40 |
Objects which can be compared by a kind of less than relation. |
1295 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
88 | 44 |
" |
45 |
! ! |
|
1 | 46 |
|
8892 | 47 |
!Magnitude class methodsFor:'queries'! |
48 |
||
49 |
isAbstract |
|
11226 | 50 |
"Return if this class is an abstract class. |
51 |
True is returned for Magnitude here; false for subclasses. |
|
52 |
Abstract subclasses must redefine again." |
|
53 |
||
8892 | 54 |
^ self == Magnitude |
55 |
! ! |
|
56 |
||
10600 | 57 |
!Magnitude methodsFor:'Compatibility-Squeak'! |
58 |
||
59 |
min:aMin max:aMax |
|
60 |
"similar to clampBetween:and:" |
|
61 |
||
62 |
^ (self min: aMin) max: aMax |
|
63 |
||
64 |
"Created: / 06-06-2007 / 11:00:22 / cg" |
|
65 |
! ! |
|
66 |
||
1 | 67 |
!Magnitude methodsFor:'comparing'! |
68 |
||
699 | 69 |
< aMagnitude |
1 | 70 |
"Compare the receiver with the argument and return true if the |
699 | 71 |
receiver is less than the argument. Otherwise return false." |
72 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
73 |
^ self subclassResponsibility |
699 | 74 |
! |
1 | 75 |
|
699 | 76 |
<= aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
77 |
"return true, if the argument is greater or equal than the receiver" |
699 | 78 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
79 |
^ (aMagnitude < self) not |
1 | 80 |
! |
81 |
||
82 |
= aMagnitude |
|
83 |
"Compare the receiver with the argument and return true if the |
|
84 |
receiver is equal to the argument. Otherwise return false." |
|
85 |
||
86 |
^ self subclassResponsibility |
|
87 |
! |
|
88 |
||
699 | 89 |
> aMagnitude |
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
90 |
"return true, if the argument is less than the receiver" |
1 | 91 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
92 |
^ aMagnitude < self |
1 | 93 |
! |
94 |
||
95 |
>= aMagnitude |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
96 |
"return true, if the argument is less or equal than the receiver" |
1 | 97 |
|
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
98 |
^ (self < aMagnitude) not |
213 | 99 |
! |
100 |
||
1226 | 101 |
clampBetween:min and:max |
102 |
"return the receiver if its between min .. max, |
|
103 |
or min if its less than min, or max of its greater than max. |
|
104 |
This is only a lazy-typers helper for: ((something min:max) max:min)" |
|
105 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
106 |
(max < self) ifTrue:[^ max]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
107 |
(self < min) ifTrue:[^ min]. |
1226 | 108 |
^ self |
109 |
||
110 |
" |
|
111 |
1 clampBetween:2 and:5 |
|
112 |
3 clampBetween:2 and:5 |
|
113 |
6 clampBetween:2 and:5 |
|
114 |
" |
|
115 |
||
116 |
"Modified: 19.4.1996 / 14:58:12 / cg" |
|
117 |
! |
|
118 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
119 |
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
|
120 |
"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
|
121 |
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
|
122 |
comparison is expensive." |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
123 |
|
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
124 |
self < arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
125 |
^ lessBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
126 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
127 |
self = arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
128 |
^ equalBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
129 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
130 |
^ greaterBlock 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 |
|
699 | 133 |
max:aMagnitude |
134 |
"return the receiver or the argument, whichever has greater magnitude" |
|
135 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
136 |
(aMagnitude < self) ifTrue:[^ self]. |
699 | 137 |
^ aMagnitude |
138 |
||
139 |
" |
|
140 |
1 max: 2 |
|
141 |
1 max: 2.0 |
|
142 |
2.0 max: 1.0 |
|
143 |
2.0 max: 2 |
|
144 |
" |
|
145 |
! |
|
146 |
||
213 | 147 |
min:aMagnitude |
148 |
"return the receiver or the argument, whichever has lesser magnitude" |
|
149 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
150 |
(self < aMagnitude) ifTrue:[^ self]. |
213 | 151 |
^ aMagnitude |
152 |
||
153 |
" |
|
154 |
1 min: 2 |
|
155 |
1 min: 2.0 |
|
156 |
2.0 min: 1.0 |
|
157 |
2.0 min: 2 |
|
158 |
" |
|
1 | 159 |
! ! |
160 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
161 |
!Magnitude methodsFor:'iteration'! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
162 |
|
11602 | 163 |
downTo:stop by:step do:aBlock |
164 |
"For each element of the interval from the receiver down to the argument stop, |
|
165 |
decrementing by step, evaluate aBlock, passing the number as argument." |
|
166 |
||
167 |
^ self to:stop by:step negated do:aBlock |
|
168 |
||
169 |
" |
|
170 |
10 downTo:1 by:2 do:[:i | Transcript showCR:i]. |
|
171 |
" |
|
172 |
! |
|
173 |
||
6073 | 174 |
downTo:stop do:aBlock |
175 |
"For each element of the interval from the receiver down to the argument stop, |
|
176 |
evaluate aBlock, passing the number as argument." |
|
177 |
||
178 |
^ self to:stop by:-1 do:aBlock |
|
179 |
||
180 |
" |
|
181 |
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
|
182 |
$d downTo:$a do:[:i | Transcript showCR:i]. |
6073 | 183 |
" |
184 |
! |
|
185 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
186 |
to:stop by:incr do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
187 |
"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
|
188 |
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
|
189 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
193 |
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
|
194 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
195 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
196 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
197 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
198 |
"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
|
199 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
200 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
201 |
[tmp < stop] whileFalse:[ |
6073 | 202 |
aBlock value:tmp. |
203 |
tmp := tmp+incr |
|
204 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
205 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
206 |
[stop < tmp] whileFalse:[ |
6073 | 207 |
aBlock value:tmp. |
208 |
tmp := tmp+incr |
|
209 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
210 |
] |
6073 | 211 |
|
212 |
" |
|
213 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
214 |
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
|
215 |
$a to:$z by:2 do:[:i | Transcript showCR:i]. |
6073 | 216 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
217 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
218 |
! |
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 |
to:stop by:incr doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
221 |
"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
|
222 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
223 |
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
|
224 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
225 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
226 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
227 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
228 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
229 |
"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
|
230 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
231 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
232 |
[tmp < stop] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
233 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
234 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
235 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
236 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
237 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
238 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
239 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
240 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
241 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
242 |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
243 |
|
5565
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 |
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
|
246 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
247 |
index > 50 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
248 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
249 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
250 |
] |
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 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
253 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
254 |
to:stop by:incr doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
255 |
"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
|
256 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
257 |
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
|
258 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
259 |
|tmp exit| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
260 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
261 |
exit := [:exitValue | ^ exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
262 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
263 |
"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
|
264 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
265 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
266 |
[tmp < stop] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
267 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
268 |
tmp := tmp+incr |
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 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
271 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
272 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
273 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
274 |
] |
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 |
1 to:100 by:5 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
279 |
Transcript showCR:index printString. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
280 |
index > 50 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
281 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
282 |
]. |
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 |
" |
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 |
|
11837 | 287 |
to:stop count:aBlock |
288 |
"same as (self to:stop) count:aBlock" |
|
289 |
||
290 |
|cnt| |
|
291 |
||
292 |
cnt := 0. |
|
293 |
self to:stop do:[:i | |
|
294 |
(aBlock value:i) ifTrue:[ cnt := cnt + 1 ]. |
|
295 |
]. |
|
296 |
^ cnt |
|
297 |
||
298 |
" |
|
299 |
1 to:10 count:[:n | n even] |
|
300 |
" |
|
301 |
! |
|
302 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
303 |
to:stop do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
304 |
"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
|
305 |
evaluate aBlock, passing the number as argument." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
306 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
307 |
|tmp| |
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 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
310 |
[stop < tmp] whileFalse:[ |
6073 | 311 |
aBlock value:tmp. |
312 |
tmp := tmp+1 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
313 |
] |
6073 | 314 |
|
315 |
" |
|
316 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
317 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
318 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
|
319 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
320 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
321 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
322 |
to:stop doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
323 |
"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
|
324 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
325 |
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
|
326 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
327 |
|tmp break| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
328 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
329 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
330 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
331 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
332 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
333 |
tmp := tmp+1 |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
334 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
335 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
336 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
337 |
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
|
338 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
339 |
index > 5 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
340 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
341 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
342 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
343 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
344 |
! |
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 |
to:stop doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
347 |
"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
|
348 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
349 |
An exitBlock is passed as second argument, which allows for the loop to be terminated early." |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
350 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
351 |
|exit tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
352 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
353 |
exit := [:exitValue | ^exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
354 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
355 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
356 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
357 |
tmp := tmp+1 |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
358 |
] |
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 |
1 to:10 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
362 |
index == 5 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
363 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
364 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
365 |
Transcript showCR:index. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
366 |
]. |
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 |
|
1 | 370 |
!Magnitude methodsFor:'testing'! |
371 |
||
372 |
between:min and:max |
|
373 |
"return whether the receiver is less than or equal to the argument max |
|
374 |
and greater than or equal to the argument min." |
|
375 |
||
376 |
(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
|
377 |
(max < self) ifTrue:[^ false]. |
1 | 378 |
^ true |
56 | 379 |
|
213 | 380 |
" |
381 |
1.2 between:1 and:2 |
|
382 |
(3/2) between:1 and:2 |
|
383 |
(3/2) between:0 and:1 |
|
384 |
" |
|
1 | 385 |
! |
386 |
||
387 |
in:anInterval |
|
388 |
"return whether the receiver is within the interval bounds" |
|
389 |
||
390 |
(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
|
391 |
(anInterval stop < self) ifTrue:[^ false]. |
1 | 392 |
^ true |
56 | 393 |
|
213 | 394 |
" |
395 |
1.2 in:(1 to: 2) |
|
396 |
(3/2) in:(1 to: 2) |
|
397 |
(3/2) in:(0 to: 1) |
|
398 |
" |
|
1 | 399 |
! ! |
699 | 400 |
|
401 |
!Magnitude class methodsFor:'documentation'! |
|
402 |
||
403 |
version |
|
11837 | 404 |
^ '$Header: /cvs/stx/stx/libbasic/Magnitude.st,v 1.25 2009-08-10 13:30:08 cg Exp $' |
699 | 405 |
! ! |