mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Merge branch 'SequenceComparer' of Common/Utils into dev
This commit is contained in:
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|||||||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Added
|
||||||
|
- Adding SequenceComparer for ordering collections of lists, arrays, etc
|
||||||
|
|
||||||
## [3.6.0] - 2018-06-19
|
## [3.6.0] - 2018-06-19
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
33
ICD.Common.Utils.Tests/Comparers/SequenceComparerTest.cs
Normal file
33
ICD.Common.Utils.Tests/Comparers/SequenceComparerTest.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using ICD.Common.Utils.Comparers;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Tests.Comparers
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public sealed class SequenceComparerTest
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void CompareTest()
|
||||||
|
{
|
||||||
|
SequenceComparer<int> comparer = new SequenceComparer<int>();
|
||||||
|
|
||||||
|
// Equal
|
||||||
|
int[] a = {1, 2, 3};
|
||||||
|
int[] b = {1, 2, 3};
|
||||||
|
|
||||||
|
Assert.AreEqual(0, comparer.Compare(a, b));
|
||||||
|
|
||||||
|
// A comes before B
|
||||||
|
a = new[] {1, 2};
|
||||||
|
b = new[] {1, 2, 3};
|
||||||
|
|
||||||
|
Assert.AreEqual(-1, comparer.Compare(a, b));
|
||||||
|
|
||||||
|
// B comes before A
|
||||||
|
a = new[] { 2, 2, 3 };
|
||||||
|
b = new[] { 1, 2, 3 };
|
||||||
|
|
||||||
|
Assert.AreEqual(1, comparer.Compare(a, b));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
66
ICD.Common.Utils/Comparers/SequenceComparer.cs
Normal file
66
ICD.Common.Utils/Comparers/SequenceComparer.cs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Comparers
|
||||||
|
{
|
||||||
|
public sealed class SequenceComparer<T> : IComparer<IEnumerable<T>>
|
||||||
|
{
|
||||||
|
private readonly IComparer<T> m_ItemComparer;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
public SequenceComparer()
|
||||||
|
: this(Comparer<T>.Default)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
public SequenceComparer(IComparer<T> comparer)
|
||||||
|
{
|
||||||
|
if (comparer == null)
|
||||||
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
|
m_ItemComparer = comparer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Compare(IEnumerable<T> x, IEnumerable<T> y)
|
||||||
|
{
|
||||||
|
if (x == null)
|
||||||
|
throw new ArgumentNullException("x");
|
||||||
|
|
||||||
|
if (y == null)
|
||||||
|
throw new ArgumentNullException("y");
|
||||||
|
|
||||||
|
using (IEnumerator<T> firstPos = x.GetEnumerator())
|
||||||
|
{
|
||||||
|
using (IEnumerator<T> secondPos = y.GetEnumerator())
|
||||||
|
{
|
||||||
|
bool hasFirst = firstPos.MoveNext();
|
||||||
|
bool hasSecond = secondPos.MoveNext();
|
||||||
|
|
||||||
|
while (hasFirst && hasSecond)
|
||||||
|
{
|
||||||
|
int result = m_ItemComparer.Compare(firstPos.Current, secondPos.Current);
|
||||||
|
if (result != 0)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
hasFirst = firstPos.MoveNext();
|
||||||
|
hasSecond = secondPos.MoveNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasFirst && !hasSecond)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (!hasFirst)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user