PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Core/System.Collections.Generic/HashSet.cs

https://bitbucket.org/foobar22/mono
C# | 788 lines | 562 code | 164 blank | 62 comment | 149 complexity | 8b895578dab3090ffde2f27ab723358b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
  1. //
  2. // HashSet.cs
  3. //
  4. // Authors:
  5. // Jb Evain <jbevain@novell.com>
  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. #if NET_4_0 || MOONLIGHT || MOBILE
  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 arrayIndex)
  147. {
  148. CopyTo (array, arrayIndex, count);
  149. }
  150. public void CopyTo (T [] array, int arrayIndex, int count)
  151. {
  152. if (array == null)
  153. throw new ArgumentNullException ("array");
  154. if (arrayIndex < 0)
  155. throw new ArgumentOutOfRangeException ("arrayIndex");
  156. if (arrayIndex > array.Length)
  157. throw new ArgumentException ("index larger than largest valid index of array");
  158. if (array.Length - arrayIndex < 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 [arrayIndex++] = 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> match)
  285. {
  286. if (match == null)
  287. throw new ArgumentNullException ("match");
  288. var candidates = new List<T> ();
  289. foreach (var item in this)
  290. if (match (item))
  291. candidates.Add (item);
  292. foreach (var item in candidates)
  293. Remove (item);
  294. return candidates.Count;
  295. }
  296. public void TrimExcess ()
  297. {
  298. Resize ();
  299. }
  300. // set operations
  301. public void IntersectWith (IEnumerable<T> other)
  302. {
  303. if (other == null)
  304. throw new ArgumentNullException ("other");
  305. var other_set = ToSet (other);
  306. RemoveWhere (item => !other_set.Contains (item));
  307. }
  308. public void ExceptWith (IEnumerable<T> other)
  309. {
  310. if (other == null)
  311. throw new ArgumentNullException ("other");
  312. foreach (var item in other)
  313. Remove (item);
  314. }
  315. public bool Overlaps (IEnumerable<T> other)
  316. {
  317. if (other == null)
  318. throw new ArgumentNullException ("other");
  319. foreach (var item in other)
  320. if (Contains (item))
  321. return true;
  322. return false;
  323. }
  324. public bool SetEquals (IEnumerable<T> other)
  325. {
  326. if (other == null)
  327. throw new ArgumentNullException ("other");
  328. var other_set = ToSet (other);
  329. if (count != other_set.Count)
  330. return false;
  331. foreach (var item in this)
  332. if (!other_set.Contains (item))
  333. return false;
  334. return true;
  335. }
  336. public void SymmetricExceptWith (IEnumerable<T> other)
  337. {
  338. if (other == null)
  339. throw new ArgumentNullException ("other");
  340. foreach (var item in ToSet (other))
  341. if (!Add (item))
  342. Remove (item);
  343. }
  344. HashSet<T> ToSet (IEnumerable<T> enumerable)
  345. {
  346. var set = enumerable as HashSet<T>;
  347. if (set == null || !Comparer.Equals (set.Comparer))
  348. set = new HashSet<T> (enumerable, Comparer);
  349. return set;
  350. }
  351. public void UnionWith (IEnumerable<T> other)
  352. {
  353. if (other == null)
  354. throw new ArgumentNullException ("other");
  355. foreach (var item in other)
  356. Add (item);
  357. }
  358. bool CheckIsSubsetOf (HashSet<T> other)
  359. {
  360. if (other == null)
  361. throw new ArgumentNullException ("other");
  362. foreach (var item in this)
  363. if (!other.Contains (item))
  364. return false;
  365. return true;
  366. }
  367. public bool IsSubsetOf (IEnumerable<T> other)
  368. {
  369. if (other == null)
  370. throw new ArgumentNullException ("other");
  371. if (count == 0)
  372. return true;
  373. var other_set = ToSet (other);
  374. if (count > other_set.Count)
  375. return false;
  376. return CheckIsSubsetOf (other_set);
  377. }
  378. public bool IsProperSubsetOf (IEnumerable<T> other)
  379. {
  380. if (other == null)
  381. throw new ArgumentNullException ("other");
  382. if (count == 0)
  383. return true;
  384. var other_set = ToSet (other);
  385. if (count >= other_set.Count)
  386. return false;
  387. return CheckIsSubsetOf (other_set);
  388. }
  389. bool CheckIsSupersetOf (HashSet<T> other)
  390. {
  391. if (other == null)
  392. throw new ArgumentNullException ("other");
  393. foreach (var item in other)
  394. if (!Contains (item))
  395. return false;
  396. return true;
  397. }
  398. public bool IsSupersetOf (IEnumerable<T> other)
  399. {
  400. if (other == null)
  401. throw new ArgumentNullException ("other");
  402. var other_set = ToSet (other);
  403. if (count < other_set.Count)
  404. return false;
  405. return CheckIsSupersetOf (other_set);
  406. }
  407. public bool IsProperSupersetOf (IEnumerable<T> other)
  408. {
  409. if (other == null)
  410. throw new ArgumentNullException ("other");
  411. var other_set = ToSet (other);
  412. if (count <= other_set.Count)
  413. return false;
  414. return CheckIsSupersetOf (other_set);
  415. }
  416. class HashSetEqualityComparer : IEqualityComparer<HashSet<T>>
  417. {
  418. public bool Equals (HashSet<T> lhs, HashSet<T> rhs)
  419. {
  420. if (lhs == rhs)
  421. return true;
  422. if (lhs == null || rhs == null || lhs.Count != rhs.Count)
  423. return false;
  424. foreach (var item in lhs)
  425. if (!rhs.Contains (item))
  426. return false;
  427. return true;
  428. }
  429. public int GetHashCode (HashSet<T> hashset)
  430. {
  431. if (hashset == null)
  432. return 0;
  433. IEqualityComparer<T> comparer = EqualityComparer<T>.Default;
  434. int hash = 0;
  435. foreach (var item in hashset)
  436. hash ^= comparer.GetHashCode (item);
  437. return hash;
  438. }
  439. }
  440. static readonly HashSetEqualityComparer setComparer = new HashSetEqualityComparer ();
  441. public static IEqualityComparer<HashSet<T>> CreateSetComparer ()
  442. {
  443. return setComparer;
  444. }
  445. [SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  446. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  447. {
  448. if (info == null) {
  449. throw new ArgumentNullException("info");
  450. }
  451. info.AddValue("Version", generation);
  452. info.AddValue("Comparer", comparer, typeof(IEqualityComparer<T>));
  453. info.AddValue("Capacity", (table == null) ? 0 : table.Length);
  454. if (table != null) {
  455. T[] tableArray = new T[count];
  456. CopyTo(tableArray);
  457. info.AddValue("Elements", tableArray, typeof(T[]));
  458. }
  459. }
  460. public virtual void OnDeserialization (object sender)
  461. {
  462. if (si != null)
  463. {
  464. generation = (int) si.GetValue("Version", typeof(int));
  465. comparer = (IEqualityComparer<T>) si.GetValue("Comparer",
  466. typeof(IEqualityComparer<T>));
  467. int capacity = (int) si.GetValue("Capacity", typeof(int));
  468. empty_slot = NO_SLOT;
  469. if (capacity > 0) {
  470. table = new int[capacity];
  471. slots = new T[capacity];
  472. T[] tableArray = (T[]) si.GetValue("Elements", typeof(T[]));
  473. if (tableArray == null)
  474. throw new SerializationException("Missing Elements");
  475. for (int iElement = 0; iElement < tableArray.Length; iElement++) {
  476. Add(tableArray[iElement]);
  477. }
  478. } else
  479. table = null;
  480. si = null;
  481. }
  482. }
  483. IEnumerator<T> IEnumerable<T>.GetEnumerator ()
  484. {
  485. return new Enumerator (this);
  486. }
  487. bool ICollection<T>.IsReadOnly {
  488. get { return false; }
  489. }
  490. void ICollection<T>.Add (T item)
  491. {
  492. Add (item);
  493. }
  494. IEnumerator IEnumerable.GetEnumerator ()
  495. {
  496. return new Enumerator (this);
  497. }
  498. public Enumerator GetEnumerator ()
  499. {
  500. return new Enumerator (this);
  501. }
  502. [Serializable]
  503. public struct Enumerator : IEnumerator<T>, IDisposable {
  504. HashSet<T> hashset;
  505. int next;
  506. int stamp;
  507. T current;
  508. internal Enumerator (HashSet<T> hashset)
  509. : this ()
  510. {
  511. this.hashset = hashset;
  512. this.stamp = hashset.generation;
  513. }
  514. public bool MoveNext ()
  515. {
  516. CheckState ();
  517. if (next < 0)
  518. return false;
  519. while (next < hashset.touched) {
  520. int cur = next++;
  521. if (hashset.GetLinkHashCode (cur) != 0) {
  522. current = hashset.slots [cur];
  523. return true;
  524. }
  525. }
  526. next = NO_SLOT;
  527. return false;
  528. }
  529. public T Current {
  530. get { return current; }
  531. }
  532. object IEnumerator.Current {
  533. get {
  534. CheckState ();
  535. if (next <= 0)
  536. throw new InvalidOperationException ("Current is not valid");
  537. return current;
  538. }
  539. }
  540. void IEnumerator.Reset ()
  541. {
  542. CheckState ();
  543. next = 0;
  544. }
  545. public void Dispose ()
  546. {
  547. hashset = null;
  548. }
  549. void CheckState ()
  550. {
  551. if (hashset == null)
  552. throw new ObjectDisposedException (null);
  553. if (hashset.generation != stamp)
  554. throw new InvalidOperationException ("HashSet have been modified while it was iterated over");
  555. }
  556. }
  557. // borrowed from System.Collections.HashTable
  558. static class PrimeHelper {
  559. static readonly int [] primes_table = {
  560. 11,
  561. 19,
  562. 37,
  563. 73,
  564. 109,
  565. 163,
  566. 251,
  567. 367,
  568. 557,
  569. 823,
  570. 1237,
  571. 1861,
  572. 2777,
  573. 4177,
  574. 6247,
  575. 9371,
  576. 14057,
  577. 21089,
  578. 31627,
  579. 47431,
  580. 71143,
  581. 106721,
  582. 160073,
  583. 240101,
  584. 360163,
  585. 540217,
  586. 810343,
  587. 1215497,
  588. 1823231,
  589. 2734867,
  590. 4102283,
  591. 6153409,
  592. 9230113,
  593. 13845163
  594. };
  595. static bool TestPrime (int x)
  596. {
  597. if ((x & 1) != 0) {
  598. int top = (int) Math.Sqrt (x);
  599. for (int n = 3; n < top; n += 2) {
  600. if ((x % n) == 0)
  601. return false;
  602. }
  603. return true;
  604. }
  605. // There is only one even prime - 2.
  606. return x == 2;
  607. }
  608. static int CalcPrime (int x)
  609. {
  610. for (int i = (x & (~1)) - 1; i < Int32.MaxValue; i += 2)
  611. if (TestPrime (i))
  612. return i;
  613. return x;
  614. }
  615. public static int ToPrime (int x)
  616. {
  617. for (int i = 0; i < primes_table.Length; i++)
  618. if (x <= primes_table [i])
  619. return primes_table [i];
  620. return CalcPrime (x);
  621. }
  622. }
  623. }
  624. }