Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadOnlyCollectionBuilder.cs
Go to the documentation of this file.
5
7
9{
10 private sealed class Enumerator : IEnumerator<T>, IEnumerator, IDisposable
11 {
13
14 private readonly int _version;
15
16 private int _index;
17
18 private T _current;
19
20 public T Current => _current;
21
22 object IEnumerator.Current
23 {
24 get
25 {
26 if (_index == 0 || _index > _builder._size)
27 {
29 }
30 return _current;
31 }
32 }
33
35 {
38 _index = 0;
39 _current = default(T);
40 }
41
42 public void Dispose()
43 {
44 }
45
46 public bool MoveNext()
47 {
49 {
50 if (_index < _builder._size)
51 {
52 _current = _builder._items[_index++];
53 return true;
54 }
55 _index = _builder._size + 1;
56 _current = default(T);
57 return false;
58 }
60 }
61
63 {
65 {
67 }
68 _index = 0;
69 _current = default(T);
70 }
71 }
72
73 private T[] _items;
74
75 private int _size;
76
77 private int _version;
78
79 public int Capacity
80 {
81 get
82 {
83 return _items.Length;
84 }
85 set
86 {
87 if (value < _size)
88 {
89 throw new ArgumentOutOfRangeException("value");
90 }
91 if (value == _items.Length)
92 {
93 return;
94 }
95 if (value > 0)
96 {
97 T[] array = new T[value];
98 if (_size > 0)
99 {
101 }
102 _items = array;
103 }
104 else
105 {
106 _items = Array.Empty<T>();
107 }
108 }
109 }
110
111 public int Count => _size;
112
113 public T this[int index]
114 {
115 get
116 {
117 if (index >= _size)
118 {
119 throw new ArgumentOutOfRangeException("index");
120 }
121 return _items[index];
122 }
123 set
124 {
125 if (index >= _size)
126 {
127 throw new ArgumentOutOfRangeException("index");
128 }
129 _items[index] = value;
130 _version++;
131 }
132 }
133
135
136 bool IList.IsReadOnly => false;
137
138 bool IList.IsFixedSize => false;
139
140 object? IList.this[int index]
141 {
142 get
143 {
144 return this[index];
145 }
146 set
147 {
148 ValidateNullValue(value, "value");
149 try
150 {
151 this[index] = (T)value;
152 }
154 {
155 throw Error.InvalidTypeException(value, typeof(T), "value");
156 }
157 }
158 }
159
160 bool ICollection.IsSynchronized => false;
161
162 object ICollection.SyncRoot => this;
163
165 {
166 _items = Array.Empty<T>();
167 }
168
170 {
171 if (capacity < 0)
172 {
173 throw new ArgumentOutOfRangeException("capacity");
174 }
175 _items = new T[capacity];
176 }
177
179 {
180 if (collection == null)
181 {
182 throw new ArgumentNullException("collection");
183 }
185 {
186 _items = new T[count];
188 _size = count;
189 return;
190 }
191 _size = 0;
192 _items = new T[4];
193 foreach (T item in collection)
194 {
195 Add(item);
196 }
197 }
198
199 public int IndexOf(T item)
200 {
201 return Array.IndexOf(_items, item, 0, _size);
202 }
203
204 public void Insert(int index, T item)
205 {
206 if (index > _size)
207 {
208 throw new ArgumentOutOfRangeException("index");
209 }
210 if (_size == _items.Length)
211 {
213 }
214 if (index < _size)
215 {
217 }
218 _items[index] = item;
219 _size++;
220 _version++;
221 }
222
223 public void RemoveAt(int index)
224 {
226 {
227 throw new ArgumentOutOfRangeException("index");
228 }
229 _size--;
230 if (index < _size)
231 {
233 }
234 _items[_size] = default(T);
235 _version++;
236 }
237
238 public void Add(T item)
239 {
240 if (_size == _items.Length)
241 {
243 }
244 _items[_size++] = item;
245 _version++;
246 }
247
248 public void Clear()
249 {
250 if (_size > 0)
251 {
252 Array.Clear(_items, 0, _size);
253 _size = 0;
254 }
255 _version++;
256 }
257
258 public bool Contains(T item)
259 {
260 if (item == null)
261 {
262 for (int i = 0; i < _size; i++)
263 {
264 if (_items[i] == null)
265 {
266 return true;
267 }
268 }
269 return false;
270 }
271 EqualityComparer<T> @default = EqualityComparer<T>.Default;
272 for (int j = 0; j < _size; j++)
273 {
274 if (@default.Equals(_items[j], item))
275 {
276 return true;
277 }
278 }
279 return false;
280 }
281
282 public void CopyTo(T[] array, int arrayIndex)
283 {
285 }
286
287 public bool Remove(T item)
288 {
289 int num = IndexOf(item);
290 if (num >= 0)
291 {
292 RemoveAt(num);
293 return true;
294 }
295 return false;
296 }
297
299 {
300 return new Enumerator(this);
301 }
302
307
308 int IList.Add(object value)
309 {
310 ValidateNullValue(value, "value");
311 try
312 {
313 Add((T)value);
314 }
316 {
317 throw Error.InvalidTypeException(value, typeof(T), "value");
318 }
319 return Count - 1;
320 }
321
322 bool IList.Contains(object value)
323 {
325 {
326 return Contains((T)value);
327 }
328 return false;
329 }
330
331 int IList.IndexOf(object value)
332 {
334 {
335 return IndexOf((T)value);
336 }
337 return -1;
338 }
339
340 void IList.Insert(int index, object value)
341 {
342 ValidateNullValue(value, "value");
343 try
344 {
345 Insert(index, (T)value);
346 }
348 {
349 throw Error.InvalidTypeException(value, typeof(T), "value");
350 }
351 }
352
353 void IList.Remove(object value)
354 {
356 {
357 Remove((T)value);
358 }
359 }
360
362 {
363 if (array == null)
364 {
365 throw new ArgumentNullException("array");
366 }
367 if (array.Rank != 1)
368 {
369 throw new ArgumentException("array");
370 }
372 }
373
374 public void Reverse()
375 {
376 Reverse(0, Count);
377 }
378
379 public void Reverse(int index, int count)
380 {
381 if (index < 0)
382 {
383 throw new ArgumentOutOfRangeException("index");
384 }
385 if (count < 0)
386 {
387 throw new ArgumentOutOfRangeException("count");
388 }
390 _version++;
391 }
392
393 public T[] ToArray()
394 {
395 T[] array = new T[_size];
397 return array;
398 }
399
401 {
402 T[] list = ((_size != _items.Length) ? ToArray() : _items);
403 _items = Array.Empty<T>();
404 _size = 0;
405 _version++;
407 }
408
409 private void EnsureCapacity(int min)
410 {
411 if (_items.Length < min)
412 {
413 int num = 4;
414 if (_items.Length != 0)
415 {
416 num = _items.Length * 2;
417 }
418 if (num < min)
419 {
420 num = min;
421 }
422 Capacity = num;
423 }
424 }
425
426 private static bool IsCompatibleObject(object value)
427 {
428 if (!(value is T))
429 {
430 if (value == null)
431 {
432 return default(T) == null;
433 }
434 return false;
435 }
436 return true;
437 }
438
439 private static void ValidateNullValue(object value, string argument)
440 {
441 if (value == null && default(T) != null)
442 {
444 }
445 }
446}
static void Reverse(Array array)
Definition Array.cs:2207
int IList. IndexOf(object value)
Definition Array.cs:1228
static unsafe void Clear(Array array)
Definition Array.cs:755
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
static Exception CollectionModifiedWhileEnumerating()
Definition Error.cs:38
static Exception EnumerationIsDone()
Definition Error.cs:843
static Exception InvalidTypeException(object value, Type type, string paramName)
Definition Error.cs:938
static Exception InvalidNullValue(Type type, string paramName)
Definition Error.cs:933
void CopyTo(T[] array, int arrayIndex)
new IEnumerator< T > GetEnumerator()
void Insert(int index, T item)