Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValueListBuilder.cs
Go to the documentation of this file.
3
5
6internal ref struct ValueListBuilder<T>
7{
8 private Span<T> _span;
9
10 private T[] _arrayFromPool;
11
12 private int _pos;
13
14 public int Length
15 {
16 get
17 {
18 return _pos;
19 }
20 set
21 {
22 _pos = value;
23 }
24 }
25
26 public ref T this[int index] => ref _span[index];
27
28 [MethodImpl(MethodImplOptions.AggressiveInlining)]
29 public T Pop()
30 {
31 _pos--;
32 return _span[_pos];
33 }
34
36 {
38 _arrayFromPool = null;
39 _pos = 0;
40 }
41
42 [MethodImpl(MethodImplOptions.AggressiveInlining)]
43 public void Append(T item)
44 {
45 int pos = _pos;
46 if (pos >= _span.Length)
47 {
48 Grow();
49 }
50 _span[pos] = item;
51 _pos = pos + 1;
52 }
53
55 {
56 return _span.Slice(0, _pos);
57 }
58
59 [MethodImpl(MethodImplOptions.AggressiveInlining)]
60 public void Dispose()
61 {
63 if (arrayFromPool != null)
64 {
65 _arrayFromPool = null;
66 ArrayPool<T>.Shared.Return(arrayFromPool);
67 }
68 }
69
70 private void Grow()
71 {
72 T[] array = ArrayPool<T>.Shared.Rent(_span.Length * 2);
73 bool flag = _span.TryCopyTo(array);
76 if (arrayFromPool != null)
77 {
78 ArrayPool<T>.Shared.Return(arrayFromPool);
79 }
80 }
81}