com.wallstop-studios.unity-helpers 2.0.0-rc05 → 2.0.0-rc06

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 (33) hide show
  1. package/Runtime/Core/DataStructure/TimedCache.cs +4 -3
  2. package/Runtime/Core/Extension/IListExtensions.cs +2 -2
  3. package/Runtime/Core/Extension/RandomExtensions.cs +23 -4
  4. package/Runtime/Core/Extension/UnityExtensions.cs +278 -90
  5. package/Runtime/Core/Helper/ArrayConverter.cs +39 -0
  6. package/Runtime/Core/Helper/ArrayConverter.cs.meta +3 -0
  7. package/Runtime/Core/Helper/Helpers.cs +209 -84
  8. package/Runtime/Core/Helper/WallMath.cs +85 -22
  9. package/Runtime/Core/Random/AbstractRandom.cs +34 -20
  10. package/Runtime/Core/Random/DotNetRandom.cs +3 -5
  11. package/Runtime/Core/Random/PRNG.cs +7 -0
  12. package/Runtime/Core/Random/PRNG.cs.meta +3 -0
  13. package/Runtime/Core/Random/PcgRandom.cs +4 -6
  14. package/Runtime/Core/Random/RandomState.cs +31 -3
  15. package/Runtime/Core/Random/SquirrelRandom.cs +3 -5
  16. package/Runtime/Core/Random/SystemRandom.cs +20 -11
  17. package/Runtime/Core/Random/ThreadLocalRandom.cs +2 -1
  18. package/Runtime/Core/Random/UnityRandom.cs +2 -4
  19. package/Runtime/Core/Random/WyRandom.cs +2 -4
  20. package/Runtime/Core/Random/XorShiftRandom.cs +3 -5
  21. package/Runtime/Core/Serialization/Serializer.cs +36 -14
  22. package/Runtime/Utils/CircleLineRenderer.cs +17 -5
  23. package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +34 -10
  24. package/Tests/Runtime/Helper/ArrayConverterTests.cs +19 -0
  25. package/Tests/Runtime/Helper/ArrayConverterTests.cs.meta +3 -0
  26. package/Tests/Runtime/Helper/WallMathTests.cs +221 -0
  27. package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
  28. package/Tests/Runtime/Helper.meta +3 -0
  29. package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
  30. package/Tests/Runtime/Random/RandomTestBase.cs +23 -3
  31. package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
  32. package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
  33. package/package.json +1 -1
@@ -2,13 +2,13 @@
2
2
  {
3
3
  using System;
4
4
  using System.ComponentModel;
5
- using Extension;
6
- using JsonConverters;
7
5
  using System.IO;
8
6
  using System.Runtime.Serialization.Formatters.Binary;
9
7
  using System.Text;
10
8
  using System.Text.Json;
11
9
  using System.Text.Json.Serialization;
10
+ using Extension;
11
+ using JsonConverters;
12
12
 
13
13
  internal static class SerializerEncoding
14
14
  {
@@ -30,7 +30,7 @@
30
30
  {
31
31
  new JsonStringEnumConverter(),
32
32
  Vector3Converter.Instance,
33
- Vector2Converter.Instance
33
+ Vector2Converter.Instance,
34
34
  },
35
35
  };
36
36
 
@@ -45,7 +45,7 @@
45
45
  {
46
46
  new JsonStringEnumConverter(),
47
47
  Vector3Converter.Instance,
48
- Vector2Converter.Instance
48
+ Vector2Converter.Instance,
49
49
  },
50
50
  WriteIndented = true,
51
51
  };
@@ -70,7 +70,10 @@
70
70
  return ProtoDeserialize<T>(serialized);
71
71
  default:
72
72
  throw new InvalidEnumArgumentException(
73
- nameof(serializationType), (int)serializationType, typeof(SerializationType));
73
+ nameof(serializationType),
74
+ (int)serializationType,
75
+ typeof(SerializationType)
76
+ );
74
77
  }
75
78
  }
76
79
 
@@ -84,7 +87,10 @@
84
87
  return ProtoSerialize(instance);
85
88
  default:
86
89
  throw new InvalidEnumArgumentException(
87
- nameof(serializationType), (int)serializationType, typeof(SerializationType));
90
+ nameof(serializationType),
91
+ (int)serializationType,
92
+ typeof(SerializationType)
93
+ );
88
94
  }
89
95
  }
90
96
 
@@ -123,9 +129,14 @@
123
129
  return memoryStream.ToArray();
124
130
  }
125
131
 
126
- public static T JsonDeserialize<T>(string data)
132
+ public static T JsonDeserialize<T>(string data, Type type = null)
127
133
  {
128
- return JsonSerializer.Deserialize<T>(data, SerializerEncoding.NormalJsonOptions);
134
+ return (T)
135
+ JsonSerializer.Deserialize(
136
+ data,
137
+ type ?? typeof(T),
138
+ SerializerEncoding.NormalJsonOptions
139
+ );
129
140
  }
130
141
 
131
142
  public static byte[] JsonSerialize<T>(T input)
@@ -135,13 +146,24 @@
135
146
 
136
147
  public static string JsonStringify<T>(T input, bool pretty = false)
137
148
  {
138
- JsonSerializerOptions options =
139
- pretty ? SerializerEncoding.PrettyJsonOptions : SerializerEncoding.NormalJsonOptions;
140
- if (typeof(T) == typeof(object))
149
+ JsonSerializerOptions options = pretty
150
+ ? SerializerEncoding.PrettyJsonOptions
151
+ : SerializerEncoding.NormalJsonOptions;
152
+ Type parameterType = typeof(T);
153
+ if (
154
+ parameterType.IsAbstract
155
+ || parameterType.IsInterface
156
+ || parameterType == typeof(object)
157
+ )
141
158
  {
142
159
  object data = input;
143
- Type type = data?.GetType();
144
- return JsonSerializer.Serialize(data, data?.GetType(), options);
160
+ if (data == null)
161
+ {
162
+ return "{}";
163
+ }
164
+
165
+ Type type = data.GetType();
166
+ return JsonSerializer.Serialize(data, type, options);
145
167
  }
146
168
 
147
169
  return JsonSerializer.Serialize(input, options);
@@ -159,4 +181,4 @@
159
181
  File.WriteAllText(path, jsonAsText);
160
182
  }
161
183
  }
162
- }
184
+ }
@@ -17,7 +17,7 @@
17
17
  public int baseSegments = 4;
18
18
  public float updateRateSeconds = 0.1f;
19
19
  public Color color = Color.grey;
20
-
20
+
21
21
  public Vector3 Offset
22
22
  {
23
23
  get => _offset;
@@ -26,6 +26,7 @@
26
26
 
27
27
  [SiblingComponent]
28
28
  private CircleCollider2D _collider;
29
+
29
30
  [SiblingComponent]
30
31
  private LineRenderer[] _lineRenderers;
31
32
 
@@ -70,7 +71,11 @@
70
71
 
71
72
  if (maxLineWidth < minLineWidth)
72
73
  {
73
- this.LogWarn("MaxLineWidth {0} smaller than MinLineWidth {1}.", maxLineWidth, minLineWidth);
74
+ this.LogWarn(
75
+ "MaxLineWidth {0} smaller than MinLineWidth {1}.",
76
+ maxLineWidth,
77
+ minLineWidth
78
+ );
74
79
  }
75
80
  }
76
81
 
@@ -98,7 +103,10 @@
98
103
  lineRenderer.positionCount = numSegments;
99
104
 
100
105
  // ReSharper disable once CompareOfFloatsByEqualityOperator
101
- float lineWidth = minLineWidth == maxLineWidth ? minLineWidth : PcgRandom.Instance.NextFloat(minLineWidth, maxLineWidth);
106
+ float lineWidth =
107
+ minLineWidth == maxLineWidth
108
+ ? minLineWidth
109
+ : PRNG.Instance.NextFloat(minLineWidth, maxLineWidth);
102
110
 
103
111
  lineRenderer.startWidth = lineWidth;
104
112
  lineRenderer.endWidth = lineWidth;
@@ -106,12 +114,16 @@
106
114
  float distanceMultiplier = _collider.radius;
107
115
 
108
116
  float angle = 360f / numSegments;
109
- float offsetRadians = PcgRandom.Instance.NextFloat(angle);
117
+ float offsetRadians = PRNG.Instance.NextFloat(angle);
110
118
  float currentOffset = offsetRadians;
111
119
  Vector3[] positions = new Vector3[numSegments];
112
120
  for (int i = 0; i < numSegments; ++i)
113
121
  {
114
- positions[i] = new Vector3(Mathf.Cos(Mathf.Deg2Rad * currentOffset), Mathf.Sin(Mathf.Deg2Rad * currentOffset)) * distanceMultiplier;
122
+ positions[i] =
123
+ new Vector3(
124
+ Mathf.Cos(Mathf.Deg2Rad * currentOffset),
125
+ Mathf.Sin(Mathf.Deg2Rad * currentOffset)
126
+ ) * distanceMultiplier;
115
127
  currentOffset += angle % 360f;
116
128
  }
117
129
 
@@ -8,9 +8,10 @@
8
8
  using NUnit.Framework;
9
9
  using Vector2 = UnityEngine.Vector2;
10
10
 
11
- public abstract class SpatialTreeTests<TTree> where TTree : ISpatialTree<Vector2>
11
+ public abstract class SpatialTreeTests<TTree>
12
+ where TTree : ISpatialTree<Vector2>
12
13
  {
13
- private IRandom Random => PcgRandom.Instance;
14
+ private IRandom Random => PRNG.Instance;
14
15
 
15
16
  protected abstract TTree CreateTree(IEnumerable<Vector2> points);
16
17
 
@@ -28,14 +29,18 @@
28
29
  do
29
30
  {
30
31
  point = Helpers.GetRandomPointInCircle(center, radius);
31
- }
32
- while (!points.Add(point));
32
+ } while (!points.Add(point));
33
33
  }
34
34
 
35
35
  TTree quadTree = CreateTree(points);
36
36
 
37
37
  List<Vector2> pointsInRange = quadTree.GetElementsInRange(center, radius).ToList();
38
- Assert.IsTrue(points.SetEquals(pointsInRange), "Found {0} points in range, expected {1}.", pointsInRange.Count, points.Count);
38
+ Assert.IsTrue(
39
+ points.SetEquals(pointsInRange),
40
+ "Found {0} points in range, expected {1}.",
41
+ pointsInRange.Count,
42
+ points.Count
43
+ );
39
44
  // Translate by a unit-square - there should be no points in this range
40
45
  Vector2 offset = center;
41
46
  offset.x -= radius * 2;
@@ -43,8 +48,14 @@
43
48
 
44
49
  pointsInRange = quadTree.GetElementsInRange(offset, radius).ToList();
45
50
  Assert.AreEqual(
46
- 0, pointsInRange.Count, "Found {0} points within {1} range of {2} (original center {3})",
47
- pointsInRange.Count, radius, offset, center);
51
+ 0,
52
+ pointsInRange.Count,
53
+ "Found {0} points within {1} range of {2} (original center {3})",
54
+ pointsInRange.Count,
55
+ radius,
56
+ offset,
57
+ center
58
+ );
48
59
  }
49
60
 
50
61
  [Test]
@@ -58,10 +69,19 @@
58
69
  List<Vector2> points = new(1) { testPoint };
59
70
 
60
71
  TTree quadTree = CreateTree(points);
61
- List<Vector2> pointsInRange = quadTree.GetElementsInRange(point, range * 0.99f).ToList();
72
+ List<Vector2> pointsInRange = quadTree
73
+ .GetElementsInRange(point, range * 0.99f)
74
+ .ToList();
62
75
  Assert.AreEqual(0, pointsInRange.Count);
63
76
  pointsInRange = quadTree.GetElementsInRange(point, range * 1.01f).ToList();
64
- Assert.AreEqual(1, pointsInRange.Count, "Failed to find point {0} from test point {1} with {2:0.00} range.", point, testPoint, range);
77
+ Assert.AreEqual(
78
+ 1,
79
+ pointsInRange.Count,
80
+ "Failed to find point {0} from test point {1} with {2:0.00} range.",
81
+ point,
82
+ testPoint,
83
+ range
84
+ );
65
85
  Assert.AreEqual(testPoint, pointsInRange[0]);
66
86
  }
67
87
 
@@ -95,7 +115,11 @@
95
115
  nearestNeighborCount = 16;
96
116
  quadTree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
97
117
  Assert.AreEqual(nearestNeighborCount, nearestNeighbors.Count);
98
- Assert.IsTrue(nearestNeighbors.All(neighbor => (neighbor - center).magnitude <= 5.6f), "Max: {0}", nearestNeighbors.Select(neighbor => (neighbor - center).magnitude).Max());
118
+ Assert.IsTrue(
119
+ nearestNeighbors.All(neighbor => (neighbor - center).magnitude <= 5.6f),
120
+ "Max: {0}",
121
+ nearestNeighbors.Select(neighbor => (neighbor - center).magnitude).Max()
122
+ );
99
123
 
100
124
  center = new Vector2(-100, -100);
101
125
 
@@ -0,0 +1,19 @@
1
+ namespace UnityHelpers.Tests.Tests.Runtime.Helper
2
+ {
3
+ using System.Linq;
4
+ using Core.Helper;
5
+ using Core.Random;
6
+ using NUnit.Framework;
7
+
8
+ public sealed class ArrayConverterTests
9
+ {
10
+ [Test]
11
+ public void IntToByteArray()
12
+ {
13
+ int[] ints = Enumerable.Range(0, 1000).Select(_ => PRNG.Instance.Next()).ToArray();
14
+ byte[] bytes = ArrayConverter.IntArrayToByteArrayBlockCopy(ints);
15
+ int[] decoded = ArrayConverter.ByteArrayToIntArrayBlockCopy(bytes);
16
+ Assert.IsTrue(ints.SequenceEqual(decoded));
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 35d3b0e1ba0e43bbb9d899a44b36812a
3
+ timeCreated: 1732492102
@@ -0,0 +1,221 @@
1
+ namespace UnityHelpers.Tests.Tests.Runtime.Helper
2
+ {
3
+ using Core.Extension;
4
+ using Core.Helper;
5
+ using Core.Random;
6
+ using NUnit.Framework;
7
+ using UnityEngine;
8
+
9
+ public sealed class WallMathTests
10
+ {
11
+ private const int TestIterations = 10_000;
12
+
13
+ [Test]
14
+ public void ApproximatelyRandom()
15
+ {
16
+ for (int i = 0; i < TestIterations; ++i)
17
+ {
18
+ float target = PRNG.Instance.NextFloat(-1_000, 1_000);
19
+ float delta = PRNG.Instance.NextFloat(0f, 10f);
20
+
21
+ float insideOffset = delta * PRNG.Instance.NextFloat();
22
+ if (PRNG.Instance.NextBool())
23
+ {
24
+ insideOffset *= -1;
25
+ }
26
+
27
+ float inside = target + insideOffset;
28
+ Assert.IsTrue(
29
+ target.Approximately(inside, delta),
30
+ $"Target {target} is not close enough to {inside} with a delta of {delta}."
31
+ );
32
+
33
+ float outsideOffset = delta * PRNG.Instance.NextFloat(1.001f, 10f);
34
+ if (PRNG.Instance.NextBool())
35
+ {
36
+ outsideOffset *= -1;
37
+ }
38
+
39
+ float outside = target + outsideOffset;
40
+ Assert.IsFalse(
41
+ target.Approximately(outside, delta),
42
+ $"Target {target} was unexpectedly close to {outside} with a delta of {delta}."
43
+ );
44
+ }
45
+ }
46
+
47
+ [Test]
48
+ public void ApproximatelyExpected()
49
+ {
50
+ Assert.IsTrue(0f.Approximately(0f, 0f));
51
+ Assert.IsTrue(0f.Approximately(0.5f, 1f));
52
+ Assert.IsFalse(0.001f.Approximately(0f, 0f));
53
+ Assert.IsFalse(100f.Approximately(5f, 2.4f));
54
+
55
+ Assert.IsTrue(0.001f.Approximately(0.0001f));
56
+ }
57
+
58
+ [Test]
59
+ public void ClampRectInsideRandom()
60
+ {
61
+ for (int i = 0; i < TestIterations; ++i)
62
+ {
63
+ Vector2 position = PRNG.Instance.NextVector2InRange(10f);
64
+
65
+ Vector2 size;
66
+ do
67
+ {
68
+ size = PRNG.Instance.NextVector2InRange(10f);
69
+ } while (size.x < 0 || size.y < 0);
70
+
71
+ Rect rect = new(position, size);
72
+
73
+ Vector2 inside = new(
74
+ PRNG.Instance.NextFloat(rect.min.x, rect.max.x),
75
+ PRNG.Instance.NextFloat(rect.min.y, rect.max.y)
76
+ );
77
+ Assert.IsTrue(rect.Contains(inside), $"Rect {rect} does not contain {inside}.");
78
+ Vector2 clamped = rect.Clamp(inside);
79
+ Assert.AreEqual(inside, clamped);
80
+ }
81
+ }
82
+
83
+ [Test]
84
+ public void ClampRectInsideExpected()
85
+ {
86
+ Rect rect = new Rect(Vector2.one, Vector2.one);
87
+ Vector2 clamped = rect.Clamp(rect.center);
88
+ Assert.AreEqual(rect.center, clamped);
89
+
90
+ Vector2 inside =
91
+ rect.center
92
+ + new Vector2(PRNG.Instance.NextFloat(0.1f), PRNG.Instance.NextFloat(0.1f));
93
+ clamped = rect.Clamp(inside);
94
+ Assert.AreEqual(
95
+ inside,
96
+ clamped,
97
+ $"Expected inside point {inside} unexpectedly was clamped to {clamped} for rect {rect}."
98
+ );
99
+ }
100
+
101
+ [Test]
102
+ public void ClampRectOutsideRandom()
103
+ {
104
+ Rect rect = new(Vector2.one, Vector2.one);
105
+ for (int i = 0; i < TestIterations; ++i)
106
+ {
107
+ Vector2 outside;
108
+ do
109
+ {
110
+ outside = PRNG.Instance.NextVector2InRange(10f, rect.center);
111
+ } while (rect.Contains(outside));
112
+
113
+ Vector2 originalOutside = outside;
114
+ Vector2 clamped = rect.Clamp(ref outside);
115
+ Debug.Log($"Clamped {originalOutside} to {clamped}.");
116
+ Assert.AreNotEqual(originalOutside, clamped);
117
+ Assert.AreEqual(clamped, outside);
118
+
119
+ // Shrink just a little to ensure within bounds, clamping will put it right on the edge
120
+ const float shrinkScaleToAccountForPrecision = 0.99f;
121
+ Vector2 delta = rect.center - clamped;
122
+ delta *= shrinkScaleToAccountForPrecision;
123
+ clamped = rect.center + delta;
124
+ Assert.IsTrue(
125
+ rect.Contains(clamped),
126
+ $"Expected rect {rect} to contain {clamped}, but it did not."
127
+ );
128
+ }
129
+ }
130
+
131
+ [Test]
132
+ public void ClampRectOutsideExpected()
133
+ {
134
+ Rect rect = new(Vector2.one, Vector2.one);
135
+ Vector2 outside = new(rect.center.x, 100f);
136
+ Vector2 clamped = rect.Clamp(ref outside);
137
+ Assert.AreEqual(clamped, outside);
138
+ Assert.AreEqual(new Vector2(rect.center.x, 2f), clamped);
139
+
140
+ // Shrink just a little to ensure within bounds, clamping will put it right on the edge
141
+ const float shrinkScaleToAccountForPrecision = 0.99f;
142
+ Vector2 delta = rect.center - clamped;
143
+ delta *= shrinkScaleToAccountForPrecision;
144
+ clamped = rect.center + delta;
145
+ Assert.IsTrue(
146
+ rect.Contains(clamped),
147
+ $"Expected rect to contain {clamped}, but it did not."
148
+ );
149
+ }
150
+
151
+ [Test]
152
+ public void ClampFloat()
153
+ {
154
+ float value = 1.25f;
155
+ float clamped = value.Clamp(0, 1);
156
+ Assert.AreEqual(1, clamped);
157
+ Assert.AreEqual(1.25f, value);
158
+
159
+ value = 0.5f;
160
+ clamped = value.Clamp(0, 1);
161
+ Assert.AreEqual(0.5f, clamped);
162
+ Assert.AreEqual(0.5f, value);
163
+ }
164
+
165
+ [Test]
166
+ public void WrappedIncrementWrapped()
167
+ {
168
+ int ten = 10;
169
+ int one = ten.WrappedIncrement(10);
170
+ Assert.AreEqual(1, one);
171
+ Assert.AreEqual(10, ten);
172
+
173
+ int toChangeToOne = 10;
174
+ int returned = WallMath.WrappedIncrement(ref toChangeToOne, 10);
175
+ Assert.AreEqual(1, returned);
176
+ Assert.AreEqual(1, toChangeToOne);
177
+ }
178
+
179
+ [Test]
180
+ public void WrappedIncrementUnwrapped()
181
+ {
182
+ int ten = 10;
183
+ int eleven = ten.WrappedIncrement(100);
184
+ Assert.AreEqual(11, eleven);
185
+ Assert.AreEqual(10, ten);
186
+
187
+ int toChangeToEleven = 10;
188
+ int returned = WallMath.WrappedIncrement(ref toChangeToEleven, 100);
189
+ Assert.AreEqual(11, returned);
190
+ Assert.AreEqual(11, toChangeToEleven);
191
+ }
192
+
193
+ [Test]
194
+ public void WrappedAddWrapped()
195
+ {
196
+ int ten = 10;
197
+ int two = ten.WrappedAdd(2, 10);
198
+ Assert.AreEqual(10, ten);
199
+ Assert.AreEqual(2, two);
200
+
201
+ int toChangeToTwo = 10;
202
+ int returned = WallMath.WrappedAdd(ref toChangeToTwo, 2, 10);
203
+ Assert.AreEqual(2, returned);
204
+ Assert.AreEqual(2, toChangeToTwo);
205
+ }
206
+
207
+ [Test]
208
+ public void WrappedAddUnwrapped()
209
+ {
210
+ int ten = 10;
211
+ int twelve = ten.WrappedAdd(2, 100);
212
+ Assert.AreEqual(10, ten);
213
+ Assert.AreEqual(12, twelve);
214
+
215
+ int toChangeToTwelve = 10;
216
+ int returned = WallMath.WrappedAdd(ref toChangeToTwelve, 2, 100);
217
+ Assert.AreEqual(12, returned);
218
+ Assert.AreEqual(12, toChangeToTwelve);
219
+ }
220
+ }
221
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 11e0e2d4098d47159de89eff698b2510
3
+ timeCreated: 1732563313
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: d1cf06665d214b7aa507e42eb1fdfbe4
3
+ timeCreated: 1732492095
@@ -1,17 +1,18 @@
1
1
  namespace UnityHelpers.Tests.Performance
2
2
  {
3
- using NUnit.Framework;
4
- using System.Collections.Generic;
5
- using System.Diagnostics;
6
3
  using System;
7
4
  using System.Collections;
5
+ using System.Collections.Generic;
6
+ using System.Diagnostics;
8
7
  using System.Linq;
9
8
  using System.Threading.Tasks;
10
- using UnityEngine;
11
9
  using Core.DataStructure;
10
+ using NUnit.Framework;
11
+ using UnityEngine;
12
12
  using UnityEngine.TestTools;
13
13
 
14
- public abstract class SpatialTreePerformanceTest<TTree> where TTree : ISpatialTree<Vector2>
14
+ public abstract class SpatialTreePerformanceTest<TTree>
15
+ where TTree : ISpatialTree<Vector2>
15
16
  {
16
17
  protected abstract TTree CreateTree(IEnumerable<Vector2> points);
17
18
 
@@ -23,11 +24,15 @@
23
24
 
24
25
  Vector2[] points = new Vector2[1_000_000];
25
26
  float radius = 500;
26
- ParallelLoopResult result = Parallel.For(0, 1_000_000, index =>
27
- {
28
- // ReSharper disable once PossibleLossOfFraction
29
- points[index] = new Vector2(index % 1_000, index / 1_000);
30
- });
27
+ ParallelLoopResult result = Parallel.For(
28
+ 0,
29
+ 1_000_000,
30
+ index =>
31
+ {
32
+ // ReSharper disable once PossibleLossOfFraction
33
+ points[index] = new Vector2(index % 1_000, index / 1_000);
34
+ }
35
+ );
31
36
 
32
37
  while (!result.IsCompleted)
33
38
  {
@@ -37,7 +42,9 @@
37
42
  Stopwatch timer = Stopwatch.StartNew();
38
43
  TTree tree = CreateTree(points);
39
44
  timer.Stop();
40
- UnityEngine.Debug.Log($"| Construction (1 million points) | {(int)Math.Floor(1 / timer.Elapsed.TotalSeconds)} ({timer.Elapsed.TotalSeconds} total) |");
45
+ UnityEngine.Debug.Log(
46
+ $"| Construction (1 million points) | {(int)Math.Floor(1 / timer.Elapsed.TotalSeconds)} ({timer.Elapsed.TotalSeconds} seconds total) |"
47
+ );
41
48
  Vector2 center = tree.Boundary.center;
42
49
  int count = 0;
43
50
  TimeSpan timeout = TimeSpan.FromSeconds(1);
@@ -47,10 +54,11 @@
47
54
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
48
55
  Assert.AreEqual(785456, elementsInRange);
49
56
  ++count;
50
- }
51
- while (timer.Elapsed < timeout);
57
+ } while (timer.Elapsed < timeout);
52
58
 
53
- UnityEngine.Debug.Log($"| Elements In Range - Full | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
59
+ UnityEngine.Debug.Log(
60
+ $"| Elements In Range - Full | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
61
+ );
54
62
 
55
63
  radius /= 2f;
56
64
  count = 0;
@@ -60,9 +68,10 @@
60
68
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
61
69
  Assert.AreEqual(196364, elementsInRange);
62
70
  ++count;
63
- }
64
- while (timer.Elapsed < timeout);
65
- UnityEngine.Debug.Log($"| Elements In Range - Half | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
71
+ } while (timer.Elapsed < timeout);
72
+ UnityEngine.Debug.Log(
73
+ $"| Elements In Range - Half | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
74
+ );
66
75
 
67
76
  radius /= 2f;
68
77
  count = 0;
@@ -72,9 +81,10 @@
72
81
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
73
82
  Assert.AreEqual(49080, elementsInRange);
74
83
  ++count;
75
- }
76
- while (timer.Elapsed < timeout);
77
- UnityEngine.Debug.Log($"| Elements In Range - Quarter | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
84
+ } while (timer.Elapsed < timeout);
85
+ UnityEngine.Debug.Log(
86
+ $"| Elements In Range - Quarter | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
87
+ );
78
88
 
79
89
  radius = 1f;
80
90
  count = 0;
@@ -84,9 +94,10 @@
84
94
  int elementsInRange = tree.GetElementsInRange(center, radius).Count();
85
95
  Assert.AreEqual(4, elementsInRange);
86
96
  ++count;
87
- }
88
- while (timer.Elapsed < timeout);
89
- UnityEngine.Debug.Log($"| Elements In Range - 1 Range | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
97
+ } while (timer.Elapsed < timeout);
98
+ UnityEngine.Debug.Log(
99
+ $"| Elements In Range - 1 Range | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
100
+ );
90
101
 
91
102
  int nearestNeighborCount = 500;
92
103
  List<Vector2> nearestNeighbors = new();
@@ -97,9 +108,10 @@
97
108
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
98
109
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
99
110
  ++count;
100
- }
101
- while (timer.Elapsed < timeout);
102
- UnityEngine.Debug.Log($"| ANN - 500 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
111
+ } while (timer.Elapsed < timeout);
112
+ UnityEngine.Debug.Log(
113
+ $"| ANN - 500 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
114
+ );
103
115
 
104
116
  nearestNeighborCount = 100;
105
117
  count = 0;
@@ -109,9 +121,10 @@
109
121
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
110
122
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
111
123
  ++count;
112
- }
113
- while (timer.Elapsed < timeout);
114
- UnityEngine.Debug.Log($"| ANN - 100 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
124
+ } while (timer.Elapsed < timeout);
125
+ UnityEngine.Debug.Log(
126
+ $"| ANN - 100 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
127
+ );
115
128
 
116
129
  nearestNeighborCount = 10;
117
130
  count = 0;
@@ -121,9 +134,10 @@
121
134
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
122
135
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
123
136
  ++count;
124
- }
125
- while (timer.Elapsed < timeout);
126
- UnityEngine.Debug.Log($"| ANN - 10 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
137
+ } while (timer.Elapsed < timeout);
138
+ UnityEngine.Debug.Log(
139
+ $"| ANN - 10 | {(int)Math.Floor(count / timeout.TotalSeconds)} |"
140
+ );
127
141
 
128
142
  nearestNeighborCount = 1;
129
143
  count = 0;
@@ -133,8 +147,7 @@
133
147
  tree.GetApproximateNearestNeighbors(center, nearestNeighborCount, nearestNeighbors);
134
148
  Assert.IsTrue(nearestNeighbors.Count <= nearestNeighborCount);
135
149
  ++count;
136
- }
137
- while (timer.Elapsed < timeout);
150
+ } while (timer.Elapsed < timeout);
138
151
  UnityEngine.Debug.Log($"| ANN - 1 | {(int)Math.Floor(count / timeout.TotalSeconds)} |");
139
152
  }
140
153
  }