Skip to content

Commit

Permalink
Build a custom route matching engine because I'm just not tired enoug…
Browse files Browse the repository at this point in the history
…h by this massive rewrite
  • Loading branch information
lilith committed Feb 3, 2024
1 parent 22cdf44 commit d78e0f0
Show file tree
Hide file tree
Showing 11 changed files with 2,091 additions and 70 deletions.
78 changes: 78 additions & 0 deletions src/Imazen.Abstractions/Internal/Fnv1AHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace Imazen.Abstractions.Internal;

internal struct Fnv1AHash
{
private const ulong FnvPrime = 0x00000100000001B3;
private const ulong FnvOffsetBasis = 0xCBF29CE484222325;
private ulong hash;
public ulong CurrentHash => hash;

public static Fnv1AHash Create()
{
return new Fnv1AHash(){
hash = FnvOffsetBasis
};
}

private void HashInternal(ReadOnlySpan<byte> array)
{
foreach (var t in array)
{
unchecked
{
hash ^= t;
hash *= FnvPrime;
}
}
}
private void HashInternal(ReadOnlySpan<char> array)
{
foreach (var t in array)
{
unchecked
{
hash ^= (byte)(t & 0xFF);
hash *= FnvPrime;
hash ^= (byte)((t >> 8) & 0xFF);
hash *= FnvPrime;
}
}
}
public void Add(string? s)
{
if (s == null) return;
HashInternal(s.AsSpan());
}
public void Add(ReadOnlySpan<char> s)
{
HashInternal(s);
}

public void Add(byte[] array)
{
HashInternal(array);
}
public void Add(int i)
{
unchecked
{
byte b1 = (byte) (i & 0xFF);
byte b2 = (byte) ((i >> 8) & 0xFF);
byte b3 = (byte) ((i >> 16) & 0xFF);
byte b4 = (byte) ((i >> 24) & 0xFF);
hash ^= b1;
hash *= FnvPrime;
hash ^= b2;
hash *= FnvPrime;
hash ^= b3;
hash *= FnvPrime;
hash ^= b4;
}
}
public void Add(long val)
{
Add((int)(val & 0xFFFFFFFF));
Add((int) (val >> 32));
}

}
71 changes: 1 addition & 70 deletions src/Imazen.Abstractions/Logging/ReLogStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Text;
using Imazen.Abstractions.Internal;
using Microsoft.Extensions.Logging;

namespace Imazen.Abstractions.Logging
Expand Down Expand Up @@ -177,74 +178,4 @@ public void Format(StringBuilder sb, int totalCount, bool relativeTime)

}
}

internal struct Fnv1AHash
{
private const ulong FnvPrime = 0x00000100000001B3;
private const ulong FnvOffsetBasis = 0xCBF29CE484222325;
private ulong hash;
public ulong CurrentHash => hash;

public static Fnv1AHash Create()
{
return new Fnv1AHash(){
hash = FnvOffsetBasis
};
}

private void HashInternal(IReadOnlyList<byte> array, int ibStart, int cbSize)
{
for (var i = ibStart; i < cbSize; i++)
{
unchecked
{
hash ^= array[i];
hash *= FnvPrime;
}
}
}

public void Add(string? s)
{
if (s == null) return;
foreach (var c in s)
{
unchecked
{
hash ^= (byte)(c & 0xFF);
hash *= FnvPrime;
hash ^= (byte)((c >> 8) & 0xFF);
hash *= FnvPrime;
}
}
}

public void Add(byte[] array)
{
HashInternal(array, 0, array.Length);
}
public void Add(int i)
{
unchecked
{
byte b1 = (byte) (i & 0xFF);
byte b2 = (byte) ((i >> 8) & 0xFF);
byte b3 = (byte) ((i >> 16) & 0xFF);
byte b4 = (byte) ((i >> 24) & 0xFF);
hash ^= b1;
hash *= FnvPrime;
hash ^= b2;
hash *= FnvPrime;
hash ^= b3;
hash *= FnvPrime;
hash ^= b4;
}
}
public void Add(long val)
{
Add((int)(val & 0xFFFFFFFF));
Add((int) (val >> 32));
}

}
}
4 changes: 4 additions & 0 deletions src/Imazen.Routing/Imazen.Routing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Supernova.Enum.Generators" Version="1.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.*" />
<PackageReference Include="Imageflow.Net" Version="0.11.2" />
<PackageReference Include="System.Collections.Immutable" Version="6.*" />
Expand Down
Loading

0 comments on commit d78e0f0

Please sign in to comment.