Skip to content

Commit

Permalink
Updated to Java commit 07f0586 (2010.06.25): incorporate BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiller committed Dec 5, 2010
1 parent dd68d3d commit 6c6552b
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 126 deletions.
14 changes: 14 additions & 0 deletions Clojure/Clojure.Source/clojure/core.clj
Expand Up @@ -3008,6 +3008,7 @@
[n]
(or (instance? Int32 n) (instance? UInt32 n) ;;; Integer -> Int32, added UInt32
(instance? Int64 n) (instance? UInt64 n) ;;; Long -> Int64, added UInt64
(instance? clojure.lang.BigInt n)
(instance? BigInteger n) (instance? Char n) ;;; added Char test
(instance? Int16 n) (instance? UInt16 n) ;;; Short -> Int16, added UInt16
(instance? Byte n) (instance? SByte n))) ;;; Added SByte test
Expand Down Expand Up @@ -3065,6 +3066,19 @@
(or (integer? n) (ratio? n) (decimal? n)))

(defn bigint
"Coerce to BigInt"
{:tag clojure.lang.BigInt
:static true
:added "1.3"}
[x] (cond
(instance? clojure.lang.BigInt x) x
(instance? BigInteger x) (clojure.lang.BigInt/fromBigInteger x)
(decimal? x) (bigint (.toBigInteger ^BigDecimal x))
(ratio? x) (bigint (.BigIntegerValue ^clojure.lang.Ratio x))
(number? x) (clojure.lang.BigInt/valueOf (long x))
:else (bigint (BigInteger. x))))

(defn biginteger
"Coerce to BigInteger"
{:tag BigInteger
:added "1.0"
Expand Down
7 changes: 6 additions & 1 deletion Clojure/Clojure.Source/clojure/core_print.clj
Expand Up @@ -279,6 +279,7 @@
;(defmethod print-dup Double [o w] (print-method o w)) ;;; java.lang.Double
(defmethod print-dup clojure.lang.Ratio [o w] (print-method o w))
(defmethod print-dup clojure.lang.BigDecimal [o w] (print-method o w)) ;;; java.math.BigDecimal
(defmethod print-dup clojure.lang.BigInt [o w] (print-method o w))
(defmethod print-dup clojure.lang.BigInteger [o w] (print-method o w)) ;;; java.math.BigInteger
(defmethod print-dup clojure.lang.PersistentHashMap [o w] (print-method o w))
(defmethod print-dup clojure.lang.PersistentHashSet [o w] (print-method o w))
Expand Down Expand Up @@ -321,10 +322,14 @@
(.Write w (str b))
(.Write w "M"))

(defmethod print-method clojure.lang.BigInteger [b, ^System.IO.TextWriter w]
(defmethod print-method clojure.lang.BigInt [b, ^System.IO.TextWriter w]
(.Write w (str b))
(.Write w "N"))

(defmethod print-method clojure.lang.BigInteger [b, ^System.IO.TextWriter w]
(.Write w (str b))
(.Write w "BIGINT"))

(defmethod print-method System.Text.RegularExpressions.Regex [p ^System.IO.TextWriter w] ;;; java.util.regex.Pattern =>
(.Write w "#\"")
(loop [[^Char c & r :as s] (seq (.ToString ^System.Text.RegularExpressions.Regex p)) ;;; .pattern => .ToString
Expand Down
2 changes: 1 addition & 1 deletion Clojure/Clojure.Source/clojure/pprint/cl_format.clj
Expand Up @@ -247,7 +247,7 @@ http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm
for improved performance"
[base val]
(let [format-str (get java-base-formats base)]
(if (and format-str (integer? val))
(if (and format-str (integer? val) (not (instance? clojure.lang.BigInt val)))
(clojure.core/format format-str val)
(base-str base val))))

Expand Down
2 changes: 1 addition & 1 deletion Clojure/Clojure.Source/clojure/test_clojure/numbers.clj
Expand Up @@ -343,7 +343,7 @@
(defn- expt
"clojure.contrib.math/expt is a better and much faster impl, but this works.
Math/pow overflows to Infinity."
[x n] (apply * (replicate n x)))
[x n] (apply *' (replicate n x)))

(deftest test-bit-shift-left
(are [x y] (= x y)
Expand Down
11 changes: 6 additions & 5 deletions Clojure/Clojure.Source/clojure/test_clojure/reader.clj
Expand Up @@ -17,7 +17,8 @@
;; Created 22 October 2008

(ns clojure.test-clojure.reader
(:use clojure.test))
(:use clojure.test)
(:import clojure.lang.BigInt))

;; Symbols

Expand Down Expand Up @@ -81,10 +82,10 @@
sequence))))

; Read BigInteger
(is (instance? BigInteger 9223372036854775808))
(is (instance? BigInteger -9223372036854775809))
(is (instance? BigInteger 10000000000000000000000000000000000000000000000000))
(is (instance? BigInteger -10000000000000000000000000000000000000000000000000))
(is (instance? BigInt 9223372036854775808))
(is (instance? BigInt -9223372036854775809))
(is (instance? BigInt 10000000000000000000000000000000000000000000000000))
(is (instance? BigInt -10000000000000000000000000000000000000000000000000))

; Read Double
(is (instance? Double +1.0e+1))
Expand Down
66 changes: 33 additions & 33 deletions Clojure/Clojure.Tests/LibTests/NumbersTests.cs
Expand Up @@ -49,41 +49,41 @@ private void ExpectEqualObject(object x, object y)

#region reduce tests

[Test]
public void ReduceOnBigIntReducesSmallerValues()
{
//BigInteger b1 = new BigInteger("123");
//BigInteger b2 = new BigInteger("0");
//BigInteger b3 = new BigInteger(Int32.MaxValue.ToString());
//BigInteger b4 = new BigInteger(Int32.MinValue.ToString()); BigInteger b1 = new BigInteger("123");
BigInteger b1 = BigInteger.Create(123);
BigInteger b2 = BigInteger.Create(0);
BigInteger b3 = BigInteger.Create(Int32.MaxValue);
BigInteger b4 = BigInteger.Create(Int32.MinValue);
//[Test]
//public void ReduceOnBigIntReducesSmallerValues()
//{
// //BigInteger b1 = new BigInteger("123");
// //BigInteger b2 = new BigInteger("0");
// //BigInteger b3 = new BigInteger(Int32.MaxValue.ToString());
// //BigInteger b4 = new BigInteger(Int32.MinValue.ToString()); BigInteger b1 = new BigInteger("123");
// BigInteger b1 = BigInteger.Create(123);
// BigInteger b2 = BigInteger.Create(0);
// BigInteger b3 = BigInteger.Create(Int32.MaxValue);
// BigInteger b4 = BigInteger.Create(Int32.MinValue);

ExpectInt32(Numbers.reduceBigInteger(b1));
ExpectInt32(Numbers.reduceBigInteger(b2));
ExpectInt32(Numbers.reduceBigInteger(b3));
ExpectInt32(Numbers.reduceBigInteger(b4));
}
// ExpectInt32(Numbers.reduceBigInt(b1));
// ExpectInt32(Numbers.reduceBigIntege(b2));
// ExpectInt32(Numbers.reduceBigInteger(b3));
// ExpectInt32(Numbers.reduceBigInteger(b4));
//}

[Test]
public void ReduceOnBigIntReturnsLargerValues()
{
//BigInteger b1 = new BigInteger("100000000000000000000", 16);
//BigInteger b2 = b1.negate();
//BigInteger b3 = new BigInteger("123456789012345678901234567890");
//BigInteger b4 = b3.negate();
BigInteger b1 = BigInteger.Parse("100000000000000000000", 16);
BigInteger b2 = b1.Negate();
BigInteger b3 = BigInteger.Parse("123456789012345678901234567890");
BigInteger b4 = b3.Negate();

ExpectSameObject(b1, Numbers.reduceBigInteger(b1));
ExpectSameObject(b2, Numbers.reduceBigInteger(b2));
ExpectSameObject(b3, Numbers.reduceBigInteger(b3));
ExpectSameObject(b4, Numbers.reduceBigInteger(b4));
}
//[Test]
//public void ReduceOnBigIntReturnsLargerValues()
//{
// //BigInteger b1 = new BigInteger("100000000000000000000", 16);
// //BigInteger b2 = b1.negate();
// //BigInteger b3 = new BigInteger("123456789012345678901234567890");
// //BigInteger b4 = b3.negate();
// BigInteger b1 = BigInteger.Parse("100000000000000000000", 16);
// BigInteger b2 = b1.Negate();
// BigInteger b3 = BigInteger.Parse("123456789012345678901234567890");
// BigInteger b4 = b3.Negate();

// ExpectSameObject(b1, Numbers.reduceBigInteger(b1));
// ExpectSameObject(b2, Numbers.reduceBigInteger(b2));
// ExpectSameObject(b3, Numbers.reduceBigInteger(b3));
// ExpectSameObject(b4, Numbers.reduceBigInteger(b4));
//}

//[Test]
//public void ReduceOnLongReducesSmallerValues()
Expand Down
1 change: 1 addition & 0 deletions Clojure/Clojure/Clojure.csproj
Expand Up @@ -187,6 +187,7 @@
<Compile Include="Lib\ArrayHelper.cs" />
<Compile Include="Lib\ATransientMap.cs" />
<Compile Include="Lib\ATransientSet.cs" />
<Compile Include="Lib\BigInt.cs" />
<Compile Include="Lib\BigInteger.cs" />
<Compile Include="Lib\ChunkBuffer.cs" />
<Compile Include="Lib\ChunkedCons.cs" />
Expand Down
166 changes: 166 additions & 0 deletions Clojure/Clojure/Lib/BigInt.cs
@@ -0,0 +1,166 @@
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/

/**
* Author: David Miller
**/

using System;

namespace clojure.lang
{
// Copying JVM's clojure.lang.BigInt class as closely as possible

public class BigInt
{
#region Data

readonly long _lpart;

public long Lpart
{
get { return _lpart; }
}

readonly BigInteger _bipart;

public BigInteger Bipart
{
get { return _bipart; }
}


public static readonly BigInt ZERO = new BigInt(0, null);
public static readonly BigInt ONE = new BigInt(1, null);

#endregion

#region Ctors and factory methods

private BigInt(long lpart, BigInteger bipart)
{
_lpart = lpart;
_bipart = bipart;
}

public static BigInt fromBigInteger(BigInteger val)
{
long n;
if (val.AsInt64(out n))
return new BigInt(n, null);
return new BigInt(0, val);
}

public static BigInt fromLong(long val)
{
return new BigInt(val, null);
}

public static BigInt valueOf(long val)
{
return new BigInt(val, null);
}

#endregion

#region Object overrides

public override bool Equals(object obj)
{
if (this == obj)
return true;
if (obj is BigInt)
{
BigInt o = (BigInt)obj;
if (_bipart == null)
return o._bipart == null && _lpart == o._lpart;
return o._bipart != null && _bipart.Equals(o._bipart);
}
return false;
}

public override int GetHashCode()
{
if (_bipart == null)
return _lpart.GetHashCode();

return _bipart.GetHashCode();
}

public override string ToString()
{
if ( _bipart == null )
return _lpart.ToString();
return _bipart.ToString();
}

#endregion

#region Conversions

public BigInteger toBigInteger()
{
if (_bipart == null)
return BigInteger.Create(_lpart);
else
return _bipart;
}

public int intValue()
{
if (_bipart == null)
return (int)_lpart;
else
return _bipart.ToInt32();
}

public long longValue()
{
if (_bipart == null)
return _lpart;
else
return _bipart.ToInt64();
}

public float floatValue()
{
if (_bipart == null)
return _lpart;
else
return _bipart.ToSingle(null);
}

public double doubleValue()
{
if (_bipart == null)
return _lpart;
else
return _bipart.ToDouble(null);
}

public byte byteValue()
{
if (_bipart == null)
return (byte)_lpart;
else
return _bipart.ToByte(null);
}

public short shortValue()
{
if (_bipart == null)
return (short)_lpart;
else
return _bipart.ToInt16(null);
}

#endregion
}
}
15 changes: 9 additions & 6 deletions Clojure/Clojure/Lib/BigInteger.cs
Expand Up @@ -1820,12 +1820,6 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
// require same as Int64 if in range
long n;
if (AsInt64(out n))
return n.GetHashCode();


int hashCode = 0;
for (int i = 0; i < _data.Length; i++)
hashCode = (int)(31 * hashCode + (_data[i] & 0xffffffffL));
Expand All @@ -1846,6 +1840,15 @@ public override int GetHashCode()
/// <returns><value>-1</value> if the first is less than second; <value>0</value> if equal; <value>+1</value> if greater</returns>
public static int Compare(BigInteger x, BigInteger y)
{
if (ReferenceEquals(x,y))
return 0;

if (ReferenceEquals(x,null))
return -1;

if (ReferenceEquals(y,null))
return 1;

return x._sign == y._sign
? x._sign * Compare(x._data,y._data)
: (x._sign < y._sign ? -1 : 1);
Expand Down

0 comments on commit 6c6552b

Please sign in to comment.