chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
namespace OpenTap
{
    /// <summary>
    /// Named Utils2 to avoid clashing with Utils from the Shared project.
    /// </summary>
    static class Utils2
    {
        public static bool IsLooped(List<(Guid, Guid)> waitingFor, Guid item)
        {
            Stack<Guid> stack = new Stack<Guid>();
            stack.Push(item);
            HashSet<Guid> visited = new HashSet<Guid>();
            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<T>(this Action del, Action<T> f, T v)
        {
            if (del == null)
                return () => f(v);
 
            del += () => f(v);
            return del;
        }
 
        public static bool IsSortedBy<T>(this IList<T> values, Func<T, double> 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<T>(this List<T> 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]);
            }
        }   
    }
}