using System;
using System.Collections.Generic;
namespace OpenTap
{
///
/// Named Utils2 to avoid clashing with Utils from the Shared project.
///
static class Utils2
{
public static bool IsLooped(List<(Guid, Guid)> waitingFor, Guid item)
{
Stack stack = new Stack();
stack.Push(item);
HashSet visited = new HashSet();
while (stack.Count > 0)
{
var current = stack.Pop();
if (!visited.Add(current))
{
continue;
}
foreach (var (waiter, waited) in waitingFor)
{
if (waiter == current)
{
if (waited == item)
return true;
stack.Push(waited);
}
}
}
return false;
}
public static Action Bind(this Action del, Action f, T v)
{
if (del == null)
return () => f(v);
del += () => f(v);
return del;
}
public static bool IsSortedBy(this IList values, Func desc)
{
var n = values.Count;
if (n <= 1) return true;
double v0 = desc(values[0]);
for (int i = 1; i < n; i++)
{
var v1 = desc(values[i]);
if (v0 > v1)
return false;
v0 = v1;
}
return true;
}
public static void Shuffle(this List list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
(list[k], list[n]) = (list[n], list[k]);
}
}
}
}