Filter.cs (2814B)
1 using Std.Convert; 2 using Std.Maybe; 3 using Std.Ops; 4 using Std.Result; 5 using System; 6 using System.Runtime.InteropServices; 7 #region Namespaces 8 namespace Std.Iter { 9 #region Types 10 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 0)] 11 public struct Filter<T, TIter>: IInto<Filter<T, TIter>>, IIterator<T> where T: notnull where TIter: notnull, IIterator<T> { 12 13 #region Type-level Constructors 14 #endregion 15 16 #region Instance Constructors 17 public Filter() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!"); 18 internal Filter(TIter iter, FnIn<T, bool> f) => (_iter, _f) = (iter, f); 19 #endregion 20 21 #region Type-level Fields 22 #endregion 23 24 #region Instance Fields 25 readonly FnIn<T, bool> _f; 26 TIter _iter; 27 #endregion 28 29 #region Type-level Properties 30 #endregion 31 32 #region Instance Properties 33 #endregion 34 35 #region Type-level Functions 36 #endregion 37 38 #region Instance Functions 39 public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T>.AdvanceByDefault(ref this, n); 40 public ulong Count() { 41 42 static Fn<ulong, T, ulong> fn(FnIn<T, bool> p) => (x, y) => p(in y) ? x + 1ul : x; 43 return _iter.Fold(ulong.MinValue, fn(_f)); 44 } 45 public override readonly bool Equals(object? _) => false; 46 public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull { 47 48 static Fn<TInit, T, TInit> fn(FnIn<T, bool> p, Fn<TInit, T, TInit> fold) => (acc, item) => p(in item) ? fold(acc, item) : acc; 49 return _iter.Fold(init, fn(_f, f)); 50 } 51 public override readonly int GetHashCode() => 0; 52 public readonly Filter<T, TIter> Into() => this; 53 public Maybe<T> Last() => IIterator<T>.LastDefault(ref this); 54 public Maybe<T> Next() => IIterator<T>.Find(ref _iter, _f); 55 public Prod<ulong, Maybe<ulong>> SizeHint() => new(ulong.MinValue, _iter.SizeHint().Item1); 56 public override readonly string ToString() => string.Empty; 57 public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull { 58 59 static Fn<TInit, T, Result<TInit, TErr>> fn(FnIn<T, bool> p, Fn<TInit, T, Result<TInit, TErr>> fold) => (acc, item) => p(in item) ? fold(acc, item) : new(acc); 60 return _iter.TryFold(init, fn(_f, f)); 61 } 62 readonly Result<Filter<T, TIter>, Bottom> ITryInto<Filter<T, TIter>, Bottom>.TryInto() => new(this); 63 #endregion 64 65 #region Operators 66 #endregion 67 68 #region Types 69 #endregion 70 } 71 #endregion 72 73 #region Namespaces 74 #endregion 75 } 76 #endregion