Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Adam White AdamWhiteHat

🏠
Working from home
View GitHub Profile
@AdamWhiteHat
AdamWhiteHat / Vector<T>.cs
Created Jan 25, 2023
Generic Vector<T> Class
View Vector<T>.cs
/// <summary>
/// A generic Vector class.
/// Uses/Requires the GenericArithmeticFactory class:
/// https://gist.github.com/AdamWhiteHat/71d548ebfb2ee67fcbd78a39a9751423
/// </summary>
public class Vector<T>
{
/// <summary>The dimension of the vector, i.e. the number of elements or its length.</summary>
public int Dimensions { get { return Elements == null ? 0 : Elements.Length; } }
@AdamWhiteHat
AdamWhiteHat / SandPile.cs
Last active Jan 7, 2023
Abelian Sandpile Group Class Arithmetic
View SandPile.cs
/// <summary>Abelian Sandpile Class</summary>
public class SandPile : IEquatable<SandPile>
{
#region static Sandpile Values
public static SandPile Zero = new SandPile(new int[,] { { 2, 1, 2 }, { 1, 0, 1 }, { 2, 1, 2 } }, 4);
public static SandPile One = new SandPile(new int[,] { { 3, 2, 3 }, { 2, 1, 2 }, { 3, 2, 3 } }, 4);
public static SandPile NegativeOne = new SandPile(new int[,] { { 1, 3, 1 }, { 3, 3, 3 }, { 1, 3, 1 } }, 4);
// These can all be constructed from the above three sandpiles.
@AdamWhiteHat
AdamWhiteHat / BestStringEqualityComparer.cs
Last active Oct 10, 2022
Enables adding hundreds of millions of string keys to a Dictionary without slow-down. Provides a GetHashCode implementation that provides the mathematically best, even distribution of hash values accross the entire 32 bit integer range of possible values. Allows a Dictionary, HashSet, SortedSet, or anything that needs a hash, to hash hundreds of…
View BestStringEqualityComparer.cs
/// <summary>
/// A IEqualityComparer<string> that provides the mathematically best GetHashCode
/// possible for adding millions of strings to Dictionaries, HashTables, HashMaps and more.
/// It works by interpreting an array of bytes (your string) as a number in base 256,
/// and then returning the unit of that number in the multiplicative group of integers modulo a prime p,
/// where p is the largest prime less than 2^32.
/// </summary>
public class BestStringEqualityComparer : IEqualityComparer<string>
{
public int GetHashCode(string obj)
View INotifyPropertyChangedEx.cs
namespace ComponentModelEx
{
public delegate void PropertyChangedExEventHandler(object sender, PropertyChangedExEventArgs e);
public interface INotifyPropertyChangedEx
{
event PropertyChangedExEventHandler PropertyChangedEx;
}
}
View DictionaryChangedEventArgs.cs
using System;
using System.Collections.Generic;
namespace ComponentModelEx
{
public class DictionaryChangedEventArgs<TKey, TValue> : EventArgs
{
public DictionaryChangedAction Action => _action;
public KeyValuePair<TKey, TValue> OldValue => _oldValue;
public KeyValuePair<TKey, TValue> NewValue => _newValue;
View IsValidIPv6Address.cs
public static bool IPv6Check(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
if (input.IndexOf("::") != input.LastIndexOf("::")) // 'The "::" can only appear once in an address.'
{
return false;
@AdamWhiteHat
AdamWhiteHat / GenericArithmeticFactory.cs
Last active Jan 25, 2023
Generic arithmetic operations with an example
View GenericArithmeticFactory.cs
public static class GenericArithmeticFactory<T>
{
private static Dictionary<ExpressionType, Func<T, T, T>> _binaryOperationDictionary;
private static Dictionary<ExpressionType, Func<T, T, bool>> _comparisonOperationDictionary;
private static Func<T, T> _sqrtOperation = null;
static GenericArithmeticFactory()
{
_binaryOperationDictionary = new Dictionary<ExpressionType, Func<T, T, T>>();
_comparisonOperationDictionary = new Dictionary<ExpressionType, Func<T, T, bool>>();
@AdamWhiteHat
AdamWhiteHat / PasswordHashing.cs
Created Dec 22, 2020
Don't use SHA256 to hash your passwords! Hash passwords the correct way, using BCrypt.
View PasswordHashing.cs
/// <summary>
/// Requires the BCrypt nuget package. Available here: https://www.nuget.org/packages/BCrypt.Net-Next
/// </summary>
public static class PasswordHashing
{
public static string HashPassword(string password, int workFactor)
{
return BCrypt.Net.BCrypt.HashPassword(password, workFactor);
}
@AdamWhiteHat
AdamWhiteHat / FastFactorial.cs
Created Dec 22, 2020
A fast factorial implementation using the divide and conquer method.
View FastFactorial.cs
public static class FastFactorial
{
public static BigInteger Factorial(BigInteger n)
{
return MultiplyRange(2, n);
}
private static BigInteger MultiplyRange(BigInteger from, BigInteger to)
{
var diff = to - from;
@AdamWhiteHat
AdamWhiteHat / NumberTheory.Maths.cs
Last active Jun 19, 2022
Number Theory functions: Legendre Symbol, Eulers Criterion, Tonelli-Shanks, Chinese Remainder Theorem, Modular Multiplicative Inverse, Primitive Root, Multiplicative Order, Discrete Logarithm, Discrete Root, Eulers Totient Phi, Carmichael Lambda, Exponentiation by Squaring, Factorial, Complex Gamma Function, Nth Root, Prime Factorization, LCM, GCD.
View NumberTheory.Maths.cs
using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
public static class Maths
{
/// <summary>
/// Legendre Symbol returns 1 for a (nonzero) quadratic residue mod p, -1 for a non-quadratic residue (non-residue), or 0 on zero.
/// </summary>