1+ using System ;
2+ using System . Numerics ;
3+
4+ namespace SpanExtensions
5+ {
6+ public static partial class ReadOnlySpanExtensions
7+ {
8+
9+ #if NET7_0_OR_GREATER
10+
11+ /// <summary>
12+ /// Computes the Average of all the values in <paramref name="source"/>.
13+ /// </summary>
14+ /// <typeparam name="T">The type of elements in the <see cref="ReadOnlySpan{T}"/>.</typeparam>
15+ /// <param name="source">The <see cref="ReadOnlySpan{T}"/> to operate on.</param>
16+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
17+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
18+ public static T Average < T > ( this ReadOnlySpan < T > source ) where T : INumberBase < T >
19+ {
20+ T sum = source . Sum ( ) ;
21+ return sum / T . CreateChecked ( source . Length ) ;
22+ }
23+ #else
24+
25+ #if NET5_0_OR_GREATER
26+
27+ /// <summary>
28+ /// Computes the Average of all the values in <paramref name="source"/>.
29+ /// </summary>
30+ /// <param name="source">The <see cref="ReadOnlySpan{Half}"/> to operate on.</param>
31+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
32+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
33+ public static Half Average ( this ReadOnlySpan < Half > source )
34+ {
35+ Half sum = source . Sum ( ) ;
36+ return ( Half ) ( ( float ) sum / source . Length ) ;
37+ }
38+ #endif
39+
40+ /// <summary>
41+ /// Computes the Average of all the values in <paramref name="source"/>.
42+ /// </summary>
43+ /// <param name="source">The <see cref="ReadOnlySpan{Byte}"/> to operate on.</param>
44+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
45+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
46+ public static byte Average ( this ReadOnlySpan < byte > source )
47+ {
48+ byte sum = source . Sum ( ) ;
49+ return ( byte ) ( sum / source . Length ) ;
50+ }
51+
52+ /// <summary>
53+ /// Computes the Average of all the values in <paramref name="source"/>.
54+ /// </summary>
55+ /// <param name="source">The <see cref="ReadOnlySpan{UInt16}"/> to operate on.</param>
56+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
57+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
58+ public static ushort Average ( this ReadOnlySpan < ushort > source )
59+ {
60+ ushort sum = source . Sum ( ) ;
61+ return ( ushort ) ( sum / source . Length ) ;
62+ }
63+
64+ /// <summary>
65+ /// Computes the Average of all the values in <paramref name="source"/>.
66+ /// </summary>
67+ /// <param name="source">The <see cref="ReadOnlySpan{uint32}"/> to operate on.</param>
68+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
69+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
70+ public static uint Average ( this ReadOnlySpan < uint > source )
71+ {
72+ uint sum = source . Sum ( ) ;
73+ return ( uint ) ( sum / source . Length ) ;
74+ }
75+
76+ /// <summary>
77+ /// Computes the Average of all the values in <paramref name="source"/>.
78+ /// </summary>
79+ /// <param name="source">The <see cref="ReadOnlySpan{UInt64}"/> to operate on.</param>
80+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
81+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
82+ public static ulong Average ( this ReadOnlySpan < ulong > source )
83+ {
84+ ulong sum = source . Sum ( ) ;
85+ return sum / ( ulong ) source . Length ;
86+ }
87+
88+ /// <summary>
89+ /// Computes the Average of all the values in <paramref name="source"/>.
90+ /// </summary>
91+ /// <param name="source">The <see cref="ReadOnlySpan{SByte}"/> to operate on.</param>
92+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
93+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
94+ public static sbyte Average ( this ReadOnlySpan < sbyte > source )
95+ {
96+ sbyte sum = source . Sum ( ) ;
97+ return ( sbyte ) ( sum / source . Length ) ;
98+ }
99+
100+ /// <summary>
101+ /// Computes the Average of all the values in <paramref name="source"/>.
102+ /// </summary>
103+ /// <param name="source">The <see cref="ReadOnlySpan{Int16}"/> to operate on.</param>
104+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
105+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
106+ public static short Average ( this ReadOnlySpan < short > source )
107+ {
108+ short sum = source . Sum ( ) ;
109+ return ( short ) ( sum / source . Length ) ;
110+ }
111+
112+ /// <summary>
113+ /// Computes the Average of all the values in <paramref name="source"/>.
114+ /// </summary>
115+ /// <param name="source">The <see cref="ReadOnlySpan{Int32}"/> to operate on.</param>
116+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
117+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
118+ public static int Average ( this ReadOnlySpan < int > source )
119+ {
120+ int sum = source . Sum ( ) ;
121+ return sum / source . Length ;
122+ }
123+
124+ /// <summary>
125+ /// Computes the Average of all the values in <paramref name="source"/>.
126+ /// </summary>
127+ /// <param name="source">The <see cref="ReadOnlySpan{Int64}"/> to operate on.</param>
128+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
129+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
130+ public static long Average ( this ReadOnlySpan < long > source )
131+ {
132+ long sum = source . Sum ( ) ;
133+ return ( sum / source . Length ) ;
134+ }
135+
136+ /// <summary>
137+ /// Computes the Average of all the values in <paramref name="source"/>.
138+ /// </summary>
139+ /// <param name="source">The <see cref="ReadOnlySpan{Single}"/> to operate on.</param>
140+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
141+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
142+ public static float Average ( this ReadOnlySpan < float > source )
143+ {
144+ float sum = source . Sum ( ) ;
145+ return ( float ) ( sum / source . Length ) ;
146+ }
147+
148+ /// <summary>
149+ /// Computes the Average of all the values in <paramref name="source"/>.
150+ /// </summary>
151+ /// <param name="source">The <see cref="ReadOnlySpan{Double}"/> to operate on.</param>
152+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
153+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
154+ public static double Average ( this ReadOnlySpan < double > source )
155+ {
156+ double sum = source . Sum ( ) ;
157+ return ( double ) ( sum / source . Length ) ;
158+ }
159+
160+ /// <summary>
161+ /// Computes the Average of all the values in <paramref name="source"/>.
162+ /// </summary>
163+ /// <param name="source">The <see cref="ReadOnlySpan{Int64}"/> to operate on.</param>
164+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
165+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
166+ public static decimal Average ( this ReadOnlySpan < decimal > source )
167+ {
168+ decimal sum = source . Sum ( ) ;
169+ return sum / source . Length ;
170+ }
171+
172+ /// <summary>
173+ /// Computes the Average of all the values in <paramref name="source"/>.
174+ /// </summary>
175+ /// <param name="source">The <see cref="ReadOnlySpan{BigInteger}"/> to operate on.</param>
176+ /// <returns>The Average of all the values in <paramref name="source"/>.</returns>
177+ /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
178+ public static BigInteger Average ( this ReadOnlySpan < BigInteger > source )
179+ {
180+ BigInteger sum = source . Sum ( ) ;
181+ return sum / source . Length ;
182+ }
183+ #endif
184+ }
185+ }
0 commit comments