Skip to content

Commit

Permalink
Merge 7a85c95 into c081197
Browse files Browse the repository at this point in the history
  • Loading branch information
Orace authored Jan 20, 2023
2 parents c081197 + 7a85c95 commit e3c5560
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
29 changes: 17 additions & 12 deletions MoreLinq.Test/ExcludeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void TestExcludeNegativeCountException()
[Test]
public void TestExcludeWithCountEqualsZero()
{
var sequence = Enumerable.Range(1, 10);
using var sequence = Enumerable.Range(1, 10).AsTestingSequence();
var resultA = sequence.Exclude(5, 0);

Assert.That(resultA, Is.SameAs(sequence));
Expand Down Expand Up @@ -88,10 +88,11 @@ public void TestExcludeEmptySequence()
public void TestExcludeSequenceHead()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
var xs = Enumerable.Range(1, count);
using var sequence = xs.AsTestingSequence();
var result = sequence.Exclude(0, count / 2);

Assert.That(result, Is.EqualTo(sequence.Skip(count / 2)));
Assert.That(result, Is.EqualTo(xs.Skip(count / 2)));
}

/// <summary>
Expand All @@ -101,10 +102,11 @@ public void TestExcludeSequenceHead()
public void TestExcludeSequenceTail()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
var xs = Enumerable.Range(1, count);
using var sequence = xs.AsTestingSequence();
var result = sequence.Exclude(count / 2, count);

Assert.That(result, Is.EqualTo(sequence.Take(count / 2)));
Assert.That(result, Is.EqualTo(xs.Take(count / 2)));
}

/// <summary>
Expand All @@ -116,10 +118,11 @@ public void TestExcludeSequenceMiddle()
const int count = 10;
const int startIndex = 3;
const int excludeCount = 5;
var sequence = Enumerable.Range(1, count);
var xs = Enumerable.Range(1, count);
using var sequence = xs.AsTestingSequence();
var result = sequence.Exclude(startIndex, excludeCount);

Assert.That(result, Is.EqualTo(sequence.Take(startIndex).Concat(sequence.Skip(startIndex + excludeCount))));
Assert.That(result, Is.EqualTo(xs.Take(startIndex).Concat(xs.Skip(startIndex + excludeCount))));
}

/// <summary>
Expand All @@ -129,7 +132,7 @@ public void TestExcludeSequenceMiddle()
public void TestExcludeEntireSequence()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
using var sequence = Enumerable.Range(1, count).AsTestingSequence();
var result = sequence.Exclude(0, count);

Assert.That(result, Is.Empty);
Expand All @@ -142,10 +145,11 @@ public void TestExcludeEntireSequence()
public void TestExcludeCountGreaterThanSequenceLength()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
var xs = Enumerable.Range(1, count);
using var sequence = xs.AsTestingSequence();
var result = sequence.Exclude(1, count * 10);

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

/// <summary>
Expand All @@ -155,10 +159,11 @@ public void TestExcludeCountGreaterThanSequenceLength()
public void TestExcludeStartIndexGreaterThanSequenceLength()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
var xs = Enumerable.Range(1, count);
using var sequence = xs.AsTestingSequence();
var result = sequence.Exclude(count + 5, count);

Assert.That(result, Is.EqualTo(sequence));
Assert.That(result, Is.EqualTo(xs));
}
}
}
18 changes: 14 additions & 4 deletions MoreLinq/Exclude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ public static IEnumerable<T> Exclude<T>(this IEnumerable<T> sequence, int startI

IEnumerable<T> _()
{
var index = -1;
var index = 0;
var endIndex = startIndex + count;
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

0 comments on commit e3c5560

Please sign in to comment.