Enumerator.cs (2888B)
1 using Std.Convert; 2 using Std.Maybe; 3 using Std.Ops; 4 using Std.Result; 5 using System; 6 using System.Collections.Generic; 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 Enumerator<T, TEnumerator>: IInto<Enumerator<T, TEnumerator>>, IFusedIterator<T> where T: notnull where TEnumerator: notnull, IEnumerator<T> { 13 14 #region Type-level Constructors 15 #endregion 16 17 #region Instance Constructors 18 public Enumerator() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!"); 19 public Enumerator(TEnumerator enumerator) => (_enumerator, _flag) = (enumerator, byte.MaxValue); 20 #endregion 21 22 #region Type-level Fields 23 #endregion 24 25 #region Instance Fields 26 TEnumerator _enumerator; 27 byte _flag; 28 #endregion 29 30 #region Type-level Properties 31 #endregion 32 33 #region Instance Properties 34 #endregion 35 36 #region Type-level Functions 37 public static Enumerator<T, TEnumerator> New(TEnumerator enumerator) => new(enumerator); 38 #endregion 39 40 #region Instance Functions 41 public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<T>.AdvanceByDefault(ref this, n); 42 public ulong Count() => IIterator<T>.CountDefault(ref this); 43 public override readonly bool Equals(object? _) => false; 44 public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull => IIterator<T>.FoldDefault(ref this, init, f); 45 public override readonly int GetHashCode() => 0; 46 public readonly Enumerator<T, TEnumerator> Into() => this; 47 public Maybe<T> Last() => IIterator<T>.LastDefault(ref this); 48 public Maybe<T> Next() { 49 50 if (_flag == byte.MinValue) { 51 return Maybe<T>.None(); 52 } else if(_enumerator.MoveNext()) { 53 return new(_enumerator.Current); 54 } else { 55 _flag = byte.MinValue; 56 _enumerator = default!; 57 return Maybe<T>.None(); 58 } 59 } 60 public readonly Prod<ulong, Maybe<ulong>> SizeHint() => new(ulong.MinValue, Maybe<ulong>.None()); 61 public override readonly string ToString() => string.Empty; 62 public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull => IIterator<T>.TryFoldDefault(ref this, init, f); 63 readonly Result<Enumerator<T, TEnumerator>, Bottom> ITryInto<Enumerator<T, TEnumerator>, Bottom>.TryInto() => new(this); 64 #endregion 65 66 #region Operators 67 #endregion 68 69 #region Types 70 #endregion 71 } 72 #endregion 73 74 #region Namespaces 75 #endregion 76 } 77 #endregion