Skip to content

Instantly share code, notes, and snippets.

@Ruzzie
Ruzzie / DateTimeOffsetUnixTimestampMillisJsonConverter.cs
Created September 7, 2023 05:29
System.Text.Json DateTimeOffset Converter for UnixTimestampMillis
public class DateTimeOffsetUnixTimestampMillisJsonConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset Read(
ref Utf8JsonReader reader
, Type typeToConvert
, JsonSerializerOptions options)
{
return DateTimeOffset.FromUnixTimeMilliseconds(reader.GetInt64());
}
@Ruzzie
Ruzzie / QueueBufferAlt.cs
Created July 1, 2023 06:29
Lock free (not wait free) MPSC Queue single writes, batch reads
using System.Buffers;
using System.Runtime.CompilerServices;
namespace Ruzzie.Common.Collections;
/// <summary>
/// A Buffer (queue) that supports
/// multiple concurrent (lock-free, not wait free (spinning)) writers (MPSC)
/// ,single reader, that reads in batches
/// the reader should only read every N messages, otherwise another
@Ruzzie
Ruzzie / Elm.fs
Last active April 24, 2021 05:28
A very rudimentary elm codegenerator in fsharp. To expose lists and maps in elm code.
module Elm =
type Expression =
| TopLevelVariable of string * Expression
| InlineList of Expression list
| Tuple of (Expression * Expression)
| String of string
| Int of int
| SimpleInvoke of (string * Expression)
@Ruzzie
Ruzzie / ConcurrentGreedyDoubleLinkedList.cs
Created May 3, 2018 12:42
A preliminary draft implementation of a lock free Double Linked List in C#
public class ConcurrentGreedyDoubleLinkedList<T> where T: ILinkable<T>
{
private readonly int _initialSize;
private ILinkable<T> _head;
private readonly ILinkable<T> _tail;
public ConcurrentGreedyDoubleLinkedList(in int initialSize = 1024)
{
if (initialSize <= 0)
{
if ( ($env:DEPLOYMENT_SOURCE -eq "") -or ($env:DEPLOYMENT_SOURCE -eq $null))
{
$env:DEPLOYMENT_SOURCE = "..\"
}
if ( ($env:DEPLOYMENT_TARGET -eq "") -or ($env:DEPLOYMENT_TARGET -eq $null))
{
$env:DEPLOYMENT_TARGET = "..\source\MtgDataImports\RuzzieMtgFunctionsWebProject"
}
@Ruzzie
Ruzzie / copyfunctiondll_to_precompiled_functions.ps1
Created January 21, 2017 14:05
Kudu post deploymentscript for compiled azure functions
if ( ($env:DEPLOYMENT_SOURCE -eq "") -or ($env:DEPLOYMENT_SOURCE -eq $null))
{
$env:DEPLOYMENT_SOURCE = "..\"
}
if ( ($env:DEPLOYMENT_TARGET -eq "") -or ($env:DEPLOYMENT_TARGET -eq $null))
{
$env:DEPLOYMENT_TARGET = "YOUR LOCAL TARGET"
}
@Ruzzie
Ruzzie / CopyDllToAllPrecompiledFunctionDirectories.msbuild
Created January 19, 2017 09:16
Post build task for a .net webproject file to copy the output assembly to the azure function directory for precompiled functions
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyDllToAllPrecompiledFunctionDirectories">
<ItemGroup>
<AllFunctionsItems Include="@(Content)"
Condition="'%(Filename)' == 'function' And '%(Extension)' == '.json'" />
<AllFunctionDirectories Include="@(AllFunctionsItems->'%(RootDir)%(Directory)'->Distinct())" />
public class SimpleRandom : Random
{
private readonly RandomSampler _randomSampler;
private const int IntMaxValueMinusOne = int.MaxValue - 1;
public SimpleRandom(int seed, int hValue, int eValue)
{
//For bytes: 0,00106736330262713 127,5 H1741966517 E1631200041
//0,000000001594884 0,499999998405116 H1612099793 E1610967361, with _pTwo PrimeToolHash.GetPrime(hashOrValue.FindNearestPowerOfTwoLessThan())
_randomSampler = new RandomSampler(seed, hValue, eValue);
public class SimpleCrypto : ICryptoTransform
{
private byte[] _key;
private byte[] _iv;
private static readonly int KeySizeInBytes = 32;
private static readonly int IVSizeInBytes = 4;
private static readonly int NumberOfPasses = 2;
private readonly SimpleRandom _generator;
public SimpleCrypto(byte[] key, byte[] iv)
public static class Statistics
{
public static double StreamAverage(double previousAverage, double currentNumber, double currentCount)
{
return ((previousAverage*currentCount) + currentNumber)/(currentCount + 1);
}
public static double Entropy<T>(this IDictionary<T, int> histogram, int dataSize)
{
double entropy = 0;