mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-26 13:58:46 +00:00
Add IList<T>.Swap (#62)
This commit is contained in:
parent
3a5b017a72
commit
02765b8b19
@ -9,6 +9,7 @@
|
|||||||
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
|
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
|
||||||
- X10D: Added `DirectoryInfo.Clear([bool])`
|
- X10D: Added `DirectoryInfo.Clear([bool])`
|
||||||
- X10D: Added `IList<T>.RemoveRange(Range)`
|
- X10D: Added `IList<T>.RemoveRange(Range)`
|
||||||
|
- X10D: Added `IList<T>.Swap(IList<T>)` (#62)
|
||||||
- X10D: Added `Point.IsOnLine(LineF)`, `Point.IsOnLine(PointF, PointF)`, and `Point.IsOnLine(Vector2, Vector2)`
|
- X10D: Added `Point.IsOnLine(LineF)`, `Point.IsOnLine(PointF, PointF)`, and `Point.IsOnLine(Vector2, Vector2)`
|
||||||
- X10D: Added `PointF.IsOnLine(LineF)`, `PointF.IsOnLine(PointF, PointF)`, and `PointF.IsOnLine(Vector2, Vector2)`
|
- X10D: Added `PointF.IsOnLine(LineF)`, `PointF.IsOnLine(PointF, PointF)`, and `PointF.IsOnLine(Vector2, Vector2)`
|
||||||
- X10D: Added `Point.ToSize()`
|
- X10D: Added `Point.ToSize()`
|
||||||
|
@ -154,4 +154,57 @@ public class ListTests
|
|||||||
{
|
{
|
||||||
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)null)!.Shuffle());
|
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)null)!.Shuffle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Swap_ShouldThrowArgumentNullException_GivenNullSource()
|
||||||
|
{
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => ((IList<int>?)null)!.Swap(new List<int>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Swap_ShouldThrowArgumentNullException_GivenNullTarget()
|
||||||
|
{
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => new List<int>().Swap(null!));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Swap_ShouldSwapElements_GivenMatchingElementCount()
|
||||||
|
{
|
||||||
|
var first = new List<int> {1, 2, 3};
|
||||||
|
var second = new List<int> {4, 5, 6};
|
||||||
|
|
||||||
|
first.Swap(second);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(new[] {4, 5, 6}, first, string.Join(' ', first));
|
||||||
|
CollectionAssert.AreEqual(new[] {1, 2, 3}, second, string.Join(' ', second));
|
||||||
|
|
||||||
|
first.Swap(second);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(new[] {1, 2, 3}, first, string.Join(' ', first));
|
||||||
|
CollectionAssert.AreEqual(new[] {4, 5, 6}, second, string.Join(' ', second));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Swap_ShouldSwapElements_GivenDifferentElementCount()
|
||||||
|
{
|
||||||
|
var first = new List<int>
|
||||||
|
{
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5
|
||||||
|
};
|
||||||
|
var second = new List<int> {6, 7};
|
||||||
|
|
||||||
|
first.Swap(second);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(new[] {6, 7}, first, string.Join(' ', first));
|
||||||
|
CollectionAssert.AreEqual(new[] {1, 2, 3, 4, 5}, second, string.Join(' ', second));
|
||||||
|
|
||||||
|
first.Swap(second);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(new[] {1, 2, 3, 4, 5}, first, string.Join(' ', first));
|
||||||
|
CollectionAssert.AreEqual(new[] {6, 7}, second, string.Join(' ', second));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -191,4 +191,56 @@ public static class ListExtensions
|
|||||||
(source[count], source[index]) = (source[index], source[count]);
|
(source[count], source[index]) = (source[index], source[count]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Swaps all elements in a list with the elements in another list.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source">The first list.</param>
|
||||||
|
/// <param name="other">The second list.</param>
|
||||||
|
/// <typeparam name="T">The type of the elements in <paramref name="source" /> and <paramref name="other" />.</typeparam>
|
||||||
|
/// <exception cref="ArgumentNullException">
|
||||||
|
/// <para><paramref name="source" /> is <see langword="null" />.</para>
|
||||||
|
/// -or-
|
||||||
|
/// <para><paramref name="other" /> is <see langword="null" />.</para>
|
||||||
|
/// </exception>
|
||||||
|
public static void Swap<T>(this IList<T> source, IList<T> other)
|
||||||
|
{
|
||||||
|
#if NET6_0_OR_GREATER
|
||||||
|
ArgumentNullException.ThrowIfNull(source);
|
||||||
|
ArgumentNullException.ThrowIfNull(other);
|
||||||
|
#else
|
||||||
|
if (source is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(source));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(other));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int min = System.Math.Min(source.Count, other.Count);
|
||||||
|
for (var index = 0; index < min; index++)
|
||||||
|
{
|
||||||
|
(source[index], other[index]) = (other[index], source[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.Count < source.Count)
|
||||||
|
{
|
||||||
|
for (int index = min; index < source.Count;)
|
||||||
|
{
|
||||||
|
other.Add(source[index]);
|
||||||
|
source.RemoveAt(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (source.Count < other.Count)
|
||||||
|
{
|
||||||
|
for (int index = min; index < other.Count;)
|
||||||
|
{
|
||||||
|
source.Add(other[index]);
|
||||||
|
other.RemoveAt(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user