zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace QiHe.CodeLib
{
    public class FastSearchList<T> : IList<T>
    {
        private IList<T> internalList;
        private IDictionary<T, int> internalLookup;
 
        public FastSearchList()
        {
            this.internalList = new List<T>();
            this.internalLookup = new Dictionary<T, int>();
        }
 
        public FastSearchList(int capacity)
        {
            this.internalList = new List<T>(capacity);
            this.internalLookup = new Dictionary<T, int>(capacity);
        }
 
        public int IndexOf(T item)
        {
            if (this.internalLookup.ContainsKey(item))
                return this.internalLookup[item];
            else
                return -1;
        }
 
        public void Insert(int index, T item)
        {
            if (internalLookup.ContainsKey(item))
                throw new ArgumentException("Duplicate item already exist in the list");
 
            this.internalList.Insert(index, item);
            this.internalLookup.Add(item, index);
 
            // require re-indexing
            for (int i = index; i < this.internalList.Count; i++)
            {
                T itemKey = this.internalList[i];
                this.internalLookup[itemKey] = i;
            }
        }
 
        public void RemoveAt(int index)
        {
            T item = this.internalList[index];
            this.internalList.RemoveAt(index);
            this.internalLookup.Remove(item);
 
            // require re-indexing
            for (int i = index; i < this.internalList.Count; i++)
            {
                T itemKey = this.internalList[i];
                this.internalLookup[itemKey] = i;
            }
        }
 
        public T this[int index]
        {
            get { return this.internalList[index]; }
            set { this.internalList[index] = value; }
        }
 
        public void Add(T item)
        {
            this.internalList.Add(item);
            if (!internalLookup.ContainsKey(item))
            {
                this.internalLookup.Add(item, internalList.Count - 1);
            }
        }
 
        public void Clear()
        {
            this.internalList.Clear();
            this.internalLookup.Clear();
        }
 
        public bool Contains(T item)
        {
            return this.internalLookup.ContainsKey(item);
        }
 
        public void CopyTo(T[] array, int arrayIndex)
        {
            this.internalList.CopyTo(array, arrayIndex);
        }
 
        public bool Remove(T item)
        {
            if (this.internalLookup.ContainsKey(item))
            {
                int index = this.internalLookup[item];
                this.RemoveAt(index); // re-indexing implictly
                return true;
            }
            else
            {
                return false;
            }
        }
 
        public int Count
        {
            get { return this.internalList.Count; }
        }
 
        public bool IsReadOnly
        {
            get { return this.internalList.IsReadOnly; }
        }
 
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return this.internalList.GetEnumerator();
        }
 
        public IEnumerator GetEnumerator()
        {
            return this.internalList.GetEnumerator();
        }
    }
}