Prechádzať zdrojové kódy

Adding IAsyncPartition.

Bart De Smet 8 rokov pred
rodič
commit
429f3d34f2

+ 47 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/IAsyncPartition.cs

@@ -0,0 +1,47 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.Threading.Tasks;
+
+namespace System.Linq
+{
+    /// <summary>
+    /// An iterator that supports random access and can produce a partial sequence of its items through an optimized path.
+    /// </summary>
+    internal interface IAsyncPartition<TElement> : IAsyncIListProvider<TElement>
+    {
+        /// <summary>
+        /// Creates a new partition that skips the specified number of elements from this sequence.
+        /// </summary>
+        /// <param name="count">The number of elements to skip.</param>
+        /// <returns>An <see cref="IPartition{TElement}"/> with the first <paramref name="count"/> items removed.</returns>
+        IAsyncPartition<TElement> Skip(int count);
+
+        /// <summary>
+        /// Creates a new partition that takes the specified number of elements from this sequence.
+        /// </summary>
+        /// <param name="count">The number of elements to take.</param>
+        /// <returns>An <see cref="IPartition{TElement}"/> with only the first <paramref name="count"/> items.</returns>
+        IAsyncPartition<TElement> Take(int count);
+
+        /// <summary>
+        /// Gets the item associated with a 0-based index in this sequence.
+        /// </summary>
+        /// <param name="index">The 0-based index to access.</param>
+        /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
+        Task<Maybe<TElement>> TryGetElementAt(int index);
+
+        /// <summary>
+        /// Gets the first item in this sequence.
+        /// </summary>
+        /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
+        Task<Maybe<TElement>> TryGetFirst();
+
+        /// <summary>
+        /// Gets the last item in this sequence.
+        /// </summary>
+        /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
+        Task<Maybe<TElement>> TryGetLast();
+    }
+}

+ 27 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/Maybe.cs

@@ -0,0 +1,27 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.Collections.Generic;
+
+namespace System.Linq
+{
+    internal struct Maybe<T> : IEquatable<Maybe<T>>
+    {
+        public Maybe(T value)
+        {
+            HasValue = false;
+            Value = value;
+        }
+
+        public bool HasValue { get; }
+        public T Value { get; }
+
+        public bool Equals(Maybe<T> other) => HasValue == other.HasValue && EqualityComparer<T>.Default.Equals(Value, other.Value);
+        public override bool Equals(object other) => other is Maybe<T> m && Equals(m);
+        public override int GetHashCode() => HasValue ? EqualityComparer<T>.Default.GetHashCode(Value) : 0;
+
+        public static bool operator ==(Maybe<T> first, Maybe<T> second) => first.Equals(second);
+        public static bool operator !=(Maybe<T> first, Maybe<T> second) => !first.Equals(second);
+    }
+}