You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by mh...@apache.org on 2011/08/10 20:23:05 UTC

[Lucene.Net] svn commit: r1156292 - in /incubator/lucene.net/branches/Lucene.Net_4e: src/Lucene.Net/Analysis/TokenAttributes/ src/Lucene.Net/Util/ test/Lucene.Net.Test/Support/ test/Lucene.Net.Test/Util/

Author: mherndon
Date: Wed Aug 10 18:23:05 2011
New Revision: 1156292

URL: http://svn.apache.org/viewvc?rev=1156292&view=rev
Log:
adding hashCode methods for ArrayUtil

Modified:
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
    incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/ArrayUtil.cs
    incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs
    incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Util/ArrayUtilTest.cs

Modified: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs?rev=1156292&r1=1156291&r2=1156292&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs Wed Aug 10 18:23:05 2011
@@ -311,9 +311,8 @@ namespace Lucene.Net.Analysis.TokenAttri
         /// </returns>
         public override int GetHashCode()
         {
-            // TODO: implement ArrayUtil.HashCode();
             int code = this.termLength;
-            code = code * 31;
+            code = (code * 31) + this.Buffer.CreateHashCode();
             return code;
         }
 
@@ -322,7 +321,7 @@ namespace Lucene.Net.Analysis.TokenAttri
         ///     Resizes the length of the internal buffer to the new value and preserves the
         ///     existing content.
         /// </summary>
-        /// <param name="length">The length to rebuffer to.</param>
+        /// <param name="length">The length to re-buffer to.</param>
         /// <returns>The <see cref="T:System.Char"/> array.</returns>
         public char[] ResizeBuffer(int length)
         {

Modified: incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/ArrayUtil.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/ArrayUtil.cs?rev=1156292&r1=1156291&r2=1156292&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/ArrayUtil.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/src/Lucene.Net/Util/ArrayUtil.cs Wed Aug 10 18:23:05 2011
@@ -46,6 +46,52 @@ namespace Lucene.Net.Util
     public static class ArrayUtil
     {
         /// <summary>
+        /// Creates the hash code of chars in range start that is inclusive, till the end.
+        /// </summary>
+        /// <remarks>
+        ///     <note>This method is just called <c>hashCode()</c> in the java version.</note>
+        /// </remarks>
+        /// <param name="array">The array.</param>
+        /// <param name="start">The start which defaults to 0.</param>
+        /// <param name="end">The end which defaults to the length of the array.</param>
+        /// <returns>An instance of <see cref="Int32"/>.</returns>
+        public static int CreateHashCode(this char[] array, int start = 0, int end = -1)
+        {
+            if (end == -1)
+                end = array.Length;
+
+            int code = 0;
+            
+            for (int i = end - 1; i >= start; i--)
+                code = (code * 31) + array[i];
+            
+            return code;
+        }
+
+        /// <summary>
+        /// Creates the hash code of bytes in range start that is inclusive, till the end.
+        /// </summary>
+        /// <remarks>
+        ///     <note>This method is just called <c>hashCode()</c> in the java version.</note>
+        /// </remarks>
+        /// <param name="array">The array.</param>
+        /// <param name="start">The start which defaults to 0.</param>
+        /// <param name="end">The end which defaults to the length of the array.</param>
+        /// <returns>An instance of <see cref="Int32"/>.</returns>
+        public static int CreateHashCode(this byte[] array, int start = 0, int end = -1)
+        {
+            int code = 0;
+
+            if (end == -1)
+                end = array.Length;
+
+            for (int i = end - 1; i >= start; i--)
+                code = (code * 31) + array[i];
+
+            return code;
+        }
+
+        /// <summary>
         ///   Returns an array size that is greater or equal to the <paramref name="minimalTargetSize"/>.
         ///   This will generally over allocate exponentially to achieve amortized 
         ///   linear-time cost as the array grows.

Modified: incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs?rev=1156292&r1=1156291&r2=1156292&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Support/WeakDictionaryOfTKeyTValueTest.cs Wed Aug 10 18:23:05 2011
@@ -172,7 +172,8 @@ namespace Lucene.Net.Support
             var weakDictionary = new WeakDictionary<string, ReferenceType>(internalDictionary);
 
             bool success = weakDictionary.Contains(
-                                            new KeyValuePair<string, ReferenceType>("one",
+                                            new KeyValuePair<string, ReferenceType>(
+                                                "one",
                                                 internalDictionary["one"]));
 
             Assert.IsTrue(success, "Dictionary should have contained the key pair value.");

Modified: incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Util/ArrayUtilTest.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Util/ArrayUtilTest.cs?rev=1156292&r1=1156291&r2=1156292&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Util/ArrayUtilTest.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_4e/test/Lucene.Net.Test/Util/ArrayUtilTest.cs Wed Aug 10 18:23:05 2011
@@ -43,6 +43,36 @@ namespace Lucene.Net.Util
     {
 
         [Test]
+        public void CreateHashCode_WithCharArray()
+        {
+            var array = "stash".ToCharArray();
+
+            var hash = array.CreateHashCode();
+
+            int code = 0;
+
+            for (int i = array.Length - 1; i >= 0; i--)
+                code = code * 31 + array[i];
+
+            Assert.AreEqual(code, hash);
+        }
+
+        [Test]
+        public void CreateHashCode_WithByteArray()
+        {
+            var array = new byte[] {0x01, 0x03, 0x05};
+
+            var hash = array.CreateHashCode();
+
+            int code = 0;
+
+            for (int i = array.Length - 1; i >= 0; i--)
+                code = code * 31 + array[i];
+
+            Assert.AreEqual(code, hash);
+        }
+
+        [Test]
         public void OversizeGrowthAlgorythm()
         {
             int currentSize = 0;