HashSet.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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, HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  40. [DebuggerDisplay ("Count={Count}")]
  41. [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
  42. public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback
  43. #if NET_4_0 || MOONLIGHT
  44. , ISet<T>
  45. #endif
  46. {
  47. const int INITIAL_SIZE = 10;
  48. const float DEFAULT_LOAD_FACTOR = (90f / 100);
  49. const int NO_SLOT = -1;
  50. const int HASH_FLAG = -2147483648;
  51. struct Link {
  52. public int HashCode;
  53. public int Next;
  54. }
  55. // The hash table contains indices into the "links" array
  56. int [] table;
  57. Link [] links;
  58. T [] slots;
  59. // The number of slots in "links" and "slots" that
  60. // are in use (i.e. filled with data) or have been used and marked as
  61. // "empty" later on.
  62. int touched;
  63. // The index of the first slot in the "empty slots chain".
  64. // "Remove ()" prepends the cleared slots to the empty chain.
  65. // "Add ()" fills the first slot in the empty slots chain with the
  66. // added item (or increases "touched" if the chain itself is empty).
  67. int empty_slot;
  68. // The number of items in this set.
  69. int count;
  70. // The number of items the set can hold without
  71. // resizing the hash table and the slots arrays.
  72. int threshold;
  73. IEqualityComparer<T> comparer;
  74. SerializationInfo si;
  75. // The number of changes made to this set. Used by enumerators
  76. // to detect changes and invalidate themselves.
  77. int generation;
  78. public int Count {
  79. get { return count; }
  80. }
  81. public HashSet ()
  82. {
  83. Init (INITIAL_SIZE, null);
  84. }
  85. public HashSet (IEqualityComparer<T> comparer)
  86. {
  87. Init (INITIAL_SIZE, comparer);
  88. }
  89. public HashSet (IEnumerable<T> collection) : this (collection, null)
  90. {
  91. }
  92. public HashSet (IEnumerable<T> collection, IEqualityComparer<T> comparer)
  93. {
  94. if (collection == null)
  95. throw new ArgumentNullException ("collection");
  96. int capacity = 0;
  97. var col = collection as ICollection<T>;
  98. if (col != null)
  99. capacity = col.Count;
  100. Init (capacity, comparer);
  101. foreach (var item in collection)
  102. Add (item);
  103. }
  104. protected HashSet (SerializationInfo info, StreamingContext context)
  105. {
  106. si = info;
  107. }
  108. void Init (int capacity, IEqualityComparer<T> comparer)
  109. {
  110. if (capacity < 0)
  111. throw new ArgumentOutOfRangeException ("capacity");
  112. this.comparer = comparer ?? EqualityComparer<T>.Default;
  113. if (capacity == 0)
  114. capacity = INITIAL_SIZE;
  115. /* Modify capacity so 'capacity' elements can be added without resizing */
  116. capacity = (int) (capacity / DEFAULT_LOAD_FACTOR) + 1;
  117. InitArrays (capacity);
  118. generation = 0;
  119. }
  120. void InitArrays (int size)
  121. {
  122. table = new int [size];
  123. links = new Link [size];
  124. empty_slot = NO_SLOT;
  125. slots = new T [size];
  126. touched = 0;
  127. threshold = (int) (table.Length * DEFAULT_LOAD_FACTOR);
  128. if (threshold == 0 && table.Length > 0)
  129. threshold = 1;
  130. }
  131. bool SlotsContainsAt (int index, int hash, T item)
  132. {
  133. int current = table [index] - 1;
  134. while (current != NO_SLOT) {
  135. Link link = links [current];
  136. if (link.HashCode == hash && ((hash == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (item, slots [current])))
  137. return true;
  138. current = link.Next;
  139. }
  140. return false;
  141. }
  142. public void CopyTo (T [] array)
  143. {
  144. CopyTo (array, 0, count);
  145. }
  146. public void CopyTo (T [] array, int index)
  147. {
  148. CopyTo (array, index, count);
  149. }
  150. public void CopyTo (T [] array, int index, int count)
  151. {
  152. if (array == null)
  153. throw new ArgumentNullException ("array");
  154. if (index < 0)
  155. throw new ArgumentOutOfRangeException ("index");
  156. if (index > array.Length)
  157. throw new ArgumentException ("index larger than largest valid index of array");
  158. if (array.Length - index < count)
  159. throw new ArgumentException ("Destination array cannot hold the requested elements!");
  160. for (int i = 0, items = 0; i < touched && items < count; i++) {
  161. if (GetLinkHashCode (i) != 0)
  162. array [index++] = slots [i];
  163. }
  164. }
  165. void Resize ()
  166. {
  167. int newSize = PrimeHelper.ToPrime ((table.Length << 1) | 1);
  168. // allocate new hash table and link slots array
  169. var newTable = new int [newSize];
  170. var newLinks = new Link [newSize];
  171. for (int i = 0; i < table.Length; i++) {
  172. int current = table [i] - 1;
  173. while (current != NO_SLOT) {
  174. int hashCode = newLinks [current].HashCode = GetItemHashCode (slots [current]);
  175. int index = (hashCode & int.MaxValue) % newSize;
  176. newLinks [current].Next = newTable [index] - 1;
  177. newTable [index] = current + 1;
  178. current = links [current].Next;
  179. }
  180. }
  181. table = newTable;
  182. links = newLinks;
  183. // allocate new data slots, copy data
  184. var newSlots = new T [newSize];
  185. Array.Copy (slots, 0, newSlots, 0, touched);
  186. slots = newSlots;
  187. threshold = (int) (newSize * DEFAULT_LOAD_FACTOR);
  188. }
  189. int GetLinkHashCode (int index)
  190. {
  191. return links [index].HashCode & HASH_FLAG;
  192. }
  193. int GetItemHashCode (T item)
  194. {
  195. if (item == null)
  196. return HASH_FLAG;
  197. return comparer.GetHashCode (item) | HASH_FLAG;
  198. }
  199. public bool Add (T item)
  200. {
  201. int hashCode = GetItemHashCode (item);
  202. int index = (hashCode & int.MaxValue) % table.Length;
  203. if (SlotsContainsAt (index, hashCode, item))
  204. return false;
  205. if (++count > threshold) {
  206. Resize ();
  207. index = (hashCode & int.MaxValue) % table.Length;
  208. }
  209. // find an empty slot
  210. int current = empty_slot;
  211. if (current == NO_SLOT)
  212. current = touched++;
  213. else
  214. empty_slot = links [current].Next;
  215. // store the hash code of the added item,
  216. // prepend the added item to its linked list,
  217. // update the hash table
  218. links [current].HashCode = hashCode;
  219. links [current].Next = table [index] - 1;
  220. table [index] = current + 1;
  221. // store item
  222. slots [current] = item;
  223. generation++;
  224. return true;
  225. }
  226. public IEqualityComparer<T> Comparer {
  227. get { return comparer; }
  228. }
  229. public void Clear ()
  230. {
  231. count = 0;
  232. Array.Clear (table, 0, table.Length);
  233. Array.Clear (slots, 0, slots.Length);
  234. Array.Clear (links, 0, links.Length);
  235. // empty the "empty slots chain"
  236. empty_slot = NO_SLOT;
  237. touched = 0;
  238. generation++;
  239. }
  240. public bool Contains (T item)
  241. {
  242. int hashCode = GetItemHashCode (item);
  243. int index = (hashCode & int.MaxValue) % table.Length;
  244. return SlotsContainsAt (index, hashCode, item);
  245. }
  246. public bool Remove (T item)
  247. {
  248. // get first item of linked list corresponding to given key
  249. int hashCode = GetItemHashCode (item);
  250. int index = (hashCode & int.MaxValue) % table.Length;
  251. int current = table [index] - 1;
  252. // if there is no linked list, return false
  253. if (current == NO_SLOT)
  254. return false;
  255. // walk linked list until right slot (and its predecessor) is
  256. // found or end is reached
  257. int prev = NO_SLOT;
  258. do {
  259. Link link = links [current];
  260. if (link.HashCode == hashCode && ((hashCode == HASH_FLAG && (item == null || null == slots [current])) ? (item == null && null == slots [current]) : comparer.Equals (slots [current], item)))
  261. break;
  262. prev = current;
  263. current = link.Next;
  264. } while (current != NO_SLOT);
  265. // if we reached the end of the chain, return false
  266. if (current == NO_SLOT)
  267. return false;
  268. count--;
  269. // remove slot from linked list
  270. // is slot at beginning of linked list?
  271. if (prev == NO_SLOT)
  272. table [index] = links [current].Next + 1;
  273. else
  274. links [prev].Next = links [current].Next;
  275. // mark slot as empty and prepend it to "empty slots chain"
  276. links [current].Next = empty_slot;
  277. empty_slot = current;
  278. // clear slot
  279. links [current].HashCode = 0;
  280. slots [current] = default (T);
  281. generation++;
  282. return true;
  283. }
  284. public int RemoveWhere (Predicate<T> predicate)
  285. {
  286. if (predicate == null)
  287. throw new ArgumentNullException ("predicate");
  288. int counter = 0;
  289. var candidates = new List<T> ();
  290. foreach (var item in this)
  291. if (predicate (item))
  292. candidates.Add (item);
  293. foreach (var item in candidates)
  294. Remove (item);
  295. return candidates.Count;
  296. }
  297. public void TrimExcess ()
  298. {
  299. Resize ();
  300. }
  301. // set operations
  302. public void IntersectWith (IEnumerable<T> other)
  303. {
  304. if (other == null)
  305. throw new ArgumentNullException ("other");
  306. var other_set = ToSet (other);
  307. RemoveWhere (item => !other_set.Contains (item));
  308. }
  309. public void ExceptWith (IEnumerable<T> other)
  310. {
  311. if (other == null)
  312. throw new ArgumentNullException ("other");
  313. foreach (var item in other)
  314. Remove (item);
  315. }
  316. public bool Overlaps (IEnumerable<T> other)
  317. {
  318. if (other == null)
  319. throw new ArgumentNullException ("other");
  320. foreach (var item in other)
  321. if (Contains (item))
  322. return true;
  323. return false;
  324. }
  325. public bool SetEquals (IEnumerable<T> other)
  326. {
  327. if (other == null)
  328. throw new ArgumentNullException ("other");
  329. var other_set = ToSet (other);
  330. if (count != other_set.Count)
  331. return false;
  332. foreach (var item in this)
  333. if (!other_set.Contains (item))
  334. return false;
  335. return true;
  336. }
  337. public void SymmetricExceptWith (IEnumerable<T> other)
  338. {
  339. if (other == null)
  340. throw new ArgumentNullException ("other");
  341. foreach (var item in ToSet (other))
  342. if (!Add (item))
  343. Remove (item);
  344. }
  345. HashSet<T> ToSet (IEnumerable<T> enumerable)
  346. {
  347. var set = enumerable as HashSet<T>;
  348. if (set == null || !Comparer.Equals (set.Comparer))
  349. set = new HashSet<T> (enumerable);
  350. return set;
  351. }
  352. public void UnionWith (IEnumerable<T> other)
  353. {
  354. if (other == null)
  355. throw new ArgumentNullException ("other");
  356. foreach (var item in other)
  357. Add (item);
  358. }
  359. bool CheckIsSubsetOf (HashSet<T> other)
  360. {
  361. if (other == null)
  362. throw new ArgumentNullException ("other");
  363. foreach (var item in this)
  364. if (!other.Contains (item))
  365. return false;
  366. return true;
  367. }
  368. public bool IsSubsetOf (IEnumerable<T> other)
  369. {
  370. if (other == null)
  371. throw new ArgumentNullException ("other");
  372. if (count == 0)
  373. return true;
  374. var other_set = ToSet (other);
  375. if (count > other_set.Count)
  376. return false;
  377. return CheckIsSubsetOf (other_set);
  378. }
  379. public bool IsProperSubsetOf (IEnumerable<T> other)
  380. {
  381. if (other == null)
  382. throw new ArgumentNullException ("other");
  383. if (count == 0)
  384. return true;
  385. var other_set = ToSet (other);
  386. if (count >= other_set.Count)
  387. return false;
  388. return CheckIsSubsetOf (other_set);
  389. }
  390. bool CheckIsSupersetOf (HashSet<T> other)
  391. {
  392. if (other == null)
  393. throw new ArgumentNullException ("other");
  394. foreach (var item in other)
  395. if (!Contains (item))
  396. return false;
  397. return true;
  398. }
  399. public bool IsSupersetOf (IEnumerable<T> other)
  400. {
  401. if (other == null)
  402. throw new ArgumentNullException ("other");
  403. var other_set = ToSet (other);
  404. if (count < other_set.Count)
  405. return false;
  406. return CheckIsSupersetOf (other_set);
  407. }
  408. public bool IsProperSupersetOf (IEnumerable<T> other)
  409. {
  410. if (other == null)
  411. throw new ArgumentNullException ("other");
  412. var other_set = ToSet (other);
  413. if (count <= other_set.Count)
  414. return false;
  415. return CheckIsSupersetOf (other_set);
  416. }
  417. [MonoTODO]
  418. public static IEqualityComparer<HashSet<T>> CreateSetComparer ()
  419. {
  420. throw new NotImplementedException ();
  421. }
  422. [MonoTODO]
  423. [SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  424. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  425. {
  426. throw new NotImplementedException ();
  427. }
  428. [MonoTODO]
  429. public virtual void OnDeserialization (object sender)
  430. {
  431. if (si == null)
  432. return;
  433. throw new NotImplementedException ();
  434. }
  435. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  436. {
  437. return new Enumerator (this);
  438. }
  439. bool ICollection<T>.IsReadOnly {
  440. get { return false; }
  441. }
  442. void ICollection<T>.CopyTo (T [] array, int index)
  443. {
  444. CopyTo (array, index);
  445. }
  446. void ICollection<T>.Add (T item)
  447. {
  448. Add (item);
  449. }
  450. IEnumerator IEnumerable.GetEnumerator ()
  451. {
  452. return new Enumerator (this);
  453. }
  454. public Enumerator GetEnumerator ()
  455. {
  456. return new Enumerator (this);
  457. }
  458. [Serializable]
  459. public struct Enumerator : IEnumerator<T>, IDisposable {
  460. HashSet<T> hashset;
  461. int next;
  462. int stamp;
  463. T current;
  464. internal Enumerator (HashSet<T> hashset)
  465. : this ()
  466. {
  467. this.hashset = hashset;
  468. this.stamp = hashset.generation;
  469. }
  470. public bool MoveNext ()
  471. {
  472. CheckState ();
  473. if (next < 0)
  474. return false;
  475. while (next < hashset.touched) {
  476. int cur = next++;
  477. if (hashset.GetLinkHashCode (cur) != 0) {
  478. current = hashset.slots [cur];
  479. return true;
  480. }
  481. }
  482. next = NO_SLOT;
  483. return false;
  484. }
  485. public T Current {
  486. get { return current; }
  487. }
  488. object IEnumerator.Current {
  489. get {
  490. CheckState ();
  491. if (next <= 0)
  492. throw new InvalidOperationException ("Current is not valid");
  493. return current;
  494. }
  495. }
  496. void IEnumerator.Reset ()
  497. {
  498. CheckState ();
  499. next = 0;
  500. }
  501. public void Dispose ()
  502. {
  503. hashset = null;
  504. }
  505. void CheckState ()
  506. {
  507. if (hashset == null)
  508. throw new ObjectDisposedException (null);
  509. if (hashset.generation != stamp)
  510. throw new InvalidOperationException ("HashSet have been modified while it was iterated over");
  511. }
  512. }
  513. // borrowed from System.Collections.HashTable
  514. static class PrimeHelper {
  515. static readonly int [] primes_table = {
  516. 11,
  517. 19,
  518. 37,
  519. 73,
  520. 109,
  521. 163,
  522. 251,
  523. 367,
  524. 557,
  525. 823,
  526. 1237,
  527. 1861,
  528. 2777,
  529. 4177,
  530. 6247,
  531. 9371,
  532. 14057,
  533. 21089,
  534. 31627,
  535. 47431,
  536. 71143,
  537. 106721,
  538. 160073,
  539. 240101,
  540. 360163,
  541. 540217,
  542. 810343,
  543. 1215497,
  544. 1823231,
  545. 2734867,
  546. 4102283,
  547. 6153409,
  548. 9230113,
  549. 13845163
  550. };
  551. static bool TestPrime (int x)
  552. {
  553. if ((x & 1) != 0) {
  554. int top = (int) Math.Sqrt (x);
  555. for (int n = 3; n < top; n += 2) {
  556. if ((x % n) == 0)
  557. return false;
  558. }
  559. return true;
  560. }
  561. // There is only one even prime - 2.
  562. return x == 2;
  563. }
  564. static int CalcPrime (int x)
  565. {
  566. for (int i = (x & (~1)) - 1; i < Int32.MaxValue; i += 2)
  567. if (TestPrime (i))
  568. return i;
  569. return x;
  570. }
  571. public static int ToPrime (int x)
  572. {
  573. for (int i = 0; i < primes_table.Length; i++)
  574. if (x <= primes_table [i])
  575. return primes_table [i];
  576. return CalcPrime (x);
  577. }
  578. }
  579. }
  580. }