Std

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

Scan.cs (4660B)


      1 using Std.Convert;
      2 using Std.Maybe;
      3 using Std.Ops;
      4 using Std.Result;
      5 using System;
      6 using System.Runtime.CompilerServices;
      7 using System.Runtime.InteropServices;
      8 #region Namespaces
      9 namespace Std.Iter {
     10     #region Types
     11     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 0)]
     12     public struct Scan<T, TIter, TState, T2>: IInto<Scan<T, TIter, TState, T2>>, IIterator<T2> where T: notnull where TIter: notnull, IIterator<T> where T2: notnull where TState: notnull {
     13 
     14         #region Type-level Constructors
     15         #endregion
     16 
     17         #region Instance Constructors
     18         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     19         public Scan() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     20         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     21         internal Scan(TIter iter, TState state, FnRef<TState, T, Maybe<T2>> f) => (_iter, _state, _f) = (iter, state, f);
     22         #endregion
     23 
     24         #region Type-level Fields
     25         #endregion
     26 
     27         #region Instance Fields
     28         readonly FnRef<TState, T, Maybe<T2>> _f;
     29         [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Can't be readonly since we call mutable methods on it.")]
     30         TIter _iter;
     31         TState _state;
     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         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T2>.AdvanceByDefault(ref this, n);
     46         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     47         public ulong Count() => IIterator<T2>.CountDefault(ref this);
     48         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     49         public override readonly bool Equals(object? _) => false;
     50         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     51         public TInit Fold<TInit>(TInit init, Fn<TInit, T2, TInit> f) where TInit: notnull {
     52 
     53             [MethodImpl(MethodImplOptions.AggressiveInlining)]
     54             static Fn<TInit, T2, Result<TInit, Bottom>> fn(Fn<TInit, T2, TInit> g) => (acc, x) => new(g(acc, x));
     55             return TryFold(init, fn(f))._ok;
     56         }
     57         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     58         public override readonly int GetHashCode() => 0;
     59         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     60         public readonly Scan<T, TIter, TState, T2> Into() => this;
     61         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     62         public Maybe<T2> Last() => IIterator<T2>.LastDefault(ref this);
     63         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     64         public Maybe<T2> Next() {
     65 
     66             var mbe = _iter.Next();
     67 
     68             if (mbe.IsSome) {
     69                 var a = mbe._some;
     70                 return _f(ref _state, ref a);
     71             } else {
     72                 return Maybe<T2>.None();
     73             }
     74         }
     75         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     76         public Prod<ulong, Maybe<ulong>> SizeHint() => new(ulong.MinValue, _iter.SizeHint().Item1);
     77         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     78         public override readonly string ToString() => string.Empty;
     79         [MethodImpl(MethodImplOptions.AggressiveInlining)]
     80         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T2, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
     81 
     82             var st = _state;
     83             [MethodImpl(MethodImplOptions.AggressiveInlining)]
     84             Fn<TInit, T, Result<TInit, Result<TInit, TErr>>> scan(FnRef<TState, T, Maybe<T2>> g, Fn<TInit, T2, Result<TInit, TErr>> h) => (acc, x) => {
     85                 
     86                 var val = g(ref st, ref x);
     87 
     88                 if (val.IsNone) {
     89                     return new Result<TInit, Result<TInit, TErr>>(new Result<TInit, TErr>(acc));
     90                 } else {
     91                     var res = h(acc, val._some);
     92                     return res.IsErr ? new(res) : new(res._ok);
     93                 }
     94             };
     95 
     96             var res =_iter.TryFold(init, scan(_f, f));
     97             _state = st;
     98             return res.IsOK ? new(res._ok) : res._err;
     99         }
    100         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    101         readonly Result<Scan<T, TIter, TState, T2>, Bottom> ITryInto<Scan<T, TIter, TState, T2>, Bottom>.TryInto() => new(this);
    102         #endregion
    103 
    104         #region Operators
    105         #endregion
    106 
    107         #region Types
    108         #endregion
    109     }
    110     #endregion
    111 
    112     #region Namespaces
    113     #endregion
    114 }
    115 #endregion