| | | 1 | | namespace Syki.Shared; |
| | | 2 | | |
| | | 3 | | public static class ListExtensions |
| | | 4 | | { |
| | | 5 | | public static bool IsSubsetOf(this List<Guid> selfs, List<Guid> others) |
| | | 6 | | { |
| | 1166 | 7 | | HashSet<Guid> set = []; |
| | 11756 | 8 | | foreach (var self in selfs) |
| | | 9 | | { |
| | 4926 | 10 | | if (!set.Add(self)) return false; |
| | | 11 | | |
| | 5324 | 12 | | if (!others.Contains(self)) return false; |
| | | 13 | | } |
| | | 14 | | |
| | 750 | 15 | | return true; |
| | 416 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static bool IsEquivalentTo(this List<Guid> selfs, List<Guid> others) |
| | | 19 | | { |
| | 0 | 20 | | if (selfs.Count != others.Count) return false; |
| | | 21 | | |
| | 0 | 22 | | return selfs.IsSubsetOf(others); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public static bool IsAllDistinct(this IEnumerable<int> list) |
| | | 26 | | { |
| | 2 | 27 | | if (list is null) return true; |
| | | 28 | | |
| | 2 | 29 | | var set = new HashSet<int>(); |
| | 80 | 30 | | foreach (var x in list) |
| | | 31 | | { |
| | 38 | 32 | | if (!set.Add(x)) return false; |
| | | 33 | | } |
| | | 34 | | |
| | 2 | 35 | | return true; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static bool IsAllDistinct(this IEnumerable<string> list) |
| | | 39 | | { |
| | 2 | 40 | | if (list is null) return true; |
| | | 41 | | |
| | 2 | 42 | | var set = new HashSet<string>(); |
| | 80 | 43 | | foreach (var x in list) |
| | | 44 | | { |
| | 38 | 45 | | if (!set.Add(x)) return false; |
| | | 46 | | } |
| | | 47 | | |
| | 2 | 48 | | return true; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public static List<AgendaDayOut> ToAgendas(this List<EnrollmentClassOut> classes) |
| | | 52 | | { |
| | 12 | 53 | | var agendas = new List<AgendaDayOut>(); |
| | | 54 | | |
| | 76 | 55 | | foreach (var @class in classes) |
| | | 56 | | { |
| | 116 | 57 | | foreach (var schedule in @class.Schedules) |
| | | 58 | | { |
| | 32 | 59 | | var discipline = new AgendaDisciplineOut { ClassId = @class.Id, Name = @class.Discipline, Start = schedu |
| | | 60 | | |
| | 72 | 61 | | var agenda = agendas.FirstOrDefault(a => a.Day == schedule.Day); |
| | 32 | 62 | | if (agenda == null) |
| | | 63 | | { |
| | 24 | 64 | | agenda = new AgendaDayOut { Day = schedule.Day }; |
| | 24 | 65 | | agenda.Disciplines.Add(discipline); |
| | 24 | 66 | | agendas.Add(agenda); |
| | 24 | 67 | | continue; |
| | | 68 | | } |
| | | 69 | | |
| | 8 | 70 | | agenda.Disciplines.Add(discipline); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | 28 | 74 | | agendas = agendas.OrderBy(a => a.Day).ToList(); |
| | 72 | 75 | | foreach (var agenda in agendas) |
| | | 76 | | { |
| | 38 | 77 | | agenda.Disciplines = agenda.Disciplines.OrderBy(d => d.Start).ToList(); |
| | | 78 | | } |
| | | 79 | | |
| | 12 | 80 | | return agendas; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public static T PickRandom<T>(this IEnumerable<T> source) |
| | | 84 | | { |
| | 658 | 85 | | return source.PickRandom(1).Single(); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count) |
| | | 89 | | { |
| | 670 | 90 | | return source.Shuffle().Take(count); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) |
| | | 94 | | { |
| | 2822940 | 95 | | return source.OrderBy(x => Guid.CreateVersion7()); |
| | | 96 | | } |
| | | 97 | | } |