PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Base/Set.cs

https://bitbucket.org/tuldok89/openpdn
C# | 269 lines | 154 code | 38 blank | 77 comment | 3 complexity | 9485820279c52a8714e765f7d652fd01 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Collections;
  11. namespace PaintDotNet.Base
  12. {
  13. /// <summary>
  14. /// Represents an enumerable collection of items. Each item can only be present
  15. /// in the collection once. An item's identity is determined by a combination
  16. /// of the return values from its GetHashCode and Equals methods.
  17. /// This class is analagous to C++'s std::set template class.
  18. /// </summary>
  19. [Serializable]
  20. public class Set
  21. : ICloneable,
  22. ICollection
  23. {
  24. private readonly Hashtable _hashtable;
  25. /// <summary>
  26. /// Adds an element to the set.
  27. /// </summary>
  28. /// <param name="item">The object reference to be included in the set.</param>
  29. /// <exception cref="ArgumentNullException">item is a null reference</exception>
  30. /// <exception cref="ArgumentException">item is already in the Set</exception>
  31. public void Add(object item)
  32. {
  33. try
  34. {
  35. _hashtable.Add(item, null);
  36. }
  37. catch (ArgumentNullException e1)
  38. {
  39. throw e1;
  40. }
  41. catch (ArgumentException e2)
  42. {
  43. throw e2;
  44. }
  45. }
  46. /// <summary>
  47. /// Removes an element from the set.
  48. /// </summary>
  49. /// <param name="item">The object reference to be excluded from the set.</param>
  50. /// <exception cref="ArgumentNullException">item is a null reference</exception>
  51. public void Remove(object item)
  52. {
  53. try
  54. {
  55. _hashtable.Remove(item);
  56. }
  57. catch (ArgumentNullException e1)
  58. {
  59. throw e1;
  60. }
  61. }
  62. /// <summary>
  63. /// Determines whether the Set includes a specific element.
  64. /// </summary>
  65. /// <param name="item">The object reference to check for.</param>
  66. /// <returns>true if the Set includes item, false if it doesn't.</returns>
  67. /// <exception cref="ArgumentNullException">item is a null reference.</exception>
  68. public bool Contains(object item)
  69. {
  70. try
  71. {
  72. return _hashtable.ContainsKey(item);
  73. }
  74. catch (ArgumentNullException e1)
  75. {
  76. throw e1;
  77. }
  78. }
  79. /// <summary>
  80. /// Constructs an empty Set.
  81. /// </summary>
  82. public Set()
  83. {
  84. _hashtable = new Hashtable();
  85. }
  86. /// <summary>
  87. /// Constructs a Set with data copied from the given list.
  88. /// </summary>
  89. /// <param name="cloneMe"></param>
  90. public Set(IEnumerable cloneMe)
  91. {
  92. _hashtable = new Hashtable();
  93. foreach (object theObject in cloneMe)
  94. {
  95. Add(theObject);
  96. }
  97. }
  98. public static Set<T> Create<T>(params T[] items)
  99. {
  100. return new Set<T>(items);
  101. }
  102. /// <summary>
  103. /// Constructs a copy of a Set.
  104. /// </summary>
  105. /// <param name="copyMe">The Set to copy from.</param>
  106. private Set(Set copyMe)
  107. {
  108. _hashtable = (Hashtable)copyMe.Clone();
  109. }
  110. #region IEnumerable Members
  111. /// <summary>
  112. /// Returns an IEnumerator that can be used to enumerate through the items in the Set.
  113. /// </summary>
  114. /// <returns>An IEnumerator for the Set.</returns>
  115. public IEnumerator GetEnumerator()
  116. {
  117. return _hashtable.Keys.GetEnumerator();
  118. }
  119. #endregion
  120. #region ICloneable Members
  121. /// <summary>
  122. /// Returns a copy of the Set. The elements in the Set are copied by-reference only.
  123. /// </summary>
  124. /// <returns></returns>
  125. public object Clone()
  126. {
  127. return new Set(this);
  128. }
  129. #endregion
  130. #region ICollection Members
  131. /// <summary>
  132. /// Gets a value indicating whether or not the Set is synchronized (thread-safe).
  133. /// </summary>
  134. public bool IsSynchronized
  135. {
  136. get
  137. {
  138. return false;
  139. }
  140. }
  141. /// <summary>
  142. /// Gets a value indicating how many elements are contained within the Set.
  143. /// </summary>
  144. public int Count
  145. {
  146. get
  147. {
  148. return _hashtable.Count;
  149. }
  150. }
  151. /// <summary>
  152. /// Copies the Set elements to a one-dimensional Array instance at a specified index.
  153. /// </summary>
  154. /// <param name="array">The one-dimensional Array that is the destination of the objects copied from the Set. The Array must have zero-based indexing.</param>
  155. /// <param name="index">The zero-based index in array at which copying begins.</param>
  156. /// <exception cref="ArgumentNullException">array is a null reference.</exception>
  157. /// <exception cref="ArgumentOutOfRangeException">index is less than zero.</exception>
  158. /// <exception cref="ArgumentException">The array is not one-dimensional, or the array could not contain the objects copied to it.</exception>
  159. /// <exception cref="IndexOutOfRangeException">The Array does not have enough space, starting from the given offset, to contain all the Set's objects.</exception>
  160. public void CopyTo(Array array, int index)
  161. {
  162. int i = index;
  163. if (array == null)
  164. {
  165. throw new ArgumentNullException("array");
  166. }
  167. if (index < 0)
  168. {
  169. throw new ArgumentOutOfRangeException("index");
  170. }
  171. foreach (object o in this)
  172. {
  173. try
  174. {
  175. array.SetValue(o, i);
  176. }
  177. catch (ArgumentException e1)
  178. {
  179. throw e1;
  180. }
  181. catch (IndexOutOfRangeException e2)
  182. {
  183. throw e2;
  184. }
  185. ++i;
  186. }
  187. }
  188. /// <summary>
  189. /// Gets an object that can be used to synchronize access to the Set.
  190. /// </summary>
  191. public object SyncRoot
  192. {
  193. get
  194. {
  195. return this;
  196. }
  197. }
  198. #endregion
  199. /// <summary>
  200. /// Copies the elements of the Set to a new generic array.
  201. /// </summary>
  202. /// <returns>An array of object references.</returns>
  203. public object[] ToArray()
  204. {
  205. var array = new object[Count];
  206. int index = 0;
  207. foreach (object o in this)
  208. {
  209. array[index] = o;
  210. ++index;
  211. }
  212. return array;
  213. }
  214. /// <summary>
  215. /// Copies the elements of the Set to a new array of the requested type.
  216. /// </summary>
  217. /// <param name="type">The Type of array to create and copy elements to.</param>
  218. /// <returns>An array of objects of the requested type.</returns>
  219. public Array ToArray(Type type)
  220. {
  221. Array array = Array.CreateInstance(type, Count);
  222. int index = 0;
  223. foreach (object o in this)
  224. {
  225. array.SetValue(o, index);
  226. ++index;
  227. }
  228. return array;
  229. }
  230. }
  231. }