KeyedCollection.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #if NET_2_0
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Runtime.InteropServices;
  36. namespace System.Collections.ObjectModel
  37. {
  38. [ComVisible(false)]
  39. [Serializable]
  40. public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
  41. {
  42. private Dictionary<TKey, TItem> dictionary;
  43. private IEqualityComparer<TKey> comparer;
  44. private int dictionaryCreationThreshold;
  45. protected KeyedCollection ()
  46. : this (null, 0)
  47. {
  48. }
  49. protected KeyedCollection (IEqualityComparer<TKey> comparer)
  50. : this(comparer, 0)
  51. {
  52. }
  53. protected KeyedCollection (IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold)
  54. {
  55. if (comparer != null)
  56. this.comparer = comparer;
  57. else
  58. this.comparer = EqualityComparer<TKey>.Default;
  59. this.dictionaryCreationThreshold = dictionaryCreationThreshold;
  60. if (dictionaryCreationThreshold == 0)
  61. dictionary = new Dictionary<TKey, TItem> (this.comparer);
  62. }
  63. public bool Contains (TKey key)
  64. {
  65. if (dictionary != null)
  66. return dictionary.ContainsKey (key);
  67. return IndexOfKey (key) >= 0;
  68. }
  69. private int IndexOfKey (TKey key)
  70. {
  71. for (int i = Count - 1; i >= 0; i--)
  72. {
  73. TKey lkey = GetKeyForItem (this [i]);
  74. if (comparer.Equals (key, lkey))
  75. return i;
  76. }
  77. return -1;
  78. }
  79. public bool Remove (TKey key)
  80. {
  81. TItem item;
  82. if (dictionary != null)
  83. {
  84. if (dictionary.TryGetValue (key, out item))
  85. return base.Remove(item);
  86. else
  87. return false;
  88. }
  89. int idx = IndexOfKey (key);
  90. if (idx == -1)
  91. return false;
  92. RemoveAt(idx);
  93. return true;
  94. }
  95. public IEqualityComparer<TKey> Comparer {
  96. get {
  97. return comparer;
  98. }
  99. }
  100. public TItem this [TKey key] {
  101. get {
  102. if (dictionary != null)
  103. return dictionary [key];
  104. int idx = IndexOfKey (key);
  105. if (idx >= 0)
  106. return base [idx];
  107. else
  108. throw new KeyNotFoundException();
  109. }
  110. }
  111. protected void ChangeItemKey (TItem item, TKey newKey)
  112. {
  113. if (!Contains(item)) throw new ArgumentException();
  114. TKey oldKey = GetKeyForItem (item);
  115. if (comparer.Equals (oldKey, newKey)) return;
  116. if (Contains (newKey)) throw new ArgumentException();
  117. if (dictionary != null)
  118. {
  119. if (!dictionary.Remove (oldKey))
  120. throw new ArgumentException();
  121. dictionary.Add (newKey, item);
  122. }
  123. }
  124. protected override void ClearItems ()
  125. {
  126. if (dictionary != null)
  127. {
  128. dictionary.Clear();
  129. }
  130. base.ClearItems ();
  131. }
  132. protected abstract TKey GetKeyForItem (TItem item);
  133. protected override void InsertItem (int index, TItem item)
  134. {
  135. if (dictionary != null)
  136. {
  137. dictionary.Add (GetKeyForItem (item), item);
  138. }
  139. else
  140. {
  141. if (dictionaryCreationThreshold != -1 && Count + 1 > dictionaryCreationThreshold)
  142. {
  143. dictionary = new Dictionary<TKey, TItem> (comparer);
  144. for (int i = Count - 1; i >= 0; i--)
  145. {
  146. TItem dictitem = this[i];
  147. dictionary.Add(GetKeyForItem(dictitem), dictitem);
  148. }
  149. dictionary.Add (GetKeyForItem (item), item);
  150. }
  151. }
  152. base.InsertItem (index, item);
  153. }
  154. protected override void RemoveItem (int index)
  155. {
  156. if (dictionary != null)
  157. {
  158. TKey key = GetKeyForItem (this [index]);
  159. dictionary.Remove (key);
  160. }
  161. base.RemoveItem (index);
  162. }
  163. protected override void SetItem (int index, TItem item)
  164. {
  165. if (dictionary != null)
  166. {
  167. dictionary.Remove (GetKeyForItem (this [index]));
  168. dictionary.Add (GetKeyForItem (item), item);
  169. }
  170. base.SetItem (index, item);
  171. }
  172. protected IDictionary<TKey, TItem> Dictionary {
  173. get {
  174. return dictionary;
  175. }
  176. }
  177. }
  178. }
  179. #endif