HashSet.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. //
  2. // HashSet.cs
  3. //
  4. // Authors:
  5. // Jb Evain <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Runtime.Serialization;
  33. using System.Runtime.InteropServices;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Diagnostics;
  37. // HashSet is basically implemented as a reduction of Dictionary<K, V>
  38. namespace System.Collections.Generic {
  39. [Serializable]
  40. [DebuggerDisplay ("Count={Count}")]
  41. [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
  42. public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback
  43. , ISet<T>
  44. {
  45. const int INITIAL_SIZE = 10;
  46. const float DEFAULT_LOAD_FACTOR = (90f / 100);
  47. const int NO_SLOT = -1;
  48. const int HASH_FLAG = -2147483648;
  49. struct Link {
  50. public int HashCode;
  51. public int Next;
  52. }
  53. // The hash table contains indices into the "links" array
  54. int [] table;
  55. Link [] links;
  56. T [] slots;
  57. // The number of slots in "links" and "slots" that
  58. // are in use (i.e. filled with data) or have been used and marked as
  59. // "empty" later on.
  60. int touched;
  61. // The index of the first slot in the "empty slots chain".
  62. // "Remove ()" prepends the cleared slots to the empty chain.
  63. // "Add ()" fills the first slot in the empty slots chain with the
  64. // added item (or increases "touched" if the chain itself is empty).
  65. int empty_slot;
  66. // The number of items in this set.
  67. int count;
  68. // The number of items the set can hold without
  69. // resizing the hash table and the slots arrays.
  70. int threshold;
  71. IEqualityComparer<T> comparer;
  72. SerializationInfo si;
  73. // The number of changes made to this set. Used by enumerators
  74. // to detect changes and invalidate themselves.
  75. int generation;
  76. public int Count {
  77. get { return count; }
  78. }
  79. public HashSet ()
  80. {
  81. Init (INITIAL_SIZE, null);
  82. }
  83. public HashSet (IEqualityComparer<T> comparer)
  84. {
  85. Init (INITIAL_SIZE, comparer);
  86. }
  87. public HashSet (IEnumerable<T> collection) : this (collection, null)
  88. {
  89. }
  90. public HashSet (IEnumerable<T> collection, IEqualityComparer<T> comparer)
  91. {
  92. if (collection == null)
  93. throw new ArgumentNullException ("collection");
  94. int capacity = 0;
  95. var col = collection as ICollection<T>;
  96. if (col != null)
  97. capacity = col.Count;
  98. Init (capacity, comparer);
  99. foreach (var item in collection)
  100. Add (item);
  101. }
  102. protected HashSet (SerializationInfo info, StreamingContext context)
  103. {
  104. si = info;
  105. }
  106. void Init (int capacity, IEqualityComparer<T> comparer)
  107. {
  108. if (capacity < 0)
  109. throw new ArgumentOutOfRangeException ("capacity");
  110. this.comparer = comparer ?? EqualityComparer<T>.Default;
  111. if (capacity == 0)
  112. capacity = INITIAL_SIZE;
  113. /* Modify capacity so 'capacity' elements can be added without resizing */
  114. capacity = (int) (capacity / DEFAULT_LOAD_FACTOR) + 1;
  115. InitArrays (capacity);
  116. generation = 0;
  117. }
  118. void InitArrays (int size)
  119. {
  120. table = new int [size];
  121. links = new Link [size];
  122. empty_slot = NO_SLOT;
  123. slots = new T [size];
  124. touched = 0;
  125. threshold = (int) (table.Length * DEFAULT_LOAD_FACTOR);
  126. if (threshold == 0 && table.Length > 0)
  127. threshold = 1;
  128. }
  129. bool SlotsContainsAt (int index, int hash, T item)
  130. {
  131. int current = table [index] - 1;
  132. while (current != NO_SLOT) {
  133. Link link = links [current];
  134. if (link.HashCode == hash && ((hash == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (item, slots [current])))
  135. return true;
  136. current = link.Next;
  137. }
  138. return false;
  139. }
  140. public void CopyTo (T [] array)
  141. {
  142. CopyTo (array, 0, count);
  143. }
  144. public void CopyTo (T [] array, int arrayIndex)
  145. {
  146. CopyTo (array, arrayIndex, count);
  147. }
  148. public void CopyTo (T [] array, int arrayIndex, int count)
  149. {
  150. if (array == null)
  151. throw new ArgumentNullException ("array");
  152. if (arrayIndex < 0)
  153. throw new ArgumentOutOfRangeException ("arrayIndex");
  154. if (arrayIndex > array.Length)
  155. throw new ArgumentException ("index larger than largest valid index of array");
  156. if (array.Length - arrayIndex < count)
  157. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  158. for (int i = 0, items = 0; i < touched && items < count; i++) {
  159. if (GetLinkHashCode (i) != 0)
  160. array [arrayIndex++] = slots [i];
  161. }
  162. }
  163. void Resize (int size)
  164. {
  165. int newSize = HashPrimeNumbers.ToPrime (size);
  166. // allocate new hash table and link slots array
  167. var newTable = new int [newSize];
  168. var newLinks = new Link [newSize];
  169. for (int i = 0; i < table.Length; i++) {
  170. int current = table [i] - 1;
  171. while (current != NO_SLOT) {
  172. int hashCode = newLinks [current].HashCode = GetItemHashCode (slots [current]);
  173. int index = (hashCode & int.MaxValue) % newSize;
  174. newLinks [current].Next = newTable [index] - 1;
  175. newTable [index] = current + 1;
  176. current = links [current].Next;
  177. }
  178. }
  179. table = newTable;
  180. links = newLinks;
  181. // allocate new data slots, copy data
  182. var newSlots = new T [newSize];
  183. Array.Copy (slots, 0, newSlots, 0, touched);
  184. slots = newSlots;
  185. threshold = (int) (newSize * DEFAULT_LOAD_FACTOR);
  186. }
  187. int GetLinkHashCode (int index)
  188. {
  189. return links [index].HashCode & HASH_FLAG;
  190. }
  191. int GetItemHashCode (T item)
  192. {
  193. if (item == null)
  194. return HASH_FLAG;
  195. return comparer.GetHashCode (item) | HASH_FLAG;
  196. }
  197. public bool Add (T item)
  198. {
  199. int hashCode = GetItemHashCode (item);
  200. int index = (hashCode & int.MaxValue) % table.Length;
  201. if (SlotsContainsAt (index, hashCode, item))
  202. return false;
  203. if (++count > threshold) {
  204. Resize ((table.Length << 1) | 1);
  205. index = (hashCode & int.MaxValue) % table.Length;
  206. }
  207. // find an empty slot
  208. int current = empty_slot;
  209. if (current == NO_SLOT)
  210. current = touched++;
  211. else
  212. empty_slot = links [current].Next;
  213. // store the hash code of the added item,
  214. // prepend the added item to its linked list,
  215. // update the hash table
  216. links [current].HashCode = hashCode;
  217. links [current].Next = table [index] - 1;
  218. table [index] = current + 1;
  219. // store item
  220. slots [current] = item;
  221. generation++;
  222. return true;
  223. }
  224. public IEqualityComparer<T> Comparer {
  225. get { return comparer; }
  226. }
  227. public void Clear ()
  228. {
  229. count = 0;
  230. Array.Clear (table, 0, table.Length);
  231. Array.Clear (slots, 0, slots.Length);
  232. Array.Clear (links, 0, links.Length);
  233. // empty the "empty slots chain"
  234. empty_slot = NO_SLOT;
  235. touched = 0;
  236. generation++;
  237. }
  238. public bool Contains (T item)
  239. {
  240. int hashCode = GetItemHashCode (item);
  241. int index = (hashCode & int.MaxValue) % table.Length;
  242. return SlotsContainsAt (index, hashCode, item);
  243. }
  244. public bool Remove (T item)
  245. {
  246. // get first item of linked list corresponding to given key
  247. int hashCode = GetItemHashCode (item);
  248. int index = (hashCode & int.MaxValue) % table.Length;
  249. int current = table [index] - 1;
  250. // if there is no linked list, return false
  251. if (current == NO_SLOT)
  252. return false;
  253. // walk linked list until right slot (and its predecessor) is
  254. // found or end is reached
  255. int prev = NO_SLOT;
  256. do {
  257. Link link = links [current];
  258. if (link.HashCode == hashCode && ((hashCode == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (slots [current], item)))
  259. break;
  260. prev = current;
  261. current = link.Next;
  262. } while (current != NO_SLOT);
  263. // if we reached the end of the chain, return false
  264. if (current == NO_SLOT)
  265. return false;
  266. count--;
  267. // remove slot from linked list
  268. // is slot at beginning of linked list?
  269. if (prev == NO_SLOT)
  270. table [index] = links [current].Next + 1;
  271. else
  272. links [prev].Next = links [current].Next;
  273. // mark slot as empty and prepend it to "empty slots chain"
  274. links [current].Next = empty_slot;
  275. empty_slot = current;
  276. // clear slot
  277. links [current].HashCode = 0;
  278. slots [current] = default (T);
  279. generation++;
  280. return true;
  281. }
  282. public int RemoveWhere (Predicate<T> match)
  283. {
  284. if (match == null)
  285. throw new ArgumentNullException ("match");
  286. var candidates = new List<T> ();
  287. foreach (var item in this)
  288. if (match (item))
  289. candidates.Add (item);
  290. foreach (var item in candidates)
  291. Remove (item);
  292. return candidates.Count;
  293. }
  294. public void TrimExcess ()
  295. {
  296. Resize (count);
  297. }
  298. // set operations
  299. public void IntersectWith (IEnumerable<T> other)
  300. {
  301. if (other == null)
  302. throw new ArgumentNullException ("other");
  303. var other_set = ToSet (other);
  304. RemoveWhere (item => !other_set.Contains (item));
  305. }
  306. public void ExceptWith (IEnumerable<T> other)
  307. {
  308. if (other == null)
  309. throw new ArgumentNullException ("other");
  310. foreach (var item in other)
  311. Remove (item);
  312. }
  313. public bool Overlaps (IEnumerable<T> other)
  314. {
  315. if (other == null)
  316. throw new ArgumentNullException ("other");
  317. foreach (var item in other)
  318. if (Contains (item))
  319. return true;
  320. return false;
  321. }
  322. public bool SetEquals (IEnumerable<T> other)
  323. {
  324. if (other == null)
  325. throw new ArgumentNullException ("other");
  326. var other_set = ToSet (other);
  327. if (count != other_set.Count)
  328. return false;
  329. foreach (var item in this)
  330. if (!other_set.Contains (item))
  331. return false;
  332. return true;
  333. }
  334. public void SymmetricExceptWith (IEnumerable<T> other)
  335. {
  336. if (other == null)
  337. throw new ArgumentNullException ("other");
  338. foreach (var item in ToSet (other))
  339. if (!Add (item))
  340. Remove (item);
  341. }
  342. HashSet<T> ToSet (IEnumerable<T> enumerable)
  343. {
  344. var set = enumerable as HashSet<T>;
  345. if (set == null || !Comparer.Equals (set.Comparer))
  346. set = new HashSet<T> (enumerable, Comparer);
  347. return set;
  348. }
  349. public void UnionWith (IEnumerable<T> other)
  350. {
  351. if (other == null)
  352. throw new ArgumentNullException ("other");
  353. foreach (var item in other)
  354. Add (item);
  355. }
  356. bool CheckIsSubsetOf (HashSet<T> other)
  357. {
  358. if (other == null)
  359. throw new ArgumentNullException ("other");
  360. foreach (var item in this)
  361. if (!other.Contains (item))
  362. return false;
  363. return true;
  364. }
  365. public bool IsSubsetOf (IEnumerable<T> other)
  366. {
  367. if (other == null)
  368. throw new ArgumentNullException ("other");
  369. if (count == 0)
  370. return true;
  371. var other_set = ToSet (other);
  372. if (count > other_set.Count)
  373. return false;
  374. return CheckIsSubsetOf (other_set);
  375. }
  376. public bool IsProperSubsetOf (IEnumerable<T> other)
  377. {
  378. if (other == null)
  379. throw new ArgumentNullException ("other");
  380. if (count == 0)
  381. return true;
  382. var other_set = ToSet (other);
  383. if (count >= other_set.Count)
  384. return false;
  385. return CheckIsSubsetOf (other_set);
  386. }
  387. bool CheckIsSupersetOf (HashSet<T> other)
  388. {
  389. if (other == null)
  390. throw new ArgumentNullException ("other");
  391. foreach (var item in other)
  392. if (!Contains (item))
  393. return false;
  394. return true;
  395. }
  396. public bool IsSupersetOf (IEnumerable<T> other)
  397. {
  398. if (other == null)
  399. throw new ArgumentNullException ("other");
  400. var other_set = ToSet (other);
  401. if (count < other_set.Count)
  402. return false;
  403. return CheckIsSupersetOf (other_set);
  404. }
  405. public bool IsProperSupersetOf (IEnumerable<T> other)
  406. {
  407. if (other == null)
  408. throw new ArgumentNullException ("other");
  409. var other_set = ToSet (other);
  410. if (count <= other_set.Count)
  411. return false;
  412. return CheckIsSupersetOf (other_set);
  413. }
  414. public static IEqualityComparer<HashSet<T>> CreateSetComparer ()
  415. {
  416. return HashSetEqualityComparer<T>.Instance;
  417. }
  418. [SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  419. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  420. {
  421. if (info == null) {
  422. throw new ArgumentNullException("info");
  423. }
  424. info.AddValue("Version", generation);
  425. info.AddValue("Comparer", comparer, typeof(IEqualityComparer<T>));
  426. info.AddValue("Capacity", (table == null) ? 0 : table.Length);
  427. if (table != null) {
  428. T[] tableArray = new T[count];
  429. CopyTo(tableArray);
  430. info.AddValue("Elements", tableArray, typeof(T[]));
  431. }
  432. }
  433. public virtual void OnDeserialization (object sender)
  434. {
  435. if (si != null)
  436. {
  437. generation = (int) si.GetValue("Version", typeof(int));
  438. comparer = (IEqualityComparer<T>) si.GetValue("Comparer",
  439. typeof(IEqualityComparer<T>));
  440. int capacity = (int) si.GetValue("Capacity", typeof(int));
  441. empty_slot = NO_SLOT;
  442. if (capacity > 0) {
  443. InitArrays(capacity);
  444. T[] tableArray = (T[]) si.GetValue("Elements", typeof(T[]));
  445. if (tableArray == null)
  446. throw new SerializationException("Missing Elements");
  447. for (int iElement = 0; iElement < tableArray.Length; iElement++) {
  448. Add(tableArray[iElement]);
  449. }
  450. } else
  451. table = null;
  452. si = null;
  453. }
  454. }
  455. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  456. {
  457. return new Enumerator (this);
  458. }
  459. bool ICollection<T>.IsReadOnly {
  460. get { return false; }
  461. }
  462. void ICollection<T>.Add (T item)
  463. {
  464. Add (item);
  465. }
  466. IEnumerator IEnumerable.GetEnumerator ()
  467. {
  468. return new Enumerator (this);
  469. }
  470. public Enumerator GetEnumerator ()
  471. {
  472. return new Enumerator (this);
  473. }
  474. [Serializable]
  475. public struct Enumerator : IEnumerator<T>, IDisposable {
  476. HashSet<T> hashset;
  477. int next;
  478. int stamp;
  479. T current;
  480. internal Enumerator (HashSet<T> hashset)
  481. : this ()
  482. {
  483. this.hashset = hashset;
  484. this.stamp = hashset.generation;
  485. }
  486. public bool MoveNext ()
  487. {
  488. CheckState ();
  489. if (next < 0)
  490. return false;
  491. while (next < hashset.touched) {
  492. int cur = next++;
  493. if (hashset.GetLinkHashCode (cur) != 0) {
  494. current = hashset.slots [cur];
  495. return true;
  496. }
  497. }
  498. next = NO_SLOT;
  499. return false;
  500. }
  501. public T Current {
  502. get { return current; }
  503. }
  504. object IEnumerator.Current {
  505. get {
  506. CheckState ();
  507. if (next <= 0)
  508. throw new InvalidOperationException ("Current is not valid");
  509. return current;
  510. }
  511. }
  512. void IEnumerator.Reset ()
  513. {
  514. CheckState ();
  515. next = 0;
  516. }
  517. public void Dispose ()
  518. {
  519. hashset = null;
  520. }
  521. void CheckState ()
  522. {
  523. if (hashset == null)
  524. throw new ObjectDisposedException (null);
  525. if (hashset.generation != stamp)
  526. throw new InvalidOperationException ("HashSet have been modified while it was iterated over");
  527. }
  528. }
  529. }
  530. sealed class HashSetEqualityComparer<T> : IEqualityComparer<HashSet<T>>
  531. {
  532. public static readonly HashSetEqualityComparer<T> Instance = new HashSetEqualityComparer<T> ();
  533. public bool Equals (HashSet<T> lhs, HashSet<T> rhs)
  534. {
  535. if (lhs == rhs)
  536. return true;
  537. if (lhs == null || rhs == null || lhs.Count != rhs.Count)
  538. return false;
  539. foreach (var item in lhs)
  540. if (!rhs.Contains (item))
  541. return false;
  542. return true;
  543. }
  544. public int GetHashCode (HashSet<T> hashset)
  545. {
  546. if (hashset == null)
  547. return 0;
  548. IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
  549. int hash = 0;
  550. foreach (var item in hashset)
  551. hash ^= comparer.GetHashCode (item);
  552. return hash;
  553. }
  554. }
  555. }