Set.cs 7.4 KB

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