Instantly share code, notes, and snippets.

@mjs3339
mjs3339 / Mapping64BitToHash32Bit.cs
Last active Feb 19, 2019
C# 32 Bit Hashing Algorithm ~No Collisions in the Int Bit Space
View Mapping64BitToHash32Bit.cs
using System.Security.Cryptography;
/// <summary>
/// Pros: ~Very few to no Collisions within int bit space ~(2,147,483,647)
/// Cons: Maintains a TinyDictionary(byte[],byte[]), Decreased performance, non-deterministic across application or
/// method domains
/// Cannot Transform or reuse.
/// </summary>
public class Mapping64BitToHash32Bit : HashAlgorithm
{
private readonly FNV1a64 hasher = new FNV1a64();
@mjs3339
mjs3339 / ArrayComparer.cs
Created Feb 17, 2019
C# Generic IEqualityComparer
View ArrayComparer.cs
[Serializable]
private class ArrayComparer<T> : IEqualityComparer<T>
{
private static Type _type;
private static int _size;
public bool Equals(T x, T y)
{
if(x == null || y == null)
return false;
if(_type == null)
@mjs3339
mjs3339 / Converters.cs
Created Feb 17, 2019
C# Convert from Primitives to Byte Array
View Converters.cs
public static class Converters
{
public static bool IsValidPrimitive(Type type)
{
switch(Type.GetTypeCode(type))
{
case TypeCode.Boolean:
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
@mjs3339
mjs3339 / FNV1a32xf.cs
Last active Feb 8, 2019
C# FNV1a32 XOR Folding Version
View FNV1a32xf.cs
/// <summary>
/// XOR folding version reduces collisions by greater than ~2/3's
/// </summary>
public class FNV1a32 : HashAlgorithm
{
private const uint K = 0x1000193;
private uint _hash;
public uint Hash;
public uint Seed = 0x811c9dc5;
public FNV1a32()
@mjs3339
mjs3339 / IEnumerableFileHelper.cs
Created Feb 6, 2019
C# IEnumerable File Helper Class, Load or Save Primitive, Class, Structure, IEnumerable To or From a File.
View IEnumerableFileHelper.cs
/// <summary>
/// IEnumerable File Helper Class.
/// </summary>
public static class IEnumerableFileHelper
{
/// <summary>
/// Save a IEnumerable to a file.
/// </summary>
public static void SaveToFileIEnum<T>(this IEnumerable<T> obj, string path, bool append = false)
{
@mjs3339
mjs3339 / Pathhelper.cs
Created Feb 1, 2019
C# Pathhelper, Analyze Path Construction Without Touching Path
View Pathhelper.cs
public static class Pathhelper
{
[Flags]
[Serializable]
public enum DeviceTypes
{
None = 0,
CDRom = 1,
InvalidCDRom = 2,
PhysicalDrive = 4,
@mjs3339
mjs3339 / FileHash128.cs
Last active Jan 27, 2019
C# 128 Bit ASynchronous File Hashing Class using the MurMur3 Hashing Algorithm
View FileHash128.cs
public class FileHash128 : Murmur3
{
private const int BufferSize = 1024 * 80;
private readonly byte[] buffer = new byte[1073741823];
public byte[] GetFileHashSync(string path)
{
var fLen = 0;
using(var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, false))
{
var offset = 0;
@mjs3339
mjs3339 / LabelMJS.cs
Created Jan 13, 2019
C# Custom Label Control, Set Shadow, Gradient, and Direction
View LabelMJS.cs
public class LabelMJS : Label
{
public enum Angles
{
/// <summary>
/// Normal drawing direction.
/// </summary>
LeftToRight = 0,
/// <summary>
/// Draw text top to bottom as viewed from the left.
@mjs3339
mjs3339 / ByteArray.cs
Created Dec 17, 2018
C# ByteArray Class Manages an Array of Bytes
View ByteArray.cs
public class ByteArray : IFormattable, IComparable, IComparable<ByteArray>, IEquatable<ByteArray>
{
public byte[] bytes;
public ByteArray() : this(0)
{
}
public ByteArray(int size)
{
bytes = new byte[size];
Active = true;
@mjs3339
mjs3339 / IEnumerableStep.cs
Created Dec 15, 2018
C# IEnumerableStep - Generate A Sequence of Numbers within a Specific Range with a given Step
View IEnumerableStep.cs
public static class IEnumerableStep
{
/// <summary>
/// Generates a sequence of 8-bit signed numbers within a specified range starting at
/// 'form' through 'to' with a given step value.
/// </summary>
public static IEnumerable<sbyte> Step(this sbyte from, sbyte to, int step)
{
return Step(from, to, step).Select(i => i);
}