com.wallstop-studios.unity-helpers 2.0.0-rc15 → 2.0.0-rc17
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.
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Extension
|
|
2
2
|
{
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using System.Linq;
|
|
3
6
|
using Helper;
|
|
4
7
|
using Random;
|
|
5
8
|
using UnityEngine;
|
|
@@ -11,6 +14,61 @@
|
|
|
11
14
|
return random.NextVector2(-amplitude, amplitude);
|
|
12
15
|
}
|
|
13
16
|
|
|
17
|
+
public static T NextEnumExcept<T>(this IRandom random, params T[] exceptions)
|
|
18
|
+
where T : struct, Enum
|
|
19
|
+
{
|
|
20
|
+
T value;
|
|
21
|
+
do
|
|
22
|
+
{
|
|
23
|
+
value = random.NextEnum<T>();
|
|
24
|
+
} while (0 <= Array.IndexOf(exceptions, value));
|
|
25
|
+
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public static T NextOfExcept<T>(
|
|
30
|
+
this IRandom random,
|
|
31
|
+
IEnumerable<T> values,
|
|
32
|
+
params T[] exceptions
|
|
33
|
+
)
|
|
34
|
+
{
|
|
35
|
+
T value;
|
|
36
|
+
|
|
37
|
+
switch (values)
|
|
38
|
+
{
|
|
39
|
+
case IReadOnlyList<T> list:
|
|
40
|
+
{
|
|
41
|
+
do
|
|
42
|
+
{
|
|
43
|
+
value = random.NextOf(list);
|
|
44
|
+
} while (0 <= Array.IndexOf(exceptions, value));
|
|
45
|
+
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case IReadOnlyCollection<T> collection:
|
|
49
|
+
{
|
|
50
|
+
do
|
|
51
|
+
{
|
|
52
|
+
value = random.NextOf(collection);
|
|
53
|
+
} while (0 <= Array.IndexOf(exceptions, value));
|
|
54
|
+
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
{
|
|
59
|
+
T[] input = values.ToArray();
|
|
60
|
+
do
|
|
61
|
+
{
|
|
62
|
+
value = random.NextOf(input);
|
|
63
|
+
} while (0 <= Array.IndexOf(exceptions, value));
|
|
64
|
+
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
|
|
14
72
|
public static Vector2 NextVector2(
|
|
15
73
|
this IRandom random,
|
|
16
74
|
float minAmplitude,
|
|
@@ -417,16 +417,40 @@
|
|
|
417
417
|
|
|
418
418
|
public Guid NextGuid()
|
|
419
419
|
{
|
|
420
|
-
|
|
421
|
-
NextBytes(guidBytes);
|
|
422
|
-
return new Guid(guidBytes);
|
|
420
|
+
return new Guid(GenerateGuidBytes());
|
|
423
421
|
}
|
|
424
422
|
|
|
425
423
|
public KGuid NextKGuid()
|
|
424
|
+
{
|
|
425
|
+
return new KGuid(GenerateGuidBytes());
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
private byte[] GenerateGuidBytes()
|
|
426
429
|
{
|
|
427
430
|
byte[] guidBytes = new byte[16];
|
|
428
431
|
NextBytes(guidBytes);
|
|
429
|
-
|
|
432
|
+
SetUuidV4Bits(guidBytes);
|
|
433
|
+
return guidBytes;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
public static void SetUuidV4Bits(byte[] bytes)
|
|
437
|
+
{
|
|
438
|
+
// Set version to 4 (bits 6-7 of byte 6)
|
|
439
|
+
|
|
440
|
+
// Clear the version bits first (clear bits 4-7)
|
|
441
|
+
byte value = bytes[6];
|
|
442
|
+
value &= 0x0f;
|
|
443
|
+
// Set version 4 (set bits 4-7 to 0100)
|
|
444
|
+
value |= 0x40;
|
|
445
|
+
bytes[6] = value;
|
|
446
|
+
|
|
447
|
+
// Set variant to RFC 4122 (bits 6-7 of byte 8)
|
|
448
|
+
value = bytes[8];
|
|
449
|
+
// Clear the variant bits first (clear bits 6-7)
|
|
450
|
+
value &= 0x3f;
|
|
451
|
+
// Set RFC 4122 variant (set bits 6-7 to 10)
|
|
452
|
+
value |= 0x80;
|
|
453
|
+
bytes[8] = value;
|
|
430
454
|
}
|
|
431
455
|
|
|
432
456
|
// Advances the RNG
|
|
@@ -549,6 +549,26 @@
|
|
|
549
549
|
}
|
|
550
550
|
}
|
|
551
551
|
|
|
552
|
+
[Test]
|
|
553
|
+
[Parallelizable]
|
|
554
|
+
public void NextEnumerableExcept()
|
|
555
|
+
{
|
|
556
|
+
IRandom random = NewRandom();
|
|
557
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
558
|
+
{
|
|
559
|
+
TestValues exception = random.NextEnum<TestValues>();
|
|
560
|
+
HashSet<TestValues> selected = Enum.GetValues(typeof(TestValues))
|
|
561
|
+
.OfType<TestValues>()
|
|
562
|
+
.Shuffled(random)
|
|
563
|
+
.Skip(3)
|
|
564
|
+
.ToHashSet();
|
|
565
|
+
|
|
566
|
+
TestValues value = random.NextOfExcept(selected.Shuffled(random), exception);
|
|
567
|
+
Assert.IsTrue(selected.Contains(value));
|
|
568
|
+
Assert.AreNotEqual(value, exception);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
552
572
|
[Test]
|
|
553
573
|
[Parallelizable]
|
|
554
574
|
public void NextArray()
|
|
@@ -636,6 +656,23 @@
|
|
|
636
656
|
Assert.AreEqual(Enum.GetValues(typeof(TestValues)).Length, seenEnums.Count);
|
|
637
657
|
}
|
|
638
658
|
|
|
659
|
+
[Test]
|
|
660
|
+
[Parallelizable]
|
|
661
|
+
public void NextEnumExcept()
|
|
662
|
+
{
|
|
663
|
+
IRandom random = NewRandom();
|
|
664
|
+
HashSet<TestValues> seenEnums = new();
|
|
665
|
+
for (int i = 0; i < NormalIterations; ++i)
|
|
666
|
+
{
|
|
667
|
+
TestValues value = random.NextEnumExcept(TestValues.Value8, TestValues.Value9);
|
|
668
|
+
_ = seenEnums.Add(value);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
Assert.AreEqual(Enum.GetValues(typeof(TestValues)).Length - 2, seenEnums.Count);
|
|
672
|
+
Assert.IsFalse(seenEnums.Contains(TestValues.Value8));
|
|
673
|
+
Assert.IsFalse(seenEnums.Contains(TestValues.Value9));
|
|
674
|
+
}
|
|
675
|
+
|
|
639
676
|
[Test]
|
|
640
677
|
[Parallelizable]
|
|
641
678
|
public void NextNoiseMap()
|