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