Skip to content

Commit 4c9f0b0

Browse files
committed
Add numeric range to KeyProperties
1 parent 332a9ba commit 4c9f0b0

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

Src/FastData/Internal/Analysis/KeyAnalyzer.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static KeyProperties<char> GetCharProperties(char[] keys)
107107
max = c > max ? c : max;
108108
}
109109

110-
return new KeyProperties<char>(min, max, false, keys.Length <= 1 || max - min == keys.Length - 1);
110+
return new KeyProperties<char>(min, max, (ulong)(max - min), false, keys.Length <= 1 || max - min == keys.Length - 1);
111111
}
112112

113113
private static KeyProperties<float> GetSingleProperties(float[] keys)
@@ -132,7 +132,8 @@ private static KeyProperties<float> GetSingleProperties(float[] keys)
132132
max = c > max ? c : max;
133133
}
134134

135-
return new KeyProperties<float>(min, max, hasZeroOrNaN, IsFloatContiguous(keys, min, max, hasNaNOrInfinity));
135+
ulong range = ClampRangeToUInt64(max - min);
136+
return new KeyProperties<float>(min, max, range, hasZeroOrNaN, IsFloatContiguous(keys, min, max, hasNaNOrInfinity));
136137
}
137138

138139
private static KeyProperties<double> GetDoubleProperties(double[] keys)
@@ -157,7 +158,8 @@ private static KeyProperties<double> GetDoubleProperties(double[] keys)
157158
max = c > max ? c : max;
158159
}
159160

160-
return new KeyProperties<double>(min, max, hasZeroOrNaN, IsDoubleContiguous(keys, min, max, hasNaNOrInfinity));
161+
ulong range = ClampRangeToUInt64(max - min);
162+
return new KeyProperties<double>(min, max, range, hasZeroOrNaN, IsDoubleContiguous(keys, min, max, hasNaNOrInfinity));
161163
}
162164

163165
private static KeyProperties<byte> GetByteProperties(byte[] keys)
@@ -171,7 +173,7 @@ private static KeyProperties<byte> GetByteProperties(byte[] keys)
171173
max = Math.Max(max, val);
172174
}
173175

174-
return new KeyProperties<byte>(min, max, false, keys.Length <= 1 || max - min == keys.Length - 1);
176+
return new KeyProperties<byte>(min, max, (ulong)(max - min), false, keys.Length <= 1 || max - min == keys.Length - 1);
175177
}
176178

177179
private static KeyProperties<sbyte> GetSByteProperties(sbyte[] keys)
@@ -185,7 +187,7 @@ private static KeyProperties<sbyte> GetSByteProperties(sbyte[] keys)
185187
max = Math.Max(max, val);
186188
}
187189

188-
return new KeyProperties<sbyte>(min, max, false, keys.Length <= 1 || max - min == keys.Length - 1);
190+
return new KeyProperties<sbyte>(min, max, (ulong)(max - min), false, keys.Length <= 1 || max - min == keys.Length - 1);
189191
}
190192

191193
private static KeyProperties<short> GetInt16Properties(short[] keys)
@@ -199,7 +201,7 @@ private static KeyProperties<short> GetInt16Properties(short[] keys)
199201
max = Math.Max(max, val);
200202
}
201203

202-
return new KeyProperties<short>(min, max, false, keys.Length <= 1 || max - min == keys.Length - 1);
204+
return new KeyProperties<short>(min, max, (ulong)(max - min), false, keys.Length <= 1 || max - min == keys.Length - 1);
203205
}
204206

205207
private static KeyProperties<ushort> GetUInt16Properties(ushort[] keys)
@@ -213,7 +215,7 @@ private static KeyProperties<ushort> GetUInt16Properties(ushort[] keys)
213215
max = Math.Max(max, val);
214216
}
215217

216-
return new KeyProperties<ushort>(min, max, false, keys.Length <= 1 || max - min == keys.Length - 1);
218+
return new KeyProperties<ushort>(min, max, (ulong)(max - min), false, keys.Length <= 1 || max - min == keys.Length - 1);
217219
}
218220

219221
private static KeyProperties<int> GetInt32Properties(int[] keys)
@@ -227,7 +229,7 @@ private static KeyProperties<int> GetInt32Properties(int[] keys)
227229
max = Math.Max(max, val);
228230
}
229231

230-
return new KeyProperties<int>(min, max, false, keys.Length <= 1 || (long)max - min == keys.Length - 1);
232+
return new KeyProperties<int>(min, max, (ulong)((long)max - min), false, keys.Length <= 1 || (long)max - min == keys.Length - 1);
231233
}
232234

233235
private static KeyProperties<uint> GetUInt32Properties(uint[] keys)
@@ -241,7 +243,7 @@ private static KeyProperties<uint> GetUInt32Properties(uint[] keys)
241243
max = Math.Max(max, val);
242244
}
243245

244-
return new KeyProperties<uint>(min, max, false, keys.Length <= 1 || (ulong)max - min == (ulong)(keys.Length - 1));
246+
return new KeyProperties<uint>(min, max, max - min, false, keys.Length <= 1 || (ulong)max - min == (ulong)(keys.Length - 1));
245247
}
246248

247249
private static KeyProperties<long> GetInt64Properties(long[] keys)
@@ -255,7 +257,8 @@ private static KeyProperties<long> GetInt64Properties(long[] keys)
255257
max = Math.Max(max, val);
256258
}
257259

258-
return new KeyProperties<long>(min, max, false, keys.Length <= 1 || unchecked((ulong)(max - min)) == (ulong)(keys.Length - 1));
260+
ulong range = unchecked((ulong)max - (ulong)min);
261+
return new KeyProperties<long>(min, max, range, false, keys.Length <= 1 || range == (ulong)(keys.Length - 1));
259262
}
260263

261264
private static KeyProperties<ulong> GetUInt64Properties(ulong[] keys)
@@ -269,7 +272,7 @@ private static KeyProperties<ulong> GetUInt64Properties(ulong[] keys)
269272
max = Math.Max(max, val);
270273
}
271274

272-
return new KeyProperties<ulong>(min, max, false, keys.Length <= 1 || max - min == (ulong)(keys.Length - 1));
275+
return new KeyProperties<ulong>(min, max, max - min, false, keys.Length <= 1 || max - min == (ulong)(keys.Length - 1));
273276
}
274277

275278
private static bool IsFloatContiguous(float[] keys, float min, float max, bool hasNaNOrInfinity)
@@ -323,4 +326,15 @@ private static bool IsDoubleContiguous(double[] keys, double min, double max, bo
323326

324327
return true;
325328
}
329+
330+
private static ulong ClampRangeToUInt64(double range)
331+
{
332+
if (double.IsNaN(range) || range <= 0.0d)
333+
return 0;
334+
335+
if (range >= ulong.MaxValue)
336+
return ulong.MaxValue;
337+
338+
return (ulong)range;
339+
}
326340
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace Genbox.FastData.Internal.Analysis.Properties;
22

3-
internal sealed record KeyProperties<T>(T MinKeyValue, T MaxKeyValue, bool HasZeroOrNaN, bool IsConsecutive) : IProperties;
3+
internal sealed record KeyProperties<T>(T MinKeyValue, T MaxKeyValue, ulong Range, bool HasZeroOrNaN, bool IsConsecutive) : IProperties;

0 commit comments

Comments
 (0)