Advertisement
vencinachev

HashSetMy

Sep 12th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. class HashSetMy<T> : IEnumerable<T>
  2.     {
  3.         private const int MIN_BUFFER_SIZE = 16;
  4.         private const int RESIZE_FACTOR = 2;
  5.         private const double FULL_FACTOR = 0.7;
  6.  
  7.         private List<T>[] buffer;
  8.  
  9.  
  10.         public int Count { get; private set; }
  11.         public HashSetMy(int size = MIN_BUFFER_SIZE)
  12.         {
  13.             buffer = new List<T>[size];
  14.             this.Count = 0;
  15.         }
  16.  
  17.         public bool AddItem(T value)
  18.         {
  19.             var hash = (uint)value.GetHashCode();
  20.             var index = hash % buffer.Length;
  21.  
  22.             var existed = buffer[index] != null && buffer[index].Contains(value);
  23.  
  24.             if (existed)
  25.             {
  26.                 return false;
  27.             }
  28.             if (buffer[index] == null)
  29.             {
  30.                 buffer[index] = new List<T>();
  31.             }
  32.             buffer[index].Add(value);
  33.             this.Count++;
  34.             if ((double)this.Count / buffer.Length > FULL_FACTOR)
  35.             {
  36.                 this.Resize(buffer.Length * RESIZE_FACTOR);
  37.             }
  38.             return true;
  39.         }
  40.  
  41.         public bool Contains(T value)
  42.         {
  43.             var hash = (uint)value.GetHashCode();
  44.             var index = hash % buffer.Length;
  45.            
  46.             return buffer[index] != null && buffer[index].Contains(value);
  47.         }
  48.  
  49.         public bool Remove(T value)
  50.         {
  51.             var hash = (uint)value.GetHashCode();
  52.             var index = hash % buffer.Length;
  53.             if (buffer[index] == null)
  54.             {
  55.                 return false;
  56.             }
  57.  
  58.             return this.buffer[index].Remove(value);
  59.         }
  60.  
  61.         private void Resize(int newSize)
  62.         {
  63.             var newBuffer = new List<T>[newSize];
  64.            
  65.             foreach (var x in this)
  66.             {
  67.                 var hash = (uint)x.GetHashCode();
  68.                 var index = hash % newSize;
  69.                 if (newBuffer[index] == null)
  70.                 {
  71.                     newBuffer[index] = new List<T>();
  72.                 }
  73.                 newBuffer[index].Add(x);
  74.             }
  75.             this.buffer = newBuffer;
  76.         }
  77.  
  78.         public IEnumerator<T> GetEnumerator()
  79.         {
  80.             foreach (var list in buffer)
  81.             {
  82.                 if (list == null)
  83.                 {
  84.                     continue;
  85.                 }
  86.                 foreach (var x in list)
  87.                 {
  88.                     yield return x;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         IEnumerator IEnumerable.GetEnumerator()
  94.         {
  95.             return this.GetEnumerator();
  96.         }
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement