KeyedCollection.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.ObjectModel.KeyedCollection
  4. //
  5. // Author:
  6. // Zoltan Varga ([email protected])
  7. //
  8. // (C) 2005 Novell, Inc.
  9. //
  10. //
  11. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Runtime.InteropServices;
  35. using System.Diagnostics;
  36. namespace System.Collections.ObjectModel
  37. {
  38. [ComVisible(false)]
  39. [Serializable]
  40. [DebuggerDisplay ("Count={Count}")]
  41. [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
  42. public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
  43. {
  44. private Dictionary<TKey, TItem> dictionary;
  45. private IEqualityComparer<TKey> comparer;
  46. private int dictionaryCreationThreshold;
  47. protected KeyedCollection ()
  48. : this (null, 0)
  49. {
  50. }
  51. protected KeyedCollection (IEqualityComparer<TKey> comparer)
  52. : this(comparer, 0)
  53. {
  54. }
  55. protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
  56. {
  57. if (comparer != null)
  58. this.comparer = comparer;
  59. else
  60. this.comparer = EqualityComparer<TKey>.Default;
  61. this.dictionaryCreationThreshold = dictionaryCreationThreshold;
  62. if (dictionaryCreationThreshold == 0)
  63. dictionary = new Dictionary<TKey, TItem> (this.comparer);
  64. }
  65. public bool Contains (TKey key)
  66. {
  67. if (dictionary != null)
  68. return dictionary.ContainsKey (key);
  69. return IndexOfKey (key) >= 0;
  70. }
  71. private int IndexOfKey (TKey key)
  72. {
  73. for (int i = Count - 1; i >= 0; i--)
  74. {
  75. TKey lkey = GetKeyForItem (this [i]);
  76. if (comparer.Equals (key, lkey))
  77. return i;
  78. }
  79. return -1;
  80. }
  81. public bool Remove (TKey key)
  82. {
  83. TItem item;
  84. if (dictionary != null)
  85. {
  86. if (dictionary.TryGetValue (key, out item))
  87. return base.Remove(item);
  88. else
  89. return false;
  90. }
  91. int idx = IndexOfKey (key);
  92. if (idx == -1)
  93. return false;
  94. RemoveAt(idx);
  95. return true;
  96. }
  97. public IEqualityComparer<TKey> Comparer {
  98. get {
  99. return comparer;
  100. }
  101. }
  102. public TItem this [TKey key] {
  103. get {
  104. if (dictionary != null)
  105. return dictionary [key];
  106. int idx = IndexOfKey (key);
  107. if (idx >= 0)
  108. return base [idx];
  109. else
  110. throw new KeyNotFoundException();
  111. }
  112. }
  113. protected void ChangeItemKey (TItem item, TKey newKey)
  114. {
  115. if (!Contains(item)) throw new ArgumentException();
  116. TKey oldKey = GetKeyForItem (item);
  117. if (comparer.Equals (oldKey, newKey)) return;
  118. if (Contains (newKey)) throw new ArgumentException();
  119. if (dictionary != null)
  120. {
  121. if (!dictionary.Remove (oldKey))
  122. throw new ArgumentException();
  123. dictionary.Add (newKey, item);
  124. }
  125. }
  126. protected override void ClearItems ()
  127. {
  128. if (dictionary != null)
  129. {
  130. dictionary.Clear();
  131. }
  132. base.ClearItems ();
  133. }
  134. protected abstract TKey GetKeyForItem (TItem item);
  135. protected override void InsertItem (int index, TItem item)
  136. {
  137. TKey key = GetKeyForItem (item);
  138. if (key == null)
  139. throw new ArgumentNullException ("GetKeyForItem(item)");
  140. if (dictionary != null && dictionary.ContainsKey (key))
  141. throw new ArgumentException ("An element with the same key already exists in the dictionary.");
  142. if (dictionary == null)
  143. for (int i = 0; i < Count; ++i) {
  144. if (comparer.Equals (key, GetKeyForItem (this [i]))) {
  145. throw new ArgumentException ("An element with the same key already exists in the dictionary.");
  146. }
  147. }
  148. base.InsertItem (index, item);
  149. if (dictionary != null)
  150. dictionary.Add (key, item);
  151. else if (dictionaryCreationThreshold != -1 && Count > dictionaryCreationThreshold) {
  152. dictionary = new Dictionary<TKey, TItem> (comparer);
  153. for (int i = 0; i < Count; ++i) {
  154. TItem dictitem = this [i];
  155. dictionary.Add (GetKeyForItem (dictitem), dictitem);
  156. }
  157. }
  158. }
  159. protected override void RemoveItem (int index)
  160. {
  161. if (dictionary != null)
  162. {
  163. TKey key = GetKeyForItem (this [index]);
  164. dictionary.Remove (key);
  165. }
  166. base.RemoveItem (index);
  167. }
  168. protected override void SetItem (int index, TItem item)
  169. {
  170. if (dictionary != null)
  171. {
  172. dictionary.Remove (GetKeyForItem (this [index]));
  173. dictionary.Add (GetKeyForItem (item), item);
  174. }
  175. base.SetItem (index, item);
  176. }
  177. protected IDictionary<TKey, TItem> Dictionary {
  178. get {
  179. return dictionary;
  180. }
  181. }
  182. }
  183. }