fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _05_MyHashSet
  7. {
  8. class CustomHashSet<T>
  9. {
  10. private CustomHashTable<T, bool> set;
  11. private int count;
  12.  
  13. public CustomHashSet()
  14. {
  15. this.count = 0;
  16. this.set = new CustomHashTable<T, bool>();
  17. }
  18.  
  19. public CustomHashTable<T, bool> Set
  20. {
  21. get { return this.set; }
  22. }
  23. public int Count
  24. {
  25. get { return this.count; }
  26. }
  27.  
  28. public void Add(T element)
  29. {
  30. this.set.Add(element, true);
  31. this.count++;
  32. }
  33. public void Remove(T element)
  34. {
  35. this.set.Remove(element);
  36. this.count--;
  37. }
  38. public T Find(T element)
  39. {
  40. bool result = this.set.Find(element);
  41. if (result)
  42. {
  43. return element;
  44. }
  45. else
  46. {
  47. throw new KeyNotFoundException(string.Format("There is NO element with value = {0}", element));
  48. }
  49. }
  50. public void Clear()
  51. {
  52. this.set = new CustomHashTable<T, bool>();
  53. this.count = 0;
  54. }
  55.  
  56. public void Union(CustomHashSet<T> otherSet)
  57. {
  58. foreach (KeyValuePair<T,bool> element in otherSet.Set)
  59. {
  60. this.set[element.Key] = true;
  61. }
  62. this.count = this.set.Count;
  63. }
  64. public void Intersect(CustomHashSet<T> otherSet)
  65. {
  66. List<T> toRemove = new List<T>(Math.Min(this.Count, otherSet.Count));
  67. foreach (KeyValuePair<T,bool> element in this.set)
  68. {
  69. if (!otherSet.Set.Contains(element.Key))
  70. {
  71. toRemove.Add(element.Key);
  72. }
  73. }
  74. foreach (T element in toRemove)
  75. {
  76. this.Remove(element);
  77. }
  78. this.count = this.set.Count;
  79. }
  80. }
  81. }
  82.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty