lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [15/50] [abbrv] lucenenet git commit: Fixed bugs in Core.Util.Automaton where HashSets were being used within Dictionaries and are expected to be compared by contained values rather than by reference.
Date Sun, 23 Oct 2016 13:02:01 GMT
Fixed bugs in Core.Util.Automaton where HashSets were being used within Dictionaries and are
expected to be compared by contained values rather than by reference.


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

Branch: refs/heads/master
Commit: a696d70079f1eb8ed2f5bd604643e70d824034fd
Parents: f958911
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Oct 16 20:45:36 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Thu Oct 20 18:20:55 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Lucene.Net.csproj           |  1 +
 src/Lucene.Net.Core/Support/ValueHashSet.cs     | 68 ++++++++++++++++++++
 .../Util/Automaton/MinimizationOperations.cs    |  4 +-
 .../Util/Automaton/SpecialOperations.cs         |  2 +-
 .../Util/automaton/AutomatonTestUtil.cs         |  4 +-
 5 files changed, 74 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a696d700/src/Lucene.Net.Core/Lucene.Net.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Lucene.Net.csproj b/src/Lucene.Net.Core/Lucene.Net.csproj
index f790de8..99729f8 100644
--- a/src/Lucene.Net.Core/Lucene.Net.csproj
+++ b/src/Lucene.Net.Core/Lucene.Net.csproj
@@ -682,6 +682,7 @@
     <Compile Include="Support\TextSupport.cs" />
     <Compile Include="Support\ThreadClass.cs" />
     <Compile Include="Support\ThreadLock.cs" />
+    <Compile Include="Support\ValueHashSet.cs" />
     <Compile Include="Support\WeakDictionary.cs" />
     <Compile Include="Util\Accountable.cs" />
     <Compile Include="Util\ArrayInPlaceMergeSorter.cs" />

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a696d700/src/Lucene.Net.Core/Support/ValueHashSet.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/ValueHashSet.cs b/src/Lucene.Net.Core/Support/ValueHashSet.cs
new file mode 100644
index 0000000..11ecf8f
--- /dev/null
+++ b/src/Lucene.Net.Core/Support/ValueHashSet.cs
@@ -0,0 +1,68 @@
+´╗┐using System.Collections.Generic;
+using System.Runtime.Serialization;
+
+namespace Lucene.Net.Support
+{
+    /// <summary>
+    /// Java's HashSet is unlike .NET's in that its equals() and hashcode() methods 
+    /// are setup to compare the values of the sets, where in .NET we only check that
+    /// the references are the same. <see cref="ValueHashSet{T}"/> acts more like the
+    /// HashSet type in Java by comparing the sets for value equality.
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class ValueHashSet<T> : HashSet<T>
+    {
+        public ValueHashSet()
+            : base()
+        { }
+
+        public ValueHashSet(IEnumerable<T> collection)
+            : base(collection)
+        { }
+
+        public ValueHashSet(IEqualityComparer<T> comparer)
+            : base(comparer)
+        { }
+
+        public ValueHashSet(SerializationInfo info, StreamingContext context)
+            : base(info, context)
+        { }
+
+        public ValueHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
+            : base(collection, comparer)
+        { }
+
+
+        public override bool Equals(object obj)
+        {
+            if (obj == this)
+            {
+                return true;
+            }
+
+            if (!(obj is ISet<T>))
+                return false;
+            ICollection<T> c = obj as ICollection<T>;
+            if (c.Count != Count)
+                return false;
+
+            // Check to ensure the sets values are the same
+            return SetEquals(c);
+        }
+
+        public override int GetHashCode()
+        {
+            int h = 0;
+            var i = GetEnumerator();
+            while (i.MoveNext())
+            {
+                T obj = i.Current;
+                if (!EqualityComparer<T>.Default.Equals(obj, default(T)))
+                {
+                    h += obj.GetHashCode();
+                }
+            }
+            return h;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a696d700/src/Lucene.Net.Core/Util/Automaton/MinimizationOperations.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/Automaton/MinimizationOperations.cs b/src/Lucene.Net.Core/Util/Automaton/MinimizationOperations.cs
index 31ac762..f90b9ae 100644
--- a/src/Lucene.Net.Core/Util/Automaton/MinimizationOperations.cs
+++ b/src/Lucene.Net.Core/Util/Automaton/MinimizationOperations.cs
@@ -81,7 +81,7 @@ namespace Lucene.Net.Util.Automaton
             State[] states = a.NumberedStates;
             int sigmaLen = sigma.Length, statesLen = states.Length;
             List<State>[,] reverse = new List<State>[statesLen, sigmaLen];
-            HashSet<State>[] partition = new HashSet<State>[statesLen];
+            HashSet<State>[] partition = new ValueHashSet<State>[statesLen];
             List<State>[] splitblock = new List<State>[statesLen];
             int[] block = new int[statesLen];
             StateList[,] active = new StateList[statesLen, sigmaLen];
@@ -92,7 +92,7 @@ namespace Lucene.Net.Util.Automaton
             for (int q = 0; q < statesLen; q++)
             {
                 splitblock[q] = new List<State>();
-                partition[q] = new HashSet<State>();
+                partition[q] = new ValueHashSet<State>();
                 for (int x = 0; x < sigmaLen; x++)
                 {
                     active[q, x] = new StateList();

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a696d700/src/Lucene.Net.Core/Util/Automaton/SpecialOperations.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/Automaton/SpecialOperations.cs b/src/Lucene.Net.Core/Util/Automaton/SpecialOperations.cs
index 45fbbdc..a6e4c3b 100644
--- a/src/Lucene.Net.Core/Util/Automaton/SpecialOperations.cs
+++ b/src/Lucene.Net.Core/Util/Automaton/SpecialOperations.cs
@@ -252,7 +252,7 @@ namespace Lucene.Net.Util.Automaton
             }
             foreach (State r in states)
             {
-                m[r] = new HashSet<Transition>();
+                m[r] = new ValueHashSet<Transition>();
                 r.accept = false;
             }
             foreach (State r in states)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a696d700/src/Lucene.Net.TestFramework/Util/automaton/AutomatonTestUtil.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.TestFramework/Util/automaton/AutomatonTestUtil.cs b/src/Lucene.Net.TestFramework/Util/automaton/AutomatonTestUtil.cs
index 64ec473..9dd45f7 100644
--- a/src/Lucene.Net.TestFramework/Util/automaton/AutomatonTestUtil.cs
+++ b/src/Lucene.Net.TestFramework/Util/automaton/AutomatonTestUtil.cs
@@ -453,7 +453,7 @@ namespace Lucene.Net.Util.Automaton
             {
                 return;
             }
-            HashSet<State> initialset = new HashSet<State>();
+            HashSet<State> initialset = new ValueHashSet<State>();
             initialset.Add(a.Initial);
             DeterminizeSimple(a, initialset);
         }
@@ -488,7 +488,7 @@ namespace Lucene.Net.Util.Automaton
                 }
                 for (int n = 0; n < points.Length; n++)
                 {
-                    ISet<State> p = new HashSet<State>();
+                    ISet<State> p = new ValueHashSet<State>();
                     foreach (State q in s)
                     {
                         foreach (Transition t in q.Transitions)


Mime
View raw message