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