You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2017/02/08 14:32:23 UTC

[44/53] [abbrv] lucenenet git commit: Lucene.Net.Core.Util.SmallFloat refactor: Renamed sbyte overloads SByte instead of Byte, and added overloads for Byte (for CLS compliance)

Lucene.Net.Core.Util.SmallFloat refactor: Renamed sbyte overloads SByte instead of Byte, and added overloads for Byte (for CLS compliance)


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/8ca0267c
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/8ca0267c
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/8ca0267c

Branch: refs/heads/api-work
Commit: 8ca0267c89d1410628da2eaa4bfe2f9d3cf67d16
Parents: 73cb5b2
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Wed Feb 8 16:54:19 2017 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Wed Feb 8 21:08:24 2017 +0700

----------------------------------------------------------------------
 .../Search/Similarities/BM25Similarity.cs       |   4 +-
 .../Search/Similarities/DefaultSimilarity.cs    |   4 +-
 .../Search/Similarities/SimilarityBase.cs       |   4 +-
 src/Lucene.Net.Core/Util/SmallFloat.cs          | 122 ++++++++++++++++---
 .../core/Util/TestSmallFloat.cs                 |  32 ++---
 5 files changed, 125 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8ca0267c/src/Lucene.Net.Core/Search/Similarities/BM25Similarity.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Search/Similarities/BM25Similarity.cs b/src/Lucene.Net.Core/Search/Similarities/BM25Similarity.cs
index 86ae76f..584640b 100644
--- a/src/Lucene.Net.Core/Search/Similarities/BM25Similarity.cs
+++ b/src/Lucene.Net.Core/Search/Similarities/BM25Similarity.cs
@@ -108,7 +108,7 @@ namespace Lucene.Net.Search.Similarities
         /// </summary>
         protected internal virtual sbyte EncodeNormValue(float boost, int fieldLength) // LUCENENET TODO: Can we use byte?
         {
-            return SmallSingle.SingleToByte315(boost / (float)Math.Sqrt(fieldLength));
+            return SmallSingle.SingleToSByte315(boost / (float)Math.Sqrt(fieldLength));
         }
 
         /// <summary>
@@ -151,7 +151,7 @@ namespace Lucene.Net.Search.Similarities
         {
             for (int i = 0; i < 256; i++)
             {
-                float f = SmallSingle.Byte315ToSingle((sbyte)i);
+                float f = SmallSingle.SByte315ToSingle((sbyte)i);
                 NORM_TABLE[i] = 1.0f / (f * f);
             }
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8ca0267c/src/Lucene.Net.Core/Search/Similarities/DefaultSimilarity.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Search/Similarities/DefaultSimilarity.cs b/src/Lucene.Net.Core/Search/Similarities/DefaultSimilarity.cs
index b2a84a9..e96f2c8 100644
--- a/src/Lucene.Net.Core/Search/Similarities/DefaultSimilarity.cs
+++ b/src/Lucene.Net.Core/Search/Similarities/DefaultSimilarity.cs
@@ -54,7 +54,7 @@ namespace Lucene.Net.Search.Similarities
         {
             for (int i = 0; i < 256; i++)
             {
-                NORM_TABLE[i] = SmallSingle.Byte315ToSingle((sbyte)i);
+                NORM_TABLE[i] = SmallSingle.SByte315ToSingle((sbyte)i);
             }
         }
 
@@ -93,7 +93,7 @@ namespace Lucene.Net.Search.Similarities
         /// <seealso cref= Lucene.Net.Util.SmallSingle </seealso>
         public override sealed long EncodeNormValue(float f)
         {
-            return SmallSingle.SingleToByte315(f);
+            return SmallSingle.SingleToSByte315(f);
         }
 
         /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8ca0267c/src/Lucene.Net.Core/Search/Similarities/SimilarityBase.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Search/Similarities/SimilarityBase.cs b/src/Lucene.Net.Core/Search/Similarities/SimilarityBase.cs
index 4ddaeb6..d0c4e18 100644
--- a/src/Lucene.Net.Core/Search/Similarities/SimilarityBase.cs
+++ b/src/Lucene.Net.Core/Search/Similarities/SimilarityBase.cs
@@ -235,7 +235,7 @@ namespace Lucene.Net.Search.Similarities
         {
             for (int i = 0; i < 256; i++)
             {
-                float floatNorm = SmallSingle.Byte315ToSingle((sbyte)i);
+                float floatNorm = SmallSingle.SByte315ToSingle((sbyte)i);
                 NORM_TABLE[i] = 1.0f / (floatNorm * floatNorm);
             }
         }
@@ -268,7 +268,7 @@ namespace Lucene.Net.Search.Similarities
         /// Encodes the length to a byte via SmallFloat. </summary>
         protected internal virtual sbyte EncodeNormValue(float boost, float length) // LUCENENET TODO: Can this be byte?
         {
-            return SmallSingle.SingleToByte315((boost / (float)Math.Sqrt(length)));
+            return SmallSingle.SingleToSByte315((boost / (float)Math.Sqrt(length)));
         }
 
         // ----------------------------- Static methods ------------------------------

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8ca0267c/src/Lucene.Net.Core/Util/SmallFloat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/SmallFloat.cs b/src/Lucene.Net.Core/Util/SmallFloat.cs
index 2bff9ff..7eb3b07 100644
--- a/src/Lucene.Net.Core/Util/SmallFloat.cs
+++ b/src/Lucene.Net.Core/Util/SmallFloat.cs
@@ -35,19 +35,37 @@ namespace Lucene.Net.Util
         }
 
         /// <summary>
-        /// Converts a 32 bit float to an 8 bit float.
+        /// Converts a 32 bit <see cref="float"/> to an 8 bit <see cref="float"/>.
         /// <br>Values less than zero are all mapped to zero.
         /// <br>Values are truncated (rounded down) to the nearest 8 bit value.
         /// <br>Values between zero and the smallest representable value
         /// are rounded up.
+        /// </summary>
+        /// <param name="f"> the 32 bit <see cref="float"/> to be converted to an 8 bit <see cref="float"/> (<see cref="byte"/>)  </param>
+        /// <param name="numMantissaBits"> the number of mantissa bits to use in the byte, with the remainder to be used in the exponent </param>
+        /// <param name="zeroExp"> the zero-point in the range of exponent values </param>
+        /// <returns> the 8 bit float representation </returns>
+        // LUCENENET specific overload for CLS compliance
+        public static byte SingleToByte(float f, int numMantissaBits, int zeroExp)
+        {
+            return (byte)SingleToSByte(f, numMantissaBits, zeroExp);
+        }
+
+        /// <summary>
+        /// Converts a 32 bit <see cref="float"/> to an 8 bit <see cref="float"/>.
+        /// <para/>Values less than zero are all mapped to zero.
+        /// <para/>Values are truncated (rounded down) to the nearest 8 bit value.
+        /// <para/>Values between zero and the smallest representable value
+        /// are rounded up.
         /// <para/>
         /// NOTE: This was floatToByte() in Lucene
         /// </summary>
-        /// <param name="f"> the 32 bit float to be converted to an 8 bit float (byte) </param>
+        /// <param name="f"> the 32 bit <see cref="float"/> to be converted to an 8 bit <see cref="float"/> (<see cref="sbyte"/>) </param>
         /// <param name="numMantissaBits"> the number of mantissa bits to use in the byte, with the remainder to be used in the exponent </param>
         /// <param name="zeroExp"> the zero-point in the range of exponent values </param>
         /// <returns> the 8 bit float representation </returns>
-        public static sbyte SingleToByte(float f, int numMantissaBits, int zeroExp) // LUCENENET TODO: can we remove the sbyte?
+        [CLSCompliant(false)]
+        public static sbyte SingleToSByte(float f, int numMantissaBits, int zeroExp)
         {
             // Adjustment from a float zero exponent to our zero exponent,
             // shifted over to our exponent position.
@@ -69,11 +87,23 @@ namespace Lucene.Net.Util
         }
 
         /// <summary>
-        /// Converts an 8 bit float to a 32 bit float. 
+        /// Converts an 8 bit <see cref="float"/> to a 32 bit <see cref="float"/>. 
+        /// <para/>
+        /// NOTE: This was byteToFloat() in Lucene
+        /// </summary>
+        // LUCENENET specific overload for CLS compliance
+        public static float ByteToSingle(byte b, int numMantissaBits, int zeroExp)
+        {
+            return SByteToSingle((sbyte)b, numMantissaBits, zeroExp);
+        }
+
+        /// <summary>
+        /// Converts an 8 bit <see cref="float"/> to a 32 bit <see cref="float"/>. 
         /// <para/>
         /// NOTE: This was byteToFloat() in Lucene
         /// </summary>
-        public static float ByteToSingle(sbyte b, int numMantissaBits, int zeroExp) // LUCENENET TODO: can we remove the sbyte?
+        [CLSCompliant(false)]
+        public static float SByteToSingle(sbyte b, int numMantissaBits, int zeroExp)
         {
             // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
             // is only a little bit faster (anywhere from 0% to 7%)
@@ -93,14 +123,29 @@ namespace Lucene.Net.Util
         //
 
         /// <summary>
-        /// floatToByte(b, mantissaBits=3, zeroExponent=15)
-        /// <br>smallest non-zero value = 5.820766E-10
-        /// <br>largest value = 7.5161928E9
-        /// <br>epsilon = 0.125
+        /// SingleToSByte((byte)b, mantissaBits=3, zeroExponent=15)
+        /// <para/>smallest non-zero value = 5.820766E-10
+        /// <para/>largest value = 7.5161928E9
+        /// <para/>epsilon = 0.125
+        /// <para/>
+        /// NOTE: This was floatToByte315() in Lucene
+        /// </summary>
+        // LUCENENET specific overload for CLS compliance
+        public static byte SingleToByte315(float f)
+        {
+            return (byte)SingleToSByte315(f);
+        }
+
+        /// <summary>
+        /// SingleToSByte(b, mantissaBits=3, zeroExponent=15)
+        /// <para/>smallest non-zero value = 5.820766E-10
+        /// <para/>largest value = 7.5161928E9
+        /// <para/>epsilon = 0.125
         /// <para/>
         /// NOTE: This was floatToByte315() in Lucene
         /// </summary>
-        public static sbyte SingleToByte315(float f) // LUCENENET TODO: can we remove the sbyte?
+        [CLSCompliant(false)]
+        public static sbyte SingleToSByte315(float f) 
         {
             int bits = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
             int smallfloat = bits >> (24 - 3);
@@ -116,11 +161,23 @@ namespace Lucene.Net.Util
         }
 
         /// <summary>
-        /// byteToFloat(b, mantissaBits=3, zeroExponent=15) 
+        /// ByteToSingle(b, mantissaBits=3, zeroExponent=15) 
         /// <para/>
         /// NOTE: This was byte315ToFloat() in Lucene
         /// </summary>
-        public static float Byte315ToSingle(sbyte b) // LUCENENET TODO: can we remove the sbyte?
+        // LUCENENET specific overload for CLS compliance
+        public static float Byte315ToSingle(byte b)
+        {
+            return SByte315ToSingle((sbyte)b);
+        }
+
+        /// <summary>
+        /// SByteToSingle(b, mantissaBits=3, zeroExponent=15) 
+        /// <para/>
+        /// NOTE: This was byte315ToFloat() in Lucene
+        /// </summary>
+        [CLSCompliant(false)]
+        public static float SByte315ToSingle(sbyte b)
         {
             // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
             // is only a little bit faster (anywhere from 0% to 7%)
@@ -134,14 +191,29 @@ namespace Lucene.Net.Util
         }
 
         /// <summary>
-        /// floatToByte(b, mantissaBits=5, zeroExponent=2)
-        /// <br>smallest nonzero value = 0.033203125
-        /// <br>largest value = 1984.0
-        /// <br>epsilon = 0.03125
+        /// SingleToByte(b, mantissaBits=5, zeroExponent=2)
+        /// <para/>smallest nonzero value = 0.033203125
+        /// <para/>largest value = 1984.0
+        /// <para/>epsilon = 0.03125
+        /// <para/>
+        /// NOTE: This was floatToByte52() in Lucene
+        /// </summary>
+        // LUCENENET specific overload for CLS compliance
+        public static byte SingleToByte52(float f)
+        {
+            return (byte)SingleToSByte315(f);
+        }
+
+        /// <summary>
+        /// SingleToSByte(b, mantissaBits=5, zeroExponent=2)
+        /// <para/>smallest nonzero value = 0.033203125
+        /// <para/>largest value = 1984.0
+        /// <para/>epsilon = 0.03125
         /// <para/>
         /// NOTE: This was floatToByte52() in Lucene
         /// </summary>
-        public static sbyte SingleToByte52(float f) // LUCENENET TODO: can we remove the sbyte?
+        [CLSCompliant(false)]
+        public static sbyte SingleToSByte52(float f)
         {
             int bits = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
             int smallfloat = bits >> (24 - 5);
@@ -157,11 +229,23 @@ namespace Lucene.Net.Util
         }
 
         /// <summary>
-        /// byteToFloat(b, mantissaBits=5, zeroExponent=2) 
+        /// ByteToFloat(b, mantissaBits=5, zeroExponent=2) 
+        /// <para/>
+        /// NOTE: This was byte52ToFloat() in Lucene
+        /// </summary>
+        // LUCENENET specific overload for CLS compliance
+        public static float Byte52ToSingle(byte b)
+        {
+            return SByte52ToSingle((sbyte)b);
+        }
+
+        /// <summary>
+        /// SByteToFloat(b, mantissaBits=5, zeroExponent=2) 
         /// <para/>
         /// NOTE: This was byte52ToFloat() in Lucene
         /// </summary>
-        public static float Byte52ToSingle(sbyte b) // LUCENENET TODO: can we remove the sbyte?
+        [CLSCompliant(false)]
+        public static float SByte52ToSingle(sbyte b)
         {
             // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
             // is only a little bit faster (anywhere from 0% to 7%)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/8ca0267c/src/Lucene.Net.Tests/core/Util/TestSmallFloat.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestSmallFloat.cs b/src/Lucene.Net.Tests/core/Util/TestSmallFloat.cs
index 0fe0e8a..34d9b9b 100644
--- a/src/Lucene.Net.Tests/core/Util/TestSmallFloat.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestSmallFloat.cs
@@ -106,13 +106,13 @@ namespace Lucene.Net.Util
             for (int i = 0; i < 256; i++)
             {
                 float f1 = Orig_byteToFloat((sbyte)i);
-                float f2 = SmallSingle.ByteToSingle((sbyte)i, 3, 15);
-                float f3 = SmallSingle.Byte315ToSingle((sbyte)i);
+                float f2 = SmallSingle.SByteToSingle((sbyte)i, 3, 15);
+                float f3 = SmallSingle.SByte315ToSingle((sbyte)i);
                 Assert.AreEqual(f1, f2, 0.0);
                 Assert.AreEqual(f2, f3, 0.0);
 
-                float f4 = SmallSingle.ByteToSingle((sbyte)i, 5, 2);
-                float f5 = SmallSingle.Byte52ToSingle((sbyte)i);
+                float f4 = SmallSingle.SByteToSingle((sbyte)i, 5, 2);
+                float f5 = SmallSingle.SByte52ToSingle((sbyte)i);
                 Assert.AreEqual(f4, f5, 0.0);
             }
         }
@@ -122,19 +122,19 @@ namespace Lucene.Net.Util
         {
             Assert.AreEqual(0, Orig_floatToByte_v13(5.8123817E-10f)); // verify the old bug (see LUCENE-2937)
             Assert.AreEqual(1, Orig_floatToByte(5.8123817E-10f)); // verify it's fixed in this test code
-            Assert.AreEqual(1, SmallSingle.SingleToByte315(5.8123817E-10f)); // verify it's fixed
+            Assert.AreEqual(1, SmallSingle.SingleToSByte315(5.8123817E-10f)); // verify it's fixed
 
             // test some constants
-            Assert.AreEqual(0, SmallSingle.SingleToByte315(0));
+            Assert.AreEqual(0, SmallSingle.SingleToSByte315(0));
             //Java's Float.MIN_VALUE equals C#'s float.Epsilon
-            Assert.AreEqual(1, SmallSingle.SingleToByte315(float.Epsilon)); // underflow rounds up to smallest positive
-            Assert.AreEqual(255, SmallSingle.SingleToByte315(float.MaxValue) & 0xff); // overflow rounds down to largest positive
-            Assert.AreEqual(255, SmallSingle.SingleToByte315(float.PositiveInfinity) & 0xff);
+            Assert.AreEqual(1, SmallSingle.SingleToSByte315(float.Epsilon)); // underflow rounds up to smallest positive
+            Assert.AreEqual(255, SmallSingle.SingleToSByte315(float.MaxValue) & 0xff); // overflow rounds down to largest positive
+            Assert.AreEqual(255, SmallSingle.SingleToSByte315(float.PositiveInfinity) & 0xff);
 
             // all negatives map to 0
-            Assert.AreEqual(0, SmallSingle.SingleToByte315(-float.Epsilon));
-            Assert.AreEqual(0, SmallSingle.SingleToByte315(-float.MaxValue));
-            Assert.AreEqual(0, SmallSingle.SingleToByte315(float.NegativeInfinity));
+            Assert.AreEqual(0, SmallSingle.SingleToSByte315(-float.Epsilon));
+            Assert.AreEqual(0, SmallSingle.SingleToSByte315(-float.MaxValue));
+            Assert.AreEqual(0, SmallSingle.SingleToSByte315(float.NegativeInfinity));
 
             // up iterations for more exhaustive test after changing something
             int num = AtLeast(100000);
@@ -146,13 +146,13 @@ namespace Lucene.Net.Util
                     continue;
                 }
                 sbyte b1 = Orig_floatToByte(f);
-                sbyte b2 = SmallSingle.SingleToByte(f, 3, 15);
-                sbyte b3 = SmallSingle.SingleToByte315(f);
+                sbyte b2 = SmallSingle.SingleToSByte(f, 3, 15);
+                sbyte b3 = SmallSingle.SingleToSByte315(f);
                 Assert.AreEqual(b1, b2);
                 Assert.AreEqual(b2, b3);
 
-                sbyte b4 = SmallSingle.SingleToByte(f, 5, 2);
-                sbyte b5 = SmallSingle.SingleToByte52(f);
+                sbyte b4 = SmallSingle.SingleToSByte(f, 5, 2);
+                sbyte b5 = SmallSingle.SingleToSByte52(f);
                 Assert.AreEqual(b4, b5);
             }
         }