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

Exclude: Add test, fix implementation #669

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions MoreLinq.Test/ExcludeTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. 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 NUnit.Framework;
Expand Down Expand Up @@ -143,5 +160,14 @@ public void TestExcludeStartIndexGreaterThanSequenceLength()

Assert.That(result, Is.EqualTo(sequence));
}

[TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 6, ExpectedResult = new int[0])]
[TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 6, ExpectedResult = new[] { 0, 1 })]
[TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 0, 3, ExpectedResult = new[] { 3, 4, 5 })]
[TestCase(new[] { 0, 1, 2, 3, 4, 5 }, 2, 3, ExpectedResult = new[] { 0, 1, 5 })]
public int[] TestExcludeOnKnownInput(int[] source, int startIndex, int count)
{
return source.AsTestingSequence().Exclude(startIndex, count).ToArray();
}
}
}
22 changes: 17 additions & 5 deletions MoreLinq/Exclude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,30 @@ public static IEnumerable<T> Exclude<T>(this IEnumerable<T> sequence, int startI

return _(); IEnumerable<T> _()
{
var index = -1;
var index = 0;
var endIndex = startIndex + count;
var iter = sequence.GetEnumerator();
using var iter = sequence.GetEnumerator();

// yield the first part of the sequence
while (iter.MoveNext() && ++index < startIndex)
for (; index < startIndex; index++)
{
if (!iter.MoveNext())
yield break;
yield return iter.Current;
}

// skip the next part (up to count items)
while (++index < endIndex && iter.MoveNext())
continue;
for (; index < endIndex; index++)
{
if (!iter.MoveNext())
yield break;
}

// yield the remainder of the sequence
while (iter.MoveNext())
{
yield return iter.Current;
}
}
}
}
Expand Down