HybridDictionary.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // System.Collections.Specialized.HybridDictionary.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. // Copyright (C) 2004 Novell (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. namespace System.Collections.Specialized {
  32. [Serializable]
  33. public class HybridDictionary : IDictionary, ICollection, IEnumerable {
  34. private const int switchAfter = 10;
  35. private ListDictionary list;
  36. private Hashtable hashtable;
  37. private bool caseInsensitive = false;
  38. // Constructors
  39. public HybridDictionary() : this (0, false) { }
  40. public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
  41. public HybridDictionary (int initialSize) : this (initialSize, false) { }
  42. public HybridDictionary(int initialSize, bool caseInsensitive)
  43. {
  44. this.caseInsensitive = caseInsensitive;
  45. if (initialSize <= switchAfter)
  46. if (caseInsensitive)
  47. list = new ListDictionary (CaseInsensitiveComparer.Default);
  48. else
  49. list = new ListDictionary ();
  50. else
  51. if (caseInsensitive)
  52. hashtable = new Hashtable (initialSize,
  53. CaseInsensitiveHashCodeProvider.Default,
  54. CaseInsensitiveComparer.Default);
  55. else
  56. hashtable = new Hashtable (initialSize);
  57. }
  58. // Properties
  59. public int Count {
  60. get {
  61. if (list != null)
  62. return list.Count;
  63. return hashtable.Count;
  64. }
  65. }
  66. public bool IsFixedSize {
  67. get { return false; }
  68. }
  69. public bool IsReadOnly {
  70. get { return false; }
  71. }
  72. public bool IsSynchronized {
  73. get { return false; }
  74. }
  75. public object this [object key] {
  76. get {
  77. if (key == null)
  78. throw new ArgumentNullException("key");
  79. if (list != null)
  80. return list [key];
  81. return hashtable [key];
  82. }
  83. set {
  84. if (list != null)
  85. if (list.Count >= switchAfter)
  86. Switch ();
  87. else {
  88. list [key] = value;
  89. return;
  90. }
  91. hashtable [key] = value;
  92. }
  93. }
  94. public ICollection Keys {
  95. get {
  96. if (list != null)
  97. return list.Keys;
  98. return hashtable.Keys;
  99. }
  100. }
  101. public object SyncRoot {
  102. get { return this; }
  103. }
  104. public ICollection Values {
  105. get {
  106. if (list != null)
  107. return list.Values;
  108. return hashtable.Values;
  109. }
  110. }
  111. // Methods
  112. public void Add (object key, object value)
  113. {
  114. if (list != null)
  115. if (list.Count >= switchAfter)
  116. Switch ();
  117. else {
  118. list.Add (key, value);
  119. return;
  120. }
  121. hashtable.Add (key, value);
  122. }
  123. public void Clear ()
  124. {
  125. if (caseInsensitive)
  126. list = new ListDictionary (CaseInsensitiveComparer.Default);
  127. else
  128. list = new ListDictionary ();
  129. hashtable = null;
  130. }
  131. public bool Contains (object key)
  132. {
  133. if (key == null) {
  134. if (this.Count == 0)
  135. return false;
  136. else
  137. throw new ArgumentNullException ("key");
  138. }
  139. if (list != null)
  140. return list.Contains (key);
  141. return hashtable.Contains (key);
  142. }
  143. public void CopyTo (Array array, int index)
  144. {
  145. if (list != null)
  146. list.CopyTo (array, index);
  147. else
  148. hashtable.CopyTo (array, index);
  149. }
  150. public IDictionaryEnumerator GetEnumerator ()
  151. {
  152. if (list != null)
  153. return list.GetEnumerator ();
  154. return hashtable.GetEnumerator ();
  155. }
  156. IEnumerator IEnumerable.GetEnumerator ()
  157. {
  158. return GetEnumerator ();
  159. }
  160. public void Remove (object key)
  161. {
  162. if (list != null)
  163. list.Remove (key);
  164. else
  165. hashtable.Remove (key);
  166. }
  167. private void Switch ()
  168. {
  169. if (caseInsensitive)
  170. hashtable = new Hashtable (switchAfter + 1,
  171. CaseInsensitiveHashCodeProvider.Default,
  172. CaseInsensitiveComparer.Default);
  173. else
  174. hashtable = new Hashtable (switchAfter + 1);
  175. IDictionaryEnumerator e = list.GetEnumerator ();
  176. while (e.MoveNext ())
  177. hashtable.Add (e.Key, e.Value);
  178. list = null;
  179. }
  180. }
  181. }