forked from ddrilling/AsbCloudServer
122 lines
3.7 KiB
C#
122 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AsbCloudInfrastructure
|
|
{
|
|
public static class Helper
|
|
{
|
|
public static T Max<T>(params T[] items)
|
|
where T : IComparable
|
|
{
|
|
var count = items.Length;
|
|
if (count < 1)
|
|
throw new ArgumentException("Count of params must be greater than 1");
|
|
|
|
var max = items[0];
|
|
for (var i = 1; i < count; i++)
|
|
if (max.CompareTo(items[i]) < 0)
|
|
max = items[i];
|
|
|
|
return max;
|
|
}
|
|
|
|
public static T Min<T>(params T[] items)
|
|
where T : IComparable
|
|
{
|
|
var count = items.Length;
|
|
if (count < 1)
|
|
throw new ArgumentException("Count of params must be greater than 1");
|
|
|
|
var min = items[0];
|
|
for (var i = 1; i < count; i++)
|
|
if (min.CompareTo(items[i]) > 0)
|
|
min = items[i];
|
|
|
|
return min;
|
|
}
|
|
|
|
public static (T min, T max) MinMax<T>(params T[] items)
|
|
where T : IComparable
|
|
{
|
|
var count = items.Length;
|
|
if (count < 1)
|
|
throw new ArgumentException("Count of params must be greater than 1");
|
|
|
|
var min = items[0];
|
|
var max = items[0];
|
|
for (var i = 1; i < count; i++)
|
|
if (max.CompareTo(items[i]) < 0)
|
|
max = items[i];
|
|
else if (min.CompareTo(items[i]) > 0)
|
|
min = items[i];
|
|
|
|
return (min, max);
|
|
}
|
|
|
|
public static List<Tuple<T, T>> MergeArrays<T, Tid>(IEnumerable<T> array1, IEnumerable<T> array2, Func<T, Tid> getId)
|
|
where Tid : IComparable
|
|
where T : class
|
|
{
|
|
var a1 = array1.ToArray();
|
|
var a2 = array2.ToArray();
|
|
|
|
var m = new List<Tuple<T, T>>((a1.Length + a2.Length)/3);
|
|
void Add(T item1, T item2) =>
|
|
m.Add(new Tuple<T, T>(item1, item2));
|
|
|
|
int i1 = 0;
|
|
int i2 = 0;
|
|
while (true)
|
|
{
|
|
var is1 = a1.Length > i1;
|
|
var is2 = a2.Length > i2;
|
|
if (!(is1 || is2))
|
|
break;
|
|
|
|
if (is1 && is2)
|
|
{
|
|
if (getId(a1[i1]).CompareTo(getId(a2[i2])) == 0)
|
|
Add(a1[i1++], a2[i2++]);
|
|
else
|
|
{
|
|
int nextI1 = Array.IndexOf(a1, a2[i2], i1);
|
|
int nextI2 = Array.IndexOf(a2, a1[i1], i2);
|
|
|
|
if ((nextI1 > -1) && ((nextI2 == -1) || ((nextI1 - i1) < (nextI2 - i2))))
|
|
{
|
|
for (; i1 < nextI1; i1++)
|
|
Add(a1[i1], null);
|
|
}
|
|
|
|
if ((nextI2 > -1) && ((nextI1 == -1) || ((nextI1 - i1) > (nextI2 - i2))))
|
|
{
|
|
for (; i2 < nextI2; i2++)
|
|
Add(null, a2[i2]);
|
|
}
|
|
|
|
if ((nextI1 == -1) && (nextI2 == -1))
|
|
{
|
|
for (; i2 < a2.Length; i2++)
|
|
Add(null, a2[i2]);
|
|
|
|
for (; i1 < a1.Length; i1++)
|
|
Add(a1[i1], null);
|
|
}
|
|
}
|
|
}
|
|
else if (is1)
|
|
{
|
|
Add(a1[i1++], null);
|
|
}
|
|
else if (is2)
|
|
{
|
|
Add(null, a2[i2++]);
|
|
}
|
|
}
|
|
|
|
return m;
|
|
}
|
|
}
|
|
}
|