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
using System; | |
/// <summary> | |
/// A pair of 32-bit hash functions with no collisions on all 32-bit inputs | |
/// </summary> | |
/// <note>Ported from https://github.com/skeeto/hash-prospector/blob/396dbe235c94dfc2e9b559fc965bcfda8b6a122c/README.md?plain=1#L237</note> | |
static class PerfectHash | |
{ | |
/// A perfect hash function for 32-bit integers. No collisions for all 32-bit values. The inverse of <see cref="Hash2"/>. | |
/// <param name="x">The integer to hash</param> |
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
using System; | |
using System.IO; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
using System.Reflection.Metadata; | |
using System.Reflection.Metadata.Ecma335; | |
using System.Reflection.PortableExecutable; | |
var assemblyPath = "test.dll"; | |
//CreateAndSaveAssembly(assemblyPath); |
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
public interface IRented<T> : IDisposable | |
{ | |
Memory<T> Memory { get; } | |
Span<T> Span { get; } | |
} | |
public static class ArrayPoolExtensions | |
{ | |
public static IRented<T> GetRented<T>(this ArrayPool<T> pool, int minimumLength, bool clearOnReturn = false) | |
{ |
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
using System.Buffers.Text; | |
using System.Text.Json; | |
using System.Text.Json.Serialization; | |
var text = "Hello, World! How are ya? Good! That's swell!"u8; | |
{ | |
using var ms = new MemoryStream(text.ToArray()); | |
var foo = new Foo("text", ms); | |
using var fs = File.Create("yes.txt", 4096, FileOptions.WriteThrough); | |
JsonSerializerOptions jso = new() { Converters = { new StreamToBase64WriteOnlyJsonConverter(fs) }}; |
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
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | |
public class TestCaseAttribute<T>(params Object?[]? arguments) : TestCaseAttribute(arguments), ITestBuilder | |
{ | |
IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test? suite) | |
{ | |
if (!method.IsGenericMethodDefinition) | |
return base.BuildFrom(method, suite); | |
var length = method.GetGenericArguments().Length; | |
if (length != 1) |
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
using System; | |
public readonly struct PrimeQuadraticResiduePermuter32(UInt32 seed) | |
{ | |
public UInt32 this[UInt32 x] => Permute(Permute(unchecked(x + seed)) ^ Xor); | |
private const UInt32 Xor = 0x5bf03635; | |
private static UInt32 Permute(UInt32 x) | |
{ |
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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
/// <summary> | |
/// A simple FIFO async lock. It does not support recursion/reentrancy. | |
/// </summary> | |
public sealed class AsyncLock | |
{ | |
private Task task = Task.CompletedTask; |
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
public sealed class OnceThenOther<T>(T once, T other) | |
{ | |
private Int32 flag = 0; | |
// Checking the flag value normally before exchanging it atomically is an optimization with two parts: | |
// A) Contentious first access will all synchronize on the atomic exchange | |
// B) Subsequent accesses will see the local cached value of `1`, so they won't have to perform the heavier atomic exchange | |
public T Value => flag == 0 && Interlocked.Exchange(ref flag, 1) == 0 ? once : other; | |
} |
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net8.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<WarningsAsErrors>Nullable</WarningsAsErrors> | |
</PropertyGroup> |
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
public static class InterfaceImplementor |
NewerOlder