FNVHash

Port of rust-fnv.
git clone https://git.philomathiclife.com/repos/FNVHash
Log | Files | Refs | README

Lib.cs (5039B)


      1 using Std;
      2 using Std.Clone;
      3 using Std.Convert;
      4 using Std.Hashing;
      5 using Std.Num;
      6 using Std.Result;
      7 using System;
      8 using System.Runtime.CompilerServices;
      9 using System.Runtime.InteropServices;
     10 using System.Security.Cryptography;
     11 #region Namespaces
     12 namespace FNVHash {
     13     #region Types
     14     [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)]
     15     public readonly struct RandomFNVHashBuilder: IBuildHasher<FNVHasher>, IClone<RandomFNVHashBuilder>, IInto<RandomFNVHashBuilder> {
     16 
     17         #region Type-level Constructors
     18         #endregion
     19 
     20         #region Instance Constructors
     21         public RandomFNVHashBuilder() => _hasher = new();
     22         RandomFNVHashBuilder(FNVHasher hasher) => _hasher = hasher;
     23         #endregion
     24 
     25         #region Type-level Fields
     26         #endregion
     27 
     28         #region Instance Fields
     29         [FieldOffset(0)] readonly FNVHasher _hasher;
     30         #endregion
     31 
     32         #region Type-level Properties
     33         #endregion
     34 
     35         #region Instance Properties
     36         #endregion
     37 
     38         #region Type-level Functions
     39         public static RandomFNVHashBuilder New() {
     40 
     41             unsafe {
     42                 var key = stackalloc ulong[1];
     43                 RandomNumberGenerator.Fill(new(key, 8));
     44                 return new(FNVHasher.WithKey(*key));
     45             }
     46         }
     47         #endregion
     48 
     49         #region Instance Functions
     50         public readonly FNVHasher BuildHasher() => _hasher;
     51         public readonly RandomFNVHashBuilder Clone() => this;
     52         public override readonly bool Equals(object? _) => false;
     53         public override readonly int GetHashCode() => 0;
     54         public readonly RandomFNVHashBuilder Into() => this;
     55         public override readonly string ToString() => string.Empty;
     56         readonly Result<RandomFNVHashBuilder, Bottom> ITryInto<RandomFNVHashBuilder, Bottom>.TryInto() => new(this);
     57         #endregion
     58 
     59         #region Operators
     60         #endregion
     61 
     62         #region Types
     63         #endregion
     64     }
     65     [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8, Size = 8)]
     66     public struct FNVHasher: IClone<FNVHasher>, IHasher, IInto<FNVHasher> {
     67 
     68         #region Type-level Constructors
     69         #endregion
     70 
     71         #region Instance Constructors
     72         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     73         public FNVHasher() => _hash = 0xcbf29ce484222325ul;
     74         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     75         public FNVHasher(ulong hash) => _hash = hash;
     76         #endregion
     77 
     78         #region Type-level Fields
     79         #endregion
     80 
     81         #region Instance Fields
     82         [FieldOffset(0)] ulong _hash;
     83         #endregion
     84 
     85         #region Type-level Properties
     86         #endregion
     87 
     88         #region Instance Properties
     89         #endregion
     90 
     91         #region Type-level Functions
     92         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     93         public static FNVHasher WithKey(ulong key) => new(key);
     94         #endregion
     95 
     96         #region Instance Functions
     97         public readonly FNVHasher Clone() => this;
     98         public override readonly bool Equals(object? _) => false;
     99         public readonly ulong Finish() => _hash;
    100         public override readonly int GetHashCode() => 0;
    101         public readonly FNVHasher Into() => this;
    102         public override readonly string ToString() => string.Empty;
    103         readonly Result<FNVHasher, Bottom> ITryInto<FNVHasher, Bottom>.TryInto() => new(this);
    104         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    105         public Unit WriteUnsafe<T>(T val) where T: unmanaged => IHasher.WriteDefaultUnsafe(ref this, val);
    106         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    107         public Unit Write(ReadOnlySpan<byte> bytes) {
    108 
    109             for (var i = 0; i < bytes.Length; i++) { _hash = (_hash ^ bytes[i]) * 0x100000001b3ul; }
    110             return new Unit();
    111         }
    112         public Unit WriteByte(byte val) => IHasher.WriteByteDefault(ref this, val);
    113         public Unit WriteI128(I128 val) => IHasher.WriteI128Default(ref this, val);
    114         public Unit WriteInt(int val) => IHasher.WriteIntDefault(ref this, val);
    115         public Unit WriteLong(long val) => IHasher.WriteLongDefault(ref this, val);
    116         public Unit WriteSbyte(sbyte val) => IHasher.WriteSbyteDefault(ref this, val);
    117         public Unit WriteShort(short val) => IHasher.WriteShortDefault(ref this, val);
    118         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    119         public Unit WriteSliceUnsafe<T>(ReadOnlySpan<T> val) where T: unmanaged => IHasher.WriteSliceDefaultUnsafe(ref this, val);
    120         public Unit WriteU128(U128 val) => IHasher.WriteU128Default(ref this, val);
    121         public Unit WriteUint(uint val) => IHasher.WriteUintDefault(ref this, val);
    122         public Unit WriteUlong(ulong val) => IHasher.WriteUlongDefault(ref this, val);
    123         public Unit WriteUshort(ushort val) => IHasher.WriteUshortDefault(ref this, val);
    124         #endregion
    125 
    126         #region Operators
    127         #endregion
    128 
    129         #region Types
    130         #endregion
    131     }
    132     #endregion
    133 
    134     #region Namespaces
    135     #endregion
    136 }
    137 #endregion