OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // TODO(srdjan): fix limitations. | 5 // TODO(srdjan): fix limitations. |
6 // - shift amount must be a Smi. | 6 // - shift amount must be a Smi. |
7 class _IntegerImplementation extends _Num { | 7 class _IntegerImplementation extends _Num { |
8 // The Dart class _Bigint extending _IntegerImplementation requires a | 8 // The Dart class _Bigint extending _IntegerImplementation requires a |
9 // default constructor. | 9 // default constructor. |
10 | 10 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 } | 383 } |
384 int t = this; | 384 int t = this; |
385 if ((t < 0) || (t >= m)) t %= m; | 385 if ((t < 0) || (t >= m)) t %= m; |
386 if (t == 1) return 1; | 386 if (t == 1) return 1; |
387 if ((t == 0) || (t.isEven && m.isEven)) { | 387 if ((t == 0) || (t.isEven && m.isEven)) { |
388 throw new Exception("Not coprime"); | 388 throw new Exception("Not coprime"); |
389 } | 389 } |
390 return _binaryGcd(m, t, true); | 390 return _binaryGcd(m, t, true); |
391 } | 391 } |
392 | 392 |
393 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0. | 393 // Returns gcd of abs(this) and abs(other). |
394 int gcd(int other) { | 394 int gcd(int other) { |
395 if (other is! int) { | 395 if (other is! int) { |
396 throw new ArgumentError.value(other, "other", "not an integer"); | 396 throw new ArgumentError.value(other, "other", "not an integer"); |
397 } | 397 } |
398 if (this == 0) { | |
399 throw new ArgumentError.value(this, "this", "must not be zero"); | |
400 } | |
401 if (other == 0) { | |
402 throw new ArgumentError.value(this, "other", "must not be zero"); | |
403 } | |
404 int x = this.abs(); | 398 int x = this.abs(); |
405 int y = other.abs(); | 399 int y = other.abs(); |
| 400 if (x == 0) return y; |
| 401 if (y == 0) return x; |
406 if ((x == 1) || (y == 1)) return 1; | 402 if ((x == 1) || (y == 1)) return 1; |
407 if (other is _Bigint) { | 403 if (other is _Bigint) { |
408 return _toBigint().gcd(other); | 404 return _toBigint().gcd(other); |
409 } | 405 } |
410 return _binaryGcd(x, y, false); | 406 return _binaryGcd(x, y, false); |
411 } | 407 } |
412 } | 408 } |
413 | 409 |
414 class _Smi extends _IntegerImplementation implements int { | 410 class _Smi extends _IntegerImplementation implements int { |
415 factory _Smi._uninstantiable() { | 411 factory _Smi._uninstantiable() { |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 // Shift by mint exceeds range that can be handled by the VM. | 619 // Shift by mint exceeds range that can be handled by the VM. |
624 int _shrFromInt(int other) { | 620 int _shrFromInt(int other) { |
625 if (other < 0) { | 621 if (other < 0) { |
626 return -1; | 622 return -1; |
627 } else { | 623 } else { |
628 return 0; | 624 return 0; |
629 } | 625 } |
630 } | 626 } |
631 int _shlFromInt(int other) native "Mint_shlFromInt"; | 627 int _shlFromInt(int other) native "Mint_shlFromInt"; |
632 } | 628 } |
OLD | NEW |