管理List<T>的優(yōu)化

C# List<T> 是個(gè)很好用的類型,但是作為數(shù)組他有自己的缺陷,插入和刪除特別耗,所以,如果這個(gè)List需要這樣的操作很多,不妨使用UGUI中的LIST,放入項(xiàng)目可以直接使用,如下

namespace UnityEngine.UI.Collections
{
  internal class IndexedSet<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
  {
    private readonly List<T> m_List = new List<T>();
    private Dictionary<T, int> m_Dictionary = new Dictionary<T, int>();

    public void Add(T item)
    {
      this.m_List.Add(item);
      this.m_Dictionary.Add(item, this.m_List.Count - 1);
    }

    public bool AddUnique(T item)
    {
      if (this.m_Dictionary.ContainsKey(item))
        return false;
      this.m_List.Add(item);
      this.m_Dictionary.Add(item, this.m_List.Count - 1);
      return true;
    }

    public bool Remove(T item)
    {
      int index = -1;
      if (!this.m_Dictionary.TryGetValue(item, out index))
        return false;
      this.RemoveAt(index);
      return true;
    }

    public IEnumerator<T> GetEnumerator()
    {
      throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return (IEnumerator) this.GetEnumerator();
    }

    public void Clear()
    {
      this.m_List.Clear();
      this.m_Dictionary.Clear();
    }

    public bool Contains(T item)
    {
      return this.m_Dictionary.ContainsKey(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
      this.m_List.CopyTo(array, arrayIndex);
    }

    public int Count
    {
      get
      {
        return this.m_List.Count;
      }
    }

    public bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public int IndexOf(T item)
    {
      int num = -1;
      this.m_Dictionary.TryGetValue(item, out num);
      return num;
    }

    public void Insert(int index, T item)
    {
      throw new NotSupportedException("Random Insertion is semantically invalid, since this structure does not guarantee ordering.");
    }

    public void RemoveAt(int index)
    {
      this.m_Dictionary.Remove(this.m_List[index]);
      if (index == this.m_List.Count - 1)
      {
        this.m_List.RemoveAt(index);
      }
      else
      {
        int index1 = this.m_List.Count - 1;
        T index2 = this.m_List[index1];
        this.m_List[index] = index2;
        this.m_Dictionary[index2] = index;
        this.m_List.RemoveAt(index1);
      }
    }

    public T this[int index]
    {
      get
      {
        return this.m_List[index];
      }
      set
      {
        T key = this.m_List[index];
        this.m_Dictionary.Remove(key);
        this.m_List[index] = value;
        this.m_Dictionary.Add(key, index);
      }
    }

    public void RemoveAll(Predicate<T> match)
    {
      int index = 0;
      while (index < this.m_List.Count)
      {
        T obj = this.m_List[index];
        if (match(obj))
          this.Remove(obj);
        else
          ++index;
      }
    }

    public void Sort(Comparison<T> sortLayoutFunction)
    {
      this.m_List.Sort(sortLayoutFunction);
      for (int index = 0; index < this.m_List.Count; ++index)
        this.m_Dictionary[this.m_List[index]] = index;
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天是中秋節(jié),現(xiàn)在外邊在放煙花 晚上微雨,天氣涼爽,月亮是看不到了,反倒欣喜于濕潤的空氣和獨(dú)自的空間和時(shí)間 月亮何...
    雀島札記閱讀 150評論 0 0
  • 根據(jù)“72法則”,我們?nèi)魏我粋€(gè)人,只要是比貧窮的過的好一點(diǎn)點(diǎn)的,都可以優(yōu)雅的成為一名百萬富翁,不需要你付出一點(diǎn)...
    Beyond_cd30閱讀 532評論 1 8
  • 早春的一天, 積雪還沒有融化, 原野上突然綻放了一朵彩虹色的花。 終于能見到太陽了, 她十分高興, 由衷的愿意跟每...
    童心媽咪閱讀 292評論 1 1
  • 吳組緗先生寫的這篇小說主要寫了“我”通過家人偶爾描述中了解的姑姑的生世,婚后和小圓去二姑姑家小住發(fā)生的情景 對于姑...
    小芳說閱讀 562評論 0 0
  • 聽說了這樣一個(gè)故事。 某個(gè)小城市的一個(gè)人整日介無所事事,拿著個(gè)手機(jī)到處亂逛,整天百無聊賴。 有一天,他無意間發(fā)現(xiàn)了...
    自在牛閱讀 419評論 0 1

友情鏈接更多精彩內(nèi)容