Std

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

Skip.cs (3670B)


      1 using Std.Convert;
      2 using Std.Maybe;
      3 using Std.Num;
      4 using Std.Ops;
      5 using Std.Result;
      6 using System;
      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 Skip<T, TIter>: IInto<Skip<T, TIter>>, IIterator<T> where T: notnull where TIter: notnull, IIterator<T> {
     13 
     14         #region Type-level Constructors
     15         #endregion
     16 
     17         #region Instance Constructors
     18         public Skip() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     19         internal Skip(TIter iter, ulong n) => (_iter, _n) = (iter, n);
     20         #endregion
     21 
     22         #region Type-level Fields
     23         #endregion
     24 
     25         #region Instance Fields
     26         ulong _n;
     27         TIter _iter;
     28         #endregion
     29 
     30         #region Type-level Properties
     31         #endregion
     32 
     33         #region Instance Properties
     34         #endregion
     35 
     36         #region Type-level Functions
     37         #endregion
     38 
     39         #region Instance Functions
     40         public Result<Unit, ulong> AdvanceBy(ulong n) {
     41 
     42             if (n == ulong.MinValue) {
     43                 return new(new Unit());
     44             } else if (_n > ulong.MinValue) {
     45                 var skip = _n;
     46                 _n = ulong.MinValue;
     47                 var res = _iter.AdvanceBy(skip);
     48 
     49                 if (res.IsErr) {
     50                     return new(ulong.MinValue);
     51                 }
     52             }
     53             return _iter.AdvanceBy(n);
     54         }
     55         public ulong Count() {
     56 
     57             var n = _n;
     58             _n = ulong.MinValue;
     59             return n > ulong.MinValue && _iter.AdvanceBy(n).IsErr ? ulong.MinValue : _iter.Count();
     60         }
     61         public override readonly bool Equals(object? _) => false;
     62         public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull {
     63 
     64             var n = _n;
     65             _n = ulong.MinValue;
     66             return n > ulong.MinValue && _iter.AdvanceBy(n).IsErr ? init : _iter.Fold(init, f);
     67         }
     68         public override readonly int GetHashCode() => 0;
     69         public readonly Skip<T, TIter> Into() => this;
     70         public Maybe<T> Last() {
     71 
     72             var n = _n;
     73             _n = ulong.MinValue;
     74 
     75             if (n > ulong.MinValue) {
     76                 var res = _iter.AdvanceBy(n);
     77 
     78                 if (res.IsErr) {
     79                     return Maybe<T>.None();
     80                 }
     81             }
     82             return _iter.Last();
     83         }
     84         public Maybe<T> Next() {
     85 
     86             if (_n == ulong.MinValue) {
     87                 return _iter.Next();
     88             } else {
     89                 var n = _n;
     90                 _n = ulong.MinValue;
     91                 return IIterator<T>.Nth(ref _iter, n);
     92             }
     93         }
     94         public Prod<ulong, Maybe<ulong>> SizeHint() {
     95 
     96             var val = _iter.SizeHint();
     97             return new(val.Item0.SaturatingSub(_n), val.Item1.IsSome ? new(val.Item1._some.SaturatingSub(_n)) : Maybe<ulong>.None());
     98         }
     99         public override readonly string ToString() => string.Empty;
    100         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull {
    101 
    102             var n = _n;
    103             _n = ulong.MinValue;
    104             return n > ulong.MinValue && _iter.AdvanceBy(n).IsErr ? (new(init)) : _iter.TryFold(init, f);
    105         }
    106         readonly Result<Skip<T, TIter>, Bottom> ITryInto<Skip<T, TIter>, Bottom>.TryInto() => new(this);
    107         #endregion
    108 
    109         #region Operators
    110         #endregion
    111 
    112         #region Types
    113         #endregion
    114     }
    115     #endregion
    116 
    117     #region Namespaces
    118     #endregion
    119 }
    120 #endregion