PageRenderTime 25ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System/System.Collections.Specialized/HybridDictionary.cs

https://bitbucket.org/danipen/mono
C# | 161 lines | 92 code | 34 blank | 35 comment | 8 complexity | 1eff8a028a3108c61e393c8bacedcb43 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Collections.Specialized.HybridDictionary.cs
  3. //
  4. // Author:
  5. // Lawrence Pit (loz@cable.a2000.nl)
  6. // Raja R Harinath <rharinath@novell.com>
  7. //
  8. // Copyright (C) 2004, 2005 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. namespace System.Collections.Specialized {
  31. [Serializable]
  32. public class HybridDictionary : IDictionary, ICollection, IEnumerable {
  33. private const int switchAfter = 10;
  34. private IDictionary inner {
  35. get { return list == null ? (IDictionary) hashtable : (IDictionary) list; }
  36. }
  37. private bool caseInsensitive;
  38. private Hashtable hashtable;
  39. private ListDictionary list;
  40. // Constructors
  41. public HybridDictionary() : this (0, false) { }
  42. public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
  43. public HybridDictionary (int initialSize) : this (initialSize, false) { }
  44. public HybridDictionary (int initialSize, bool caseInsensitive)
  45. {
  46. this.caseInsensitive = caseInsensitive;
  47. IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
  48. IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
  49. if (initialSize <= switchAfter)
  50. list = new ListDictionary (comparer);
  51. else
  52. hashtable = new Hashtable (initialSize, hcp, comparer);
  53. }
  54. // Properties
  55. public int Count {
  56. get { return inner.Count; }
  57. }
  58. public bool IsFixedSize {
  59. get { return false; }
  60. }
  61. public bool IsReadOnly {
  62. get { return false; }
  63. }
  64. public bool IsSynchronized {
  65. get { return false; }
  66. }
  67. public object this [object key] {
  68. get { return inner [key]; }
  69. set {
  70. inner [key] = value;
  71. if (list != null && Count > switchAfter)
  72. Switch ();
  73. }
  74. }
  75. public ICollection Keys {
  76. get { return inner.Keys; }
  77. }
  78. public object SyncRoot {
  79. get { return this; }
  80. }
  81. public ICollection Values {
  82. get { return inner.Values; }
  83. }
  84. // Methods
  85. public void Add (object key, object value)
  86. {
  87. inner.Add (key, value);
  88. if (list != null && Count > switchAfter)
  89. Switch ();
  90. }
  91. public void Clear ()
  92. {
  93. // According to MSDN, this doesn't switch a Hashtable back to a ListDictionary
  94. inner.Clear ();
  95. }
  96. public bool Contains (object key)
  97. {
  98. return inner.Contains (key);
  99. }
  100. public void CopyTo (Array array, int index)
  101. {
  102. inner.CopyTo (array, index);
  103. }
  104. public IDictionaryEnumerator GetEnumerator ()
  105. {
  106. return inner.GetEnumerator ();
  107. }
  108. IEnumerator IEnumerable.GetEnumerator ()
  109. {
  110. return GetEnumerator ();
  111. }
  112. public void Remove (object key)
  113. {
  114. // According to MSDN, this does not switch a Hashtable back to a ListDictionary
  115. // even if Count falls below switchAfter
  116. inner.Remove (key);
  117. }
  118. private void Switch ()
  119. {
  120. IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
  121. IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
  122. hashtable = new Hashtable (list, hcp, comparer);
  123. list.Clear ();
  124. list = null;
  125. }
  126. }
  127. }