Merge pull request #961 from ermshiperete/bug-xamarin-18118
[mono.git] / mcs / class / System.Core / System.Collections.Generic / HashSet.cs
index 73aee92940be40ee8e7b8af5ebe3b3fbef73f744..a146fd9002379272a51617de921f214143b5714f 100644 (file)
@@ -44,7 +44,7 @@ namespace System.Collections.Generic {
        [DebuggerDisplay ("Count={Count}")]
        [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
        public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback
-#if NET_4_0 || MOONLIGHT || MOBILE
+#if NET_4_0
                                                        , ISet<T>
 #endif
        {
@@ -199,9 +199,9 @@ namespace System.Collections.Generic {
                        }
                }
 
-               void Resize ()
+               void Resize (int size)
                {
-                       int newSize = PrimeHelper.ToPrime ((table.Length << 1) | 1);
+                       int newSize = HashPrimeNumbers.ToPrime (size);
 
                        // allocate new hash table and link slots array
                        var newTable = new int [newSize];
@@ -250,7 +250,7 @@ namespace System.Collections.Generic {
                                return false;
 
                        if (++count > threshold) {
-                               Resize ();
+                               Resize ((table.Length << 1) | 1);
                                index = (hashCode & int.MaxValue) % table.Length;
                        }
 
@@ -371,7 +371,7 @@ namespace System.Collections.Generic {
 
                public void TrimExcess ()
                {
-                       Resize ();
+                       Resize (count);
                }
 
                // set operations
@@ -534,42 +534,9 @@ namespace System.Collections.Generic {
                        return CheckIsSupersetOf (other_set);
                }
 
-               class HashSetEqualityComparer : IEqualityComparer<HashSet<T>>
-               {
-                       public bool Equals (HashSet<T> lhs, HashSet<T> rhs)
-                       {
-                               if (lhs == rhs)
-                                       return true;
-
-                               if (lhs == null || rhs == null || lhs.Count != rhs.Count)
-                                       return false;
-
-                               foreach (var item in lhs)
-                                       if (!rhs.Contains (item))
-                                               return false;
-
-                               return true;
-                       }
-
-                       public int GetHashCode (HashSet<T> hashset)
-                       {
-                               if (hashset == null)
-                                       return 0;
-
-                               IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
-                               int hash = 0;
-                               foreach (var item in hashset)
-                                       hash ^= comparer.GetHashCode (item);
-
-                               return hash;
-                       }
-               }
-
-               static readonly HashSetEqualityComparer setComparer = new HashSetEqualityComparer ();
-
                public static IEqualityComparer<HashSet<T>> CreateSetComparer ()
                {
-                       return setComparer;
+                       return HashSetEqualityComparer<T>.Instance;
                }
 
                [SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
@@ -599,8 +566,7 @@ namespace System.Collections.Generic {
 
                                empty_slot = NO_SLOT;
                                if (capacity > 0) {
-                                       table = new int[capacity];
-                                       slots = new T[capacity];
+                                       InitArrays(capacity);
 
                                        T[] tableArray = (T[]) si.GetValue("Elements", typeof(T[]));
                                        if (tableArray == null) 
@@ -708,81 +674,38 @@ namespace System.Collections.Generic {
                                        throw new InvalidOperationException ("HashSet have been modified while it was iterated over");
                        }
                }
+       }
+       
+       sealed class HashSetEqualityComparer<T> : IEqualityComparer<HashSet<T>>
+       {
+               public static readonly HashSetEqualityComparer<T> Instance = new HashSetEqualityComparer<T> ();
+                       
+               public bool Equals (HashSet<T> lhs, HashSet<T> rhs)
+               {
+                       if (lhs == rhs)
+                               return true;
 
-               // borrowed from System.Collections.HashTable
-               static class PrimeHelper {
-
-                       static readonly int [] primes_table = {
-                               11,
-                               19,
-                               37,
-                               73,
-                               109,
-                               163,
-                               251,
-                               367,
-                               557,
-                               823,
-                               1237,
-                               1861,
-                               2777,
-                               4177,
-                               6247,
-                               9371,
-                               14057,
-                               21089,
-                               31627,
-                               47431,
-                               71143,
-                               106721,
-                               160073,
-                               240101,
-                               360163,
-                               540217,
-                               810343,
-                               1215497,
-                               1823231,
-                               2734867,
-                               4102283,
-                               6153409,
-                               9230113,
-                               13845163
-                       };
-
-                       static bool TestPrime (int x)
-                       {
-                               if ((x & 1) != 0) {
-                                       int top = (int) Math.Sqrt (x);
-
-                                       for (int n = 3; n < top; n += 2) {
-                                               if ((x % n) == 0)
-                                                       return false;
-                                       }
-
-                                       return true;
-                               }
+                       if (lhs == null || rhs == null || lhs.Count != rhs.Count)
+                               return false;
 
-                               // There is only one even prime - 2.
-                               return x == 2;
-                       }
+                       foreach (var item in lhs)
+                               if (!rhs.Contains (item))
+                                       return false;
 
-                       static int CalcPrime (int x)
-                       {
-                               for (int i = (x & (~1)) - 1; i < Int32.MaxValue; i += 2)
-                                       if (TestPrime (i))
-                                               return i;
+                       return true;
+               }
 
-                               return x;
-                       }
+               public int GetHashCode (HashSet<T> hashset)
+               {
+                       if (hashset == null)
+                               return 0;
 
-                       public static int ToPrime (int x)
-                       {
-                               for (int i = 0; i < primes_table.Length; i++)
-                                       if (x <= primes_table [i])
-                                               return primes_table [i];
+                       IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
+                       int hash = 0;
+                       foreach (var item in hashset)
+                               hash ^= comparer.GetHashCode (item);
 
-                               return CalcPrime (x);
-                       }
+                       return hash;
                }
        }
 }