管理List<T>的优化

C# List<T> 是个很好用的类型,但是作为数组他有自己的缺陷,插入和删除特别耗,所以,如果这个List需要这样的操作很多,不妨使用UGUI中的LIST,放入项目可以直接使用,如下

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;
    }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天是中秋节,现在外边在放烟花 晚上微雨,天气凉爽,月亮是看不到了,反倒欣喜于湿润的空气和独自的空间和时间 月亮何...
    雀岛札记阅读 115评论 0 0
  • 今天中秋节,依旧早上六点半起床。 学习了两小时左右刑法知识——法律小白,根据APP推荐的学习顺序是三法三诉(民...
    熊澄澄阅读 224评论 1 1
  • 根据“72法则”,我们任何一个人,只要是比贫穷的过的好一点点的,都可以优雅的成为一名百万富翁,不需要你付出一点...
    Beyond_cd30阅读 482评论 1 8
  • 早春的一天, 积雪还没有融化, 原野上突然绽放了一朵彩虹色的花。 终于能见到太阳了, 她十分高兴, 由衷的愿意跟每...
    童心妈咪阅读 212评论 1 1
  • 吴组缃先生写的这篇小说主要写了“我”通过家人偶尔描述中了解的姑姑的生世,婚后和小圆去二姑姑家小住发生的情景 对于姑...
    小芳说阅读 469评论 0 0