Advertisement
Guest User

Collection improved performance

a guest
Mar 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. class Collection<T> : IDisposable
  2.     {
  3.         internal T[] Item = { };
  4.  
  5.         /// <summary>
  6.         /// Quantidade de Items na Coleção
  7.         /// </summary>
  8.         internal int Length { get; private set; }
  9.  
  10.         [System.Runtime.CompilerServices.IndexerName("HelperItem")]
  11.         internal T this[int Index]
  12.         {
  13.             get => Item[Index];
  14.             set => Item[Index] = value;
  15.         }
  16.  
  17.         /// <summary>
  18.         /// Copia outra coleção
  19.         /// </summary>
  20.         /// <param name="value"></param>
  21.         public void Copy(Collection<T> value)
  22.         {
  23.             Clear();
  24.            
  25.             if (value.Length > 0)
  26.             {
  27.                 Item = new T[value.Length];
  28.                 for (int i = 0; i < Item.Length; i++)
  29.                     Item[i] = value[i];
  30.  
  31.                 Length = value.Length;
  32.             }
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Limpa a coleção
  37.         /// </summary>
  38.         internal void Clear()
  39.         {
  40.             Item = new T[]{ };
  41.             Length = 0;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Adiciona um item na coleção
  46.         /// </summary>
  47.         /// <param name="value"></param>
  48.         internal void Add(T value)
  49.         {
  50.             Array.Resize(ref Item, Length + 1);
  51.             Item[Length] = value;
  52.             Length++;
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Remove um item da coleção pelo seu ID
  57.         /// </summary>
  58.         /// <param name="at"></param>
  59.         internal void RemoveAt(int at)
  60.         {
  61.             if (Length == 0)
  62.                 return;
  63.  
  64.             if (at < 0 || at >= Length)
  65.                 return;
  66.  
  67.             // Re-Organiza a coleção
  68.             if (at < Length - 1)
  69.             {
  70.                 for (int i = 0; i < Length - 2; i++)
  71.                 {
  72.                     Item[i] = Item[i + 1];
  73.                 }
  74.             }
  75.  
  76.             Array.Resize(ref Item, Length - 1);
  77.             Length--;
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Remove um item da coleção
  82.         /// </summary>
  83.         /// <param name="value"></param>
  84.         internal void Remove(T value)
  85.         {
  86.             if (Length == 0)
  87.                 return;
  88.  
  89.             int ID = IndexOf(value);
  90.             RemoveAt(ID);
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Identifica a ID na coleção de um determinado Item
  95.         /// </summary>
  96.         /// <param name="value"></param>
  97.         /// <returns></returns>
  98.         public int IndexOf(T value) => Array.IndexOf(Item, value);
  99.  
  100.         ~Collection() => Dispose();
  101.         public void Dispose() => GC.SuppressFinalize(this);
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement