| | | 1 | | /* |
| | | 2 | | * Copyright 2018 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 System; |
| | | 20 | | using System.Collections.Generic; |
| | | 21 | | using System.Text; |
| | | 22 | | using System.Threading; |
| | | 23 | | using System.Threading.Tasks; |
| | | 24 | | |
| | | 25 | | namespace AsyncEnumeration.Implementation.Enumerable |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// This class provides some general ways to generate instances of <see cref="IAsyncEnumerable{T}"/>, similar to how |
| | | 29 | | /// </summary> |
| | | 30 | | public static class AsyncEnumerable |
| | | 31 | | { |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the given item specified amount of times. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 37 | | /// <param name="item">The item to repeat.</param> |
| | | 38 | | /// <param name="count">Amount of times to repeat the <paramref name="item"/>.</param> |
| | | 39 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 40 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 41 | | /// <seealso cref="System.Linq.Enumerable.Repeat{TResult}(TResult, Int32)"/> |
| | | 42 | | /// <seealso cref="Repeat{T}(T, Int64, IAsyncProvider)"/> |
| | | 43 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 44 | | T item, |
| | | 45 | | Int32 count, |
| | | 46 | | IAsyncProvider asyncProvider |
| | | 47 | | ) |
| | | 48 | | { |
| | 6 | 49 | | return count <= 0 ? |
| | 6 | 50 | | EmptyAsync<T>.Enumerable : |
| | 23 | 51 | | Neverending( () => item, asyncProvider ).Take( count ); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the given item specified amount of times, expressed |
| | | 56 | | /// </summary> |
| | | 57 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 58 | | /// <param name="item">The item to repeat.</param> |
| | | 59 | | /// <param name="count">Amount of times to repeat the <paramref name="item"/>, as 64-bit integer.</param> |
| | | 60 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 61 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 62 | | /// <seealso cref="System.Linq.Enumerable.Repeat{TResult}(TResult, Int32)"/> |
| | | 63 | | /// <seealso cref="Repeat{T}(T, Int32, IAsyncProvider)"/> |
| | | 64 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 65 | | T item, |
| | | 66 | | Int64 count, |
| | | 67 | | IAsyncProvider asyncProvider |
| | | 68 | | ) |
| | | 69 | | { |
| | 3 | 70 | | return count <= 0 ? |
| | 3 | 71 | | EmptyAsync<T>.Enumerable : |
| | 13 | 72 | | Neverending( () => item, asyncProvider ).Take( count ); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the result of given synchronous item factory callba |
| | | 77 | | /// </summary> |
| | | 78 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 79 | | /// <param name="generator">The synchronous callback to generate an item to repeat.</param> |
| | | 80 | | /// <param name="count">Amount of times to repeat the item.</param> |
| | | 81 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 82 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 83 | | /// <seealso cref="System.Linq.Enumerable.Repeat{TResult}(TResult, Int32)"/> |
| | | 84 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 85 | | Func<T> generator, |
| | | 86 | | Int32 count, |
| | | 87 | | IAsyncProvider asyncProvider |
| | | 88 | | ) |
| | | 89 | | { |
| | 1 | 90 | | return count <= 0 ? |
| | 1 | 91 | | EmptyAsync<T>.Enumerable : |
| | 1 | 92 | | Neverending( generator, asyncProvider ).Take( count ); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the result of given potentially asynchronous item f |
| | | 97 | | /// </summary> |
| | | 98 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 99 | | /// <param name="asyncGenerator">The potentially asynchronous callback to generate an item to repeat.</param> |
| | | 100 | | /// <param name="count">Amount of times to repeat the item.</param> |
| | | 101 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 102 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 103 | | /// <seealso cref="System.Linq.Enumerable.Repeat{TResult}(TResult, Int32)"/> |
| | | 104 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 105 | | Func<ValueTask<T>> asyncGenerator, |
| | | 106 | | Int32 count, |
| | | 107 | | IAsyncProvider asyncProvider |
| | | 108 | | ) |
| | | 109 | | { |
| | 1 | 110 | | return count <= 0 ? |
| | 1 | 111 | | EmptyAsync<T>.Enumerable : |
| | 1 | 112 | | Neverending( asyncGenerator, asyncProvider ).Take( count ); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the result of given synchronous item factory callba |
| | | 117 | | /// </summary> |
| | | 118 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 119 | | /// <param name="generator">The synchronous callback to generate an item to repeat.</param> |
| | | 120 | | /// <param name="count">Amount of times to repeat the item.</param> |
| | | 121 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 122 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 123 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 124 | | Func<T> generator, |
| | | 125 | | Int64 count, |
| | | 126 | | IAsyncProvider asyncProvider |
| | | 127 | | ) |
| | | 128 | | { |
| | 1 | 129 | | return count <= 0 ? |
| | 1 | 130 | | EmptyAsync<T>.Enumerable : |
| | 1 | 131 | | Neverending( generator, asyncProvider ).Take( count ); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return the result of given potentially asynchronous item f |
| | | 136 | | /// </summary> |
| | | 137 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 138 | | /// <param name="asyncGenerator">The potentially asynchronous callback to generate an item to repeat.</param> |
| | | 139 | | /// <param name="count">Amount of times to repeat the item.</param> |
| | | 140 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 141 | | /// <returns>An empty <see cref="IAsyncEnumerable{T}"/> if <paramref name="count"/> is <c>0</c> or less; otherwise |
| | | 142 | | public static IAsyncEnumerable<T> Repeat<T>( |
| | | 143 | | Func<ValueTask<T>> asyncGenerator, |
| | | 144 | | Int64 count, |
| | | 145 | | IAsyncProvider asyncProvider |
| | | 146 | | ) |
| | | 147 | | { |
| | 1 | 148 | | return count <= 0 ? |
| | 1 | 149 | | EmptyAsync<T>.Enumerable : |
| | 1 | 150 | | Neverending( asyncGenerator, asyncProvider ).Take( count ); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return numbers in given range specification. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="initial">The start of the range, inclusive.</param> |
| | | 158 | | /// <param name="target">The end of the range, exclusive.</param> |
| | | 159 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 160 | | /// <param name="step">The amount to increase for each number within the range. By default, is <c>1</c> for increa |
| | | 161 | | /// <returns>An enumerable that contains numbers within the given range specification.</returns> |
| | | 162 | | /// <remarks> |
| | | 163 | | /// Note that unlike <see cref="System.Linq.Enumerable.Range(Int32, Int32)"/>, this method has exclusive _maximum_ |
| | | 164 | | /// This method also handles both increasing and decreasing number ranges. |
| | | 165 | | /// </remarks> |
| | | 166 | | /// <seealso cref="System.Linq.Enumerable.Range(Int32, Int32)"/> |
| | | 167 | | public static IAsyncEnumerable<Int32> Range( |
| | | 168 | | Int32 initial, |
| | | 169 | | Int32 target, |
| | | 170 | | IAsyncProvider asyncProvider, |
| | | 171 | | Int32 step = 1 |
| | | 172 | | ) |
| | | 173 | | { |
| | | 174 | | IAsyncEnumerable<Int32> retVal; |
| | 9 | 175 | | if ( target > initial ) |
| | | 176 | | { |
| | | 177 | | // Increasing range from initial to target, step must be 1 or greater |
| | 7 | 178 | | step = Math.Max( 1, step ); |
| | 7 | 179 | | retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable( () => |
| | 7 | 180 | | { |
| | 14 | 181 | | var current = initial; |
| | 14 | 182 | | return AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo( |
| | 14 | 183 | | ( out Boolean success ) => |
| | 14 | 184 | | { |
| | 45 | 185 | | var prev = current; |
| | 45 | 186 | | success = current < target; |
| | 45 | 187 | | current += step; |
| | 45 | 188 | | return prev; |
| | 14 | 189 | | } ); |
| | 7 | 190 | | }, asyncProvider ); |
| | 7 | 191 | | } |
| | 2 | 192 | | else if ( initial == target ) |
| | | 193 | | { |
| | | 194 | | // Empty |
| | 1 | 195 | | retVal = EmptyAsync<Int32>.Enumerable; |
| | 1 | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | | 199 | | // Decreasing range from target to initial, step must be -1 or less |
| | 1 | 200 | | step = Math.Min( -1, step ); |
| | 1 | 201 | | retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable( () => |
| | 1 | 202 | | { |
| | 2 | 203 | | var current = initial; |
| | 2 | 204 | | return AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo( |
| | 2 | 205 | | ( out Boolean success ) => |
| | 2 | 206 | | { |
| | 18 | 207 | | var prev = current; |
| | 18 | 208 | | success = current > target; |
| | 18 | 209 | | current += step; // Notice it is plus, since the "step" variable is always negative. |
| | 18 | 210 | | return prev; |
| | 2 | 211 | | } ); |
| | 1 | 212 | | }, asyncProvider ); |
| | | 213 | | } |
| | | 214 | | |
| | 9 | 215 | | return retVal; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will return numbers in given range specification, as 64-bit int |
| | | 220 | | /// </summary> |
| | | 221 | | /// <param name="initial">The start of the range, inclusive.</param> |
| | | 222 | | /// <param name="target">The end of the range, exclusive.</param> |
| | | 223 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 224 | | /// <param name="step">The amount to increase for each number within the range. By default, is <c>1</c> for increa |
| | | 225 | | /// <returns>An enumerable that contains numbers within the given range specification.</returns> |
| | | 226 | | /// <remarks> |
| | | 227 | | /// Note that unlike <see cref="System.Linq.Enumerable.Range(Int32, Int32)"/>, this method has exclusive _maximum_ |
| | | 228 | | /// This method also handles both increasing and decreasing number ranges. |
| | | 229 | | /// </remarks> |
| | | 230 | | /// <seealso cref="System.Linq.Enumerable.Range(Int32, Int32)"/> |
| | | 231 | | public static IAsyncEnumerable<Int64> Range( |
| | | 232 | | Int64 initial, |
| | | 233 | | Int64 target, |
| | | 234 | | IAsyncProvider asyncProvider, |
| | | 235 | | Int64 step = 1 |
| | | 236 | | ) |
| | | 237 | | { |
| | | 238 | | IAsyncEnumerable<Int64> retVal; |
| | 3 | 239 | | if ( target > initial ) |
| | | 240 | | { |
| | | 241 | | // Increasing range from initial to target, step must be 1 or greater |
| | 1 | 242 | | step = Math.Max( 1, step ); |
| | 1 | 243 | | retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable( () => |
| | 1 | 244 | | { |
| | 2 | 245 | | var current = initial; |
| | 2 | 246 | | return AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo( |
| | 2 | 247 | | ( out Boolean success ) => |
| | 2 | 248 | | { |
| | 18 | 249 | | var prev = current; |
| | 18 | 250 | | success = current < target; |
| | 18 | 251 | | current += step; |
| | 18 | 252 | | return prev; |
| | 2 | 253 | | } ); |
| | 1 | 254 | | }, asyncProvider ); |
| | 1 | 255 | | } |
| | 2 | 256 | | else if ( initial == target ) |
| | | 257 | | { |
| | | 258 | | // Empty |
| | 1 | 259 | | retVal = EmptyAsync<Int64>.Enumerable; |
| | 1 | 260 | | } |
| | | 261 | | else |
| | | 262 | | { |
| | | 263 | | // Decreasing range from target to initial, step must be -1 or less |
| | 1 | 264 | | step = Math.Min( -1, step ); |
| | 1 | 265 | | retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable( () => |
| | 1 | 266 | | { |
| | 2 | 267 | | var current = initial; |
| | 2 | 268 | | return AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo( |
| | 2 | 269 | | ( out Boolean success ) => |
| | 2 | 270 | | { |
| | 18 | 271 | | var prev = current; |
| | 18 | 272 | | success = current > target; |
| | 18 | 273 | | current += step; // Notice it is plus, since the "step" variable is always negative. |
| | 18 | 274 | | return prev; |
| | 2 | 275 | | } ); |
| | 1 | 276 | | }, asyncProvider ); |
| | | 277 | | } |
| | | 278 | | |
| | 3 | 279 | | return retVal; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will indefinetly repeat the given value. |
| | | 284 | | /// </summary> |
| | | 285 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 286 | | /// <param name="item">An item to repeat.</param> |
| | | 287 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 288 | | /// <returns>An enumerable that will indefinetly repeat the given value.</returns> |
| | | 289 | | public static IAsyncEnumerable<T> Neverending<T>( |
| | | 290 | | T item, |
| | | 291 | | IAsyncProvider asyncProvider |
| | | 292 | | ) |
| | | 293 | | { |
| | 3 | 294 | | return AsyncEnumerationFactory.CreateStatelessWrappingEnumerable( AsyncEnumerationFactory.CreateSynchronousWrap |
| | 3 | 295 | | ( out Boolean success ) => |
| | 3 | 296 | | { |
| | 14 | 297 | | success = true; |
| | 14 | 298 | | return item; |
| | 3 | 299 | | } |
| | 3 | 300 | | ), asyncProvider ); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will indefinetly repeat the value returned by given synchronous |
| | | 305 | | /// </summary> |
| | | 306 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 307 | | /// <param name="generator">A synchronous callback to dynamically generate the value to repeat.</param> |
| | | 308 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 309 | | /// <returns>An enumerable that will indefinetly repeat the given value.</returns> |
| | | 310 | | public static IAsyncEnumerable<T> Neverending<T>( |
| | | 311 | | Func<T> generator, |
| | | 312 | | IAsyncProvider asyncProvider |
| | | 313 | | ) |
| | | 314 | | { |
| | 7 | 315 | | return AsyncEnumerationFactory.CreateStatelessWrappingEnumerable( AsyncEnumerationFactory.CreateSynchronousWrap |
| | 7 | 316 | | ( out Boolean success ) => |
| | 7 | 317 | | { |
| | 54 | 318 | | success = true; |
| | 54 | 319 | | return generator(); |
| | 7 | 320 | | } |
| | 7 | 321 | | ), asyncProvider ); |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Returns <see cref="IAsyncEnumerable{T}"/> that will indefinetly repeat the value returned by given potentially |
| | | 326 | | /// </summary> |
| | | 327 | | /// <typeparam name="T">The type of item to repeat.</typeparam> |
| | | 328 | | /// <param name="asyncGenerator">A potentially asynchronous callback to dynamically generate the value to repeat.< |
| | | 329 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | | 330 | | /// <returns>An enumerable that will indefinetly repeat the given value.</returns> |
| | | 331 | | public static IAsyncEnumerable<T> Neverending<T>( |
| | | 332 | | Func<ValueTask<T>> asyncGenerator, |
| | | 333 | | IAsyncProvider asyncProvider |
| | | 334 | | ) |
| | | 335 | | { |
| | 4 | 336 | | return AsyncEnumerationFactory.CreateSequentialEnumerable( () => AsyncEnumerationFactory.CreateSequentialStartI |
| | 4 | 337 | | async () => |
| | 4 | 338 | | { |
| | 24 | 339 | | return (true, await asyncGenerator()); |
| | 24 | 340 | | }, |
| | 4 | 341 | | null |
| | 4 | 342 | | ), asyncProvider ); |
| | | 343 | | } |
| | | 344 | | } |
| | | 345 | | } |