Skip to content

Commit c38eeeb

Browse files
committed
Refactor DataAnalyzer
1 parent 0df4357 commit c38eeeb

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

Src/FastData.InternalShared/Helpers/TestHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Genbox.FastData.Internal.Abstracts;
1111
using Genbox.FastData.Internal.Analysis;
1212
using Genbox.FastData.Internal.Analysis.Properties;
13+
using Genbox.FastData.Internal.Helpers;
1314
using Genbox.FastData.Internal.Misc;
1415
using Genbox.FastData.Internal.Structures;
1516
using Genbox.FastData.InternalShared.TestClasses;
@@ -99,12 +100,11 @@ public static GeneratorSpec Generate<T>(Func<string, ICodeGenerator> func, TestV
99100
DataType dataType = Enum.Parse<DataType>(typeof(T).Name);
100101

101102
IProperties props;
102-
ReadOnlySpan<T> span = vector.Values.AsReadOnlySpan();
103103

104-
if (typeof(T) == typeof(string))
105-
props = DataAnalyzer.GetStringProperties(span);
104+
if (vector.Values is string[] arr)
105+
props = DataAnalyzer.GetStringProperties(new ReadOnlySpan<string>(arr));
106106
else
107-
props = DataAnalyzer.GetValueProperties(span, dataType);
107+
props = DataAnalyzer.GetValueProperties(new ReadOnlySpan<T>(vector.Values), dataType);
108108

109109
ICodeGenerator generator = func(vector.Identifier);
110110
GeneratorEncoding encoding = generator.Encoding;

Src/FastData/Internal/Analysis/DataAnalyzer.cs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,36 @@ internal static class DataAnalyzer
1010
{
1111
internal static ValueProperties<T> GetValueProperties<T>(ReadOnlySpan<T> data, DataType dataType) => dataType switch
1212
{
13-
DataType.Char => GetCharProperties<T>(UnsafeHelper.ConvertSpan<T, char>(data)),
14-
DataType.SByte => GetSByteProperties<T>(UnsafeHelper.ConvertSpan<T, sbyte>(data)),
15-
DataType.Byte => GetByteProperties<T>(UnsafeHelper.ConvertSpan<T, byte>(data)),
16-
DataType.Int16 => GetInt16Properties<T>(UnsafeHelper.ConvertSpan<T, short>(data)),
17-
DataType.UInt16 => GetUInt16Properties<T>(UnsafeHelper.ConvertSpan<T, ushort>(data)),
18-
DataType.Int32 => GetInt32Properties<T>(UnsafeHelper.ConvertSpan<T, int>(data)),
19-
DataType.UInt32 => GetUInt32Properties<T>(UnsafeHelper.ConvertSpan<T, uint>(data)),
20-
DataType.Int64 => GetInt64Properties<T>(UnsafeHelper.ConvertSpan<T, long>(data)),
21-
DataType.UInt64 => GetUInt64Properties<T>(UnsafeHelper.ConvertSpan<T, ulong>(data)),
22-
DataType.Single => GetSingleProperties<T>(UnsafeHelper.ConvertSpan<T, float>(data)),
23-
DataType.Double => GetDoubleProperties<T>(UnsafeHelper.ConvertSpan<T, double>(data)),
13+
DataType.Char => (ValueProperties<T>)(object)GetCharProperties(UnsafeHelper.ConvertSpan<T, char>(data)),
14+
DataType.SByte => (ValueProperties<T>)(object)GetSByteProperties(UnsafeHelper.ConvertSpan<T, sbyte>(data)),
15+
DataType.Byte => (ValueProperties<T>)(object)GetByteProperties(UnsafeHelper.ConvertSpan<T, byte>(data)),
16+
DataType.Int16 => (ValueProperties<T>)(object)GetInt16Properties(UnsafeHelper.ConvertSpan<T, short>(data)),
17+
DataType.UInt16 => (ValueProperties<T>)(object)GetUInt16Properties(UnsafeHelper.ConvertSpan<T, ushort>(data)),
18+
DataType.Int32 => (ValueProperties<T>)(object)GetInt32Properties(UnsafeHelper.ConvertSpan<T, int>(data)),
19+
DataType.UInt32 => (ValueProperties<T>)(object)GetUInt32Properties(UnsafeHelper.ConvertSpan<T, uint>(data)),
20+
DataType.Int64 => (ValueProperties<T>)(object)GetInt64Properties(UnsafeHelper.ConvertSpan<T, long>(data)),
21+
DataType.UInt64 => (ValueProperties<T>)(object)GetUInt64Properties(UnsafeHelper.ConvertSpan<T, ulong>(data)),
22+
DataType.Single => (ValueProperties<T>)(object)GetSingleProperties(UnsafeHelper.ConvertSpan<T, float>(data)),
23+
DataType.Double => (ValueProperties<T>)(object)GetDoubleProperties(UnsafeHelper.ConvertSpan<T, double>(data)),
2424
_ => throw new InvalidOperationException($"Unsupported data type: {dataType}")
2525
};
2626

2727
internal static StringProperties GetStringProperties(ReadOnlySpan<string> data)
28-
{
29-
return GetStringProperties<string>(data);
30-
}
31-
32-
internal static StringProperties GetStringProperties<T>(ReadOnlySpan<T> data) where T : notnull
3328
{
3429
//Contains a map of unique lengths
3530
LengthBitArray lengthMap = new LengthBitArray();
3631

3732
//We need to know the longest string for optimal mixing. Probably not 100% necessary.
38-
string maxStr = (string)(object)data[0];
33+
string maxStr = data[0];
3934
int minLength = int.MaxValue;
4035
int minUtf8ByteLength = int.MaxValue;
4136
int maxUtf8ByteLength = int.MinValue;
4237
int minUtf16ByteLength = int.MaxValue;
4338
int maxUtf16ByteLength = int.MinValue;
4439
bool uniq = true;
4540

46-
foreach (T val in data)
41+
foreach (string str in data)
4742
{
48-
string str = (string)(object)val;
49-
5043
if (str.Length > maxStr.Length)
5144
maxStr = str;
5245

@@ -69,10 +62,8 @@ internal static StringProperties GetStringProperties<T>(ReadOnlySpan<T> data) wh
6962
bool flag = true;
7063
bool allAscii = true;
7164

72-
foreach (T val in data)
65+
foreach (string str in data)
7366
{
74-
string str = (string)(object)val;
75-
7667
for (int i = 0; i < str.Length; i++)
7768
{
7869
char c = str[i];
@@ -107,7 +98,7 @@ internal static StringProperties GetStringProperties<T>(ReadOnlySpan<T> data) wh
10798
return new StringProperties(new LengthData((uint)minLength, (uint)maxStr.Length, (uint)minUtf8ByteLength, (uint)maxUtf8ByteLength, (uint)minUtf16ByteLength, (uint)maxUtf16ByteLength, uniq, lengthMap), new DeltaData(left, right), new CharacterData(allAscii));
10899
}
109100

110-
internal static ValueProperties<T> GetCharProperties<T>(ReadOnlySpan<char> data)
101+
private static ValueProperties<char> GetCharProperties(ReadOnlySpan<char> data)
111102
{
112103
char min = char.MaxValue;
113104
char max = char.MinValue;
@@ -118,10 +109,10 @@ internal static ValueProperties<T> GetCharProperties<T>(ReadOnlySpan<char> data)
118109
max = c > max ? c : max;
119110
}
120111

121-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
112+
return new ValueProperties<char>(min, max, false);
122113
}
123114

124-
internal static ValueProperties<T> GetSingleProperties<T>(ReadOnlySpan<float> data)
115+
private static ValueProperties<float> GetSingleProperties(ReadOnlySpan<float> data)
125116
{
126117
float min = float.MaxValue;
127118
float max = float.MinValue;
@@ -138,10 +129,10 @@ internal static ValueProperties<T> GetSingleProperties<T>(ReadOnlySpan<float> da
138129
max = c > max ? c : max;
139130
}
140131

141-
return new ValueProperties<T>((T)(object)min, (T)(object)max, hasZeroOrNaN);
132+
return new ValueProperties<float>(min, max, hasZeroOrNaN);
142133
}
143134

144-
internal static ValueProperties<T> GetDoubleProperties<T>(ReadOnlySpan<double> data)
135+
private static ValueProperties<double> GetDoubleProperties(ReadOnlySpan<double> data)
145136
{
146137
double min = double.MaxValue;
147138
double max = double.MinValue;
@@ -158,10 +149,10 @@ internal static ValueProperties<T> GetDoubleProperties<T>(ReadOnlySpan<double> d
158149
max = c > max ? c : max;
159150
}
160151

161-
return new ValueProperties<T>((T)(object)min, (T)(object)max, hasZeroOrNaN);
152+
return new ValueProperties<double>(min, max, hasZeroOrNaN);
162153
}
163154

164-
internal static ValueProperties<T> GetByteProperties<T>(ReadOnlySpan<byte> data)
155+
private static ValueProperties<byte> GetByteProperties(ReadOnlySpan<byte> data)
165156
{
166157
byte min = byte.MaxValue;
167158
byte max = byte.MinValue;
@@ -177,10 +168,10 @@ internal static ValueProperties<T> GetByteProperties<T>(ReadOnlySpan<byte> data)
177168
max = Math.Max(max, val);
178169
}
179170

180-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
171+
return new ValueProperties<byte>(min, max, false);
181172
}
182173

183-
internal static ValueProperties<T> GetSByteProperties<T>(ReadOnlySpan<sbyte> data)
174+
private static ValueProperties<sbyte> GetSByteProperties(ReadOnlySpan<sbyte> data)
184175
{
185176
sbyte min = sbyte.MaxValue;
186177
sbyte max = sbyte.MinValue;
@@ -196,10 +187,10 @@ internal static ValueProperties<T> GetSByteProperties<T>(ReadOnlySpan<sbyte> dat
196187
max = Math.Max(max, val);
197188
}
198189

199-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
190+
return new ValueProperties<sbyte>(min, max, false);
200191
}
201192

202-
internal static ValueProperties<T> GetInt16Properties<T>(ReadOnlySpan<short> data)
193+
private static ValueProperties<short> GetInt16Properties(ReadOnlySpan<short> data)
203194
{
204195
short min = short.MaxValue;
205196
short max = short.MinValue;
@@ -215,10 +206,10 @@ internal static ValueProperties<T> GetInt16Properties<T>(ReadOnlySpan<short> dat
215206
max = Math.Max(max, val);
216207
}
217208

218-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
209+
return new ValueProperties<short>(min, max, false);
219210
}
220211

221-
internal static ValueProperties<T> GetUInt16Properties<T>(ReadOnlySpan<ushort> data)
212+
private static ValueProperties<ushort> GetUInt16Properties(ReadOnlySpan<ushort> data)
222213
{
223214
ushort min = ushort.MaxValue;
224215
ushort max = ushort.MinValue;
@@ -234,10 +225,10 @@ internal static ValueProperties<T> GetUInt16Properties<T>(ReadOnlySpan<ushort> d
234225
max = Math.Max(max, val);
235226
}
236227

237-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
228+
return new ValueProperties<ushort>(min, max, false);
238229
}
239230

240-
internal static ValueProperties<T> GetInt32Properties<T>(ReadOnlySpan<int> data)
231+
private static ValueProperties<int> GetInt32Properties(ReadOnlySpan<int> data)
241232
{
242233
int min = int.MaxValue;
243234
int max = int.MinValue;
@@ -253,10 +244,10 @@ internal static ValueProperties<T> GetInt32Properties<T>(ReadOnlySpan<int> data)
253244
max = Math.Max(max, val);
254245
}
255246

256-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
247+
return new ValueProperties<int>(min, max, false);
257248
}
258249

259-
internal static ValueProperties<T> GetUInt32Properties<T>(ReadOnlySpan<uint> data)
250+
private static ValueProperties<uint> GetUInt32Properties(ReadOnlySpan<uint> data)
260251
{
261252
uint min = uint.MaxValue;
262253
uint max = uint.MinValue;
@@ -272,10 +263,10 @@ internal static ValueProperties<T> GetUInt32Properties<T>(ReadOnlySpan<uint> dat
272263
max = Math.Max(max, val);
273264
}
274265

275-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
266+
return new ValueProperties<uint>(min, max, false);
276267
}
277268

278-
internal static ValueProperties<T> GetInt64Properties<T>(ReadOnlySpan<long> data)
269+
private static ValueProperties<long> GetInt64Properties(ReadOnlySpan<long> data)
279270
{
280271
long min = long.MaxValue;
281272
long max = long.MinValue;
@@ -291,10 +282,10 @@ internal static ValueProperties<T> GetInt64Properties<T>(ReadOnlySpan<long> data
291282
max = Math.Max(max, val);
292283
}
293284

294-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
285+
return new ValueProperties<long>(min, max, false);
295286
}
296287

297-
internal static ValueProperties<T> GetUInt64Properties<T>(ReadOnlySpan<ulong> data)
288+
private static ValueProperties<ulong> GetUInt64Properties(ReadOnlySpan<ulong> data)
298289
{
299290
ulong min = ulong.MaxValue;
300291
ulong max = ulong.MinValue;
@@ -310,6 +301,6 @@ internal static ValueProperties<T> GetUInt64Properties<T>(ReadOnlySpan<ulong> da
310301
max = Math.Max(max, val);
311302
}
312303

313-
return new ValueProperties<T>((T)(object)min, (T)(object)max, false);
304+
return new ValueProperties<ulong>(min, max, false);
314305
}
315306
}

0 commit comments

Comments
 (0)