author | Claus Gittinger <cg@exept.de> |
Wed, 10 Apr 2019 06:08:33 +0200 | |
changeset 24050 | f39b5494bc52 |
parent 21838 | 0682509bb3fb |
child 24396 | 3dfb65acb1f0 |
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 |
" |
|
21838 | 41 |
This is an abstract class defining 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 |
21838 | 104 |
"return the receiver if it is between min .. max, |
105 |
or min if it is less than min, or max if it is greater than max. |
|
1226 | 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 |
||
21445 | 118 |
"Modified: / 19-04-1996 / 14:58:12 / cg" |
119 |
"Modified (comment): / 13-02-2017 / 20:26:41 / cg" |
|
21838 | 120 |
"Modified (comment): / 19-06-2017 / 14:59:47 / mawalch" |
1226 | 121 |
! |
122 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
123 |
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
|
124 |
"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
|
125 |
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
|
126 |
comparison is expensive." |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
127 |
|
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
128 |
self < arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
129 |
^ lessBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
130 |
]. |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
131 |
self = arg ifTrue:[ |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
132 |
^ equalBlock 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 |
^ greaterBlock value |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
135 |
! |
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
136 |
|
19006 | 137 |
compareWith:arg |
138 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
139 |
greater, 0 if equal and -1 if less than the argument." |
|
140 |
||
141 |
self < arg ifTrue:[ |
|
142 |
^ -1 |
|
143 |
]. |
|
144 |
self = arg ifTrue:[ |
|
145 |
^ 0 |
|
146 |
]. |
|
147 |
^ 1 |
|
148 |
||
149 |
" |
|
150 |
100 compareWith: 101 |
|
151 |
" |
|
152 |
! |
|
153 |
||
16275 | 154 |
hash |
155 |
"instances, for which #= answers true must answer the same hash" |
|
156 |
||
157 |
^ self subclassResponsibility |
|
158 |
! |
|
159 |
||
699 | 160 |
max:aMagnitude |
161 |
"return the receiver or the argument, whichever has greater magnitude" |
|
162 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
163 |
(aMagnitude < self) ifTrue:[^ self]. |
699 | 164 |
^ aMagnitude |
165 |
||
166 |
" |
|
167 |
1 max: 2 |
|
168 |
1 max: 2.0 |
|
169 |
2.0 max: 1.0 |
|
170 |
2.0 max: 2 |
|
171 |
" |
|
172 |
! |
|
173 |
||
213 | 174 |
min:aMagnitude |
175 |
"return the receiver or the argument, whichever has lesser magnitude" |
|
176 |
||
8181
b2560b88a5b1
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
177 |
(self < aMagnitude) ifTrue:[^ self]. |
213 | 178 |
^ aMagnitude |
179 |
||
180 |
" |
|
181 |
1 min: 2 |
|
182 |
1 min: 2.0 |
|
183 |
2.0 min: 1.0 |
|
184 |
2.0 min: 2 |
|
185 |
" |
|
1 | 186 |
! ! |
187 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
188 |
!Magnitude methodsFor:'iteration'! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
189 |
|
11602 | 190 |
downTo:stop by:step do:aBlock |
191 |
"For each element of the interval from the receiver down to the argument stop, |
|
192 |
decrementing by step, evaluate aBlock, passing the number as argument." |
|
193 |
||
194 |
^ self to:stop by:step negated do:aBlock |
|
195 |
||
196 |
" |
|
197 |
10 downTo:1 by:2 do:[:i | Transcript showCR:i]. |
|
198 |
" |
|
199 |
! |
|
200 |
||
6073 | 201 |
downTo:stop do:aBlock |
202 |
"For each element of the interval from the receiver down to the argument stop, |
|
203 |
evaluate aBlock, passing the number as argument." |
|
204 |
||
205 |
^ self to:stop by:-1 do:aBlock |
|
206 |
||
207 |
" |
|
208 |
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
|
209 |
$d downTo:$a do:[:i | Transcript showCR:i]. |
6073 | 210 |
" |
211 |
! |
|
212 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
213 |
to:stop by:incr do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
214 |
"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
|
215 |
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
|
216 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
217 |
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
|
218 |
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
|
219 |
|
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
220 |
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
|
221 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
222 |
|tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
223 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
224 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
225 |
"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
|
226 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
227 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
228 |
[tmp < stop] whileFalse:[ |
6073 | 229 |
aBlock value:tmp. |
230 |
tmp := tmp+incr |
|
231 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
232 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
233 |
[stop < tmp] whileFalse:[ |
6073 | 234 |
aBlock value:tmp. |
235 |
tmp := tmp+incr |
|
236 |
] |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
237 |
] |
6073 | 238 |
|
239 |
" |
|
240 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
241 |
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
|
242 |
$a to:$z by:2 do:[:i | Transcript showCR:i]. |
6073 | 243 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
244 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
245 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
246 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
247 |
to:stop by:incr doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
248 |
"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
|
249 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
250 |
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
|
251 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
252 |
|tmp break| |
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 |
break := [^ self]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
255 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
256 |
"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
|
257 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
258 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
259 |
[tmp < stop] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
260 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
261 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
262 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
263 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
264 |
[stop < tmp] whileFalse:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
265 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
266 |
tmp := tmp+incr |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
267 |
] |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
268 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
269 |
|
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
270 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
271 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
272 |
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
|
273 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
274 |
index > 50 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
275 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
276 |
]. |
5565
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 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
280 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
281 |
to:stop by:incr doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
282 |
"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
|
283 |
by step, evaluate aBlock passing the element as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
284 |
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
|
285 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
286 |
|tmp exit| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
287 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
288 |
exit := [:exitValue | ^ exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
289 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
290 |
"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
|
291 |
"/ incr negative ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
292 |
incr < 0 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
293 |
[tmp < stop] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
294 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
295 |
tmp := tmp+incr |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
296 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
297 |
] ifFalse:[ |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
298 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
299 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
300 |
tmp := tmp+incr |
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 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
304 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
305 |
1 to:100 by:5 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
306 |
Transcript showCR:index printString. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
307 |
index > 50 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
308 |
exit value:nil |
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 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
312 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
313 |
|
11837 | 314 |
to:stop count:aBlock |
315 |
"same as (self to:stop) count:aBlock" |
|
316 |
||
317 |
|cnt| |
|
318 |
||
319 |
cnt := 0. |
|
320 |
self to:stop do:[:i | |
|
321 |
(aBlock value:i) ifTrue:[ cnt := cnt + 1 ]. |
|
322 |
]. |
|
323 |
^ cnt |
|
324 |
||
325 |
" |
|
326 |
1 to:10 count:[:n | n even] |
|
327 |
" |
|
328 |
! |
|
329 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
330 |
to:stop do:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
331 |
"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
|
332 |
evaluate aBlock, passing the number as argument." |
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| |
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 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
337 |
[stop < tmp] whileFalse:[ |
6073 | 338 |
aBlock value:tmp. |
339 |
tmp := tmp+1 |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
340 |
] |
6073 | 341 |
|
342 |
" |
|
343 |
1 to:10 do:[:i | Transcript showCR:i]. |
|
344 |
1 to:10 by:2 do:[:i | Transcript showCR:i]. |
|
345 |
10 to:1 by:-1 do:[:i | Transcript showCR:i]. |
|
346 |
" |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
347 |
! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
348 |
|
21806 | 349 |
to:stop do:aBlock separatedBy:actionBetween |
350 |
"For each element of the interval from the receiver up to the argument stop, |
|
351 |
evaluate aBlock, passing the number as argument. |
|
352 |
Between each, evaluate actionBetween (but not before the first and not after the last)" |
|
353 |
||
354 |
|tmp first| |
|
355 |
||
356 |
tmp := self. |
|
357 |
first := true. |
|
358 |
[stop < tmp] whileFalse:[ |
|
359 |
first ifFalse:[ |
|
360 |
actionBetween value. |
|
361 |
]. |
|
362 |
first := false. |
|
363 |
aBlock value:tmp. |
|
364 |
tmp := tmp+1. |
|
365 |
] |
|
366 |
||
367 |
" |
|
368 |
1 to:10 |
|
369 |
do:[:i | Transcript show:i] |
|
370 |
separatedBy:[ Transcript show:', ']. |
|
371 |
" |
|
372 |
||
373 |
"Created: / 14-06-2017 / 15:23:21 / cg" |
|
374 |
! |
|
375 |
||
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
376 |
to:stop doWithBreak:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
377 |
"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
|
378 |
evaluate aBlock, passing the number as argument. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
379 |
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
|
380 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
381 |
|tmp break| |
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 |
break := [^ self]. |
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:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
386 |
aBlock value:tmp value:break. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
387 |
tmp := tmp+1 |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
388 |
] |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
389 |
|
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 |
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
|
392 |
Transcript showCR:index printString. |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
393 |
index > 5 ifTrue:[ |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
394 |
break value |
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
395 |
]. |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
396 |
] |
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 |
to:stop doWithExit:aBlock |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
401 |
"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
|
402 |
evaluate aBlock, passing the number as argument. |
15374 | 403 |
Passes an additional exit object, which can be used to leave |
404 |
the loop early, by sending it a #value: message. |
|
405 |
Returns nil or the value passed to the exit>>value: message. |
|
406 |
||
407 |
Notice, that this is different to a return statement in the block, |
|
408 |
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
|
409 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
410 |
|exit tmp| |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
411 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
412 |
exit := [:exitValue | ^exitValue]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
413 |
tmp := self. |
8204
ac5db5b88079
Only use the base compare mechanism, #< for iterating
Stefan Vogel <sv@exept.de>
parents:
8181
diff
changeset
|
414 |
[stop < tmp] whileFalse:[ |
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
415 |
aBlock value:tmp value:exit. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
416 |
tmp := tmp+1 |
15374 | 417 |
]. |
418 |
^ nil |
|
5565
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
419 |
|
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
420 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
421 |
1 to:10 doWithExit:[:index :exit | |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
422 |
index == 5 ifTrue:[ |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
423 |
exit value:nil |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
424 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
425 |
Transcript showCR:index. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
426 |
]. |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
427 |
" |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
428 |
! ! |
a815d6b86ace
moved iterators up in hierarchy
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
429 |
|
1 | 430 |
!Magnitude methodsFor:'testing'! |
431 |
||
432 |
between:min and:max |
|
21838 | 433 |
"return true if the receiver is greater than or equal to the argument min |
18396 | 434 |
and less than or equal to the argument max" |
1 | 435 |
|
436 |
(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
|
437 |
(max < self) ifTrue:[^ false]. |
1 | 438 |
^ true |
56 | 439 |
|
213 | 440 |
" |
441 |
1.2 between:1 and:2 |
|
442 |
(3/2) between:1 and:2 |
|
443 |
(3/2) between:0 and:1 |
|
444 |
" |
|
21838 | 445 |
|
446 |
"Modified (comment): / 19-06-2017 / 14:59:05 / mawalch" |
|
1 | 447 |
! |
448 |
||
449 |
in:anInterval |
|
450 |
"return whether the receiver is within the interval bounds" |
|
451 |
||
452 |
(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
|
453 |
(anInterval stop < self) ifTrue:[^ false]. |
1 | 454 |
^ true |
56 | 455 |
|
213 | 456 |
" |
457 |
1.2 in:(1 to: 2) |
|
458 |
(3/2) in:(1 to: 2) |
|
459 |
(3/2) in:(0 to: 1) |
|
460 |
" |
|
1 | 461 |
! ! |
699 | 462 |
|
463 |
!Magnitude class methodsFor:'documentation'! |
|
464 |
||
465 |
version |
|
19005 | 466 |
^ '$Header$' |
19006 | 467 |
! |
468 |
||
469 |
version_CVS |
|
470 |
^ '$Header$' |
|
699 | 471 |
! ! |
15374 | 472 |