Fuse.cs (3525B)
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 Fuse<T, TIter>: IInto<Fuse<T, TIter>>, IFusedIterator<T> where T: notnull where TIter: notnull, IIterator<T> { 12 13 #region Type-level Constructors 14 #endregion 15 16 #region Instance Constructors 17 public Fuse() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!"); 18 internal Fuse(TIter iter) => (_iter, _iterIsSome) = (iter, true); 19 #endregion 20 21 #region Type-level Fields 22 #endregion 23 24 #region Instance Fields 25 [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Can't be readonly since we call mutable methods on it.")] 26 TIter _iter; 27 bool _iterIsSome; 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) => IIterator<T>.AdvanceByDefault(ref this, n); 41 public ulong Count() { 42 43 if (_iterIsSome) { 44 var val = _iter.Count(); 45 _iterIsSome = false; 46 return val; 47 } else { 48 return ulong.MinValue; 49 } 50 } 51 public override readonly bool Equals(object? _) => false; 52 public TInit Fold<TInit>(TInit init, Fn<TInit, T, TInit> f) where TInit: notnull { 53 54 if (_iterIsSome) { 55 init = _iter.Fold(init, f); 56 _iterIsSome = false; 57 } 58 return init; 59 } 60 public override readonly int GetHashCode() => 0; 61 public readonly Fuse<T, TIter> Into() => this; 62 public Maybe<T> Last() { 63 64 if (_iterIsSome) { 65 var val = _iter.Last(); 66 _iterIsSome = false; 67 return val; 68 } else { 69 return Maybe<T>.None(); 70 } 71 } 72 public Maybe<T> Next() { 73 74 if (_iterIsSome) { 75 var mbe = _iter.Next(); 76 77 if (mbe.IsNone) { 78 _iterIsSome = false; 79 } 80 return mbe; 81 } else { 82 return Maybe<T>.None(); 83 } 84 } 85 public Prod<ulong, Maybe<ulong>> SizeHint() => _iterIsSome ? _iter.SizeHint() : new(ulong.MinValue, new(ulong.MinValue)); 86 public override readonly string ToString() => string.Empty; 87 public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, T, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull { 88 89 if (_iterIsSome) { 90 var val = _iter.TryFold(init, f); 91 92 if (val.IsErr) { 93 return val; 94 } 95 _iterIsSome = false; 96 return val; 97 } else { 98 return new(init); 99 } 100 } 101 readonly Result<Fuse<T, TIter>, Bottom> ITryInto<Fuse<T, TIter>, 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