Advertisement
Unchpokable

Simple C# Set with Add and Find methods

Nov 19th, 2020
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. public class Set<T> : IEnumerable<T>, IEnumerable
  2.                                  where T: IComparable
  3.     {
  4.         public int Length => Buffer.Count();
  5.         private List<T> Buffer { get; set; }
  6.         private int Pointer { get; set; }
  7.  
  8.         public Set()
  9.         {
  10.             Buffer = new List<T>();
  11.             Pointer = 0;
  12.         }
  13.  
  14.         public Set(int size)
  15.         {
  16.             Buffer = new List<T>(size);
  17.             Pointer = 0;
  18.         }
  19.  
  20.         public Set(IEnumerable<T> initializer)
  21.         {
  22.             Buffer = initializer.OrderBy(x => x).Distinct().ToList();
  23.             Pointer = 0;
  24.         }
  25.  
  26.         public Set(params T[] initializer)
  27.         {
  28.             Buffer = initializer.OrderBy(x => x).Distinct().ToList();
  29.             Pointer = 0;
  30.         }
  31.  
  32.         public bool Add(T item)
  33.         {
  34.             if (!Buffer.Contains(item))
  35.             {
  36.                 Pointer = GetLeftBorder(item);
  37.                 Buffer.Insert(Pointer + 1, item);
  38.                 return true;
  39.             }
  40.             return false;
  41.         }
  42.  
  43.         public T FindNext(T item)
  44.         {
  45.             var itemIndex = Search(item);
  46.             if (itemIndex != -1)
  47.                 return Buffer[itemIndex + 1];
  48.             return default(T);
  49.         }
  50.  
  51.         private int GetLeftBorder(T item)
  52.         {
  53.             var (left, right) = (-1, Buffer.Count());
  54.             return GetLeftBorder(item, left, right);
  55.         }
  56.  
  57.         private int GetLeftBorder(T item, int left, int right)
  58.         {
  59.             while (!(right <= left + 1))
  60.             {
  61.                 var mid = (right - left) / 2 + left;
  62.                 if (item.CompareTo(Buffer[mid]) < 0)
  63.                 {
  64.                     right = mid;
  65.                     continue;
  66.                 }
  67.                 left = mid;
  68.             }
  69.             return left;
  70.         }
  71.  
  72.         private int Search(T item)
  73.         {
  74.             var (left, right) = (0, Buffer.Count()-1);
  75.             while (left < right)
  76.             {
  77.                 var mid = (right - left) / 2 + left;
  78.                 if (item.CompareTo(Buffer[mid]) <= 0)
  79.                 {
  80.                     right = mid;
  81.                     continue;
  82.                 }
  83.                 left = mid + 1;
  84.             }
  85.             if (Buffer[right].Equals(item))
  86.                 return right;
  87.             return -1;
  88.         }
  89.  
  90.         IEnumerator IEnumerable.GetEnumerator()
  91.         {
  92.             return this.GetEnumerator();
  93.         }
  94.  
  95.         public IEnumerator<T> GetEnumerator()
  96.         {
  97.             foreach (var item in Buffer)
  98.             {
  99.                 yield return (T)item;
  100.             }
  101.         }
  102.  
  103.         public T this[int index]
  104.         {
  105.             get
  106.             {
  107.                 if (index >= 0 && index < Buffer.Count)
  108.                     return Buffer[index];
  109.                 throw new IndexOutOfRangeException($"Недопустимое значение индекса {index}: Вне диапазона");
  110.             }
  111.         }
  112.  
  113.         public bool EnumerableCompareTo(Set<T> other)
  114.         {
  115.             if (Buffer.Count != other.Length)
  116.                 return false;
  117.  
  118.             var length = other.Length;
  119.             for (int i = 0; i < length; i++)
  120.             {
  121.                 if (!this[i].Equals(other[i]))
  122.                     return false;
  123.             }
  124.             return true;
  125.         }
  126.  
  127.         public bool EqualsTo(Set<T> other)
  128.         {
  129.             return EnumerableCompareTo(other);
  130.         }
  131.     }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement