Skip to content
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

MoveEnd operator #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions MoreLinq.Test/MoveEnd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2017 Leandro F. Vieira (leandromoh). All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq.Test
{
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture]
public class MoveEndTest
{
[Test]
public void MoveEndEndWithNegativeFromIndex()
{
AssertThrowsArgument.OutOfRangeException("fromIndex", () =>
new[] { 1 }.MoveEnd(-1, 0, 0));
}

[Test]
public void MoveEndWithNegativeCount()
{
AssertThrowsArgument.OutOfRangeException("count", () =>
new[] { 1 }.MoveEnd(0, -1, 0));
}

[Test]
public void MoveEndWithNegativeToIndex()
{
AssertThrowsArgument.OutOfRangeException("toIndex", () =>
new[] { 1 }.MoveEnd(0, 0, -1));
}

[Test]
public void MoveEndIsLazy()
{
new BreakingSequence<int>().MoveEnd(0, 0, 0);
}

[TestCaseSource(nameof(MoveEndSource))]
public void MoveEnd(int length, int fromIndex, int count, int toIndex)
{
var source = Enumerable.Range(0, length);

var exclude = source.Exclude(fromIndex, count);
var slice = source.Slice(fromIndex, count);
var expectations = exclude.SkipLast(toIndex).Concat(slice).Concat(exclude.TakeLast(toIndex));

using (var test = source.AsTestingSequence())
{
var result = test.MoveEnd(fromIndex, count, toIndex);
Assert.That(result, Is.EquivalentTo(expectations));
}
}

public static IEnumerable<object> MoveEndSource()
{
const int length = 10;

return from index in Enumerable.Range(0, length)
from count in Enumerable.Range(0, length + 1)
select new TestCaseData(length, index, count, index);
}

[TestCaseSource(nameof(MoveEndWithSequenceShorterThanToIndexSource))]
public void MoveEndWithSequenceShorterThanToIndex(int length, int fromIndex, int count, int toIndex)
{
var source = Enumerable.Range(0, length);

var exclude = source.Exclude(fromIndex, count);
var slice = source.Slice(fromIndex, count);

var expectations = slice.Concat(exclude);

using (var test = source.AsTestingSequence())
{
var result = test.MoveEnd(fromIndex, count, toIndex);
Assert.That(result, Is.EquivalentTo(expectations));
}
}

public static IEnumerable<object> MoveEndWithSequenceShorterThanToIndexSource()
{
const int length = 10;

return Enumerable.Range(length, length + 5)
.Select(toIndex => new TestCaseData(length, 5, 2, toIndex));
}

[Test]
public void MoveEndWithFromIndexEqualsToIndex()
{
var source = Enumerable.Range(0, 10);
var result = source.MoveEnd(5, 999, 5);

Assert.That(source, Is.SameAs(result));
}

[Test]
public void MoveEndWithCountEqualsZero()
{
var source = Enumerable.Range(0, 10);
var result = source.MoveEnd(5, 0, 999);

Assert.That(source, Is.SameAs(result));
}
}
}
93 changes: 93 additions & 0 deletions MoreLinq/MoveEnd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2017 Leandro F. Vieira (leandromoh). All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq
{
using System;
using System.Collections.Generic;

static partial class MoreEnumerable
{
/// <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="fromIndex">
/// The zero-based index identifying the first element in the range of
/// elements to move.</param>
/// <param name="count">The count of items 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>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
/// <example>
/// <code><![CDATA[
/// var result = Enumerable.Range(0, 6).Move(3, 2, 0);
/// ]]></code>
/// The <c>result</c> variable will contain <c>{ 3, 4, 0, 1, 2, 5 }</c>.
/// </example>

public static IEnumerable<T> MoveEnd<T>(this IEnumerable<T> source, int fromIndex, int count, int toIndex)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (fromIndex < 0) throw new ArgumentOutOfRangeException(nameof(fromIndex), "The source index cannot be negative.");
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Count cannot be negative.");
if (toIndex < 0) throw new ArgumentOutOfRangeException(nameof(toIndex), "Target index of range to move cannot be negative.");

if (toIndex == fromIndex || count == 0)
return source;

return _(); IEnumerable<T> _()
{
var queue = new Queue<T>(toIndex);
var i = -1;
var endIndex = fromIndex + count - 1;
var list = new List<T>();

foreach (var item in source)
{
i++;

if (fromIndex <= i && i <= endIndex)
{
list.Add(item);
continue;
}

if (queue.Count < toIndex)
{
queue.Enqueue(item);
continue;
}

yield return queue.Dequeue();
queue.Enqueue(item);
}

foreach (var item in list) yield return item;

foreach (var item in queue) yield return item;
}
}
}
}