mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Util method for getting a sequence as a series of adjacent pairs
This commit is contained in:
@@ -331,6 +331,25 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.AreEqual(8, items[2][1]);
|
Assert.AreEqual(8, items[2][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAdjacentPairsTest()
|
||||||
|
{
|
||||||
|
IEnumerable<int> a = new int[] {};
|
||||||
|
Assert.AreEqual(0, a.GetAdjacentPairs().Count());
|
||||||
|
|
||||||
|
IEnumerable<int> b = new int[] { 1 };
|
||||||
|
Assert.AreEqual(0, b.GetAdjacentPairs().Count());
|
||||||
|
|
||||||
|
IEnumerable<int> c = new int[] { 1, 2, 3 };
|
||||||
|
int[][] cPairs = c.GetAdjacentPairs().ToArray();
|
||||||
|
|
||||||
|
Assert.AreEqual(2, cPairs.Length);
|
||||||
|
Assert.AreEqual(1, cPairs[0][0]);
|
||||||
|
Assert.AreEqual(2, cPairs[0][1]);
|
||||||
|
Assert.AreEqual(2, cPairs[1][0]);
|
||||||
|
Assert.AreEqual(3, cPairs[1][1]);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void MinByTest()
|
public void MinByTest()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -563,6 +563,27 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given a sequence [A, B, C] returns a sequence [[A, B], [B, C]]
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IEnumerable<T[]> GetAdjacentPairs<T>(this IEnumerable<T> extends)
|
||||||
|
{
|
||||||
|
T previous = default(T);
|
||||||
|
bool first = true;
|
||||||
|
|
||||||
|
foreach (T item in extends)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
yield return new T[] {previous, item};
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
previous = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the minimal element of the given sequence, based on
|
/// Returns the minimal element of the given sequence, based on
|
||||||
/// the given projection.
|
/// the given projection.
|
||||||
|
|||||||
Reference in New Issue
Block a user