com.wallstop-studios.unity-helpers 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/Docs/ILIST_SORTING_PERFORMANCE.md +92 -0
  2. package/Docs/ILIST_SORTING_PERFORMANCE.md.meta +7 -0
  3. package/Docs/INDEX.md +10 -1
  4. package/Docs/Images/random_generators.svg +7 -7
  5. package/Docs/RANDOM_PERFORMANCE.md +17 -14
  6. package/Docs/SPATIAL_TREE_2D_PERFORMANCE.md +64 -64
  7. package/Docs/SPATIAL_TREE_3D_PERFORMANCE.md +64 -64
  8. package/Editor/Core/Helper/AnimationEventHelpers.cs +1 -1
  9. package/README.md +25 -15
  10. package/Runtime/Core/Extension/IListExtensions.cs +720 -12
  11. package/Runtime/Core/Helper/UnityMainThreadDispatcher.cs +2 -3
  12. package/Runtime/Core/Random/AbstractRandom.cs +52 -5
  13. package/Runtime/Core/Random/DotNetRandom.cs +3 -3
  14. package/Runtime/Core/Random/FlurryBurstRandom.cs +285 -0
  15. package/Runtime/Core/Random/FlurryBurstRandom.cs.meta +3 -0
  16. package/Runtime/Core/Random/IllusionFlow.cs +3 -3
  17. package/Runtime/Core/Random/LinearCongruentialGenerator.cs +3 -3
  18. package/Runtime/Core/Random/PcgRandom.cs +6 -6
  19. package/Runtime/Core/Random/PhotonSpinRandom.cs +387 -0
  20. package/Runtime/Core/Random/PhotonSpinRandom.cs.meta +3 -0
  21. package/Runtime/Core/Random/RomuDuo.cs +3 -3
  22. package/Runtime/Core/Random/SplitMix64.cs +3 -3
  23. package/Runtime/Core/Random/SquirrelRandom.cs +6 -4
  24. package/Runtime/Core/Random/StormDropRandom.cs +271 -0
  25. package/Runtime/Core/Random/StormDropRandom.cs.meta +3 -0
  26. package/Runtime/Core/Random/UnityRandom.cs +3 -3
  27. package/Runtime/Core/Random/WyRandom.cs +6 -4
  28. package/Runtime/Core/Random/XorShiftRandom.cs +3 -3
  29. package/Runtime/Core/Random/XoroShiroRandom.cs +3 -3
  30. package/Runtime/Tags/AttributeMetadataCache.cs +312 -3
  31. package/Tests/Editor/Tags/AttributeMetadataCacheTests.cs +192 -0
  32. package/Tests/Editor/Tags/AttributeMetadataCacheTests.cs.meta +11 -0
  33. package/Tests/Editor/Tags.meta +8 -0
  34. package/Tests/Runtime/Extensions/IListExtensionTests.cs +187 -1
  35. package/Tests/Runtime/Helper/ObjectsTests.cs +3 -3
  36. package/Tests/Runtime/Integrations/Reflex/RelationalComponentsReflexTests.cs +2 -2
  37. package/Tests/Runtime/Performance/IListSortingPerformanceTests.cs +346 -0
  38. package/Tests/Runtime/Performance/IListSortingPerformanceTests.cs.meta +11 -0
  39. package/Tests/Runtime/Performance/RandomPerformanceTests.cs +3 -0
  40. package/Tests/Runtime/Random/FlurryBurstRandomTests.cs +12 -0
  41. package/Tests/Runtime/Random/FlurryBurstRandomTests.cs.meta +3 -0
  42. package/Tests/Runtime/Random/PhotonSpinRandomTests.cs +12 -0
  43. package/Tests/Runtime/Random/PhotonSpinRandomTests.cs.meta +3 -0
  44. package/Tests/Runtime/Random/RandomProtoSerializationTests.cs +14 -0
  45. package/Tests/Runtime/Random/RandomTestBase.cs +39 -4
  46. package/Tests/Runtime/Random/StormDropRandomTests.cs +12 -0
  47. package/Tests/Runtime/Random/StormDropRandomTests.cs.meta +3 -0
  48. package/Tests/Runtime/Serialization/ProtoInterfaceResolutionEdgeTests.cs +2 -2
  49. package/Tests/Runtime/Serialization/ProtoRootRegistrationTests.cs +1 -1
  50. package/Tests/Runtime/Serialization/ProtoSerializeBehaviorTests.cs +1 -1
  51. package/Tests/Runtime/Tags/PeriodicEffectDefinitionSerializationTests.cs +2 -2
  52. package/package.json +1 -1
@@ -0,0 +1,192 @@
1
+ namespace WallstopStudios.UnityHelpers.Tests.Editor.Tags
2
+ {
3
+ using System;
4
+ using System.Reflection;
5
+ using NUnit.Framework;
6
+ using UnityEngine;
7
+ using UnityHelpers.Tags;
8
+
9
+ [TestFixture]
10
+ public sealed class AttributeMetadataCacheTests
11
+ {
12
+ [Test]
13
+ public void SetMetadataSortsSerializedContent()
14
+ {
15
+ AttributeMetadataCache cache =
16
+ ScriptableObject.CreateInstance<AttributeMetadataCache>();
17
+
18
+ try
19
+ {
20
+ string[] attributeNames = new string[] { "Gamma", "Alpha", "Beta" };
21
+
22
+ string alphaAttributesTypeName =
23
+ typeof(AlphaAttributesComponent).AssemblyQualifiedName ?? string.Empty;
24
+ string bravoAttributesTypeName =
25
+ typeof(BravoAttributesComponent).AssemblyQualifiedName ?? string.Empty;
26
+
27
+ AttributeMetadataCache.TypeFieldMetadata alphaTypeMetadata = new(
28
+ alphaAttributesTypeName,
29
+ new string[] { "gammaField", "betaField" }
30
+ );
31
+
32
+ AttributeMetadataCache.TypeFieldMetadata bravoTypeMetadata = new(
33
+ bravoAttributesTypeName,
34
+ new string[] { "zetaField", "alphaField" }
35
+ );
36
+
37
+ string alphaRelationalTypeName =
38
+ typeof(AlphaRelationalComponent).AssemblyQualifiedName ?? string.Empty;
39
+ string bravoRelationalTypeName =
40
+ typeof(BravoRelationalComponent).AssemblyQualifiedName ?? string.Empty;
41
+
42
+ AttributeMetadataCache.RelationalFieldMetadata[] alphaRelationalFields =
43
+ new AttributeMetadataCache.RelationalFieldMetadata[]
44
+ {
45
+ new(
46
+ "betaRelation",
47
+ AttributeMetadataCache.RelationalAttributeKind.Parent,
48
+ AttributeMetadataCache.FieldKind.HashSet,
49
+ typeof(Light).AssemblyQualifiedName ?? string.Empty,
50
+ true
51
+ ),
52
+ new(
53
+ "alphaRelation",
54
+ AttributeMetadataCache.RelationalAttributeKind.Child,
55
+ AttributeMetadataCache.FieldKind.Single,
56
+ typeof(Transform).AssemblyQualifiedName ?? string.Empty,
57
+ false
58
+ ),
59
+ };
60
+
61
+ AttributeMetadataCache.RelationalFieldMetadata[] bravoRelationalFields =
62
+ new AttributeMetadataCache.RelationalFieldMetadata[]
63
+ {
64
+ new(
65
+ "zetaRelation",
66
+ AttributeMetadataCache.RelationalAttributeKind.Sibling,
67
+ AttributeMetadataCache.FieldKind.List,
68
+ typeof(Camera).AssemblyQualifiedName ?? string.Empty,
69
+ true
70
+ ),
71
+ new(
72
+ "alphaRelation",
73
+ AttributeMetadataCache.RelationalAttributeKind.Child,
74
+ AttributeMetadataCache.FieldKind.Single,
75
+ typeof(Transform).AssemblyQualifiedName ?? string.Empty,
76
+ false
77
+ ),
78
+ };
79
+
80
+ AttributeMetadataCache.RelationalTypeMetadata[] relationalMetadata =
81
+ new AttributeMetadataCache.RelationalTypeMetadata[]
82
+ {
83
+ new(
84
+ bravoRelationalTypeName,
85
+ new AttributeMetadataCache.RelationalFieldMetadata[]
86
+ {
87
+ bravoRelationalFields[0],
88
+ null,
89
+ bravoRelationalFields[1],
90
+ }
91
+ ),
92
+ null,
93
+ new(alphaRelationalTypeName, alphaRelationalFields),
94
+ };
95
+
96
+ AttributeMetadataCache.TypeFieldMetadata[] typeMetadata =
97
+ new AttributeMetadataCache.TypeFieldMetadata[]
98
+ {
99
+ bravoTypeMetadata,
100
+ null,
101
+ alphaTypeMetadata,
102
+ };
103
+
104
+ cache.SetMetadata(attributeNames, typeMetadata, relationalMetadata);
105
+
106
+ string[] storedAttributeNames = GetPrivateField<string[]>(
107
+ cache,
108
+ "_allAttributeNames"
109
+ );
110
+ Assert.That(
111
+ storedAttributeNames,
112
+ Is.EqualTo(new string[] { "Alpha", "Beta", "Gamma" })
113
+ );
114
+
115
+ AttributeMetadataCache.TypeFieldMetadata[] storedTypeMetadata =
116
+ GetPrivateField<AttributeMetadataCache.TypeFieldMetadata[]>(
117
+ cache,
118
+ "_typeMetadata"
119
+ );
120
+ Assert.That(storedTypeMetadata.Length, Is.EqualTo(2));
121
+ Assert.That(storedTypeMetadata[0].typeName, Is.EqualTo(alphaAttributesTypeName));
122
+ Assert.That(
123
+ storedTypeMetadata[0].fieldNames,
124
+ Is.EqualTo(new string[] { "betaField", "gammaField" })
125
+ );
126
+ Assert.That(storedTypeMetadata[1].typeName, Is.EqualTo(bravoAttributesTypeName));
127
+ Assert.That(
128
+ storedTypeMetadata[1].fieldNames,
129
+ Is.EqualTo(new string[] { "alphaField", "zetaField" })
130
+ );
131
+
132
+ AttributeMetadataCache.RelationalTypeMetadata[] storedRelationalMetadata =
133
+ GetPrivateField<AttributeMetadataCache.RelationalTypeMetadata[]>(
134
+ cache,
135
+ "_relationalTypeMetadata"
136
+ );
137
+ Assert.That(storedRelationalMetadata.Length, Is.EqualTo(2));
138
+ Assert.That(
139
+ storedRelationalMetadata[0].typeName,
140
+ Is.EqualTo(alphaRelationalTypeName)
141
+ );
142
+ Assert.That(storedRelationalMetadata[0].fields.Length, Is.EqualTo(2));
143
+ Assert.That(
144
+ storedRelationalMetadata[0].fields[0].fieldName,
145
+ Is.EqualTo("alphaRelation")
146
+ );
147
+ Assert.That(
148
+ storedRelationalMetadata[0].fields[1].fieldName,
149
+ Is.EqualTo("betaRelation")
150
+ );
151
+ Assert.That(
152
+ storedRelationalMetadata[1].typeName,
153
+ Is.EqualTo(bravoRelationalTypeName)
154
+ );
155
+ Assert.That(storedRelationalMetadata[1].fields.Length, Is.EqualTo(2));
156
+ Assert.That(
157
+ storedRelationalMetadata[1].fields[0].fieldName,
158
+ Is.EqualTo("alphaRelation")
159
+ );
160
+ Assert.That(
161
+ storedRelationalMetadata[1].fields[1].fieldName,
162
+ Is.EqualTo("zetaRelation")
163
+ );
164
+ }
165
+ finally
166
+ {
167
+ UnityEngine.Object.DestroyImmediate(cache);
168
+ }
169
+ }
170
+
171
+ private static T GetPrivateField<T>(AttributeMetadataCache cache, string fieldName)
172
+ {
173
+ BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
174
+ FieldInfo field = typeof(AttributeMetadataCache).GetField(fieldName, bindingFlags);
175
+ if (field == null)
176
+ {
177
+ throw new InvalidOperationException($"Field '{fieldName}' was not found.");
178
+ }
179
+
180
+ object value = field.GetValue(cache);
181
+ return value is T castValue ? castValue : default;
182
+ }
183
+
184
+ private sealed class AlphaAttributesComponent : AttributesComponent { }
185
+
186
+ private sealed class BravoAttributesComponent : AttributesComponent { }
187
+
188
+ private sealed class AlphaRelationalComponent : MonoBehaviour { }
189
+
190
+ private sealed class BravoRelationalComponent : MonoBehaviour { }
191
+ }
192
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: e6e89a6fc9964f9f92a7bb5cbb4f793d
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -0,0 +1,8 @@
1
+ fileFormatVersion: 2
2
+ guid: 43bfcdf80a014948b5377ffa29afa8df
3
+ folderAsset: yes
4
+ DefaultImporter:
5
+ externalObjects: {}
6
+ userData:
7
+ assetBundleName:
8
+ assetBundleVariant:
@@ -17,6 +17,14 @@ namespace WallstopStudios.UnityHelpers.Tests.Extensions
17
17
  public int Compare(int x, int y) => x.CompareTo(y);
18
18
  }
19
19
 
20
+ private sealed class StableTupleComparer : IComparer<ValueTuple<int, int>>
21
+ {
22
+ public int Compare(ValueTuple<int, int> x, ValueTuple<int, int> y)
23
+ {
24
+ return x.Item1.CompareTo(y.Item1);
25
+ }
26
+ }
27
+
20
28
  private readonly struct IntEqualityComparer : IEqualityComparer<int>
21
29
  {
22
30
  public bool Equals(int x, int y) => x == y;
@@ -135,7 +143,7 @@ namespace WallstopStudios.UnityHelpers.Tests.Extensions
135
143
  }
136
144
 
137
145
  [Test]
138
- public void ShellSortEnhanced()
146
+ public void GhostSort()
139
147
  {
140
148
  for (int i = 0; i < NumTries; ++i)
141
149
  {
@@ -153,6 +161,120 @@ namespace WallstopStudios.UnityHelpers.Tests.Extensions
153
161
  }
154
162
  }
155
163
 
164
+ [Test]
165
+ public void MeteorSort()
166
+ {
167
+ for (int i = 0; i < NumTries; ++i)
168
+ {
169
+ int[] input = Enumerable
170
+ .Range(0, 100)
171
+ .Select(_ => PRNG.Instance.Next(int.MinValue, int.MaxValue))
172
+ .ToArray();
173
+ int[] conventionalSorted = input.ToArray();
174
+ Array.Sort(conventionalSorted);
175
+
176
+ int[] meteorSorted = input.ToArray();
177
+ meteorSorted.MeteorSort(new IntComparer());
178
+ Assert.That(conventionalSorted, Is.EqualTo(meteorSorted));
179
+ Assert.That(input.OrderBy(x => x), Is.EqualTo(meteorSorted));
180
+ }
181
+ }
182
+
183
+ [Test]
184
+ public void PatternDefeatingQuickSort()
185
+ {
186
+ for (int i = 0; i < NumTries; ++i)
187
+ {
188
+ int[] input = Enumerable
189
+ .Range(0, 100)
190
+ .Select(_ => PRNG.Instance.Next(int.MinValue, int.MaxValue))
191
+ .ToArray();
192
+ int[] conventionalSorted = input.ToArray();
193
+ Array.Sort(conventionalSorted);
194
+
195
+ int[] pdqSorted = input.ToArray();
196
+ pdqSorted.PatternDefeatingQuickSort(new IntComparer());
197
+ Assert.That(conventionalSorted, Is.EqualTo(pdqSorted));
198
+ Assert.That(input.OrderBy(x => x), Is.EqualTo(pdqSorted));
199
+ }
200
+ }
201
+
202
+ [Test]
203
+ public void GrailSort()
204
+ {
205
+ for (int i = 0; i < NumTries; ++i)
206
+ {
207
+ int[] input = Enumerable
208
+ .Range(0, 100)
209
+ .Select(_ => PRNG.Instance.Next(int.MinValue, int.MaxValue))
210
+ .ToArray();
211
+ int[] conventionalSorted = input.ToArray();
212
+ Array.Sort(conventionalSorted);
213
+
214
+ int[] grailSorted = input.ToArray();
215
+ grailSorted.GrailSort(new IntComparer());
216
+ Assert.That(conventionalSorted, Is.EqualTo(grailSorted));
217
+ Assert.That(input.OrderBy(x => x), Is.EqualTo(grailSorted));
218
+ }
219
+ }
220
+
221
+ [Test]
222
+ public void PowerSort()
223
+ {
224
+ for (int i = 0; i < NumTries; ++i)
225
+ {
226
+ int[] input = Enumerable
227
+ .Range(0, 100)
228
+ .Select(_ => PRNG.Instance.Next(int.MinValue, int.MaxValue))
229
+ .ToArray();
230
+ int[] conventionalSorted = input.ToArray();
231
+ Array.Sort(conventionalSorted);
232
+
233
+ int[] powerSorted = input.ToArray();
234
+ powerSorted.PowerSort(new IntComparer());
235
+ Assert.That(conventionalSorted, Is.EqualTo(powerSorted));
236
+ Assert.That(input.OrderBy(x => x), Is.EqualTo(powerSorted));
237
+ }
238
+ }
239
+
240
+ [Test]
241
+ public void GrailSortIsStable()
242
+ {
243
+ ValueTuple<int, int>[] input = Enumerable
244
+ .Range(0, 100)
245
+ .Select(i => ValueTuple.Create(i / 5, i))
246
+ .ToArray();
247
+ ValueTuple<int, int>[] grailSorted = input.ToArray();
248
+ grailSorted.GrailSort(new StableTupleComparer());
249
+
250
+ for (int i = 1; i < grailSorted.Length; ++i)
251
+ {
252
+ if (grailSorted[i - 1].Item1 == grailSorted[i].Item1)
253
+ {
254
+ Assert.That(grailSorted[i - 1].Item2, Is.LessThan(grailSorted[i].Item2));
255
+ }
256
+ }
257
+ }
258
+
259
+ [Test]
260
+ public void PowerSortIsStable()
261
+ {
262
+ ValueTuple<int, int>[] input = Enumerable
263
+ .Range(0, 100)
264
+ .Select(i => ValueTuple.Create(i / 4, i))
265
+ .ToArray();
266
+ ValueTuple<int, int>[] powerSorted = input.ToArray();
267
+ powerSorted.PowerSort(new StableTupleComparer());
268
+
269
+ for (int i = 1; i < powerSorted.Length; ++i)
270
+ {
271
+ if (powerSorted[i - 1].Item1 == powerSorted[i].Item1)
272
+ {
273
+ Assert.That(powerSorted[i - 1].Item2, Is.LessThan(powerSorted[i].Item2));
274
+ }
275
+ }
276
+ }
277
+
156
278
  [Test]
157
279
  public void SortDefaultAlgorithm()
158
280
  {
@@ -731,6 +853,70 @@ namespace WallstopStudios.UnityHelpers.Tests.Extensions
731
853
  Assert.That(single, Is.EqualTo(new[] { 42 }));
732
854
  }
733
855
 
856
+ [Test]
857
+ public void PatternDefeatingQuickSortEmptyList()
858
+ {
859
+ int[] empty = Array.Empty<int>();
860
+ empty.PatternDefeatingQuickSort(new IntComparer());
861
+ Assert.That(empty, Is.Empty);
862
+ }
863
+
864
+ [Test]
865
+ public void PatternDefeatingQuickSortSingleElement()
866
+ {
867
+ int[] single = { 42 };
868
+ single.PatternDefeatingQuickSort(new IntComparer());
869
+ Assert.That(single, Is.EqualTo(new[] { 42 }));
870
+ }
871
+
872
+ [Test]
873
+ public void GrailSortEmptyList()
874
+ {
875
+ int[] empty = Array.Empty<int>();
876
+ empty.GrailSort(new IntComparer());
877
+ Assert.That(empty, Is.Empty);
878
+ }
879
+
880
+ [Test]
881
+ public void GrailSortSingleElement()
882
+ {
883
+ int[] single = { 42 };
884
+ single.GrailSort(new IntComparer());
885
+ Assert.That(single, Is.EqualTo(new[] { 42 }));
886
+ }
887
+
888
+ [Test]
889
+ public void PowerSortEmptyList()
890
+ {
891
+ int[] empty = Array.Empty<int>();
892
+ empty.PowerSort(new IntComparer());
893
+ Assert.That(empty, Is.Empty);
894
+ }
895
+
896
+ [Test]
897
+ public void PowerSortSingleElement()
898
+ {
899
+ int[] single = { 42 };
900
+ single.PowerSort(new IntComparer());
901
+ Assert.That(single, Is.EqualTo(new[] { 42 }));
902
+ }
903
+
904
+ [Test]
905
+ public void MeteorSortEmptyList()
906
+ {
907
+ int[] empty = Array.Empty<int>();
908
+ empty.MeteorSort(new IntComparer());
909
+ Assert.That(empty, Is.Empty);
910
+ }
911
+
912
+ [Test]
913
+ public void MeteorSortSingleElement()
914
+ {
915
+ int[] single = { 42 };
916
+ single.MeteorSort(new IntComparer());
917
+ Assert.That(single, Is.EqualTo(new[] { 42 }));
918
+ }
919
+
734
920
  [Test]
735
921
  public void CombinedOperationsShuffleThenSort()
736
922
  {
@@ -343,8 +343,8 @@ namespace WallstopStudios.UnityHelpers.Tests.Helper
343
343
  CustomEnumerable ascending = new(1, 2, 3);
344
344
  CustomEnumerable descending = new(3, 2, 1);
345
345
 
346
- int ascHash = Objects.EnumerableHashCode<int>(ascending);
347
- int descHash = Objects.EnumerableHashCode<int>(descending);
346
+ int ascHash = Objects.EnumerableHashCode(ascending);
347
+ int descHash = Objects.EnumerableHashCode(descending);
348
348
 
349
349
  Assert.AreNotEqual(ascHash, descHash);
350
350
  }
@@ -354,7 +354,7 @@ namespace WallstopStudios.UnityHelpers.Tests.Helper
354
354
  {
355
355
  DisposableEnumerable enumerable = new(1, 2, 3);
356
356
 
357
- int result = Objects.EnumerableHashCode<int>(enumerable);
357
+ int result = Objects.EnumerableHashCode(enumerable);
358
358
 
359
359
  Assert.AreNotEqual(0, result);
360
360
  Assert.IsTrue(enumerable.LastEnumerator.WasDisposed);
@@ -321,7 +321,7 @@ namespace WallstopStudios.UnityHelpers.Tests.Integrations.Reflex
321
321
  GameObject root = Track(new GameObject("ReflexComponentPrefab"));
322
322
  ReflexRelationalTester tester = root.AddComponent<ReflexRelationalTester>();
323
323
 
324
- GameObject child = new GameObject("ReflexComponentPrefabChild");
324
+ GameObject child = new("ReflexComponentPrefabChild");
325
325
  child.AddComponent<CapsuleCollider>();
326
326
  child.transform.SetParent(root.transform, false);
327
327
 
@@ -334,7 +334,7 @@ namespace WallstopStudios.UnityHelpers.Tests.Integrations.Reflex
334
334
  GameObject root = Track(new GameObject("ReflexGameObjectPrefab"));
335
335
  root.AddComponent<ReflexRelationalTester>();
336
336
 
337
- GameObject child = new GameObject("ReflexGameObjectPrefabChild");
337
+ GameObject child = new("ReflexGameObjectPrefabChild");
338
338
  child.AddComponent<CapsuleCollider>();
339
339
  child.transform.SetParent(root.transform, false);
340
340