Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SortedMultiCollection.cs
浏览该文件的文档.
1namespace Game {
2 public class SortedMultiCollection<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> {
3 public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> {
5
6 public KeyValuePair<TKey, TValue> m_current;
7
8 public int m_index;
9
10 public int m_version;
11
12 public KeyValuePair<TKey, TValue> Current => m_current;
13
14 object IEnumerator.Current => m_current;
15
17 m_collection = collection;
18 m_current = default;
19 m_index = 0;
20 m_version = collection.m_version;
21 }
22
23 public void Dispose() { }
24
25 public bool MoveNext() {
26 if (m_collection.m_version != m_version) {
27 throw new InvalidOperationException("SortedMultiCollection was modified, enumeration cannot continue.");
28 }
29 if (m_index < m_collection.m_count) {
31 m_index++;
32 return true;
33 }
34 m_current = default;
35 return false;
36 }
37
38 public void Reset() {
39 if (m_collection.m_version != m_version) {
40 throw new InvalidOperationException("SortedMultiCollection was modified, enumeration cannot continue.");
41 }
42 m_index = 0;
43 m_current = default;
44 }
45 }
46
47 public const int MinCapacity = 4;
48
49 public KeyValuePair<TKey, TValue>[] m_array;
50
51 public int m_count;
52
53 public int m_version;
54
55 public IComparer<TKey> m_comparer;
56
57 public int Count => m_count;
58
59 public int Capacity {
60 get => m_array.Length;
61 set {
62 value = Math.Max(Math.Max(4, m_count), value);
63 if (value != m_array.Length) {
64 KeyValuePair<TKey, TValue>[] array = new KeyValuePair<TKey, TValue>[value];
65 Array.Copy(m_array, array, m_count);
66 m_array = array;
67 }
68 }
69 }
70
71 public KeyValuePair<TKey, TValue> this[int i] {
72 get {
73 if (i < m_count) {
74 return m_array[i];
75 }
76 throw new ArgumentOutOfRangeException();
77 }
78 }
79
81 m_array = new KeyValuePair<TKey, TValue>[4];
82 m_comparer = Comparer<TKey>.Default;
83 }
84
85 public SortedMultiCollection(IComparer<TKey> comparer) {
86 m_array = new KeyValuePair<TKey, TValue>[4];
87 m_comparer = comparer;
88 }
89
90 public SortedMultiCollection(int capacity) : this(capacity, null) {
91 capacity = Math.Max(capacity, 4);
92 m_array = new KeyValuePair<TKey, TValue>[capacity];
93 m_comparer = Comparer<TKey>.Default;
94 }
95
96 public SortedMultiCollection(int capacity, IComparer<TKey> comparer) {
97 capacity = Math.Max(capacity, 4);
98 m_array = new KeyValuePair<TKey, TValue>[capacity];
99 m_comparer = comparer;
100 }
101
102 public void Add(TKey key, TValue value) {
103 int num = Find(key);
104 if (num < 0) {
105 num = ~num;
106 }
108 Array.Copy(m_array, num, m_array, num + 1, m_count - num);
109 m_array[num] = new KeyValuePair<TKey, TValue>(key, value);
110 m_count++;
111 m_version++;
112 }
113
114 public void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> items) {
115 foreach (KeyValuePair<TKey, TValue> item in items) {
116 Add(item.Key, item.Value);
117 }
118 }
119
120 public bool Remove(TKey key) {
121 int num = Find(key);
122 if (num >= 0) {
123 Array.Copy(m_array, num + 1, m_array, num, m_count - num - 1);
124 m_array[m_count - 1] = default;
125 m_count--;
126 m_version++;
127 return true;
128 }
129 return false;
130 }
131
132 public void Clear() {
133 for (int i = 0; i < m_count; i++) {
134 m_array[i] = default;
135 }
136 m_count = 0;
137 m_version++;
138 }
139
140 public bool TryGetValue(TKey key, out TValue value) {
141 int num = Find(key);
142 if (num >= 0) {
143 value = m_array[num].Value;
144 return true;
145 }
146 value = default;
147 return false;
148 }
149
150 public bool ContainsKey(TKey key) => Find(key) >= 0;
151
152 public Enumerator GetEnumerator() => new(this);
153
154 IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => new Enumerator(this);
155
156 IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
157
158 public void EnsureCapacity(int capacity) {
159 if (capacity > Capacity) {
160 Capacity = Math.Max(capacity, 2 * Capacity);
161 }
162 }
163
164 // Find the last one. Edited by Deepseek.
165 public int Find(TKey key) {
166 if (m_count > 0) {
167 int num = 0;
168 int num2 = m_count - 1;
169 int lastFound = -1;
170 while (num <= num2) {
171 int num3 = (num + num2) >> 1;
172 int num4 = m_comparer.Compare(m_array[num3].Key, key);
173 if (num4 == 0) {
174 lastFound = num3;
175 num = num3 + 1;
176 }
177 else if (num4 < 0) {
178 num = num3 + 1;
179 }
180 else {
181 num2 = num3 - 1;
182 }
183 }
184 if (lastFound != -1) {
185 return lastFound;
186 }
187 return ~num;
188 }
189 return -1;
190 }
191 }
192}
KeyValuePair< TKey, TValue >[] m_array
SortedMultiCollection(IComparer< TKey > comparer)
SortedMultiCollection(int capacity, IComparer< TKey > comparer)
SortedMultiCollection< TKey, TValue > m_collection
void AddRange(IEnumerable< KeyValuePair< TKey, TValue > > items)
Enumerator(SortedMultiCollection< TKey, TValue > collection)
KeyValuePair< TKey, TValue > m_current
bool TryGetValue(TKey key, out TValue value)