Set`1.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace SmartCoalApplication
  5. {
  6. /// <summary>
  7. /// Represents an enumerable collection of items. Each item can only be present
  8. /// in the collection once. An item's identity is determined by a combination
  9. /// of the return values from its GetHashCode and Equals methods.
  10. /// This class is analagous to C++'s std::set template class.
  11. /// </summary>
  12. [Serializable]
  13. public class Set<T>
  14. : ICloneable,
  15. ICollection<T>
  16. {
  17. private Dictionary<T, object> dictionary;
  18. public static Set<T> Intersect(Set<T> set1, Set<T> set2)
  19. {
  20. Set<T> intersection = new Set<T>();
  21. foreach (T item in set1)
  22. {
  23. if (set2.Contains(item))
  24. {
  25. intersection.Add(item);
  26. }
  27. }
  28. return intersection;
  29. }
  30. public static Set<T> Union(Set<T> set1, Set<T> set2)
  31. {
  32. Set<T> union = new Set<T>(set1);
  33. foreach (T item in set2)
  34. {
  35. if (!union.Contains(item))
  36. {
  37. union.Add(item);
  38. }
  39. }
  40. return union;
  41. }
  42. public static Set<T> Without(Set<T> withUs, Set<T> withoutUs)
  43. {
  44. Set<T> result = new Set<T>();
  45. foreach (T item in withUs)
  46. {
  47. if (!withoutUs.Contains(item))
  48. {
  49. result.Add(item);
  50. }
  51. }
  52. return result;
  53. }
  54. public static bool AreEqual(Set<T> set1, Set<T> set2)
  55. {
  56. if (set1.Count != set2.Count)
  57. {
  58. // Can't be equal if sizes are different
  59. return false;
  60. }
  61. if (set1.Count == 0)
  62. {
  63. // Empty sets are equal to each other.
  64. // We know that set1.Count=set2.Count, so no need to check set2.Count for 0 as well.
  65. return true;
  66. }
  67. // At this point we know that either everything in set1 is in set2, or
  68. // that there is something in set1 which is not in set2.
  69. foreach (T item in set1)
  70. {
  71. if (!set2.Contains(item))
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. public bool IsEqualTo(Set<T> set2)
  79. {
  80. return AreEqual(this, set2);
  81. }
  82. public bool IsSubsetOf(Set<T> set2)
  83. {
  84. foreach (T item in this)
  85. {
  86. if (!set2.Contains(item))
  87. {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. public Set<T> Without(Set<T> withoutUs)
  94. {
  95. return Set<T>.Without(this, withoutUs);
  96. }
  97. /// <summary>
  98. /// Adds an element to the set.
  99. /// </summary>
  100. /// <param name="item">The object reference to be included in the set.</param>
  101. /// <exception cref="ArgumentNullException">item is a null reference</exception>
  102. /// <exception cref="ArgumentException">item is already in the Set</exception>
  103. public void Add(T item)
  104. {
  105. try
  106. {
  107. this.dictionary.Add(item, null);
  108. }
  109. catch (ArgumentNullException e1)
  110. {
  111. throw e1;
  112. }
  113. catch (ArgumentException e2)
  114. {
  115. throw e2;
  116. }
  117. }
  118. public void AddRange(IEnumerable<T> items)
  119. {
  120. foreach (T item in items)
  121. {
  122. Add(item);
  123. }
  124. }
  125. public void AddRange(params T[] items)
  126. {
  127. AddRange((IEnumerable<T>)items);
  128. }
  129. /// <summary>
  130. /// Removes an element from the set.
  131. /// </summary>
  132. /// <param name="item">The object reference to be excluded from the set.</param>
  133. /// <exception cref="ArgumentNullException">item is a null reference</exception>
  134. public bool Remove(T item)
  135. {
  136. try
  137. {
  138. this.dictionary.Remove(item);
  139. return true;
  140. }
  141. catch (ArgumentNullException)
  142. {
  143. return false;
  144. }
  145. }
  146. /// <summary>
  147. /// Determines whether the Set includes a specific element.
  148. /// </summary>
  149. /// <param name="item">The object reference to check for.</param>
  150. /// <returns>true if the Set includes item, false if it doesn't.</returns>
  151. /// <exception cref="ArgumentNullException">item is a null reference.</exception>
  152. public bool Contains(T item)
  153. {
  154. try
  155. {
  156. return this.dictionary.ContainsKey(item);
  157. }
  158. catch (ArgumentNullException e1)
  159. {
  160. throw e1;
  161. }
  162. }
  163. /// <summary>
  164. /// Constructs an empty Set.
  165. /// </summary>
  166. public Set()
  167. {
  168. this.dictionary = new Dictionary<T, object>();
  169. }
  170. /// <summary>
  171. /// Constructs a Set with data copied from the given list.
  172. /// </summary>
  173. /// <param name="cloneMe"></param>
  174. public Set(IEnumerable<T> cloneMe)
  175. {
  176. this.dictionary = new Dictionary<T, object>();
  177. foreach (T theObject in cloneMe)
  178. {
  179. Add(theObject);
  180. }
  181. }
  182. public Set(params T[] items)
  183. : this((IEnumerable<T>)items)
  184. {
  185. }
  186. /// <summary>
  187. /// Constructs a copy of a Set.
  188. /// </summary>
  189. /// <param name="copyMe">The Set to copy from.</param>
  190. private Set(Set<T> copyMe)
  191. {
  192. this.dictionary = new Dictionary<T, object>(copyMe.dictionary);
  193. }
  194. #region IEnumerable<T> Members
  195. /// <summary>
  196. /// Returns an IEnumerator that can be used to enumerate through the items in the Set.
  197. /// </summary>
  198. /// <returns>An IEnumerator for the Set.</returns>
  199. IEnumerator IEnumerable.GetEnumerator()
  200. {
  201. return this.dictionary.Keys.GetEnumerator();
  202. }
  203. public IEnumerator<T> GetEnumerator()
  204. {
  205. return this.dictionary.Keys.GetEnumerator();
  206. }
  207. #endregion
  208. public Set<T> Clone()
  209. {
  210. return new Set<T>(this);
  211. }
  212. #region ICloneable Members
  213. /// <summary>
  214. /// Returns a copy of the Set. The elements in the Set are copied by-reference only.
  215. /// </summary>
  216. /// <returns></returns>
  217. object ICloneable.Clone()
  218. {
  219. return Clone();
  220. }
  221. #endregion
  222. #region ICollection<T> Members
  223. /// <summary>
  224. /// Gets a value indicating whether or not the Set is synchronized (thread-safe).
  225. /// </summary>
  226. public bool IsSynchronized
  227. {
  228. get
  229. {
  230. return false;
  231. }
  232. }
  233. /// <summary>
  234. /// Gets a value indicating how many elements are contained within the Set.
  235. /// </summary>
  236. public int Count
  237. {
  238. get
  239. {
  240. return this.dictionary.Count;
  241. }
  242. }
  243. /// <summary>
  244. /// Copies the Set elements to a one-dimensional Array instance at a specified index.
  245. /// </summary>
  246. /// <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>
  247. /// <param name="index">The zero-based index in array at which copying begins.</param>
  248. /// <exception cref="ArgumentNullException">array is a null reference.</exception>
  249. /// <exception cref="ArgumentOutOfRangeException">index is less than zero.</exception>
  250. /// <exception cref="ArgumentException">The array is not one-dimensional, or the array could not contain the objects copied to it.</exception>
  251. /// <exception cref="IndexOutOfRangeException">The Array does not have enough space, starting from the given offset, to contain all the Set's objects.</exception>
  252. public void CopyTo(T[] array, int index)
  253. {
  254. int i = index;
  255. if (array == null)
  256. {
  257. throw new ArgumentNullException("array");
  258. }
  259. if (index < 0)
  260. {
  261. throw new ArgumentOutOfRangeException("index");
  262. }
  263. foreach (T o in this)
  264. {
  265. try
  266. {
  267. array.SetValue(o, i);
  268. }
  269. catch (ArgumentException e1)
  270. {
  271. throw e1;
  272. }
  273. catch (IndexOutOfRangeException e2)
  274. {
  275. throw e2;
  276. }
  277. ++i;
  278. }
  279. }
  280. /// <summary>
  281. /// Gets an object that can be used to synchronize access to the Set.
  282. /// </summary>
  283. public object SyncRoot
  284. {
  285. get
  286. {
  287. return this;
  288. }
  289. }
  290. #endregion
  291. /// <summary>
  292. /// Copies the elements of the Set to a new generic array.
  293. /// </summary>
  294. /// <returns>An array of object references.</returns>
  295. public T[] ToArray()
  296. {
  297. T[] array = new T[Count];
  298. int index = 0;
  299. foreach (T o in this)
  300. {
  301. array[index] = o;
  302. ++index;
  303. }
  304. return array;
  305. }
  306. #region ICollection<T> Members
  307. public void Clear()
  308. {
  309. this.dictionary = new Dictionary<T, object>();
  310. }
  311. public bool IsReadOnly
  312. {
  313. get
  314. {
  315. return false;
  316. }
  317. }
  318. #endregion
  319. }
  320. }