//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license
using System.Collections.Generic;
namespace Dotway.BuildTasks.Utils
{
///
/// Compares two sequences.
///
/// Type of item in the sequences.
///
/// Compares elements from the two input sequences in turn. If we
/// run out of list before finding unequal elements, then the shorter
/// list is deemed to be the lesser list.
///
public class EnumerableComparer : IComparer>
{
///
/// Object used for comparing each element.
///
private readonly IComparer comp;
///
/// Create a sequence comparer using the default comparer for T.
///
public EnumerableComparer()
{
comp = Comparer.Default;
}
///
/// Create a sequence comparer, using the specified item comparer
/// for T.
///
/// Comparer for comparing each pair of
/// items from the sequences.
public EnumerableComparer(IComparer comparer)
{
comp = comparer;
}
#region IComparer> Members
///
/// Compare two sequences of T.
///
/// First sequence.
/// Second sequence.
public int Compare(IEnumerable x, IEnumerable y)
{
using (IEnumerator leftIt = x.GetEnumerator())
using (IEnumerator rightIt = y.GetEnumerator())
{
while (true)
{
bool left = leftIt.MoveNext();
bool right = rightIt.MoveNext();
if (!(left || right)) return 0;
if (!left) return -1;
if (!right) return 1;
int itemResult = comp.Compare(leftIt.Current, rightIt.Current);
if (itemResult != 0) return itemResult;
}
}
}
#endregion
}
}