Skip to content

Instantly share code, notes, and snippets.

@mjs3339
mjs3339 / Mapping64BitToHash32Bit.cs
Last active February 19, 2019 23:12
C# 32 Bit Hashing Algorithm ~No Collisions in the Int Bit Space
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 February 17, 2019 08:52
C# Generic IEqualityComparer
[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 February 17, 2019 08:43
C# Convert from Primitives to Byte Array
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 February 8, 2019 20:49
C# FNV1a32 XOR Folding Version
/// <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 February 6, 2019 14:53
C# IEnumerable File Helper Class, Load or Save Primitive, Class, Structure, IEnumerable To or From a File.
/// <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 February 1, 2019 23:12
C# Pathhelper, Analyze Path Construction Without Touching Path
public static class Pathhelper
{
[Flags]
[Serializable]
public enum DeviceTypes
{
None = 0,
CDRom = 1,
InvalidCDRom = 2,
PhysicalDrive = 4,
@mjs3339
mjs3339 / FileHash128.cs
Last active January 27, 2019 08:30
C# 128 Bit ASynchronous File Hashing Class using the MurMur3 Hashing Algorithm
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 January 13, 2019 06:43
C# Custom Label Control, Set Shadow, Gradient, and Direction
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 December 17, 2018 07:53
C# ByteArray Class Manages an Array of Bytes
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 December 15, 2018 11:28
C# IEnumerableStep - Generate A Sequence of Numbers within a Specific Range with a given Step
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);
}