comparison base/src/java/math/BigInteger.d @ 112:9f4c18c268b2

Update to compile and execute with dmd 2.052.
author kntroh
date Wed, 16 Mar 2011 21:53:53 +0900
parents 5d5bd660917f
children
comparison
equal deleted inserted replaced
111:b6e9904989ed 112:9f4c18c268b2
3 import java.lang.all; 3 import java.lang.all;
4 import java.util.Random; 4 import java.util.Random;
5 version(Tango){ 5 version(Tango){
6 import tango.math.BigInt; 6 import tango.math.BigInt;
7 } else { // Phobos 7 } else { // Phobos
8 import std.bigint;
8 } 9 }
9 10
10 class BigInteger : Number { 11 class BigInteger : Number {
11 12
12 static const BigInteger ZERO; 13 static const BigInteger ZERO;
13 static const BigInteger ONE; 14 static const BigInteger ONE;
14 15
15 private BigInt bi; 16 private BigInt bi;
16 17
17 static this(){ 18 static this(){
18 ZERO = BigInteger.valueOf(0); 19 ZERO = new BigInteger("0");
19 ONE = BigInteger.valueOf(1); 20 ONE = new BigInteger("1");
20 } 21 }
21 22
22 this(byte[] val){ 23 this(byte[] val){
23 implMissing(__FILE__, __LINE__ ); 24 implMissing(__FILE__, __LINE__ );
24 } 25 }
45 else { 46 else {
46 implMissing(__FILE__, __LINE__ ); 47 implMissing(__FILE__, __LINE__ );
47 } 48 }
48 } 49 }
49 private this( BigInt v ){ 50 private this( BigInt v ){
50 getDwtLogger.error( __FILE__, __LINE__, "this({})", v.toHex );
51 bi = v; 51 bi = v;
52 } 52 }
53 private this( BigInteger v ){ 53 private this( BigInteger v ){
54 getDwtLogger.error( __FILE__, __LINE__, "this({})", bi.toHex );
55 bi = v.bi; 54 bi = v.bi;
56 } 55 }
57 private this( long v ){ 56 private this( long v ){
58 getDwtLogger.error( __FILE__, __LINE__, "this({})", v ); 57 getDwtLogger.error( __FILE__, __LINE__, "this({})", v );
59 bi = BigInt(v); 58 bi = BigInt(v);
138 bool isProbablePrime(int certainty){ 137 bool isProbablePrime(int certainty){
139 implMissing(__FILE__, __LINE__ ); 138 implMissing(__FILE__, __LINE__ );
140 return 0; 139 return 0;
141 } 140 }
142 long longValue(){ 141 long longValue(){
143 getDwtLogger.error( __FILE__, __LINE__, "{}", bi.toHex ); 142 version(Tango){
144 long res = 0; 143 getDwtLogger.error( __FILE__, __LINE__, "{}", bi.toHex );
145 auto txt = bi.toHex; 144 long res = 0;
146 bool sign = false; 145 auto txt = bi.toHex;
147 if( txt[0] is '-' ){ 146 bool sign = false;
148 sign = true; 147 if( txt[0] is '-' ){
149 txt = txt[1 .. $]; 148 sign = true;
150 } 149 txt = txt[1 .. $];
151 int nibbles = 0; 150 }
152 foreach( uint idx, char c; txt ){ 151 int nibbles = 0;
153 if( c is '_' ) continue; 152 foreach( uint idx, char c; txt ){
154 void addNibble( int v ){ 153 if( c is '_' ) continue;
155 res <<= 4; 154 void addNibble( int v ){
156 res |= v; 155 res <<= 4;
157 nibbles++; 156 res |= v;
158 } 157 nibbles++;
159 if( c >= '0' && c <= '9' ) { 158 }
160 addNibble( c - '0' ); 159 if( c >= '0' && c <= '9' ) {
161 } 160 addNibble( c - '0' );
162 else if( c >= 'a' && c <= 'f' ) { 161 }
163 addNibble( c - 'a' + 10 ); 162 else if( c >= 'a' && c <= 'f' ) {
164 } 163 addNibble( c - 'a' + 10 );
165 else if( c >= 'A' && c <= 'F' ) { 164 }
166 addNibble( c - 'A' + 10 ); 165 else if( c >= 'A' && c <= 'F' ) {
167 } 166 addNibble( c - 'A' + 10 );
168 else{ 167 }
169 getDwtLogger.error( __FILE__, __LINE__, "unknown char {} @{}", c, idx ); 168 else{
170 } 169 getDwtLogger.error( __FILE__, __LINE__, "unknown char {} @{}", c, idx );
171 } 170 }
172 if( nibbles > 16 ){ 171 }
173 getDwtLogger.error( __FILE__, __LINE__, "too much nibbles {}", nibbles ); 172 if( nibbles > 16 ){
174 } 173 getDwtLogger.error( __FILE__, __LINE__, "too much nibbles {}", nibbles );
175 return res; 174 }
175 return res;
176 } else { // Phobos
177 return bi.toLong();
178 }
176 } 179 }
177 BigInteger max(BigInteger val){ 180 BigInteger max(BigInteger val){
178 implMissing(__FILE__, __LINE__ ); 181 implMissing(__FILE__, __LINE__ );
179 return null; 182 return null;
180 } 183 }
193 BigInteger modPow(BigInteger exponent, BigInteger m){ 196 BigInteger modPow(BigInteger exponent, BigInteger m){
194 implMissing(__FILE__, __LINE__ ); 197 implMissing(__FILE__, __LINE__ );
195 return null; 198 return null;
196 } 199 }
197 BigInteger multiply(BigInteger val){ 200 BigInteger multiply(BigInteger val){
198 getDwtLogger.error( __FILE__, __LINE__, "multiply ({})", val.bi.toHex );
199 auto res = new BigInteger(this); 201 auto res = new BigInteger(this);
200 res.bi *= val.bi; 202 res.bi *= val.bi;
201 return res; 203 return res;
202 } 204 }
203 BigInteger negate(){ 205 BigInteger negate(){
214 } 216 }
215 BigInteger pow(int exponent){ 217 BigInteger pow(int exponent){
216 if( exponent < 0 ){ 218 if( exponent < 0 ){
217 throw new ArithmeticException("Negative exponent"); 219 throw new ArithmeticException("Negative exponent");
218 } 220 }
219 if( bi.isZero() ){ 221 version(Tango){
220 return exponent is 0 ? ONE : this; 222 if( bi.isZero() ){
223 return exponent is 0 ? ONE : this;
224 }
225 } else { // Phobos
226 if( bi == 0 ){
227 return exponent is 0 ? cast(BigInteger)ONE : this;
228 }
221 } 229 }
222 auto a = bi; 230 auto a = bi;
223 while(exponent>0){ 231 while(exponent>0){
224 a *= bi; 232 a *= bi;
225 exponent--; 233 exponent--;
245 BigInteger shiftRight(int n){ 253 BigInteger shiftRight(int n){
246 implMissing(__FILE__, __LINE__ ); 254 implMissing(__FILE__, __LINE__ );
247 return null; 255 return null;
248 } 256 }
249 int signum(){ 257 int signum(){
250 if( bi.isZero() ) return 0; 258 version(Tango) {
251 if( bi.isNegative() ) return -1; 259 if( bi.isZero() ) return 0;
260 if( bi.isNegative() ) return -1;
261 } else { // Phobos
262 if( bi == 0 ) return 0;
263 if( bi < 0 ) return -1;
264 }
252 return 1; 265 return 1;
253 } 266 }
254 BigInteger subtract(BigInteger val){ 267 BigInteger subtract(BigInteger val){
255 implMissing(__FILE__, __LINE__ ); 268 implMissing(__FILE__, __LINE__ );
256 return null; 269 return null;