Std

Mainly a port of Rust's std.
git clone https://git.philomathiclife.com/repos/Std
Log | Files | Refs | README

Bitmask.cs (5562B)


      1 using Std.Clone;
      2 using Std.Convert;
      3 using Std.Hashbrown.Raw.SSE2;
      4 using Std.Iter;
      5 using Std.Maybe;
      6 using Std.Num;
      7 using Std.Ops;
      8 using Std.Result;
      9 using System;
     10 using System.Runtime.CompilerServices;
     11 using System.Runtime.InteropServices;
     12 #region Namespaces
     13 namespace Std.Hashbrown.Raw.Bitmask {
     14     #region Types
     15     [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2, Size = 2)]
     16     struct BitMask: IClone<BitMask>, IInto<BitMask>, IIntoIterator<ulong, BitMaskIter> {
     17 
     18         #region Type-level Constructors
     19         #endregion
     20 
     21         #region Instance Constructors
     22         public BitMask() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     23         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     24         internal BitMask(ushort bitMaskWord) => _bitMaskWord = bitMaskWord;
     25         #endregion
     26 
     27         #region Type-level Fields
     28         #endregion
     29 
     30         #region Instance Fields
     31         [FieldOffset(0)] internal ushort _bitMaskWord;
     32         #endregion
     33 
     34         #region Type-level Properties
     35         #endregion
     36 
     37         #region Instance Properties
     38         #endregion
     39 
     40         #region Type-level Functions
     41         #endregion
     42 
     43         #region Instance Functions
     44         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     45         internal readonly bool AnyBitSet() => _bitMaskWord != uint.MinValue;
     46         public readonly BitMask Clone() => this;
     47         public override readonly bool Equals(object? _) => false;
     48         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     49         internal bool Flip(ulong index) {
     50 
     51             var mask = (ushort)(1u << (int)((index * Group.BITMASK_STRIDE) + Group.BITMASK_STRIDE - 1ul));
     52             _bitMaskWord ^= mask;
     53             return (_bitMaskWord & mask) == 0u;
     54         }
     55         public override readonly int GetHashCode() => 0;
     56         public readonly BitMask Into() => this;
     57         public readonly BitMaskIter IntoIter() => new(this);
     58         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     59         internal readonly BitMask Invert() => new((ushort)(_bitMaskWord ^ Group.BITMASK_MASK));
     60         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     61         internal readonly ulong LeadingZeros() => _bitMaskWord.LeadingZeros() / Group.BITMASK_STRIDE;
     62         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     63         internal readonly Maybe<ulong> LowestSetBit() => _bitMaskWord == uint.MinValue ? Maybe<ulong>.None() : new(LowestSetBitNonZero());
     64         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     65         internal readonly ulong LowestSetBitNonZero() => TrailingZeros();
     66         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     67         internal readonly BitMask RemoveLowestBit() => new((ushort)(_bitMaskWord & (_bitMaskWord - 1u)));
     68         public override readonly string ToString() => string.Empty;
     69         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     70         internal readonly ulong TrailingZeros() => _bitMaskWord.TrailingZeros() / Group.BITMASK_STRIDE;
     71         readonly Result<BitMask, Bottom> ITryInto<BitMask, Bottom>.TryInto() => new(this);
     72         #endregion
     73 
     74         #region Operators
     75         #endregion
     76 
     77         #region Types
     78         #endregion
     79     }
     80     [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 2, Size = 2)]
     81     struct BitMaskIter: IInto<BitMaskIter>, IIterator<ulong> {
     82 
     83         #region Type-level Constructors
     84         #endregion
     85 
     86         #region Instance Constructors
     87         public BitMaskIter() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     88         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     89         internal BitMaskIter(BitMask bitMask) => _bitMask = bitMask;
     90         #endregion
     91 
     92         #region Type-level Fields
     93         #endregion
     94 
     95         #region Instance Fields
     96         [FieldOffset(0)] BitMask _bitMask;
     97         #endregion
     98 
     99         #region Type-level Properties
    100         #endregion
    101 
    102         #region Instance Properties
    103         #endregion
    104 
    105         #region Type-level Functions
    106         #endregion
    107 
    108         #region Instance Functions
    109         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<ulong>.AdvanceByDefault(ref this, n);
    110         public ulong Count() => IIterator<ulong>.CountDefault(ref this);
    111         public TInit Fold<TInit>(TInit init, Fn<TInit, ulong, TInit> f) where TInit: notnull => IIterator<ulong>.FoldDefault(ref this, init, f);
    112         public override readonly bool Equals(object? _) => false;
    113         public override readonly int GetHashCode() => 0;
    114         public readonly BitMaskIter Into() => this;
    115         public Maybe<ulong> Last() => IIterator<ulong>.LastDefault(ref this);
    116         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    117         public Maybe<ulong> Next() {
    118 
    119             var val = _bitMask.LowestSetBit();
    120 
    121             if (val.IsSome) {
    122                 _bitMask = _bitMask.RemoveLowestBit();
    123                 return new(val._some);
    124             } else {
    125                 return Maybe<ulong>.None();
    126             }
    127         }
    128         public override readonly string ToString() => string.Empty;
    129         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, ulong, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull => IIterator<ulong>.TryFoldDefault(ref this, init, f);
    130         readonly Result<BitMaskIter, Bottom> ITryInto<BitMaskIter, Bottom>.TryInto() => new(this);
    131         #endregion
    132 
    133         #region Operators
    134         #endregion
    135 
    136         #region Types
    137         #endregion
    138     }
    139     #endregion
    140 
    141     #region Namespaces
    142     #endregion
    143 }
    144 #endregion