| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using AsyncEnumeration.Abstractions; |
| | 19 | | using AsyncEnumeration.Implementation.Enumerable; |
| | 20 | | using System; |
| | 21 | | using System.Collections.Generic; |
| | 22 | | using System.Text; |
| | 23 | | using System.Threading; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | | using UtilPack; |
| | 26 | |
|
| | 27 | | namespace AsyncEnumeration |
| | 28 | | { |
| | 29 | | namespace Implementation.Enumerable |
| | 30 | | { |
| | 31 | | internal sealed class ArrayEnumerator<T> : IAsyncEnumerator<T> |
| | 32 | | { |
| | 33 | | private const Int32 NOT_FETCHED = 0; |
| | 34 | | private const Int32 FETCHED = 1; |
| | 35 | |
|
| | 36 | | private readonly T[] _array; |
| | 37 | | private Int32 _index; |
| | 38 | |
|
| | 39 | | public ArrayEnumerator( T[] array ) |
| | 40 | | => this._array = ArgumentValidator.ValidateNotNull( nameof( array ), array ); |
| | 41 | |
|
| | 42 | | public Task<Boolean> WaitForNextAsync() |
| | 43 | | => TaskUtils.TaskFromBoolean( this._index < this._array.Length ); |
| | 44 | |
|
| | 45 | | public T TryGetNext( out Boolean success ) |
| | 46 | | { |
| | 47 | | var array = this._array; |
| | 48 | | var idx = Interlocked.Increment( ref this._index ); |
| | 49 | | success = idx <= array.Length; |
| | 50 | | return success ? array[idx - 1] : default; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public Task DisposeAsync() |
| | 54 | | => TaskUtils.CompletedTask; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | internal sealed class SynchronousEnumerableEnumerator<T> : IAsyncEnumerator<T> |
| | 58 | | { |
| | 59 | | private const Int32 STATE_INITIAL = 0; |
| | 60 | | private const Int32 STATE_MOVENEXT_CALLED = 1; |
| | 61 | | private const Int32 STATE_ENDED = 2; |
| | 62 | |
|
| | 63 | | private readonly IEnumerator<T> _enumerator; |
| | 64 | | private Int32 _state; |
| | 65 | |
|
| 30 | 66 | | public SynchronousEnumerableEnumerator( IEnumerator<T> syncEnumerator ) |
| 30 | 67 | | => this._enumerator = ArgumentValidator.ValidateNotNull( nameof( syncEnumerator ), syncEnumerator ); |
| | 68 | |
|
| | 69 | | public Task<Boolean> WaitForNextAsync() |
| 51 | 70 | | => TaskUtils.TaskFromBoolean( Interlocked.CompareExchange( ref this._state, STATE_MOVENEXT_CALLED, STATE_INI |
| | 71 | |
|
| | 72 | | public T TryGetNext( out Boolean success ) |
| | 73 | | { |
| 159 | 74 | | success = this._state == STATE_MOVENEXT_CALLED; |
| 159 | 75 | | var retVal = success ? this._enumerator.Current : default; |
| 159 | 76 | | if ( success && !this._enumerator.MoveNext() ) |
| | 77 | | { |
| 27 | 78 | | Interlocked.Exchange( ref this._state, STATE_ENDED ); |
| | 79 | | } |
| 159 | 80 | | return retVal; |
| | 81 | | } |
| | 82 | |
|
| | 83 | |
|
| | 84 | | public Task DisposeAsync() |
| | 85 | | { |
| 30 | 86 | | this._enumerator.Dispose(); |
| 30 | 87 | | return TaskUtils.CompletedTask; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// This class contains extension methods for types not defined in this assembly. |
| | 95 | | /// </summary> |
| | 96 | | public static partial class AsyncEnumerationExtensions |
| | 97 | | { |
| | 98 | | /// <summary> |
| | 99 | | /// This extension method will wrap this array into <see cref="IAsyncEnumerable{T}"/>. |
| | 100 | | /// </summary> |
| | 101 | | /// <typeparam name="T">The type of array elements.</typeparam> |
| | 102 | | /// <param name="array">This array.</param> |
| | 103 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 104 | | /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over the contents of the array.</returns> |
| | 105 | | /// <exception cref="NullReferenceException">If this array is <c>null</c>.</exception> |
| | 106 | | public static IAsyncEnumerable<T> AsAsyncEnumerable<T>( |
| | 107 | | this T[] array, |
| | 108 | | IAsyncProvider asyncProvider |
| | 109 | | ) => AsyncEnumerationFactory.FromGeneratorCallback( ArgumentValidator.ValidateNotNullReference( array ), a => n |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// This extension method will wrap this <see cref="IEnumerable{T}"/> into <see cref="IAsyncEnumerable{T}"/>. |
| | 113 | | /// </summary> |
| | 114 | | /// <typeparam name="T">The type of <see cref="IEnumerable{T}"/> elements.</typeparam> |
| | 115 | | /// <param name="enumerable">This <see cref="IEnumerable{T}"/>.</param> |
| | 116 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 117 | | /// <returns><see cref="IAsyncEnumerable{T}"/> which will enumerate over this <see cref="IEnumerable{T}"/>.</retur |
| | 118 | | /// <exception cref="NullReferenceException">If this <see cref="IEnumerable{T}"/> is <c>null</c>.</exception> |
| | 119 | | public static IAsyncEnumerable<T> AsAsyncEnumerable<T>( |
| | 120 | | this IEnumerable<T> enumerable, |
| | 121 | | IAsyncProvider asyncProvider |
| | 122 | | ) => AsyncEnumerationFactory.FromGeneratorCallback( ArgumentValidator.ValidateNotNullReference( enumerable ), e |
| | 123 | | } |
| | 124 | | } |