Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SetFactory.cs
Go to the documentation of this file.
1using System;
3
4namespace Terraria.ID;
5
6public class SetFactory
7{
8 protected int _size;
9
10 private readonly Queue<int[]> _intBufferCache = new Queue<int[]>();
11
13
15
17
18 private object _queueLock = new object();
19
20 public SetFactory(int size)
21 {
22 if (size == 0)
23 {
24 throw new ArgumentOutOfRangeException("size cannot be 0, the intializer for Count must run first");
25 }
26 _size = size;
27 }
28
29 protected bool[] GetBoolBuffer()
30 {
32 {
33 if (_boolBufferCache.Count == 0)
34 {
35 return new bool[_size];
36 }
38 }
39 }
40
41 protected int[] GetIntBuffer()
42 {
44 {
45 if (_intBufferCache.Count == 0)
46 {
47 return new int[_size];
48 }
49 return _intBufferCache.Dequeue();
50 }
51 }
52
53 protected ushort[] GetUshortBuffer()
54 {
56 {
58 {
59 return new ushort[_size];
60 }
62 }
63 }
64
65 protected float[] GetFloatBuffer()
66 {
68 {
69 if (_floatBufferCache.Count == 0)
70 {
71 return new float[_size];
72 }
74 }
75 }
76
77 public void Recycle<T>(T[] buffer)
78 {
80 {
81 if (typeof(T).Equals(typeof(bool)))
82 {
83 _boolBufferCache.Enqueue((bool[])(object)buffer);
84 }
85 else if (typeof(T).Equals(typeof(int)))
86 {
87 _intBufferCache.Enqueue((int[])(object)buffer);
88 }
89 }
90 }
91
92 public bool[] CreateBoolSet(params int[] types)
93 {
94 return CreateBoolSet(defaultState: false, types);
95 }
96
97 public bool[] CreateBoolSet(bool defaultState, params int[] types)
98 {
99 bool[] boolBuffer = GetBoolBuffer();
100 for (int i = 0; i < boolBuffer.Length; i++)
101 {
103 }
104 for (int j = 0; j < types.Length; j++)
105 {
107 }
108 return boolBuffer;
109 }
110
111 public int[] CreateIntSet(params int[] types)
112 {
113 return CreateIntSet(-1, types);
114 }
115
116 public int[] CreateIntSet(int defaultState, params int[] inputs)
117 {
118 if (inputs.Length % 2 != 0)
119 {
120 throw new Exception("You have a bad length for inputs on CreateArraySet");
121 }
122 int[] intBuffer = GetIntBuffer();
123 for (int i = 0; i < intBuffer.Length; i++)
124 {
126 }
127 for (int j = 0; j < inputs.Length; j += 2)
128 {
129 intBuffer[inputs[j]] = inputs[j + 1];
130 }
131 return intBuffer;
132 }
133
134 public ushort[] CreateUshortSet(ushort defaultState, params ushort[] inputs)
135 {
136 if (inputs.Length % 2 != 0)
137 {
138 throw new Exception("You have a bad length for inputs on CreateArraySet");
139 }
140 ushort[] ushortBuffer = GetUshortBuffer();
141 for (int i = 0; i < ushortBuffer.Length; i++)
142 {
144 }
145 for (int j = 0; j < inputs.Length; j += 2)
146 {
147 ushortBuffer[inputs[j]] = inputs[j + 1];
148 }
149 return ushortBuffer;
150 }
151
152 public float[] CreateFloatSet(float defaultState, params float[] inputs)
153 {
154 if (inputs.Length % 2 != 0)
155 {
156 throw new Exception("You have a bad length for inputs on CreateArraySet");
157 }
158 float[] floatBuffer = GetFloatBuffer();
159 for (int i = 0; i < floatBuffer.Length; i++)
160 {
162 }
163 for (int j = 0; j < inputs.Length; j += 2)
164 {
165 floatBuffer[(int)inputs[j]] = inputs[j + 1];
166 }
167 return floatBuffer;
168 }
169
171 {
172 if (inputs.Length % 2 != 0)
173 {
174 throw new Exception("You have a bad length for inputs on CreateCustomSet");
175 }
176 T[] array = new T[_size];
177 for (int i = 0; i < array.Length; i++)
178 {
179 array[i] = defaultState;
180 }
181 if (inputs != null)
182 {
183 for (int j = 0; j < inputs.Length; j += 2)
184 {
185 T val = (typeof(T).IsPrimitive ? ((T)inputs[j + 1]) : ((typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>)) ? ((T)inputs[j + 1]) : ((!typeof(T).IsClass) ? ((T)Convert.ChangeType(inputs[j + 1], typeof(T))) : ((T)inputs[j + 1]))));
186 if (inputs[j] is ushort)
187 {
188 array[(ushort)inputs[j]] = val;
189 }
190 else if (inputs[j] is int)
191 {
192 array[(int)inputs[j]] = val;
193 }
194 else
195 {
196 array[(short)inputs[j]] = val;
197 }
198 }
199 }
200 return array;
201 }
202}
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
readonly Queue< float[]> _floatBufferCache
Definition SetFactory.cs:16
int[] CreateIntSet(int defaultState, params int[] inputs)
ushort[] GetUshortBuffer()
Definition SetFactory.cs:53
bool[] CreateBoolSet(bool defaultState, params int[] types)
Definition SetFactory.cs:97
void Recycle< T >(T[] buffer)
Definition SetFactory.cs:77
ushort[] CreateUshortSet(ushort defaultState, params ushort[] inputs)
readonly Queue< bool[]> _boolBufferCache
Definition SetFactory.cs:14
readonly Queue< int[]> _intBufferCache
Definition SetFactory.cs:10
float[] CreateFloatSet(float defaultState, params float[] inputs)
bool[] CreateBoolSet(params int[] types)
Definition SetFactory.cs:92
readonly Queue< ushort[]> _ushortBufferCache
Definition SetFactory.cs:12
T[] CreateCustomSet< T >(T defaultState, params object[] inputs)
int[] CreateIntSet(params int[] types)