Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SlotVector.cs
Go to the documentation of this file.
1using System;
5
6namespace ReLogic.Utilities;
7
8public sealed class SlotVector<T> : IEnumerable<SlotVector<T>.ItemPair>, IEnumerable
9{
10 public sealed class Enumerator : IEnumerator<ItemPair>, IEnumerator, IDisposable
11 {
13
14 private int _index = -1;
15
17
18 object IEnumerator.Current => _slotVector.GetPair(_index);
19
24
25 public void Reset()
26 {
27 _index = -1;
28 }
29
30 public bool MoveNext()
31 {
32 while (++_index < _slotVector._usedSpaceLength)
33 {
34 if (_slotVector.Has(_index))
35 {
36 return true;
37 }
38 }
39 return false;
40 }
41
42 public void Dispose()
43 {
44 _slotVector = null;
45 }
46 }
47
48 public struct ItemPair
49 {
50 public readonly T Value;
51
52 public readonly SlotId Id;
53
54 public ItemPair(T value, SlotId id)
55 {
56 Value = value;
57 Id = id;
58 }
59 }
60
61 private const uint MAX_INDEX = 65535u;
62
63 private readonly ItemPair[] _array;
64
65 private uint _freeHead;
66
67 private uint _usedSpaceLength;
68
69 public T this[int index]
70 {
71 get
72 {
73 if (index < 0 || index >= _array.Length)
74 {
76 }
77 if (!_array[index].Id.IsActive)
78 {
79 throw new KeyNotFoundException();
80 }
81 return _array[index].Value;
82 }
83 set
84 {
85 if (index < 0 || index >= _array.Length)
86 {
88 }
89 if (!_array[index].Id.IsActive)
90 {
91 throw new KeyNotFoundException();
92 }
94 }
95 }
96
97 public T this[SlotId id]
98 {
99 get
100 {
101 uint index = id.Index;
102 if (index >= _array.Length)
103 {
104 throw new ArgumentOutOfRangeException();
105 }
106 if (!_array[index].Id.IsActive || id != _array[index].Id)
107 {
108 throw new KeyNotFoundException();
109 }
110 return _array[index].Value;
111 }
112 set
113 {
114 uint index = id.Index;
115 if (index >= _array.Length)
116 {
117 throw new ArgumentOutOfRangeException();
118 }
119 if (!_array[index].Id.IsActive || id != _array[index].Id)
120 {
121 throw new KeyNotFoundException();
122 }
123 _array[index] = new ItemPair(value, id);
124 }
125 }
126
127 public int Count { get; private set; }
128
129 public int Capacity => _array.Length;
130
132 {
133 _array = new ItemPair[capacity];
134 Clear();
135 }
136
137 public SlotId Add(T value)
138 {
139 if (_freeHead == 65535)
140 {
141 return new SlotId(65535u);
142 }
143 uint freeHead = _freeHead;
146 {
148 }
149 _freeHead = itemPair.Id.Index;
150 _array[freeHead] = new ItemPair(value, itemPair.Id.ToActive(freeHead));
151 Count++;
152 return _array[freeHead].Id;
153 }
154
155 public void Clear()
156 {
158 Count = 0;
159 _freeHead = 0u;
160 for (uint num = 0u; num < _array.Length - 1; num++)
161 {
162 _array[num] = new ItemPair(default(T), new SlotId(num + 1));
163 }
164 _array[_array.Length - 1] = new ItemPair(default(T), new SlotId(65535u));
165 }
166
167 public bool Remove(SlotId id)
168 {
169 if (id.IsActive)
170 {
171 uint index = id.Index;
172 _array[index] = new ItemPair(default(T), id.ToInactive(_freeHead));
174 Count--;
175 return true;
176 }
177 return false;
178 }
179
180 public bool Has(SlotId id)
181 {
182 uint index = id.Index;
183 if (index >= _array.Length)
184 {
185 return false;
186 }
187 if (!_array[index].Id.IsActive || id != _array[index].Id)
188 {
189 return false;
190 }
191 return true;
192 }
193
194 public bool Has(int index)
195 {
196 if (index < 0 || index >= _array.Length)
197 {
198 return false;
199 }
200 if (!_array[index].Id.IsActive)
201 {
202 return false;
203 }
204 return true;
205 }
206
208 {
209 if (Has(index))
210 {
211 return _array[index];
212 }
213 return new ItemPair(default(T), SlotId.Invalid);
214 }
215
217 {
218 return new Enumerator(this);
219 }
220
225
226 public bool TryGetValue(SlotId id, [NotNullWhen(true)] out T? result)
227 {
228 uint index = id.Index;
229 if (index >= _array.Length)
230 {
231 result = default(T);
232 return false;
233 }
235 if (!arrayEntry.Id.IsActive || id != arrayEntry.Id)
236 {
237 result = default(T);
238 return false;
239 }
240 result = arrayEntry.Value;
241 return true;
242 }
243
244 public bool TryGetValue(int index, [NotNullWhen(true)] out T? result)
245 {
246 if (index < 0 || index >= _array.Length)
247 {
248 result = default(T);
249 return false;
250 }
252 if (!arrayEntry.Id.IsActive)
253 {
254 result = default(T);
255 return false;
256 }
257 result = arrayEntry.Value;
258 return true;
259 }
260}
Enumerator(SlotVector< T > slotVector)
Definition SlotVector.cs:20
ItemPair GetPair(int index)
bool TryGetValue(int index, [NotNullWhen(true)] out T? result)
bool TryGetValue(SlotId id, [NotNullWhen(true)] out T? result)
readonly ItemPair[] _array
Definition SlotVector.cs:63
new IEnumerator< T > GetEnumerator()
static readonly SlotId Invalid
Definition SlotId.cs:5