|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | + |
| 3 | +namespace VReflector; |
| 4 | + |
| 5 | +public static class IsSequence |
| 6 | +{ |
| 7 | + public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected) |
| 8 | + { |
| 9 | + if (actual == null || expected == null) |
| 10 | + return actual == expected; |
| 11 | + |
| 12 | + return actual.SequenceEqual(expected); |
| 13 | + } |
| 14 | + public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer) |
| 15 | + { |
| 16 | + if (actual == null || expected == null) |
| 17 | + return actual == expected; |
| 18 | + |
| 19 | + return actual.SequenceEqual(expected, comparer); |
| 20 | + } |
| 21 | + public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected) |
| 22 | + { |
| 23 | + if (actual == null || expected == null) |
| 24 | + return actual == expected; |
| 25 | + |
| 26 | + var actualList = actual.ToList(); |
| 27 | + var expectedList = expected.ToList(); |
| 28 | + |
| 29 | + if (actualList.Count != expectedList.Count) |
| 30 | + return false; |
| 31 | + |
| 32 | + actualList.Sort(); |
| 33 | + expectedList.Sort(); |
| 34 | + |
| 35 | + return actualList.SequenceEqual(expectedList); |
| 36 | + } |
| 37 | + public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer) |
| 38 | + { |
| 39 | + if (actual == null || expected == null) |
| 40 | + return actual == expected; |
| 41 | + |
| 42 | + var actualList = actual.ToList(); |
| 43 | + var expectedList = expected.ToList(); |
| 44 | + |
| 45 | + if (actualList.Count != expectedList.Count) |
| 46 | + return false; |
| 47 | + |
| 48 | + actualList.Sort(); |
| 49 | + expectedList.Sort(); |
| 50 | + |
| 51 | + return actualList.SequenceEqual(expectedList, comparer); |
| 52 | + } |
| 53 | + public static bool AreSpansEqual([DisallowNull] this ReadOnlySpan<char> span1, [NotNullWhen(true)] ReadOnlySpan<char> span2) |
| 54 | + { |
| 55 | + return span1.SequenceEqual(span2); |
| 56 | + } |
| 57 | + public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence) |
| 58 | + { |
| 59 | + var set = new HashSet<T>(); |
| 60 | + foreach (var item in sequence) |
| 61 | + { |
| 62 | + if (!set.Add(item)) |
| 63 | + return false; |
| 64 | + } |
| 65 | + return true; |
| 66 | + } |
| 67 | + public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence, IEqualityComparer<T> comparer) |
| 68 | + { |
| 69 | + var set = new HashSet<T>(comparer); |
| 70 | + foreach (var item in sequence) |
| 71 | + { |
| 72 | + if (!set.Add(item)) |
| 73 | + return false; |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + public static (long DuplicateCount, long Count) GetDuplicateItems<T>([DisallowNull] this IEnumerable<T> sequence, IEqualityComparer<T> comparer = null) |
| 78 | + { |
| 79 | + long nonUniqueCount = 0; |
| 80 | + long allCount = 0; |
| 81 | + if (sequence != null) |
| 82 | + { |
| 83 | + HashSet<T> set = comparer != null ? new HashSet<T>(comparer) : new HashSet<T>(); |
| 84 | + |
| 85 | + foreach (var item in sequence) |
| 86 | + { |
| 87 | + allCount++; |
| 88 | + if (!set.Add(item)) |
| 89 | + { |
| 90 | + nonUniqueCount++; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return (nonUniqueCount, allCount); |
| 95 | + } |
| 96 | + public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T> |
| 97 | + { |
| 98 | + if (collection == null || !collection.Any()) |
| 99 | + { |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + T previous = collection.First(); |
| 104 | + |
| 105 | + foreach (T item in collection.Skip(1)) |
| 106 | + { |
| 107 | + if (previous.CompareTo(item) >= 0) |
| 108 | + { |
| 109 | + return false; |
| 110 | + } |
| 111 | + previous = item; |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | + } |
| 116 | + public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T> |
| 117 | + { |
| 118 | + if (collection == null || !collection.Any()) |
| 119 | + { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + T previous = collection.First(); |
| 124 | + |
| 125 | + foreach (T item in collection.Skip(1)) |
| 126 | + { |
| 127 | + if (previous.CompareTo(item) <= 0) |
| 128 | + { |
| 129 | + return false; |
| 130 | + } |
| 131 | + previous = item; |
| 132 | + } |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | + public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T> |
| 137 | + { |
| 138 | + if (collection == null || !collection.Any()) |
| 139 | + { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + T previous = collection.First(); |
| 144 | + |
| 145 | + foreach (T item in collection.Skip(1)) |
| 146 | + { |
| 147 | + if (previous.CompareTo(item) > 0) |
| 148 | + { |
| 149 | + return false; |
| 150 | + } |
| 151 | + previous = item; |
| 152 | + } |
| 153 | + |
| 154 | + return true; |
| 155 | + } |
| 156 | + public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T> |
| 157 | + { |
| 158 | + if (collection == null || !collection.Any()) |
| 159 | + { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + T previous = collection.First(); |
| 164 | + |
| 165 | + foreach (T item in collection.Skip(1)) |
| 166 | + { |
| 167 | + if (previous.CompareTo(item) < 0) |
| 168 | + { |
| 169 | + return false; |
| 170 | + } |
| 171 | + previous = item; |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | + } |
| 176 | + public static bool IsSubset<T>([DisallowNull] this IEnumerable<T> subset, IEnumerable<T> superset) |
| 177 | + { |
| 178 | + if (subset == null || superset == null) |
| 179 | + { |
| 180 | + return false; |
| 181 | + } |
| 182 | + var supersetHashSet = new HashSet<T>(superset); |
| 183 | + return subset.All(item => supersetHashSet.Contains(item)); |
| 184 | + } |
| 185 | + public static IDictionary<TKey, TValue> MergeWith<TKey, TValue>([DisallowNull] this IDictionary<TKey, TValue> first, [DisallowNull] IDictionary<TKey, TValue> second) |
| 186 | + { |
| 187 | + if (first == null || second == null) |
| 188 | + { |
| 189 | + throw new ArgumentNullException(first == null ? nameof(first) : nameof(second)); |
| 190 | + } |
| 191 | + |
| 192 | + var result = new Dictionary<TKey, TValue>(first); |
| 193 | + |
| 194 | + foreach (var kvp in second) |
| 195 | + { |
| 196 | + result[kvp.Key] = kvp.Value; |
| 197 | + } |
| 198 | + |
| 199 | + return result; |
| 200 | + } |
| 201 | + public static bool ContainsAllKeys<TKey, TValue>([DisallowNull] this IDictionary<TKey, TValue> dictionary, [DisallowNull] IEnumerable<TKey> keys) |
| 202 | + { |
| 203 | + if (dictionary == null || keys == null) |
| 204 | + { |
| 205 | + throw new ArgumentNullException(dictionary == null ? nameof(dictionary) : nameof(keys)); |
| 206 | + } |
| 207 | + |
| 208 | + return keys.All(key => dictionary.ContainsKey(key)); |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | +} |
0 commit comments