HybridDictionary.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // System.Collections.Specialized.HybridDictionary.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. namespace System.Collections.Specialized {
  10. [Serializable]
  11. public class HybridDictionary : IDictionary, ICollection, IEnumerable {
  12. private const int switchAfter = 10;
  13. private ListDictionary list;
  14. private Hashtable hashtable;
  15. private bool caseInsensitive = false;
  16. // Constructors
  17. public HybridDictionary() : this (0, false) { }
  18. public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
  19. public HybridDictionary (int initialSize) : this (initialSize, false) { }
  20. public HybridDictionary(int initialSize, bool caseInsensitive)
  21. {
  22. this.caseInsensitive = caseInsensitive;
  23. if (initialSize <= switchAfter)
  24. if (caseInsensitive)
  25. list = new ListDictionary (CaseInsensitiveComparer.Default);
  26. else
  27. list = new ListDictionary ();
  28. else
  29. if (caseInsensitive)
  30. hashtable = new Hashtable (initialSize,
  31. CaseInsensitiveHashCodeProvider.Default,
  32. CaseInsensitiveComparer.Default);
  33. else
  34. hashtable = new Hashtable (initialSize);
  35. }
  36. // Properties
  37. public int Count {
  38. get {
  39. if (list != null)
  40. return list.Count;
  41. return hashtable.Count;
  42. }
  43. }
  44. public bool IsFixedSize {
  45. get { return false; }
  46. }
  47. public bool IsReadOnly {
  48. get { return false; }
  49. }
  50. public bool IsSynchronized {
  51. get { return false; }
  52. }
  53. public object this [object key] {
  54. get {
  55. if (key == null)
  56. throw new ArgumentNullException("key");
  57. if (list != null)
  58. return list [key];
  59. return hashtable [key];
  60. }
  61. set {
  62. if (list != null)
  63. if (list.Count >= switchAfter)
  64. Switch ();
  65. else {
  66. list [key] = value;
  67. return;
  68. }
  69. hashtable [key] = value;
  70. }
  71. }
  72. public ICollection Keys {
  73. get {
  74. if (list != null)
  75. return list.Keys;
  76. return hashtable.Keys;
  77. }
  78. }
  79. public object SyncRoot {
  80. get { return this; }
  81. }
  82. public ICollection Values {
  83. get {
  84. if (list != null)
  85. return list.Values;
  86. return hashtable.Values;
  87. }
  88. }
  89. // Methods
  90. public void Add (object key, object value)
  91. {
  92. if (list != null)
  93. if (list.Count >= switchAfter)
  94. Switch ();
  95. else {
  96. list.Add (key, value);
  97. return;
  98. }
  99. hashtable.Add (key, value);
  100. }
  101. public void Clear ()
  102. {
  103. if (caseInsensitive)
  104. list = new ListDictionary (CaseInsensitiveComparer.Default);
  105. else
  106. list = new ListDictionary ();
  107. hashtable = null;
  108. }
  109. public bool Contains (object key)
  110. {
  111. if (list != null)
  112. return list.Contains (key);
  113. return hashtable.Contains (key);
  114. }
  115. public void CopyTo (Array array, int index)
  116. {
  117. if (list != null)
  118. list.CopyTo (array, index);
  119. else
  120. hashtable.CopyTo (array, index);
  121. }
  122. public IDictionaryEnumerator GetEnumerator ()
  123. {
  124. if (list != null)
  125. return list.GetEnumerator ();
  126. return hashtable.GetEnumerator ();
  127. }
  128. IEnumerator IEnumerable.GetEnumerator ()
  129. {
  130. return GetEnumerator ();
  131. }
  132. public void Remove (object key)
  133. {
  134. if (list != null)
  135. list.Remove (key);
  136. else
  137. hashtable.Remove (key);
  138. }
  139. private void Switch ()
  140. {
  141. if (caseInsensitive)
  142. hashtable = new Hashtable (switchAfter + 1,
  143. CaseInsensitiveHashCodeProvider.Default,
  144. CaseInsensitiveComparer.Default);
  145. else
  146. hashtable = new Hashtable (switchAfter + 1);
  147. IDictionaryEnumerator e = list.GetEnumerator ();
  148. while (e.MoveNext ())
  149. hashtable.Add (e.Key, e.Value);
  150. list = null;
  151. }
  152. }
  153. }