Author: raja
Date: 2005-08-03 06:53:55 -0400 (Wed, 03 Aug 2005)
New Revision: 47950

Modified:
   trunk/mcs/class/System/System.Collections.Specialized/ChangeLog
   trunk/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs
Log:
(Now that the compiler has started using HybridDictionary, ...)
* HybridDictionary.cs: Rewrite to avoid a test on each operation.
(list, hashtable): Remove.
(inner): New IDictionary field that can hold either a list
dictionary or a hashtable.
(is_list): New.  Notes whether 'inner' holds a list dictionary or not.
(Clear): Don't change a hashtable to a list dictionary.


Modified: trunk/mcs/class/System/System.Collections.Specialized/ChangeLog
===================================================================
--- trunk/mcs/class/System/System.Collections.Specialized/ChangeLog     
2005-08-03 09:15:35 UTC (rev 47949)
+++ trunk/mcs/class/System/System.Collections.Specialized/ChangeLog     
2005-08-03 10:53:55 UTC (rev 47950)
@@ -1,3 +1,12 @@
+2005-08-03  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       * HybridDictionary.cs: Rewrite to avoid a test on each operation.
+       (list, hashtable): Remove.
+       (inner): New IDictionary field that can hold either a list
+       dictionary or a hashtable.
+       (is_list): New.  Notes whether 'inner' holds a list dictionary or not.
+       (Clear): Don't change a hashtable to a list dictionary.
+
 2005-07-25  Lluis Sanchez Gual <[EMAIL PROTECTED]>
 
        * NameObjectCollectionBase.cs: Don't throw an exception in

Modified: 
trunk/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs
===================================================================
--- trunk/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs   
2005-08-03 09:15:35 UTC (rev 47949)
+++ trunk/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs   
2005-08-03 10:53:55 UTC (rev 47950)
@@ -1,213 +1,163 @@
-//
-// System.Collections.Specialized.HybridDictionary.cs
-//
-// Author:
-//   Lawrence Pit ([EMAIL PROTECTED])
-//
-// Copyright (C) 2004 Novell (http://www.novell.com)
-//
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-// 
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-// 
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System;
-using System.Collections;
-
-namespace System.Collections.Specialized {
-       
-       [Serializable]
-       public class HybridDictionary : IDictionary, ICollection, IEnumerable {
-               
-               private const int switchAfter = 10;
-
-               private ListDictionary list;
-               private Hashtable hashtable;
-               private bool caseInsensitive = false;
-
-               // Constructors
-               
-               public HybridDictionary() : this (0, false) { }
-               
-               public HybridDictionary (bool caseInsensitive) : this (0, 
caseInsensitive) { }
-               
-               public HybridDictionary (int initialSize) : this (initialSize, 
false) { }
-               
-               public HybridDictionary(int initialSize, bool caseInsensitive) 
-               {
-                       this.caseInsensitive = caseInsensitive;
-               
-                       if (initialSize <= switchAfter)
-                               if (caseInsensitive)
-                                       list = new ListDictionary 
(CaseInsensitiveComparer.Default);
-                               else
-                                       list = new ListDictionary ();
-                       else
-                               if (caseInsensitive) 
-                                       hashtable = new Hashtable (initialSize, 
-                                                       
CaseInsensitiveHashCodeProvider.Default, 
-                                                       
CaseInsensitiveComparer.Default);
-                               else
-                                       hashtable = new Hashtable (initialSize);
-               }               
-
-               
-               // Properties
-               
-               public int Count {
-                       get {
-                               if (list != null)
-                                       return list.Count;
-                               return hashtable.Count;
-                       }
-               }
-               
-               public bool IsFixedSize {
-                       get { return false; }
-               }
-               
-               public bool IsReadOnly {
-                       get { return false; }
-               }
-               
-               public bool IsSynchronized {
-                       get { return false; }
-               }
-               
-               public object this [object key] {
-                       get { 
-                               if (key == null)
-                                       throw new ArgumentNullException("key");
-                               if (list != null)
-                                       return list [key];
-                               return hashtable [key];
-                       }
-                       set { 
-                               if (list != null)
-                                       if (list.Count >= switchAfter) 
-                                               Switch ();
-                                       else {
-                                               list [key] = value;
-                                               return;
-                                       }
-                               hashtable [key] = value;
-                       }
-               }
-               
-               public ICollection Keys {
-                       get {
-                               if (list != null)
-                                       return list.Keys;
-                               return hashtable.Keys;
-                       }
-               }
-               
-               public object SyncRoot {
-                       get { return this; }
-               }
-               
-               public ICollection Values {
-                       get { 
-                               if (list != null)
-                                       return list.Values;
-                               return hashtable.Values;
-                       }
-               }
-               
-               
-               // Methods
-               
-               public void Add (object key, object value) 
-               {
-                       if (list != null)
-                               if (list.Count >= switchAfter) 
-                                       Switch ();
-                               else {
-                                       list.Add (key, value);
-                                       return;
-                               }
-                       hashtable.Add (key, value);
-               }
-               
-               public void Clear ()
-               {
-                       if (caseInsensitive)
-                               list = new ListDictionary 
(CaseInsensitiveComparer.Default);
-                       else
-                               list = new ListDictionary ();
-                       hashtable = null;
-               }
-               
-               public bool Contains (object key)
-               {
-                       if (key == null) {
-                               if (this.Count == 0)
-                                       return false;
-                               else
-                                       throw new ArgumentNullException ("key");
-                       }
-                       if (list != null)
-                               return list.Contains (key);
-                       return hashtable.Contains (key);
-               }
-               
-               public void CopyTo (Array array, int index)
-               {
-                       if (list != null) 
-                               list.CopyTo (array, index);
-                       else
-                               hashtable.CopyTo (array, index);
-               }
-               
-               public IDictionaryEnumerator GetEnumerator ()
-               {
-                       if (list != null)
-                               return list.GetEnumerator ();
-                       return hashtable.GetEnumerator ();
-               }
-
-               IEnumerator IEnumerable.GetEnumerator ()
-               {
-                       return GetEnumerator ();
-               }
-
-               public void Remove (object key)
-               {
-                       if (list != null)
-                               list.Remove (key);
-                       else    
-                               hashtable.Remove (key);
-               }
-               
-               private void Switch ()
-               {
-                       if (caseInsensitive) 
-                               hashtable = new Hashtable (switchAfter + 1,
-                                                       
CaseInsensitiveHashCodeProvider.Default, 
-                                                       
CaseInsensitiveComparer.Default);
-                       else
-                               hashtable = new Hashtable (switchAfter + 1);
-                       IDictionaryEnumerator e = list.GetEnumerator ();
-                       while (e.MoveNext ())
-                               hashtable.Add (e.Key, e.Value);
-                       list = null;
-               }
-       }
-}
+//
+// System.Collections.Specialized.HybridDictionary.cs
+//
+// Author:
+//   Lawrence Pit ([EMAIL PROTECTED])
+//   Raja R Harinath <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2004, 2005 Novell (http://www.novell.com)
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+
+namespace System.Collections.Specialized {
+
+       [Serializable]
+       public class HybridDictionary : IDictionary, ICollection, IEnumerable {
+
+               private const int switchAfter = 10;
+
+               private IDictionary inner;
+               private bool is_list;
+               private bool caseInsensitive;
+
+               // Constructors
+
+               public HybridDictionary() : this (0, false) { }
+
+               public HybridDictionary (bool caseInsensitive) : this (0, 
caseInsensitive) { }
+
+               public HybridDictionary (int initialSize) : this (initialSize, 
false) { }
+
+               public HybridDictionary (int initialSize, bool caseInsensitive)
+               {
+                       this.caseInsensitive = caseInsensitive;
+                       this.is_list = (initialSize <= switchAfter);
+
+                       IComparer comparer = caseInsensitive ? 
CaseInsensitiveComparer.Default : null;
+                       IHashCodeProvider hcp = caseInsensitive ? 
CaseInsensitiveHashCodeProvider.Default : null;
+
+                       if (is_list)
+                               inner = new ListDictionary (comparer);
+                       else
+                               inner = new Hashtable (initialSize, hcp, 
comparer);
+               }
+
+               // Properties
+
+               public int Count {
+                       get { return inner.Count; }
+               }
+
+               public bool IsFixedSize {
+                       get { return false; }
+               }
+
+               public bool IsReadOnly {
+                       get { return false; }
+               }
+
+               public bool IsSynchronized {
+                       get { return false; }
+               }
+
+               public object this [object key] {
+                       get { return inner [key]; }
+
+                       set {
+                               inner [key] = value;
+                               if (is_list && Count > switchAfter)
+                                       Switch ();
+                       }
+               }
+
+               public ICollection Keys {
+                       get { return inner.Keys; }
+               }
+
+               public object SyncRoot {
+                       get { return this; }
+               }
+
+               public ICollection Values {
+                       get { return inner.Values; }
+               }
+
+
+               // Methods
+
+               public void Add (object key, object value)
+               {
+                       inner.Add (key, value);
+                       if (is_list && Count > switchAfter)
+                               Switch ();
+               }
+
+               public void Clear ()
+               {
+                       // According to MSDN, this doesn't switch a Hashtable 
back to a ListDictionary
+                       inner.Clear ();
+               }
+
+               public bool Contains (object key)
+               {
+                       // if the dictionary is empty, 'Contains (null)' 
doesn't throw an ArgumentNullException
+                       return Count != 0 && inner.Contains (key);
+               }
+
+               public void CopyTo (Array array, int index)
+               {
+                       inner.CopyTo (array, index);
+               }
+
+               public IDictionaryEnumerator GetEnumerator ()
+               {
+                       return inner.GetEnumerator ();
+               }
+
+               IEnumerator IEnumerable.GetEnumerator ()
+               {
+                       return GetEnumerator ();
+               }
+
+               public void Remove (object key)
+               {
+                       // According to MSDN, this does not switch a Hashtable 
back to a ListDictionary
+                       // even if Count falls below switchAfter
+                       inner.Remove (key);
+               }
+
+               private void Switch ()
+               {
+                       IDictionary old = inner;
+                       IComparer comparer = caseInsensitive ? 
CaseInsensitiveComparer.Default : null;
+                       IHashCodeProvider hcp = caseInsensitive ? 
CaseInsensitiveHashCodeProvider.Default : null;
+
+                       is_list = false;
+                       inner = new Hashtable (old, hcp, comparer);
+                       old.Clear ();
+               }
+       }
+}


Property changes on: 
trunk/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to