按C++说明下:

内部维护了一个vector和unordered_map,vector用来存储数据,unordered_map以数据为key,数据在vector的索引为值。

利用unordered_map存储的数据实现对元素的随机查找和删除

不允许Insert

删除需要特殊处理:

  1. 如果删除的是vector的最后一个元素,直接删除
  2. 否则和vector最后一个元素交换位置,修改unordered_map原本最后一个元素的数组索引,再删除最后一个元素

这样做的目的是为了保证hash表里存的数组索引是正确的

排序:

排序就是排序数组,然后重新赋值hash表

实现

继承c#的IList接口

//https://referencesource.microsoft.com/#mscorlib/system/collections/generic/ilist.cs,5fd4a456a7fab8b0,references
public interface IList<T> : ICollection<T>
    {
        // The Item property provides methods to read and edit entries in the List.
        T this[int index] {
            get;
            set;
        }
    
        // Returns the index of a particular item, if it is in the list.
        // Returns -1 if the item isn't in the list.
        int IndexOf(T item);
    
        // Inserts value into the list at position index.
        // index must be non-negative and less than or equal to the 
        // number of elements in the list.  If index equals the number
        // of items in the list, then value is appended to the end.
        void Insert(int index, T item);
        
        // Removes the item at position index.
        void RemoveAt(int index);
    }

特点:

  1. 元素不同,set
  2. 快速随机删除
  3. 不同的元素快速加到最后
  4. 顺序存取

缺点:

  1. 使用更多的内存