Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectCollection.cs
Go to the documentation of this file.
4
6
7[DebuggerDisplay("Count = {Count}")]
9internal sealed class ObjectCollection<T> : ICollection<T>, IEnumerable<T>, IEnumerable where T : class
10{
12 {
13 private readonly ObjectCollection<T> _list;
14
15 private int _index;
16
17 private T _current;
18
19 public T Current => _current;
20
21 object IEnumerator.Current => _current;
22
24 {
25 _list = list;
26 _index = 0;
27 _current = null;
28 }
29
30 public void Dispose()
31 {
32 }
33
34 public bool MoveNext()
35 {
37 if ((uint)_index < (uint)list._size)
38 {
39 _current = ((list._items is T[] array) ? array[_index] : ((T)list._items));
40 _index++;
41 return true;
42 }
43 _index = _list._size + 1;
44 _current = null;
45 return false;
46 }
47
49 {
50 _index = 0;
51 _current = null;
52 }
53 }
54
55 internal sealed class DebugView
56 {
58
60 public T[] Items
61 {
62 get
63 {
64 T[] array = new T[_collection.Count];
66 return array;
67 }
68 }
69
71 {
72 _collection = collection ?? throw new ArgumentNullException("collection");
73 }
74 }
75
76 private readonly Action<T> _validator;
77
78 internal object _items;
79
80 internal int _size;
81
82 public int Count => _size;
83
84 public bool IsReadOnly => false;
85
87 {
88 }
89
90 public ObjectCollection(Action<T> validator)
91 {
92 _validator = validator;
93 }
94
95 public void Add(T item)
96 {
97 if (_validator == null)
98 {
99 if (item == null)
100 {
101 throw new ArgumentNullException("item");
102 }
103 }
104 else
105 {
107 }
108 if (_items == null)
109 {
110 _items = item;
111 _size = 1;
112 return;
113 }
114 if (_items is T val)
115 {
116 _items = new T[4] { val, item, null, null };
117 _size = 2;
118 return;
119 }
120 T[] array = (T[])_items;
121 int size = _size;
122 if ((uint)size < (uint)array.Length)
123 {
124 array[size] = item;
125 }
126 else
127 {
128 T[] array2 = new T[array.Length * 2];
129 Array.Copy(array, array2, size);
130 _items = array2;
131 array2[size] = item;
132 }
133 _size = size + 1;
134 }
135
136 public void Clear()
137 {
138 _items = null;
139 _size = 0;
140 }
141
142 public bool Contains(T item)
143 {
144 if (_size > 0)
145 {
146 if (!(_items is T val))
147 {
148 if (_items is T[] array)
149 {
150 return Array.IndexOf(array, item, 0, _size) != -1;
151 }
152 return false;
153 }
154 return val.Equals(item);
155 }
156 return false;
157 }
158
159 public void CopyTo(T[] array, int arrayIndex)
160 {
161 if (_items is T[] sourceArray)
162 {
164 }
165 else if (array == null || _size > array.Length - arrayIndex)
166 {
167 new T[1] { (T)_items }.CopyTo(array, arrayIndex);
168 }
169 else if (_size == 1)
170 {
172 }
173 }
174
175 public bool Remove(T item)
176 {
177 if (_items is T val)
178 {
179 if (val.Equals(item))
180 {
181 _items = null;
182 _size = 0;
183 return true;
184 }
185 }
186 else if (_items is T[] array)
187 {
188 int num = Array.IndexOf(array, item, 0, _size);
189 if (num != -1)
190 {
191 _size--;
192 if (num < _size)
193 {
194 Array.Copy(array, num + 1, array, num, _size - num);
195 }
196 array[_size] = null;
197 return true;
198 }
199 }
200 return false;
201 }
202
204 {
205 return new Enumerator(this);
206 }
207
212
217}
int IList. IndexOf(object value)
Definition Array.cs:1228
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void CopyTo(T[] array, int arrayIndex)
new IEnumerator< T > GetEnumerator()