Std

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

Zip.cs (3568B)


      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 Zip<T, TIter, T2, TIter2>: IInto<Zip<T, TIter, T2, TIter2>>, IIterator<Prod<T, T2>> where T: notnull where TIter: notnull, IIterator<T> where T2: notnull where TIter2: notnull, IIterator<T2> {
     12 
     13         #region Type-level Constructors
     14         #endregion
     15 
     16         #region Instance Constructors
     17         public Zip() => throw new InvalidOperationException("Parameterless constructor is not allowed to be called!");
     18         internal Zip(TIter iter, TIter2 iter2) => (_iter, _iter2) = (iter, iter2);
     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         [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Can't be readonly since we call mutable methods on it.")]
     28         TIter2 _iter2;
     29         #endregion
     30 
     31         #region Type-level Properties
     32         #endregion
     33 
     34         #region Instance Properties
     35         #endregion
     36 
     37         #region Type-level Functions
     38         #endregion
     39 
     40         #region Instance Functions
     41         public Result<Unit, ulong> AdvanceBy(ulong n) => IIterator<Prod<T, T2>>.AdvanceByDefault(ref this, n);
     42         public ulong Count() => IIterator<Prod<T, T2>>.CountDefault(ref this);
     43         public TInit Fold<TInit>(TInit init, Fn<TInit, Prod<T, T2>, TInit> f) where TInit: notnull => IIterator<Prod<T, T2>>.FoldDefault(ref this, init, f);
     44         public override readonly bool Equals(object? _) => false;
     45         public override readonly int GetHashCode() => 0;
     46         public readonly Zip<T, TIter, T2, TIter2> Into() => this;
     47         public Maybe<Prod<T, T2>> Last() => IIterator<Prod<T, T2>>.LastDefault(ref this);
     48         public Maybe<Prod<T, T2>> Next() {
     49 
     50             var x = _iter.Next();
     51 
     52             if (x.IsNone) {
     53                 return Maybe<Prod<T, T2>>.None();
     54             }
     55             var y = _iter2.Next();
     56             return y.IsNone ? Maybe<Prod<T, T2>>.None() : (new(new(x._some, y._some)));
     57         }
     58         public Prod<ulong, Maybe<ulong>> SizeHint() {
     59 
     60             var (lower1, upper1) = _iter.SizeHint();
     61             var (lower2, upper2) = _iter2.SizeHint();
     62             var lower = lower1 <= lower2 ? lower1 : lower2;
     63             var upper = (upper1.IsSome, upper2.IsSome) switch {
     64                 (true, true) => upper1._some <= upper2._some ? upper1 : upper2,
     65                 (true, false) => upper1,
     66                 (false, true) => upper2,
     67                 _ => Maybe<ulong>.None(),
     68             };
     69             return new(lower, upper);
     70         }
     71         public override readonly string ToString() => string.Empty;
     72         public Result<TInit, TErr> TryFold<TInit, TErr>(TInit init, Fn<TInit, Prod<T, T2>, Result<TInit, TErr>> f) where TInit: notnull where TErr: notnull => IIterator<Prod<T, T2>>.TryFoldDefault(ref this, init, f);
     73         readonly Result<Zip<T, TIter, T2, TIter2>, Bottom> ITryInto<Zip<T, TIter, T2, TIter2>, Bottom>.TryInto() => new(this);
     74         #endregion
     75 
     76         #region Operators
     77         #endregion
     78 
     79         #region Types
     80         #endregion
     81     }
     82     #endregion
     83 
     84     #region Namespaces
     85     #endregion
     86 }
     87 #endregion