-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding V8 * Fixing batch generation issues * Adding UUID Max * Unified slice for bytes to uuids * Adding or extending tests
- Loading branch information
Showing
18 changed files
with
232 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
public static partial class Convert { | ||
/// <summary>Slices the given bytes into UUIDs</summary> | ||
/// <param name="bytes">The bytes to slice</param> | ||
/// <param name="version">The version to stamp in the UUID</param> | ||
/// <param name="variant">The variant to stamp in the UUID</param> | ||
/// <returns>A collection of <see cref="UUID"/></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] | ||
public static UUID[] Slice(ReadOnlySpan<Byte> bytes, Version version, Variant variant) { | ||
Vector128<Byte> mask = Format.VersionVariantMaskNot(version, variant); | ||
Vector128<Byte> overlay = Format.VersionVariantOverlayer(version, variant); | ||
|
||
return Slice(bytes, mask, overlay); | ||
} | ||
|
||
/// <summary>Slices the given bytes into UUIDs</summary> | ||
/// <param name="bytes">The bytes to slice</param> | ||
/// <param name="mask">The mask to apply</param> | ||
/// <param name="overlay">The overlay to add to the <see cref="UUID"/> usually version and variant</param> | ||
/// <returns>A collection of <see cref="UUID"/></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] | ||
public static UUID[] Slice(ReadOnlySpan<Byte> bytes, Vector128<Byte> mask, Vector128<Byte> overlay) { | ||
Int32 count = bytes.Length; | ||
Int32 amount = count / Format.UUID_BYTE_LENGTH; | ||
Int32 max = count - Format.UUID_BYTE_LENGTH; | ||
var uuids = new UUID[amount]; | ||
|
||
Int32 J = 0; | ||
for (Int32 I = 0; I <= max; I += Format.UUID_BYTE_LENGTH) { | ||
var data = Vector128.Create(bytes.Slice(I, Format.UUID_BYTE_LENGTH)); | ||
Vector128<Byte> uuid = Format.StampVersion(mask, overlay, data); | ||
uuids[J++] = new UUID(uuid); | ||
} | ||
|
||
return uuids; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
public static partial class Format { | ||
/// <summary>Uses the given mask and overlay to insert the version and variant onto the data</summary> | ||
/// <param name="versionMask">The mask of the versions and variants</param> | ||
/// <param name="versionOverlay">The overlay of the versions and variants</param> | ||
/// <param name="data">The data to stamp the version and variant onto</param> | ||
/// <returns>Returns a <see cref="Vector128{T}"/></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] | ||
public static Vector128<Byte> StampVersion(Vector128<Byte> versionMask, Vector128<Byte> versionOverlay, ReadOnlySpan<Byte> data) { | ||
var temp = Vector128.Create(data); | ||
return StampVersion(versionMask, versionOverlay, temp); | ||
} | ||
|
||
/// <inheritdoc cref="StampVersion(Vector128{Byte},Vector128{Byte},ReadOnlySpan{Byte})"/> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] | ||
public static Vector128<Byte> StampVersion(Vector128<Byte> versionMask, Vector128<Byte> versionOverlay, Vector128<Byte> data) { | ||
var result = Vector128.BitwiseAnd(data, versionMask); | ||
result = Vector128.BitwiseOr(result, versionOverlay); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
public static partial class V8 { | ||
/// <summary>Turns the entire bytes collection into UUIDs</summary> | ||
/// <param name="bytes">The byte to chunk into <see cref="UUID"/></param> | ||
/// <returns>A collection of <see cref="UUID"/></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveOptimization)] | ||
public static UUID[] Batch(ReadOnlySpan<Byte> bytes) { | ||
return Convert.Slice(bytes, _VersionMask, _VersionOverlay); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Runtime.Intrinsics; | ||
using System.Security.Cryptography; | ||
|
||
namespace DaanV2.UUID; | ||
public static partial class V8 { | ||
/// <inheritdoc cref="V1.Version"/> | ||
public const Version Version = DaanV2.UUID.Version.V8; | ||
/// <inheritdoc cref="V1.Variant"/> | ||
public const Variant Variant = DaanV2.UUID.Variant.V1; | ||
|
||
private static readonly Vector128<Byte> _VersionMask = Format.VersionVariantMaskNot(V8.Version, V8.Variant); | ||
private static readonly Vector128<Byte> _VersionOverlay = Format.VersionVariantOverlayer(V8.Version, V8.Variant); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Runtime.Intrinsics; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
public static partial class V8 { | ||
/// <summary>Extracts the three given data set that UUID 8 holds</summary> | ||
/// <param name="uuid">The UUID to extract the data from</param> | ||
/// <returns>48 bits of data, 12 bits of data, and 62 bits data</returns> | ||
public static (UInt64 bits48, UInt16 bits12, UInt64 bits62) Extract(UUID uuid) { | ||
Vector128<UInt64> d = uuid._Data.AsUInt64(); | ||
|
||
UInt64 dataA = d.GetElement(0); | ||
UInt64 dataC = d.GetElement(1); | ||
// Lower 12 bits of the first 64 bits | ||
UInt16 dataB = (UInt16)(dataA & 0b1111_1111_1111); | ||
|
||
// Remove the lower 16 bits, so we have 48 bits of data | ||
dataA >>= 16; | ||
|
||
// Remove top 2 bits, so we have 62 bits of data | ||
const UInt64 mask = 0b11u << 62; | ||
dataC &= mask; | ||
|
||
return (dataA, dataB, dataC); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
|
||
|
||
|
||
public static partial class V8 { | ||
/// <summary>Creates a <see cref="UUID"/> of the data around the version and variant, the provided data will be placed around the version and variant</summary> | ||
/// <param name="dataA">48 bits of data, top 16 will be removed</param> | ||
/// <param name="dataB">12 bits of data, top 4 will be removed</param> | ||
/// <param name="dataC">62 bits of data, top 2 will be removed</param> | ||
/// <returns>A new <see cref="UUID"/></returns> | ||
public static UUID Generate(UInt64 dataA, UInt16 dataB, UInt64 dataC) { | ||
Vector128<Byte> u = Format.Create(V8.Version, V8.Variant, dataA, dataB, dataC); | ||
return new UUID(u); | ||
} | ||
|
||
|
||
/// <summary>Generates a <see cref="UUID"/> from the given data, will override the necessary bits to write the version and variant</summary> | ||
/// <param name="source">The data to create a <see cref="UUID"/> from</param> | ||
/// <returns>A new <see cref="UUID"/></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] | ||
public static UUID Generate(ReadOnlySpan<Byte> source) { | ||
Vector128<Byte> u = Format.Create(V8.Version, V8.Variant, source); | ||
return new UUID(u); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DaanV2.UUID; | ||
|
||
/// <summary>The <see cref="UUID"/> version that is based on given data of 122 bits</summary> | ||
public static partial class V8 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using DaanV2.UUID; | ||
|
||
namespace Tests.Generation; | ||
public sealed partial class V8Tests { | ||
[Theory(DisplayName = "When batch generating, will always return unique UUIDs")] | ||
[InlineData(10)] | ||
[InlineData(100)] | ||
[InlineData(1000)] | ||
public void TestBatchUnique(Int32 Amount) { | ||
Int32 byteCount = Amount * UUID.BYTE_LENGTH; | ||
Byte[] data = new Byte[byteCount]; | ||
Random.Shared.NextBytes(data); | ||
|
||
UUID[] UUIDs = V8.Batch(data); | ||
Assert.Equal(Amount, UUIDs.Length); | ||
|
||
for (Int32 i = 0; i < UUIDs.Length; i++) { | ||
Utility.ValidateUUID(UUIDs[i], V8.Version, V8.Variant); | ||
|
||
for (Int32 j = 0; j < UUIDs.Length; j++) { | ||
if (i == j) { | ||
continue; | ||
} | ||
|
||
Assert.NotEqual(UUIDs[i], UUIDs[j]); | ||
} | ||
} | ||
} | ||
} |