-2008-05-23 Miguel de Icaza <miguel@novell.com>
-
- * Hashtable.cs: Implement a faster clone that does not use an
- enumerator to iterate over the values, this doubles the speed of
- Clone.
-
- The use of an internal constructor that builds from a Hashtable is
- done to avoid populating a Hashtable and then updating every field
- and structure with new data.
-
2008-05-08 Robert Jordan <robertj@gmx.net>
* DictionaryEntry.cs: Rename fields for serialization
{
}
- //
- // Used as a faster Clone
- //
- internal Hashtable (Hashtable source)
- {
- inUse = source.inUse;
- loadFactor = source.loadFactor;
-
- table = (Slot []) source.table.Clone ();
- hashes = (int []) source.hashes.Clone ();
- threshold = source.threshold;
- hcpRef = source.hcpRef;
- comparerRef = source.comparerRef;
- serializationInfo = source.serializationInfo;
-
-#if NET_2_0
- equalityComparer = source.equalityComparer;
-#endif
- }
-
#if NET_2_0
[Obsolete ("Please use Hashtable(int, IEqualityComparer) instead")]
#endif
public virtual object Clone ()
{
- return new Hashtable (this);
+#if NET_2_0
+ Hashtable ht = new Hashtable (Count, equalityComparer);
+ ht.hcp = this.hcp;
+ ht.comparer = this.comparer;
+#else
+ Hashtable ht = new Hashtable (Count, hcp, comparer);
+#endif
+ ht.inUse = 0;
+ ht.loadFactor = this.loadFactor;
+
+ // FIXME: maybe it's faster to simply
+ // copy the back-end array?
+
+ IDictionaryEnumerator it = GetEnumerator ();
+ while (it.MoveNext ()) {
+ ht [it.Key] = it.Value;
+ }
+
+ return ht;
}
public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
0, h.Count);\r
}\r
\r
-#if NET_2_0\r
- public class MyEqualityComparer : IEqualityComparer {\r
- public bool Equals (object x, object y) { return x == y; }\r
- public int GetHashCode (object obj) { return 1; }\r
- }\r
-\r
- public class MyHashtable : Hashtable {\r
- public MyHashtable (IEqualityComparer c): base (c){}\r
-\r
- public IEqualityComparer GetComparer ()\r
- {\r
- return EqualityComparer;\r
- }\r
- }\r
- \r
-#endif\r
- \r
[Test]\r
public void TestClone() {\r
{\r
\r
((char[])h1[c1[0]])[0] = 'z';\r
AssertEquals("shallow copy", h1[c1[0]], h2[c1[0]]);\r
-\r
-#if NET_2_0\r
- // NET 2.0 stuff\r
- MyEqualityComparer a = new MyEqualityComparer ();\r
- MyHashtable mh1 = new MyHashtable (a);\r
- MyHashtable mh2 = (MyHashtable) h1.Clone ();\r
- \r
- AssertEquals ("EqualityComparer", mh1.GetComparer (), mh2.GetComparer ());\r
-#endif\r
}\r
}\r
\r