-
-
Notifications
You must be signed in to change notification settings - Fork 12
Move range #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Move range #584
Changes from 6 commits
719597a
12e4d6b
60fc1de
c1a8dbe
00b5a86
5752529
d0ff98e
8cab48c
555488b
2dc801b
1922769
d006520
f22b599
3f48c9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,4 +72,52 @@ static IEnumerable<T> Core(IEnumerable<T> source, int bufferStartIndex, int buff | |
| yield return e.Current; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a sequence with a range of elements in the source sequence moved to a new offset. | ||
| /// </summary> | ||
| /// <typeparam name="T"> | ||
| /// Type of the source sequence. | ||
| /// </typeparam> | ||
| /// <param name="source"> | ||
| /// The source sequence. | ||
| /// </param> | ||
| /// <param name="range"> | ||
| /// The range of values to move. | ||
| /// </param> | ||
| /// <param name="toIndex"> | ||
| /// The index where the specified range will be moved.</param> | ||
| /// <returns> | ||
| /// A sequence with the specified range moved to the new position. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="source"/> is <see langword="null"/>. | ||
| /// </exception> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// <paramref name="range"/>'s start is less than <c>0</c> or <paramref name="range"/>'s end is before start in the sequence. | ||
| /// </exception> | ||
| /// <remarks> | ||
| /// This operator uses deferred executing and streams its results. | ||
| /// </remarks> | ||
| public static IEnumerable<T> Move<T>(this IEnumerable<T> source, Range range, Index toIndex) | ||
| { | ||
| int? length = 0; | ||
| if (range.Start.IsFromEnd || range.End.IsFromEnd || toIndex.IsFromEnd) | ||
| { | ||
| length = source.TryGetCollectionCount(); | ||
| if (!length.HasValue) | ||
| { | ||
| length = source.GetCollectionCount(); | ||
|
||
| } | ||
| } | ||
| var fromIndex = range.Start.IsFromEnd ? range.Start.GetOffset(length.Value) : range.Start.Value; | ||
|
||
| var count = (range.End.IsFromEnd ? range.End.GetOffset(length.Value) : range.End.Value) - fromIndex; | ||
| var to = toIndex.IsFromEnd ? toIndex.GetOffset(length.Value) : toIndex.Value; | ||
| return source.Move | ||
| ( | ||
| fromIndex, | ||
| count, | ||
| to | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.