Skip to content

Commit

Permalink
major: update
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Sep 12, 2024
1 parent 447679f commit 77718ae
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 20 deletions.
21 changes: 21 additions & 0 deletions src/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class CollectionExtensions
/// </summary>
public static void AddRange<T>(this ICollection<T> self, IEnumerable<T> items)
{
ArgumentNullException.ThrowIfNull(self);
ArgumentNullException.ThrowIfNull(items);
switch (self)
{
Expand All @@ -27,4 +28,24 @@ public static void AddRange<T>(this ICollection<T> self, IEnumerable<T> items)
break;
}
}

/// <summary>
/// Removes the list item at the specified index.
/// </summary>
public static void RemoveAt<T>(this IList<T> self, Index index)
{
ArgumentNullException.ThrowIfNull(self);
var offset = index.GetOffset(self.Count);
self.RemoveAt(offset);
}

/// <summary>
/// Removes the list item at the specified range.
/// </summary>
public static void RemoveRange<T>(this List<T> self, Range range)
{
ArgumentNullException.ThrowIfNull(self);
var (offset, length) = range.GetOffsetAndLength(self.Count);
self.RemoveRange(offset, length);
}
}
36 changes: 18 additions & 18 deletions src/EnumerablePlus/LinqEnumerablePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,25 @@ public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random? rand
switch (source)
{
case ICollection<T> collection:
{
var copy = collection.ToArray();
random.Shuffle(copy);
return copy;
}
{
var copy = collection.ToArray();
random.Shuffle(copy);
return copy;
}
case IReadOnlyCollection<T> readOnly:
{
int count = readOnly.Count;
if (count == 0)
return Array.Empty<T>();

var result = new T[count];
var index = 0;
foreach (var item in readOnly)
result[index++] = item;

random.Shuffle(result);
return result;
}
{
int count = readOnly.Count;
if (count == 0)
return Array.Empty<T>();

var result = new T[count];
var index = 0;
foreach (var item in readOnly)
result[index++] = item;

random.Shuffle(result);
return result;
}
default:
return source.OrderBy(_ => random.Next());
}
Expand Down
4 changes: 2 additions & 2 deletions src/RangeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public static IEnumerable<TResult> SelectMany<TResult>(
Func<int, int, TResult> project)
{
foreach (var n1 in range)
foreach (var n2 in projection(n1))
yield return project(n1, n2);
foreach (var n2 in projection(n1))
yield return project(n1, n2);
}

/// <summary>
Expand Down
60 changes: 60 additions & 0 deletions tests/CSharpPlus.Tests/CollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace CSharpPlus.Tests;

#pragma warning disable S6605
#pragma warning disable S6602

public class CollectionExtensionsTests
{
[Test]
public void ShouldAddRange()
{
List<int> currentItems = [1, 2, 3];
int[] newItems = [4, 5, 6];
int[] expected = [1, 2, 3, 4, 5, 6];

currentItems.AddRange(newItems);
currentItems.Should().BeEquivalentTo(expected);
}

[Test]
public void ShouldRemoveRange()
{
List<int> currentItems = [1, 2, 3, 4, 5, 6];
int[] expected = [1, 2, 3];
var removeRange = 3..6;

currentItems.RemoveRange(removeRange);
currentItems.Should().BeEquivalentTo(expected);
}

[Test]
public void ShouldRemoveRangeMid()
{
List<int> currentItems = [1, 2, 3, 4, 5, 6];
int[] expected = [1, 6];
var removeRange = 1..^1;

currentItems.RemoveRange(removeRange);
currentItems.Should().BeEquivalentTo(expected);
}

[Test]
public void ShouldRemoveLast()
{
List<int> currentItems = [1, 2, 3, 4];
int[] expected = [1, 2, 3];

currentItems.RemoveAt(^1);
currentItems.Should().BeEquivalentTo(expected);
}

[Test]
public void ShouldRemoveBeforeLast()
{
List<int> currentItems = [1, 2, 3, 4];
int[] expected = [1, 2, 4];

currentItems.RemoveAt(^2);
currentItems.Should().BeEquivalentTo(expected);
}
}

0 comments on commit 77718ae

Please sign in to comment.