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