XXHash

Port of Rust's twox-hash crate.
git clone https://git.philomathiclife.com/repos/XXHash
Log | Files | Refs | README

Lib.cs (2931B)


      1 using Std.Maybe;
      2 using System;
      3 using System.Runtime.CompilerServices;
      4 #region Namespaces
      5 namespace XXHash {
      6     #region Types
      7     ref struct UnalignedBuffer {
      8 
      9         #region Type-level Constructors
     10         #endregion
     11 
     12         #region Instance Constructors
     13         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     14         public UnalignedBuffer() => _buf = ReadOnlySpan<byte>.Empty;
     15         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     16         UnalignedBuffer(ReadOnlySpan<byte> buf) => _buf = buf;
     17         #endregion
     18 
     19         #region Type-level Fields
     20         #endregion
     21 
     22         #region Instance Fields
     23         ReadOnlySpan<byte> _buf;
     24         #endregion
     25 
     26         #region Type-level Properties
     27         #endregion
     28 
     29         #region Instance Properties
     30         #endregion
     31 
     32         #region Type-level Functions
     33         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     34         internal static UnalignedBuffer New(ReadOnlySpan<byte> buf) => new(buf);
     35         #endregion
     36 
     37         #region Instance Functions
     38         public override readonly bool Equals(object? _) => false;
     39         public override readonly int GetHashCode() => 0;
     40         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     41         internal ReadOnlySpan<ulong> Next4UlongReadOnlySpan(out bool exists) {
     42 
     43             if (_buf.Length >= 32) {
     44                 unsafe {
     45                     fixed (byte* ptr = _buf) {
     46                         exists = true;
     47                         _buf = _buf[32..];
     48                         return new ReadOnlySpan<ulong>((ulong*)ptr, 4);
     49                     }
     50                 }
     51             } else {
     52                 exists = false;
     53                 return ReadOnlySpan<ulong>.Empty;
     54             }
     55         }
     56         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     57         internal Maybe<uint> NextUint() {
     58 
     59             if (_buf.Length >= 4) {
     60                 unsafe {
     61                     fixed (byte* ptr = _buf) {
     62                         _buf = _buf[4..];
     63                         return new(*(uint*)ptr);
     64                     }
     65                 }
     66             } else {
     67                 return Maybe<uint>.None();
     68             }
     69         }
     70         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     71         internal Maybe<ulong> NextUlong() {
     72 
     73             if (_buf.Length >= 8) {
     74                 unsafe {
     75                     fixed (byte* ptr = _buf) {
     76                         _buf = _buf[8..];
     77                         return new(*(ulong*)ptr);
     78                     }
     79                 }
     80             } else {
     81                 return Maybe<ulong>.None();
     82             }
     83         }[MethodImpl(MethodImplOptions.AggressiveInlining)]
     84         internal readonly ReadOnlySpan<byte> Remaining() => _buf;
     85         public override readonly string ToString() => string.Empty;
     86         #endregion
     87 
     88         #region Operators
     89         #endregion
     90 
     91         #region Types
     92         #endregion
     93     }
     94     #endregion
     95 
     96     #region Namespaces
     97     #endregion
     98 }
     99 #endregion