From 1c4f4874b766c6482b87fea023f1dafe94ead18e Mon Sep 17 00:00:00 2001 From: Michael Krisper <michael.krisper@tugraz.at> Date: Fri, 20 Aug 2021 10:51:02 +0200 Subject: [PATCH] moved and refactored some extension methods --- VectoCommon/VectoCommon/Utils/EnumHelper.cs | 17 +++-- .../Utils/EnumerableExtensionMethods.cs | 64 +++++++++++++++---- .../Utils/DictionaryExtensionMethods.cs | 9 --- 3 files changed, 59 insertions(+), 31 deletions(-) diff --git a/VectoCommon/VectoCommon/Utils/EnumHelper.cs b/VectoCommon/VectoCommon/Utils/EnumHelper.cs index 39d890167c..40cd386359 100644 --- a/VectoCommon/VectoCommon/Utils/EnumHelper.cs +++ b/VectoCommon/VectoCommon/Utils/EnumHelper.cs @@ -38,15 +38,14 @@ namespace TUGraz.VectoCommon.Utils { public static class EnumHelper { - public static T ParseEnum<T>(this string s, bool ignoreCase = true) - { - var r = new Regex("[^a-zA-Z0-9_]"); - return (T)Enum.Parse(typeof(T), r.Replace(s, ""), ignoreCase); - } + public static T ParseEnum<T>(this string s, bool ignoreCase = true) => + (T)Enum.Parse(typeof(T), Regex.Replace(s, "[^a-zA-Z0-9_-]", ""), ignoreCase); - public static IEnumerable<T> GetValues<T>() - { - return Enum.GetValues(typeof(T)).Cast<T>(); - } + public static T ParseEnum<T>(this object o, bool ignoreCase = true) => + o is string s ? ParseEnum<T>(s, ignoreCase) : (T)o; + + + public static IEnumerable<T> GetValues<T>() => + Enum.GetValues(typeof(T)).Cast<T>(); } } \ No newline at end of file diff --git a/VectoCommon/VectoCommon/Utils/EnumerableExtensionMethods.cs b/VectoCommon/VectoCommon/Utils/EnumerableExtensionMethods.cs index 6cce2041f3..4a57e53b14 100644 --- a/VectoCommon/VectoCommon/Utils/EnumerableExtensionMethods.cs +++ b/VectoCommon/VectoCommon/Utils/EnumerableExtensionMethods.cs @@ -39,9 +39,24 @@ namespace TUGraz.VectoCommon.Utils { public static class EnumerableExtensionMethods { - public static IEnumerable<(T value, int index)> Select<T>(this IEnumerable<T> self) => + public static IEnumerable<(T value, int index)> Select<T>(this IEnumerable<T> self) => self.Select((value, index) => (value, index)); + public static T[] Slice<T>(this T[] source, int start, int end) + { + if (start < 0) start = source.Length + start; + if (end < 0) end = source.Length + end; + var dest = new T[end-start]; + Array.ConstrainedCopy(source, start, dest, 0, end - start); + return dest; + } + + /// <summary> + /// Joins the items of the enumerable into a string. + /// </summary> + public static string JoinString(this IEnumerable<string> self, string separator = ", ") => + string.Join(separator, self ?? Enumerable.Empty<string>()); + public static IEnumerable<double> ToDouble(this IEnumerable<string> self, double? defaultValue = null) { @@ -102,8 +117,27 @@ namespace TUGraz.VectoCommon.Utils /// Zips all elements of two enumerable together. If the enumerables dont have the same length an exception is thrown. /// </summary> /// <exception cref="System.InvalidOperationException">Enumeration already finished. Thrown if the enumerables dont have the same length.</exception> - public static IEnumerable<(T1 Item1, T2 Item2)> Zip<T1,T2>(this IEnumerable<T1> self, IEnumerable<T2> other) => - self.ZipAll(other, (arg1, arg2) => (arg1,arg2)); + public static IEnumerable<(T1 Item1, T2 Item2)> Zip<T1, T2>(this IEnumerable<T1> self, IEnumerable<T2> other) => + self.ZipAll(other, (arg1, arg2) => (arg1, arg2)); + + /// <summary> + /// Zips all elements of two enumerable together. If the enumerables dont have the same length an exception is thrown. + /// </summary> + /// <exception cref="System.InvalidOperationException">Enumeration already finished. Thrown if the enumerables dont have the same length.</exception> + public static IEnumerable<(T1 Item1, T2 Item2, T3 Item3)> Zip<T1, T2, T3>(this IEnumerable<T1> item1, IEnumerable<T2> item2, IEnumerable<T3> item3) + { + using (var first = item1.GetEnumerator()) { + using (var second = item2.GetEnumerator()) { + using (var third = item3.GetEnumerator()) { + while (first.MoveNext() | second.MoveNext() | third.MoveNext()) { + yield return (first.Current, second.Current, third.Current); + } + } + } + } + } + + /// <summary> /// Sums up the values of selector. @@ -229,22 +263,26 @@ namespace TUGraz.VectoCommon.Utils public static IEnumerable<TResult> Pairwise<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TSource, TResult> resultSelector) { - var previous = default(TSource); - using (var it = source.GetEnumerator()) { if (it.MoveNext()) { - previous = it.Current; - } - - while (it.MoveNext()) { - yield return resultSelector(previous, previous = it.Current); + var previous = it.Current; + while (it.MoveNext()) { + yield return resultSelector(previous, previous = it.Current); + } } } } - public static IEnumerable<Tuple<TSource, TSource>> Pairwise<TSource>(this IEnumerable<TSource> source) + public static IEnumerable<(TSource, TSource)> Pairwise<TSource>(this IEnumerable<TSource> source) { - return Pairwise(source, Tuple.Create); + using (var it = source.GetEnumerator()) { + if (it.MoveNext()) { + var previous = it.Current; + while (it.MoveNext()) { + yield return (previous, previous = it.Current); + } + } + } } /// <summary> @@ -291,7 +329,7 @@ namespace TUGraz.VectoCommon.Utils /// <param name="item5"></param> /// <param name="item6"></param> /// <param name="item7"></param> - public static void Deconstruct<T>(this IEnumerable<T> values, out T item1, out T item2, + public static void Deconstruct<T>(this IEnumerable<T> values, out T item1, out T item2, out T item3, out T item4, out T item5, out T item6, out T item7) { using (var enumerator = values.GetEnumerator()) { diff --git a/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs b/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs index 8e65cd7a3d..6e57338fe2 100644 --- a/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs +++ b/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs @@ -67,14 +67,5 @@ namespace TUGraz.VectoCore.Utils public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue defaultValue) => self.TryGetValue(key, out var value) ? value : defaultValue; - - - /// <summary> - /// Joins the items of the enumerable into a string. - /// </summary> - /// <param name="self"></param> - /// <param name="separator"></param> - /// <returns></returns> - public static string JoinString(this IEnumerable<string> self, string separator = ", ") => string.Join(separator, self ?? Enumerable.Empty<string>()); } } \ No newline at end of file -- GitLab