123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- using System.Numerics;
- namespace Universal;
- public static class Extensions
- {
- public static bool TryGetSubValue<TKey1, TKey2, TValue>(this ConcurrentDictionary<TKey1, ConcurrentDictionary<TKey2, TValue>> input, TKey1 key1, TKey2 key2, out TValue? value)
- where TKey1 : notnull
- where TKey2 : notnull
- {
- value = default;
- if (input is null || key1 is null || key2 is null)
- return false;
- if (!input.TryGetValue(key1, out ConcurrentDictionary<TKey2, TValue>? output1) || output1 is null)
- return false;
- if (!output1.TryGetValue(key2, out value) || value is null)
- return false;
- return true;
- }
- public static bool TryGetSubValue<TKey1, TKey2, TValue>(this IDictionary<TKey1, IDictionary<TKey2, TValue>> input, TKey1 key1, TKey2 key2, out TValue? value)
- where TKey1 : notnull
- where TKey2 : notnull
- {
- value = default;
- if (input is null || key1 is null || key2 is null)
- return false;
- if (!input.TryGetValue(key1, out IDictionary<TKey2, TValue>? output1) || output1 is null)
- return false;
- if (!output1.TryGetValue(key2, out value) || value is null)
- return false;
- return true;
- }
- public static bool TryGetValueNotNull<TKey, TValue>(this IDictionary<TKey, TValue> input, TKey key, out TValue value)
- {
- if (!input.TryGetValue(key, out TValue? output) || output is null)
- {
- value = default!;
- return false;
- }
- value = output;
- return true;
- }
- public static bool In<T>(this T input, params T[] values)
- {
- return values.Contains(input);
- }
- public static int Foreach<T>(this IEnumerable<T> t, Action<T>? action = null)
- {
- foreach (var item in t)
- action?.Invoke(item);
- return t.Count();
- }
- public static void For<T>(this IEnumerable<T> t, uint startIndex, uint count, Action<T>? action = null)
- {
- T[] values = [.. t];
- uint endIndex = startIndex + count;
- for (uint i = startIndex; i < endIndex; i++)
- action?.Invoke(values[i]);
- }
- public static bool InRange<T>(this T t, T leftBorder, T rightBorder, bool startInclude = true, bool endInclude = true) where T : INumber<T>
- {
- if (startInclude)
- {
- if (t < leftBorder)
- return false;
- }
- else
- {
- if (t <= leftBorder)
- return false;
- }
- if (endInclude)
- {
- if (t > rightBorder)
- return false;
- }
- else
- {
- if (t >= rightBorder)
- return false;
- }
- return true;
- }
- public static void ForeachWhere<T>(this IEnumerable<T> t, Action<T> action, Func<T, bool> judger)
- {
- foreach (var item in t)
- {
- if (!judger.Invoke(item))
- continue;
- action.Invoke(item);
- }
- }
- public static bool TryForeach<T>(this IEnumerable<T> t, Func<T, bool> func)
- {
- foreach (var item in t)
- {
- if (!func.Invoke(item))
- return false;
- }
- return true;
- }
- public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> arr, int size)
- {
- for (var i = 0; i < arr.Count() / size + 1; i++)
- yield return arr.Skip(i * size).Take(size);
- }
- public static string ToBase64(this string input)
- {
- if (string.IsNullOrEmpty(input))
- return string.Empty;
- byte[] bytes = Encoding.ASCII.GetBytes(input);
- return Convert.ToBase64String(bytes);
- }
- public static string FromBase64(this string input)
- {
- if (string.IsNullOrEmpty(input))
- return string.Empty;
- byte[] bytes = Convert.FromBase64String(input);
- return Encoding.ASCII.GetString(bytes);
- }
- }
- //Require C# 14+
- #if false
- public static class ExtensionTest
- {
- extension<TSource>(TSource source) where TSource : INumber<TSource>
- {
- public bool InRange(TSource leftBorder, TSource rightBorder, bool startInclude = true, bool endInclude = true)
- {
- if (startInclude)
- {
- if (source < leftBorder)
- return false;
- }
- else
- {
- if (source <= leftBorder)
- return false;
- }
- if (endInclude)
- {
- if (source > rightBorder)
- return false;
- }
- else
- {
- if (source >= rightBorder)
- return false;
- }
- return true;
- }
- }
- extension(string input)
- {
- public string ToBase64()
- {
- if (string.IsNullOrEmpty(input))
- return string.Empty;
- byte[] bytes = Encoding.Default.GetBytes(input);
- return Convert.ToBase64String(bytes);
- }
- public string FromBase64()
- {
- if (string.IsNullOrEmpty(input))
- return string.Empty;
- byte[] bytes = Convert.FromBase64String(input);
- return Encoding.Default.GetString(bytes);
- }
- }
- extension<TSource>(TSource source) where TSource : struct
- {
- public bool ValueIn(params TSource[] values) => values.Contains(source);
- }
- extension<TSource>(TSource source) where TSource : class
- {
- public bool ReferenceIn(params TSource[] values) => values.Contains(source);
- }
- extension<TSource>(IEnumerable<TSource> sources)
- {
- public bool IsEmpty => !sources.Any();
- public void For(uint startIndex, uint count, Action<TSource> action)
- {
- TSource[] values = [.. sources];
- uint endIndex = startIndex + count;
- for (uint i = startIndex; i < endIndex; i++)
- action?.Invoke(values[i]);
- }
- public void Foreach(Action<TSource> action)
- {
- foreach (TSource source in sources)
- action?.Invoke(source);
- }
- public int ConditionForeach(Action<TSource> action, Func<TSource, bool> condition)
- {
- int count = 0;
- foreach (TSource source in sources)
- {
- if (!condition.Invoke(source))
- continue;
- action.Invoke(source);
- count++;
- }
- return count;
- }
- public IEnumerable<IEnumerable<TSource>> Splits(int size)
- {
- for (var i = 0; i < sources.Count() / size + 1; i++)
- yield return sources.Skip(i * size).Take(size);
- }
- }
- }
- #endif
|