1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
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 |
"
|
|
12 |
|
|
13 |
Number subclass:#Integer
|
|
14 |
instanceVariableNames:''
|
49
|
15 |
classVariableNames:'DefaultDisplayRadix'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Magnitude-Numbers'
|
|
18 |
!
|
|
19 |
|
|
20 |
Integer comment:'
|
88
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
92
|
23 |
|
359
|
24 |
$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.28 1995-06-27 02:13:06 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!Integer class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
359
|
45 |
$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.28 1995-06-27 02:13:06 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
abstract superclass for all integer numbers.
|
|
52 |
See details in concrete subclasses LargeInteger and SmallInteger.
|
212
|
53 |
|
|
54 |
Class variables:
|
|
55 |
|
|
56 |
DefaultDisplayRadix the radix in which integers present their
|
|
57 |
displayString (which is used in inspectors)
|
|
58 |
If you are to look at many hex numbers, bitmasks
|
|
59 |
etc. you may set this to 2 or 16.
|
|
60 |
(avoids typing printStringRadix:.. all the time
|
|
61 |
- I know - I am lazy ;-). Default is 10.
|
|
62 |
|
88
|
63 |
"
|
|
64 |
! !
|
1
|
65 |
|
|
66 |
!Integer class methodsFor:'constants'!
|
|
67 |
|
|
68 |
zero
|
44
|
69 |
"return the neutral element for addition"
|
|
70 |
|
1
|
71 |
^ 0
|
|
72 |
!
|
|
73 |
|
|
74 |
unity
|
44
|
75 |
"return the neutral element for multiplication"
|
|
76 |
|
1
|
77 |
^ 1
|
|
78 |
! !
|
|
79 |
|
92
|
80 |
!Integer class methodsFor:'instance creation'!
|
|
81 |
|
|
82 |
new:numberOfBytes neg:negative
|
|
83 |
"for ST-80 compatibility:
|
|
84 |
Return an empty Integer (uninitialized value) with space for
|
|
85 |
numberOfBytes bytes (= digitLength). The additional argument
|
132
|
86 |
negative specifies if the result should be a negative number.
|
|
87 |
The digits can be stored byte-wise into the result, using digitAt:put:"
|
92
|
88 |
|
132
|
89 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes)
|
159
|
90 |
sign:(negative ifTrue:[-1] ifFalse:[1])
|
92
|
91 |
! !
|
|
92 |
|
49
|
93 |
!Integer class methodsFor:'misc'!
|
|
94 |
|
|
95 |
displayRadix:aNumber
|
54
|
96 |
"being tired of always sending #printStringRadix: in the inspectors,
|
49
|
97 |
this allows you to change the default print radix for the displayString
|
|
98 |
method."
|
|
99 |
|
|
100 |
DefaultDisplayRadix := aNumber
|
132
|
101 |
|
|
102 |
"
|
|
103 |
Integer displayRadix:16. 123456 inspect
|
|
104 |
Integer displayRadix:10. 123456 inspect
|
|
105 |
"
|
49
|
106 |
! !
|
|
107 |
|
1
|
108 |
!Integer methodsFor:'arithmetic'!
|
|
109 |
|
|
110 |
quo:aNumber
|
|
111 |
"Return the integer quotient of dividing the receiver by aNumber with
|
54
|
112 |
truncation towards zero. For Integers this is same as //"
|
1
|
113 |
|
|
114 |
^ self // aNumber
|
|
115 |
|
|
116 |
! !
|
|
117 |
|
|
118 |
!Integer methodsFor:'double dispatching'!
|
|
119 |
|
|
120 |
sumFromFraction:aFraction
|
213
|
121 |
"sent when a fraction does not know how to add the receiver, an integer"
|
1
|
122 |
|
16
|
123 |
|d|
|
|
124 |
|
|
125 |
d := aFraction denominator.
|
|
126 |
^ (Fraction numerator:(aFraction numerator + (self * d))
|
159
|
127 |
denominator:d) reduced
|
1
|
128 |
!
|
|
129 |
|
|
130 |
differenceFromFraction:aFraction
|
213
|
131 |
"sent when a fraction does not know how to subtract the receiver, an integer"
|
1
|
132 |
|
16
|
133 |
|d|
|
|
134 |
|
|
135 |
d := aFraction denominator.
|
|
136 |
^ (Fraction numerator:(aFraction numerator - (self * d))
|
159
|
137 |
denominator:d) reduced
|
1
|
138 |
!
|
|
139 |
|
|
140 |
productFromFraction:aFraction
|
213
|
141 |
"sent when a fraction does not know how to multiply the receiver, an integer"
|
1
|
142 |
|
|
143 |
^ (Fraction numerator:(self * aFraction numerator)
|
159
|
144 |
denominator:aFraction denominator) reduced
|
69
|
145 |
!
|
|
146 |
|
|
147 |
quotientFromFraction:aFraction
|
213
|
148 |
"sent when a fraction does not know how to divide the receiver, an integer"
|
69
|
149 |
|
|
150 |
^ (Fraction numerator:aFraction numerator
|
159
|
151 |
denominator:(self * aFraction denominator)) reduced
|
69
|
152 |
! !
|
|
153 |
|
|
154 |
!Integer methodsFor:'bit operators'!
|
|
155 |
|
|
156 |
bitAnd:anInteger
|
357
|
157 |
"return the bitwise-and of the receiver and the argument, anInteger.
|
|
158 |
This is a general and slow implementation, walking over the bytes of
|
|
159 |
the receiver and the argument."
|
69
|
160 |
|
92
|
161 |
|result n byte|
|
69
|
162 |
|
92
|
163 |
n := (anInteger digitLength) min:(self digitLength).
|
|
164 |
result := self class basicNew numberOfDigits:n.
|
69
|
165 |
|
|
166 |
1 to:n do:[:index |
|
159
|
167 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index).
|
|
168 |
result digitAt:index put:byte.
|
69
|
169 |
].
|
357
|
170 |
(byte == 0 or:[n < 5]) ifTrue:[
|
159
|
171 |
^ result normalize
|
92
|
172 |
].
|
|
173 |
^ result
|
357
|
174 |
|
|
175 |
"
|
|
176 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16
|
|
177 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16
|
|
178 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16
|
|
179 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16
|
|
180 |
"
|
69
|
181 |
!
|
|
182 |
|
|
183 |
bitOr:anInteger
|
357
|
184 |
"return the bitwise-or of the receiver and the argument, anInteger.
|
|
185 |
This is a general and slow implementation, walking over the bytes of
|
|
186 |
the receiver and the argument."
|
69
|
187 |
|
92
|
188 |
|result n byte|
|
69
|
189 |
|
92
|
190 |
n := (anInteger digitLength) max:(self digitLength).
|
|
191 |
result := self class basicNew numberOfDigits:n.
|
69
|
192 |
|
|
193 |
1 to:n do:[:index |
|
159
|
194 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index).
|
|
195 |
result digitAt:index put:byte.
|
69
|
196 |
].
|
357
|
197 |
"/ no need to normalize - if the operands were correct
|
|
198 |
"/ byte == 0 ifTrue:[
|
|
199 |
"/ ^ result normalize
|
|
200 |
"/ ].
|
92
|
201 |
^ result
|
69
|
202 |
!
|
|
203 |
|
|
204 |
bitXor:anInteger
|
357
|
205 |
"return the bitwise-or of the receiver and the argument, anInteger.
|
|
206 |
This is a general and slow implementation, walking over the bytes of
|
|
207 |
the receiver and the argument."
|
69
|
208 |
|
92
|
209 |
|result n byte|
|
69
|
210 |
|
92
|
211 |
n := (anInteger digitLength) max:(self digitLength).
|
|
212 |
result := self class basicNew numberOfDigits:n.
|
69
|
213 |
|
|
214 |
1 to:n do:[:index |
|
159
|
215 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index).
|
|
216 |
result digitAt:index put:byte.
|
92
|
217 |
].
|
|
218 |
byte == 0 ifTrue:[
|
159
|
219 |
^ result normalize
|
69
|
220 |
].
|
92
|
221 |
^ result
|
357
|
222 |
|
|
223 |
"
|
|
224 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866'
|
|
225 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799'
|
|
226 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899'
|
|
227 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16
|
|
228 |
"
|
92
|
229 |
!
|
|
230 |
|
|
231 |
highBit
|
|
232 |
"return the bitIndex of the highest bit set. The returned bitIndex
|
|
233 |
starts at 1 for the least significant bit. Returns -1 if no bit is set."
|
|
234 |
|
|
235 |
|byteNr highByte|
|
|
236 |
|
|
237 |
byteNr := self digitLength.
|
|
238 |
highByte := self digitAt:byteNr.
|
|
239 |
^ (byteNr - 1) * 8 + highByte highBit
|
|
240 |
|
|
241 |
"
|
|
242 |
1 highBit
|
|
243 |
(1 bitShift:1) highBit
|
|
244 |
(1 bitShift:30) highBit
|
|
245 |
(1 bitShift:31) highBit
|
|
246 |
(1 bitShift:32) highBit
|
|
247 |
(1 bitShift:33) highBit
|
|
248 |
(1 bitShift:64) highBit
|
|
249 |
(1 bitShift:1000) highBit
|
|
250 |
((1 bitShift:64)-1) highBit
|
|
251 |
"
|
85
|
252 |
!
|
|
253 |
|
|
254 |
bitAt:index
|
92
|
255 |
"return the value of the index's bit (index starts at 1).
|
|
256 |
Notice: the result of bitShift: on negative receivers is not
|
159
|
257 |
defined in the language standard (since the implementation
|
|
258 |
is free to choose any internal representation for integers)"
|
85
|
259 |
|
|
260 |
|i "{Class: SmallInteger}"|
|
|
261 |
|
|
262 |
i := index - 1.
|
|
263 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1)
|
92
|
264 |
|
|
265 |
"
|
|
266 |
1 bitAt:1
|
|
267 |
(1 bitShift:30) bitAt:30
|
|
268 |
(1 bitShift:30) bitAt:31
|
|
269 |
(1 bitShift:30) bitAt:32
|
|
270 |
(1 bitShift:31) bitAt:31
|
|
271 |
(1 bitShift:31) bitAt:32
|
|
272 |
(1 bitShift:31) bitAt:33
|
|
273 |
(1 bitShift:32) bitAt:32
|
|
274 |
(1 bitShift:32) bitAt:33
|
|
275 |
(1 bitShift:32) bitAt:34
|
|
276 |
(1 bitShift:64) bitAt:64
|
|
277 |
(1 bitShift:64) bitAt:65
|
|
278 |
(1 bitShift:64) bitAt:66
|
|
279 |
(1 bitShift:1000) bitAt:1000
|
|
280 |
(1 bitShift:1000) bitAt:1001
|
|
281 |
(1 bitShift:1000) bitAt:1002
|
|
282 |
(1 bitShift:1000) bitAt:2000
|
|
283 |
"
|
85
|
284 |
!
|
|
285 |
|
|
286 |
bitShift:shiftCount
|
|
287 |
"return the value of the receiver shifted by shiftCount bits;
|
|
288 |
leftShift if shiftCount > 0; rightShift otherwise.
|
92
|
289 |
|
|
290 |
Notice: the result of bitShift: on negative receivers is not
|
159
|
291 |
defined in the language standard (since the implementation
|
|
292 |
is free to choose any internal representation for integers)"
|
85
|
293 |
|
92
|
294 |
|result
|
|
295 |
prev "{ Class: SmallInteger }"
|
|
296 |
next "{ Class: SmallInteger }"
|
|
297 |
byte "{ Class: SmallInteger }"
|
359
|
298 |
byte2 "{ Class: SmallInteger }"
|
92
|
299 |
bitShift "{ Class: SmallInteger }"
|
|
300 |
revShift "{ Class: SmallInteger }"
|
|
301 |
digitShift "{ Class: SmallInteger }"
|
|
302 |
n "{ Class: SmallInteger }"
|
|
303 |
nn "{ Class: SmallInteger }"|
|
85
|
304 |
|
|
305 |
shiftCount > 0 ifTrue:[
|
159
|
306 |
"left shift"
|
85
|
307 |
|
159
|
308 |
digitShift := shiftCount // 8.
|
|
309 |
bitShift := shiftCount \\ 8.
|
|
310 |
n := self digitLength.
|
92
|
311 |
|
159
|
312 |
"
|
|
313 |
modulu 8 shifts can be done faster ...
|
|
314 |
"
|
|
315 |
bitShift == 0 ifTrue:[
|
|
316 |
result := self class basicNew numberOfDigits:n + digitShift.
|
|
317 |
result sign:self sign.
|
|
318 |
result digits replaceFrom:(digitShift + 1) with:self digits.
|
|
319 |
"
|
|
320 |
no normalize needed, since receiver was already normalized
|
|
321 |
"
|
|
322 |
^ result
|
|
323 |
].
|
92
|
324 |
|
159
|
325 |
"
|
|
326 |
less-than-8 shifts can be done faster ...
|
|
327 |
"
|
|
328 |
digitShift == 0 ifTrue:[
|
|
329 |
n := n + 1.
|
|
330 |
result := self class basicNew numberOfDigits:n.
|
|
331 |
result sign:self sign.
|
|
332 |
prev := 0.
|
|
333 |
1 to:n-1 do:[:index |
|
|
334 |
byte := self digitAt:index.
|
|
335 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
336 |
result digitAt:index put:(byte bitAnd:16rFF).
|
|
337 |
prev := byte bitShift:-8.
|
|
338 |
].
|
|
339 |
result digitAt:n put:prev.
|
|
340 |
"
|
|
341 |
might have stored a 0-byte ...
|
|
342 |
"
|
|
343 |
prev == 0 ifTrue:[
|
|
344 |
^ result normalize
|
|
345 |
].
|
|
346 |
^ result.
|
|
347 |
].
|
92
|
348 |
|
159
|
349 |
"
|
|
350 |
slow case ...
|
|
351 |
"
|
|
352 |
n := n + digitShift + 1.
|
|
353 |
result := self class basicNew numberOfDigits:n.
|
|
354 |
result sign:self sign.
|
359
|
355 |
byte := self digitAt:1.
|
|
356 |
byte := (byte bitShift:bitShift) bitAnd:16rFF.
|
159
|
357 |
result digitAt:(digitShift + 1) put:byte.
|
|
358 |
revShift := -8 + bitShift.
|
|
359 |
2 to:(self digitLength) do:[:index |
|
359
|
360 |
byte := self digitAt:index.
|
|
361 |
byte2 := self digitAt:index-1.
|
|
362 |
byte := byte bitShift:bitShift.
|
|
363 |
byte2 := byte2 bitShift:revShift.
|
|
364 |
byte := byte bitOr:byte2.
|
159
|
365 |
byte := byte bitAnd:16rFF.
|
359
|
366 |
result digitAt:(index + digitShift) put:byte.
|
159
|
367 |
].
|
359
|
368 |
byte2 := self digitAt:self digitLength.
|
|
369 |
byte2 := byte2 bitShift:revShift.
|
|
370 |
byte2 := byte2 bitAnd:16rFF.
|
|
371 |
result digitAt:(self digitLength + digitShift + 1) put:byte2.
|
159
|
372 |
"
|
|
373 |
might have stored a 0-byte ...
|
|
374 |
"
|
359
|
375 |
byte2 == 0 ifTrue:[
|
159
|
376 |
^ result normalize
|
|
377 |
].
|
|
378 |
^ result
|
92
|
379 |
].
|
|
380 |
|
|
381 |
shiftCount < 0 ifTrue:[
|
159
|
382 |
"right shift"
|
85
|
383 |
|
159
|
384 |
digitShift := shiftCount negated // 8.
|
|
385 |
bitShift := shiftCount negated \\ 8.
|
|
386 |
n := self digitLength.
|
92
|
387 |
|
159
|
388 |
digitShift >= n ifTrue:[
|
|
389 |
^ 0
|
|
390 |
].
|
92
|
391 |
|
159
|
392 |
"
|
|
393 |
modulu 8 shifts can be done faster ...
|
|
394 |
"
|
|
395 |
bitShift == 0 ifTrue:[
|
|
396 |
n := n-digitShift.
|
|
397 |
result := self class basicNew numberOfDigits:n.
|
|
398 |
result sign:self sign.
|
|
399 |
result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) .
|
|
400 |
n <= 4 ifTrue:[
|
|
401 |
^ result normalize
|
|
402 |
].
|
|
403 |
^ result
|
|
404 |
].
|
85
|
405 |
|
159
|
406 |
"
|
|
407 |
less-than-8 shifts can be done faster ...
|
|
408 |
"
|
|
409 |
digitShift == 0 ifTrue:[
|
|
410 |
result := self class basicNew numberOfDigits:n.
|
|
411 |
result sign:self sign.
|
|
412 |
prev := 0.
|
|
413 |
bitShift := bitShift negated.
|
|
414 |
revShift := 8 + bitShift.
|
|
415 |
n to:1 by:-1 do:[:index |
|
|
416 |
byte := self digitAt:index.
|
|
417 |
next := (byte bitShift:revShift) bitAnd:16rFF.
|
|
418 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
419 |
result digitAt:index put:(byte bitAnd:16rFF).
|
|
420 |
prev := next.
|
|
421 |
].
|
|
422 |
(n <= 5) ifTrue:[
|
|
423 |
^ result normalize
|
|
424 |
].
|
|
425 |
^ result
|
|
426 |
].
|
92
|
427 |
|
159
|
428 |
"
|
|
429 |
slow case ...
|
|
430 |
"
|
|
431 |
nn := n-digitShift.
|
|
432 |
result := self class basicNew numberOfDigits:nn.
|
|
433 |
result sign:self sign.
|
85
|
434 |
|
159
|
435 |
prev := 0.
|
|
436 |
bitShift := bitShift negated.
|
|
437 |
revShift := 8 + bitShift.
|
|
438 |
n to:(1 + digitShift) by:-1 do:[:index |
|
|
439 |
byte := self digitAt:index.
|
|
440 |
next := (byte bitShift:revShift) bitAnd:16rFF.
|
|
441 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
442 |
result digitAt:(index - digitShift) put:byte.
|
|
443 |
prev := next.
|
|
444 |
].
|
|
445 |
"the last stored byte ..."
|
|
446 |
^ result normalize
|
85
|
447 |
].
|
92
|
448 |
|
|
449 |
^ self "no shift"
|
1
|
450 |
! !
|
|
451 |
|
|
452 |
!Integer methodsFor:'truncation & rounding'!
|
|
453 |
|
|
454 |
ceiling
|
|
455 |
"I am my ceiling"
|
|
456 |
|
|
457 |
^ self
|
|
458 |
!
|
|
459 |
|
|
460 |
floor
|
|
461 |
"I am my floor"
|
|
462 |
|
|
463 |
^ self
|
|
464 |
!
|
|
465 |
|
|
466 |
rounded
|
|
467 |
"return the receiver rounded toward the next Integer -
|
|
468 |
for integers this is self"
|
|
469 |
|
|
470 |
^ self
|
|
471 |
!
|
|
472 |
|
|
473 |
truncated
|
|
474 |
"return the receiver truncated towards zero -
|
|
475 |
for integers this is self"
|
|
476 |
|
|
477 |
^ self
|
92
|
478 |
!
|
|
479 |
|
|
480 |
normalize
|
|
481 |
"this is ST/X's name for ST-80's compressed.
|
|
482 |
ST/X may be changed for full compatibility."
|
|
483 |
|
|
484 |
^ self
|
|
485 |
!
|
|
486 |
|
|
487 |
compressed
|
|
488 |
"this is ST-80's name for ST/X's normalize.
|
|
489 |
ST/X may be changed for full compatibility."
|
|
490 |
|
357
|
491 |
^ self normalize
|
1
|
492 |
! !
|
|
493 |
|
|
494 |
!Integer methodsFor:'queries'!
|
|
495 |
|
|
496 |
digitLength
|
|
497 |
"return the number of bytes needed for the binary representation
|
|
498 |
of the receiver"
|
|
499 |
|
|
500 |
^ (self log:256) ceiling asInteger
|
|
501 |
!
|
|
502 |
|
|
503 |
digitAt:n
|
92
|
504 |
"return the n-th byte of the binary representation.
|
|
505 |
This is a very stupid implementation, and should be redefined in
|
|
506 |
concrete subclasses."
|
1
|
507 |
|
|
508 |
|num count|
|
|
509 |
|
|
510 |
num := self.
|
|
511 |
count := n.
|
|
512 |
[count > 1] whileTrue:[
|
159
|
513 |
num := num // 256.
|
|
514 |
count := count - 1
|
1
|
515 |
].
|
|
516 |
^ num \\ 256
|
77
|
517 |
|
|
518 |
"
|
|
519 |
16r44332211 digitAt:1
|
|
520 |
16r44332211 digitAt:2
|
|
521 |
16r44332211 digitAt:3
|
|
522 |
16r44332211 digitAt:4
|
|
523 |
16r44332211 digitAt:5
|
|
524 |
16r00332211 digitAt:4
|
|
525 |
16r00332211 digitAt:5
|
|
526 |
"
|
1
|
527 |
!
|
|
528 |
|
|
529 |
isInteger
|
|
530 |
"return true, if the receiver is some kind of integer number"
|
|
531 |
|
|
532 |
^ true
|
|
533 |
! !
|
|
534 |
|
|
535 |
!Integer methodsFor:'misc math'!
|
|
536 |
|
|
537 |
factorial
|
|
538 |
"return 1*2*3...*self"
|
|
539 |
|
356
|
540 |
(self >= 2) ifTrue:[
|
159
|
541 |
^ self * (self - 1) factorial
|
1
|
542 |
].
|
356
|
543 |
^ 1
|
212
|
544 |
|
|
545 |
"
|
|
546 |
10 factorial
|
|
547 |
1000 factorial
|
|
548 |
"
|
1
|
549 |
!
|
|
550 |
|
|
551 |
gcd:anInteger
|
|
552 |
"return the greatest common divisor (Euclid's algorithm)"
|
|
553 |
|
|
554 |
|ttt selfInteger temp|
|
|
555 |
|
|
556 |
ttt := anInteger.
|
|
557 |
selfInteger := self.
|
|
558 |
[ttt ~~ 0] whileTrue:[
|
159
|
559 |
temp := selfInteger \\ ttt.
|
|
560 |
selfInteger := ttt.
|
|
561 |
ttt := temp
|
1
|
562 |
].
|
|
563 |
^ selfInteger
|
52
|
564 |
|
92
|
565 |
"
|
|
566 |
65 gcd:15
|
|
567 |
3 gcd:15
|
|
568 |
132 gcd:55
|
|
569 |
"
|
1
|
570 |
!
|
|
571 |
|
|
572 |
lcm:anInteger
|
52
|
573 |
"return the least common multiple (using gcd:)"
|
|
574 |
|
|
575 |
^ (self * anInteger) abs // (self gcd: anInteger)
|
|
576 |
|
92
|
577 |
"
|
|
578 |
65 lcm:15
|
|
579 |
3 lcm:15
|
|
580 |
"
|
1
|
581 |
!
|
|
582 |
|
|
583 |
fib
|
|
584 |
"dont use this method if you need fibionacci numbers -
|
|
585 |
this method is for benchmarking purposes only.
|
|
586 |
(use fastFib instead and dont ever try 60 fib ...)"
|
|
587 |
|
|
588 |
(self > 1) ifTrue:[
|
159
|
589 |
^ (self - 1) fib + (self - 2) fib
|
1
|
590 |
].
|
|
591 |
^ 1
|
|
592 |
|
85
|
593 |
"
|
212
|
594 |
10 fib
|
|
595 |
20 fib
|
|
596 |
Transcript showCr:(Time millisecondsToRun:[30 fib])
|
85
|
597 |
"
|
328
|
598 |
|
308
|
599 |
"
|
328
|
600 |
ds3100 486/50 Indy(no cache)
|
|
601 |
20804 4800 2500
|
308
|
602 |
"
|
1
|
603 |
!
|
|
604 |
|
|
605 |
fastFib
|
|
606 |
"this method just to show how a changed algorithm can
|
|
607 |
change things much more drastic than tuning ...
|
|
608 |
(compare 30 fib with 30 fastFib / dont even try 60 fib)"
|
|
609 |
|
92
|
610 |
^ self fibUsingCache:(OrderedCollection new)
|
|
611 |
|
|
612 |
"
|
212
|
613 |
Transcript showCr:(Time millisecondsToRun:[30 fib])
|
92
|
614 |
Transcript showCr:(Time millisecondsToRun:[30 fastFib])
|
|
615 |
Transcript showCr:(Time millisecondsToRun:[60 fastFib])
|
|
616 |
Transcript showCr:(Time millisecondsToRun:[200 fastFib])
|
|
617 |
"
|
|
618 |
!
|
|
619 |
|
|
620 |
fibUsingCache:fibCache
|
|
621 |
"actual fastfib worker; keep track of already computed fib
|
|
622 |
numbers in fibCache, which is passed around. Take value from
|
|
623 |
the cache if already present. Doing so, the double recursive
|
|
624 |
fib actually becomes a single recursive algorithm."
|
|
625 |
|
1
|
626 |
|fib|
|
|
627 |
|
|
628 |
self <= 1 ifTrue:[^ 1].
|
|
629 |
|
92
|
630 |
fibCache size >= self ifTrue:[
|
159
|
631 |
^ fibCache at:self
|
1
|
632 |
].
|
92
|
633 |
fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache).
|
1
|
634 |
|
92
|
635 |
fibCache grow:self.
|
|
636 |
fibCache at:self put:fib.
|
1
|
637 |
^ fib
|
|
638 |
!
|
|
639 |
|
|
640 |
acker:n
|
|
641 |
"return the value of acker(self, n)"
|
|
642 |
|
|
643 |
(self == 0) ifTrue:[^ n + 1].
|
|
644 |
(n == 0) ifTrue:[^ (self - 1) acker: 1].
|
|
645 |
^ (self - 1) acker:(self acker:(n - 1))
|
|
646 |
|
92
|
647 |
"
|
|
648 |
3 acker:2
|
|
649 |
"
|
1
|
650 |
! !
|
|
651 |
|
|
652 |
!Integer methodsFor:'coercing and converting'!
|
|
653 |
|
|
654 |
asFraction
|
|
655 |
"return a Fraction with same value as receiver"
|
|
656 |
|
|
657 |
^ Fraction numerator:self denominator:1
|
|
658 |
!
|
|
659 |
|
|
660 |
asInteger
|
|
661 |
"return the receiver truncated towards zero -
|
|
662 |
for integers this is self"
|
|
663 |
|
|
664 |
^ self
|
|
665 |
! !
|
|
666 |
|
|
667 |
!Integer methodsFor:'printing & storing'!
|
|
668 |
|
54
|
669 |
printOn:aStream
|
|
670 |
"append a printed description of the receiver to aStream"
|
1
|
671 |
|
54
|
672 |
aStream nextPutAll:(self printStringRadix:10)
|
1
|
673 |
!
|
|
674 |
|
202
|
675 |
printOn:aStream radix:base
|
|
676 |
"append a printed description of the receiver to aStream"
|
|
677 |
|
|
678 |
aStream nextPutAll:(self printStringRadix:base)
|
|
679 |
!
|
|
680 |
|
1
|
681 |
printString
|
|
682 |
"return a string representation of the receiver"
|
|
683 |
|
|
684 |
^ self printStringRadix:10
|
|
685 |
!
|
|
686 |
|
216
|
687 |
hexPrintString
|
|
688 |
"return a hex string representation of the receiver"
|
|
689 |
|
|
690 |
^ self printStringRadix:16
|
|
691 |
!
|
|
692 |
|
49
|
693 |
displayString
|
|
694 |
"return a string for displaying in a view (as in inspector).
|
|
695 |
The output radix is usually 10, but can be changed by setting
|
|
696 |
DefaultDisplayRadix (see Integer>>displayRadix:)"
|
|
697 |
|
54
|
698 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[
|
159
|
699 |
^ self printString
|
54
|
700 |
].
|
49
|
701 |
^ self radixPrintStringRadix:DefaultDisplayRadix
|
|
702 |
|
92
|
703 |
"
|
|
704 |
Integer displayRadix:16. 12345
|
|
705 |
Integer displayRadix:2. 12345
|
|
706 |
Integer displayRadix:10. 12345
|
|
707 |
"
|
49
|
708 |
!
|
|
709 |
|
1
|
710 |
radixPrintStringRadix:aRadix
|
|
711 |
"return a string representation of the receiver in the specified
|
|
712 |
radix; prepend XXr to the string"
|
|
713 |
|
|
714 |
^ (aRadix printString) , 'r', (self printStringRadix:aRadix)
|
|
715 |
|
92
|
716 |
"
|
|
717 |
31 radixPrintStringRadix:2
|
|
718 |
31 radixPrintStringRadix:3
|
|
719 |
31 radixPrintStringRadix:36
|
|
720 |
"
|
1
|
721 |
!
|
|
722 |
|
|
723 |
printStringRadix:aRadix
|
|
724 |
"return a string representation of the receiver in the specified
|
|
725 |
radix (without the initial XXr)"
|
|
726 |
|
92
|
727 |
|num s "rx leftPart"|
|
1
|
728 |
|
54
|
729 |
(aRadix between:2 and:36) ifFalse:[
|
159
|
730 |
self error:'invalid radix'.
|
|
731 |
^ self printStringRadix:10
|
54
|
732 |
].
|
|
733 |
|
1
|
734 |
(self = 0) ifTrue:[^ '0'].
|
|
735 |
(self < 0) ifTrue:[
|
159
|
736 |
^ '-' , (self negated printStringRadix:aRadix)
|
1
|
737 |
].
|
92
|
738 |
|
|
739 |
"
|
|
740 |
claus: changed it from a recursive algorithm;
|
|
741 |
(it used to trigger stack-overflow exceptions when printing
|
|
742 |
3000 factorial ...)
|
|
743 |
"
|
|
744 |
"/ leftPart := self // aRadix.
|
|
745 |
"/ (leftPart ~= 0) ifTrue:[
|
|
746 |
"/ ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix))
|
|
747 |
"/ ].
|
|
748 |
"/ ^ (Character digitValue:self) asString
|
|
749 |
|
|
750 |
num := self.
|
|
751 |
s := (Character digitValue:(num \\ aRadix)) asString.
|
|
752 |
num := num // aRadix.
|
|
753 |
[num ~= 0] whileTrue:[
|
159
|
754 |
s := (Character digitValue:(num \\ aRadix)) asString , s.
|
|
755 |
num := num // aRadix.
|
1
|
756 |
].
|
92
|
757 |
^ s
|
1
|
758 |
!
|
|
759 |
|
|
760 |
printStringRadix:aRadix size:sz fill:fillCharacter
|
|
761 |
"return a string representation of the receiver in the specified
|
|
762 |
radix. The string is padded on the left with fillCharacter to make
|
|
763 |
its size as specified in sz."
|
|
764 |
|
92
|
765 |
|s|
|
1
|
766 |
|
|
767 |
s := self printStringRadix:aRadix.
|
|
768 |
s size < sz ifTrue:[
|
159
|
769 |
s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s
|
1
|
770 |
].
|
|
771 |
^ s
|
|
772 |
|
92
|
773 |
"
|
|
774 |
1024 printStringRadix:16 size:4 fill:$0
|
|
775 |
1024 printStringRadix:2 size:16 fill:$.
|
|
776 |
1024 printStringRadix:16 size:8 fill:(Character space)
|
|
777 |
"
|
216
|
778 |
!
|
|
779 |
|
|
780 |
printHex
|
|
781 |
"print the receiver as a hex number on the standard output stream"
|
|
782 |
|
|
783 |
(self printStringRadix:16) print
|
|
784 |
!
|
|
785 |
|
|
786 |
errorPrintHex
|
|
787 |
"print the receiver as a hex number on the standard error stream"
|
|
788 |
|
|
789 |
(self printStringRadix:16) errorPrint
|
1
|
790 |
! !
|
|
791 |
|
|
792 |
!Integer class methodsFor:'instance creation'!
|
|
793 |
|
|
794 |
readFrom:aStream radix:radix
|
159
|
795 |
"return the next unsigned Integer from the (character-)stream aStream
|
|
796 |
in radix; (assumes that the initial XXr has already been read).
|
|
797 |
No whitespace-skipping is done.
|
345
|
798 |
Returns 0 if no number available.
|
202
|
799 |
|
345
|
800 |
NOTICE:
|
|
801 |
This behaves different from the default readFrom:, in returning
|
|
802 |
0 (instead of raising an error) in case no number can be read.
|
|
803 |
It is unclear, if this is the correct behavior (ST-80 does this)
|
|
804 |
- depending on the upcoming ANSI standard, this may change."
|
|
805 |
|
|
806 |
^ self readFrom:aStream radix:radix onError:0
|
202
|
807 |
!
|
|
808 |
|
|
809 |
readFrom:aStream radix:radix onError:exceptionBlock
|
|
810 |
"return the next unsigned Integer from the (character-)stream aStream
|
|
811 |
in radix; (assumes that the initial XXr has already been read).
|
|
812 |
No whitespace-skipping is done.
|
|
813 |
Returns the value of exceptionBlock, if no number is available."
|
1
|
814 |
|
|
815 |
|nextChar value|
|
|
816 |
|
|
817 |
nextChar := aStream peek.
|
202
|
818 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[
|
|
819 |
^ exceptionBlock value
|
|
820 |
].
|
|
821 |
|
|
822 |
value := nextChar digitValue.
|
|
823 |
nextChar := aStream nextPeek.
|
1
|
824 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
|
159
|
825 |
value := value * radix + nextChar digitValue.
|
|
826 |
nextChar := aStream nextPeek
|
1
|
827 |
].
|
|
828 |
^ value
|
159
|
829 |
|
|
830 |
"
|
|
831 |
Integer readFrom:(ReadStream on:'12345') radix:10
|
|
832 |
Integer readFrom:(ReadStream on:'FFFF') radix:16
|
|
833 |
Integer readFrom:(ReadStream on:'1010') radix:2
|
345
|
834 |
Integer readFrom:(ReadStream on:'foobar') radix:10
|
|
835 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil
|
159
|
836 |
"
|
1
|
837 |
!
|
|
838 |
|
202
|
839 |
readFrom:aStream
|
|
840 |
"return the next unsigned Integer from the (character-)stream aStream
|
345
|
841 |
as decimal number.
|
202
|
842 |
|
345
|
843 |
NOTICE:
|
|
844 |
This behaves different from the default readFrom:, in returning
|
|
845 |
0 (instead of raising an error) in case no number can be read.
|
|
846 |
It is unclear, if this is the correct behavior (ST-80 does this)
|
|
847 |
- depending on the upcoming ANSI standard, this may change."
|
|
848 |
|
|
849 |
^ self readFrom:aStream onError:0
|
|
850 |
|
|
851 |
"
|
|
852 |
Integer readFrom:(ReadStream on:'foobar')
|
|
853 |
Integer readFrom:(ReadStream on:'foobar') onError:nil
|
|
854 |
"
|
202
|
855 |
!
|
|
856 |
|
|
857 |
readFrom:aStringOrStream onError:exceptionBlock
|
1
|
858 |
"return the next Integer from the (character-)stream aStream,
|
202
|
859 |
handling initial XXr for arbitrary radix numbers and initial sign.
|
159
|
860 |
Also, all initial whitespace is skipped.
|
202
|
861 |
If the string does not represent a valid integer number,
|
|
862 |
return the value of exceptionBlock."
|
|
863 |
|
|
864 |
|str nextChar value negative|
|
1
|
865 |
|
202
|
866 |
aStringOrStream isString ifTrue:[
|
|
867 |
str := aStringOrStream readStream.
|
|
868 |
] ifFalse:[
|
|
869 |
str := aStringOrStream
|
|
870 |
].
|
1
|
871 |
|
202
|
872 |
nextChar := str skipSeparators.
|
1
|
873 |
(nextChar == $-) ifTrue:[
|
159
|
874 |
negative := true.
|
202
|
875 |
nextChar := str nextPeek
|
1
|
876 |
] ifFalse:[
|
159
|
877 |
negative := false
|
1
|
878 |
].
|
317
|
879 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[
|
159
|
880 |
"
|
|
881 |
the string does not represent an integer
|
|
882 |
"
|
202
|
883 |
^ exceptionBlock value
|
159
|
884 |
].
|
202
|
885 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value].
|
|
886 |
nextChar := str peek.
|
1
|
887 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
|
159
|
888 |
"-xxr<number> is invalid; should be xxr-<val>"
|
69
|
889 |
|
159
|
890 |
negative ifTrue:[
|
|
891 |
'invalid (negative) radix ignored' errorPrintNL.
|
|
892 |
negative := false
|
|
893 |
].
|
202
|
894 |
str next.
|
|
895 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value]
|
1
|
896 |
].
|
|
897 |
negative ifTrue:[
|
159
|
898 |
^ value negated
|
1
|
899 |
].
|
|
900 |
^ value
|
159
|
901 |
|
|
902 |
"
|
202
|
903 |
Integer readFrom:'12345'
|
345
|
904 |
Integer readFrom:'-12345'
|
|
905 |
Integer readFrom:'+12345'
|
202
|
906 |
Integer readFrom:'16rFFFF'
|
|
907 |
Integer readFrom:'12345.1234'
|
|
908 |
Integer readFrom:'foo'
|
|
909 |
Integer readFrom:'foo' onError:[0]
|
159
|
910 |
"
|
1
|
911 |
! !
|