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

Adds Sort and Reverse methods to ListPool #75

Open
wants to merge 3 commits into
base: main
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
18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
/src/ListPool/bin
/tests/ListPool.Netstandard2_0.UnitTests/bin
/tests/ListPool.Netstandard2_0.UnitTests/obj
/tests/ListPool.Resolvers.Utf8Json.Tests/obj
/tests/ListPool.Resolvers.Utf8Json.Tests/bin/Debug/netcoreapp3.1
/src/ListPool.Resolvers.Utf8Json/obj
/src/ListPool.Resolvers.Utf8Json/bin/Debug/netstandard2.0
/src/ListPool.Resolvers.Utf8Json/bin
/tests/ListPool.Resolvers.Utf8Json.Tests/bin/Release/netcoreapp3.1
/tests/ListPool.Serializers.Utf8Json.Formatters.UnitTests/obj
/tests/ListPool.Serializers.Utf8Json.Formatters.UnitTests/bin
/tests/ListPool.Serializers.Utf8Json.Resolvers.UnitTests/obj
/tests/ListPool.Serializers.Utf8Json.Resolvers.UnitTests/bin
/tests/ListPool.Serializers.SystemTextJson.Converters.UnitTests/obj
/tests/ListPool.Serializers.SystemTextJson.Converters.UnitTests/bin
src/ListPool.Serializers.Utf8Json.Formatters/obj
src/ListPool.Serializers.Utf8Json.Formatters/bin
src/ListPool.Serializers.Utf8Json.Resolvers/obj
src/ListPool.Serializers.Utf8Json.Resolvers/bin
src/ListPool.Serializers.SystemTextJson.Converters/obj
src/ListPool.Serializers.SystemTextJson.Converters/bin
/ListPool.Utf8Json.Tests/obj
/ListPool.Utf8Json.Tests/bin
43 changes: 43 additions & 0 deletions src/ListPool/ListPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,49 @@ public void EnsureCapacity(int capacity)
/// <param name="offset"></param>
public void SetOffsetManually(int offset) => Count = offset;

/// <summary>
/// Sorts the elements in the entire ListPool using the default comparer.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Sort() => Sort(0, Count, null);

/// <summary>
/// Sorts the elements in the entire ListPool using the specified comparer.
/// </summary>
/// <param name="comparer">The IComparer<T> implementation to use when comparing elements, or null to use the default comparer Comparer<T>.Default.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Sort(IComparer<T> comparer) => Sort(0, Count, comparer);

/// <summary>
/// Sorts the elements in a range of elements in the ListPool using the specified comparer.
/// </summary>
/// <param name="index">The zero-based starting index of the range to sort.</param>
/// <param name="count">The length of the range to sort.</param>
/// <param name="comparer">The IComparer<T> implementation to use when comparing elements, or null to use the default comparer Comparer<T>.Default.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Sort(int index, int count, IComparer<T> comparer) => Array.Sort<T>(_items, index, count, comparer);

/// <summary>
/// Sorts the elements in the entire ListPool using the specified Comparison<T>.
/// </summary>
/// <param name="comparison">The Comparison<T> to use when comparing elements.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Sort(Comparison<T> comparison) => Array.Sort<T>(_items, 0, Count, Comparer<T>.Create(comparison));

/// <summary>
/// Reverses the order of the elements in the entire ListPool.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reverse() => Array.Reverse(_items, 0, Count);

/// <summary>
/// Reverses the order of the elements in the specified range.
/// </summary>
/// <param name="index">The zero-based starting index of the range to reverse.</param>
/// <param name="count">The length of the range to reverse.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reverse(int index, int count) => Array.Reverse(_items, index, count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() => new Enumerator(_items, Count);

Expand Down
107 changes: 106 additions & 1 deletion tests/ListPool.Netstandard2_0.UnitTests/ListPool/ListPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ public void EnsureCapacity_increase_capacity_of_inner_buffer()
Assert.Equal(biggerCapacity, sut.Capacity);
}

[Fact]
[Fact]
public void GetRawBuffer_returns_underline_buffer()
{
int[] expectedItems = s_fixture.CreateMany<int>().ToArray();
Expand Down Expand Up @@ -799,5 +799,110 @@ public void SetOffsetManually_allows_to_set_decrease_internal_offset()
Assert.Equal(expectedItems[i], sut[i]);
}
}

// New Sort and Reverse tests

[Fact]
public void Sort_sorts_items_in_ascending_order()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in unsortedItems)
{
Console.WriteLine(item);
sut.Add(item);
}

var sortedItems = unsortedItems.OrderBy(x => x).ToList();

sut.Sort();
Assert.Equal(sortedItems, sut.ToArray());
}

[Fact]
public void Sort_with_comparer_sorts_items_in_custom_order()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in unsortedItems)
{
sut.Add(item);
}

var sortedItems = unsortedItems.OrderByDescending(x => x).ToList();

sut.Sort(Comparer<int>.Create((x, y) => y.CompareTo(x))); // Descending order
Assert.Equal(sortedItems, sut.ToArray());
}

[Fact]
public void Sort_with_index_and_count_sorts_specified_range()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in unsortedItems)
{
sut.Add(item);
}

var sortedItems = unsortedItems.Skip(2).Take(5).OrderBy(x => x).ToList();
sortedItems.InsertRange(0, unsortedItems.Take(2));
sortedItems.AddRange(unsortedItems.Skip(7));

sut.Sort(2, 5, null); // Sort a range
Assert.Equal(sortedItems, sut.ToArray());
}

[Fact]
public void Sort_with_comparison_sorts_items_in_custom_order()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> unsortedItems = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in unsortedItems)
{
sut.Add(item);
}

var sortedItems = unsortedItems.OrderByDescending(x => x).ToList();

sut.Sort((x, y) => y.CompareTo(x)); // Descending order
Assert.Equal(sortedItems, sut.ToArray());
}

[Fact]
public void Reverse_reverses_the_entire_list()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> items = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in items)
{
sut.Add(item);
}

sut.Reverse();

var reversedItems = items.AsEnumerable().Reverse().ToList();
Assert.Equal(reversedItems, sut.ToArray());
}

[Fact]
public void Reverse_with_index_and_count_reverses_specified_range()
{
using ListPool<int> sut = new ListPool<int>(20);
List<int> items = s_fixture.CreateMany<int>(10).ToList();
foreach (var item in items)
{
sut.Add(item);
}

sut.Reverse(2, 5);

var expectedItems = items.Take(2)
.Concat(items.Skip(2).Take(5).Reverse())
.Concat(items.Skip(7))
.ToList();

Assert.Equal(expectedItems, sut.ToArray());
}
}
}