| | | 1 | | #if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP_2_0_OR_GREATER |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | |
| | | 7 | | namespace DirectSight.Common; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods on <see cref="IEnumerable{T}"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class EnumerableExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 18 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 19 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 20 | | /// <returns>The sum of the projected values.</returns> |
| | | 21 | | public static int SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector) |
| | 9156 | 22 | | { |
| | | 23 | | checked |
| | 9156 | 24 | | { |
| | | 25 | | try |
| | 9156 | 26 | | { |
| | 9156 | 27 | | return source.Sum(selector); |
| | | 28 | | } |
| | 1 | 29 | | catch (OverflowException) |
| | 1 | 30 | | { |
| | 1 | 31 | | return int.MaxValue; |
| | | 32 | | } |
| | | 33 | | } |
| | 9156 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea |
| | | 38 | | /// </summary> |
| | | 39 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 40 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 41 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 42 | | /// <returns>The sum of the projected values.</returns> |
| | | 43 | | public static int? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector) |
| | 3618 | 44 | | { |
| | | 45 | | checked |
| | 3618 | 46 | | { |
| | | 47 | | try |
| | 3618 | 48 | | { |
| | 3618 | 49 | | return source.Sum(selector); |
| | | 50 | | } |
| | 0 | 51 | | catch (OverflowException) |
| | 0 | 52 | | { |
| | 0 | 53 | | return int.MaxValue; |
| | | 54 | | } |
| | | 55 | | } |
| | 3618 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Computes the sum of the sequence of System.Int32 values that are obtained by invoking a transform function on ea |
| | | 60 | | /// </summary> |
| | | 61 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 62 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 63 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 64 | | /// <returns>The sum of the projected values.</returns> |
| | | 65 | | public static int? SafeSumNullable<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector) |
| | 0 | 66 | | { |
| | 0 | 67 | | if (source.All(x => !selector(x).HasValue)) |
| | 0 | 68 | | { |
| | 0 | 69 | | return null; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return source.SafeSum(selector); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Computes the sum of the sequence of System.Int64 values that are obtained by invoking a transform function on ea |
| | | 77 | | /// </summary> |
| | | 78 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 79 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 80 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 81 | | /// <returns>The sum of the projected values.</returns> |
| | | 82 | | public static long SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector) |
| | 2 | 83 | | { |
| | | 84 | | checked |
| | 2 | 85 | | { |
| | | 86 | | try |
| | 2 | 87 | | { |
| | 2 | 88 | | return source.Sum(selector); |
| | | 89 | | } |
| | 1 | 90 | | catch (OverflowException) |
| | 1 | 91 | | { |
| | 1 | 92 | | return long.MaxValue; |
| | | 93 | | } |
| | | 94 | | } |
| | 2 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Computes the sum of the sequence of System.Int64 values that are obtained by invoking a transform function on ea |
| | | 99 | | /// </summary> |
| | | 100 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 101 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 102 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 103 | | /// <returns>The sum of the projected values.</returns> |
| | | 104 | | public static long? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector) |
| | 0 | 105 | | { |
| | | 106 | | checked |
| | 0 | 107 | | { |
| | | 108 | | try |
| | 0 | 109 | | { |
| | 0 | 110 | | return source.Sum(selector); |
| | | 111 | | } |
| | 0 | 112 | | catch (OverflowException) |
| | 0 | 113 | | { |
| | 0 | 114 | | return long.MaxValue; |
| | | 115 | | } |
| | | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Computes the sum of the sequence of decimal values that are obtained by invoking a transform function on each el |
| | | 121 | | /// </summary> |
| | | 122 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 123 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 124 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 125 | | /// <returns>The sum of the projected values.</returns> |
| | | 126 | | public static decimal SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector) |
| | 2 | 127 | | { |
| | | 128 | | checked |
| | 2 | 129 | | { |
| | | 130 | | try |
| | 2 | 131 | | { |
| | 2 | 132 | | return source.Sum(selector); |
| | | 133 | | } |
| | 1 | 134 | | catch (OverflowException) |
| | 1 | 135 | | { |
| | 1 | 136 | | return decimal.MaxValue; |
| | | 137 | | } |
| | | 138 | | } |
| | 2 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Computes the sum of the sequence of decimal values that are obtained by invoking a transform function on each el |
| | | 143 | | /// </summary> |
| | | 144 | | /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| | | 145 | | /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| | | 146 | | /// <param name="selector">A transform function to apply to each element.</param> |
| | | 147 | | /// <returns>The sum of the projected values.</returns> |
| | | 148 | | public static decimal? SafeSum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector) |
| | 0 | 149 | | { |
| | | 150 | | checked |
| | 0 | 151 | | { |
| | | 152 | | try |
| | 0 | 153 | | { |
| | 0 | 154 | | return source.Sum(selector); |
| | | 155 | | } |
| | 0 | 156 | | catch (OverflowException) |
| | 0 | 157 | | { |
| | 0 | 158 | | return decimal.MaxValue; |
| | | 159 | | } |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | private static IEnumerable<T> KeepElementByCount<T>(this IEnumerable<T> source, int count) |
| | 0 | 164 | | { |
| | 0 | 165 | | var queue = new Queue<T>(count); |
| | 0 | 166 | | foreach (var item in source) |
| | 0 | 167 | | { |
| | 0 | 168 | | if (queue.Count == count) |
| | 0 | 169 | | { |
| | 0 | 170 | | queue.Dequeue(); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | queue.Enqueue(item); |
| | 0 | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | return queue; |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | private static IEnumerable<T> TakeLast<T>(IList<T> list, int count) |
| | 0 | 180 | | { |
| | 0 | 181 | | for (int i = list.Count - count; i < list.Count; i++) |
| | 0 | 182 | | { |
| | 0 | 183 | | yield return list[i]; |
| | 0 | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | #endif |