Jypeli 10
The simple game programming library
ListHelpers.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5namespace Jypeli
6{
10 public static class ListHelpers
11 {
17 public static double Min( this IEnumerable<double> values )
18 {
19 double min = double.PositiveInfinity;
20
21 foreach ( var value in values )
22 {
23 if ( value < min ) min = value;
24 }
25
26 return min;
27 }
28
34 public static double Max( this IEnumerable<double> values )
35 {
36 double max = double.NegativeInfinity;
37
38 foreach ( var value in values )
39 {
40 if ( value > max ) max = value;
41 }
42
43 return max;
44 }
45
51 public static double Average( this IEnumerable<double> values )
52 {
53 double sum = 0;
54 int count = 0;
55
56 foreach ( var value in values )
57 {
58 sum += value;
59 count++;
60 }
61
62 return sum / count;
63 }
64
65#if JYPELI
66
72 public static Vector Average( this IEnumerable<Vector> values )
73 {
74 double xsum = 0, ysum = 0;
75 int count = 0;
76
77 foreach ( var value in values )
78 {
79 xsum += value.X;
80 ysum += value.Y;
81 count++;
82 }
83
84 return new Vector( xsum / count, ysum / count );
85 }
86
87#endif
88
89#if WINDOWS_STOREAPP
98 public delegate TOutput Converter<in TInput, out TOutput>(TInput input);
99
106 public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
107 {
108 foreach (T item in items)
109 {
110 action( item );
111 }
112 }
113#endif
114
115 //TODO: Iso osa seuraavista metodeista on jo toteutettu standardikirjastoon.
116#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
117
126 public static IEnumerable<TOutput> ConvertAll<TInput, TOutput>( this IEnumerable<TInput> items, Converter<TInput, TOutput> converter )
127 {
128 // Huom/TK: ConvertAll<TOutput>-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
129
130 List<TOutput> outList = new List<TOutput>();
131
132 foreach ( TInput item in items )
133 {
134 outList.Add( converter( item ) );
135 }
136
137 return outList;
138 }
139
140
141 public static List<T> FindAll<T>( this IEnumerable<T> items, Predicate<T> pred )
142 {
143 // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
144
145 List<T> outList = new List<T>();
146
147 foreach ( var item in items )
148 {
149 if ( pred( item ) )
150 outList.Add( item );
151 }
152
153 return outList;
154 }
155
156 public static void AddItems<T>(this List<T> list, params T[] items) =>
157 list.AddRange(items);
158
159 public static void RemoveAll<T>( this List<T> items, Predicate<T> pred )
160 {
161 // Huom/TK: RemoveAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
162
163 foreach ( var item in items.FindAll( pred ) )
164 {
165 items.Remove( item );
166 }
167 }
168
169 public static T Find<T>( this List<T> items, Predicate<T> pred )
170 {
171 // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
172
173#if WINDOWS
174 return items.Find( pred );
175#else
176 foreach ( var item in items )
177 {
178 if ( pred( item ) )
179 return item;
180 }
181
182 return default( T );
183#endif
184 }
185
186 public static T ArrayFind<T>( T[] array, Predicate<T> pred )
187 {
188 // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
189
190#if WINDOWS
191 return Array.Find( array, pred );
192#else
193 for (int i = 0; i < array.Length; i++)
194 {
195 if ( pred( array[i] ) )
196 return array[i];
197 }
198
199 return default( T );
200#endif
201 }
202
203 public static IEnumerable<K> FindAll<K,V>( this Dictionary<K,V>.KeyCollection keys, Predicate<K> pred )
204 {
205 for ( int i = 0; i < keys.Count; i++ )
206 {
207 K key = keys.ElementAt( i );
208 if ( pred( key ) ) yield return key;
209 }
210 }
211
212 public static void ForEach<T>( this T[] array, Action<T> action )
213 {
214 for ( int i = 0; i < array.Length; i++ )
215 {
216 action( array[i] );
217 }
218 }
219 }
220}
Apufunktioita listojen ja muiden tietorakenteiden käyttöön.
Definition: ListHelpers.cs:11
static void ForEach< T >(this T[] array, Action< T > action)
Definition: ListHelpers.cs:212
static double Max(this IEnumerable< double > values)
Laskee maksimin.
Definition: ListHelpers.cs:34
static void AddItems< T >(this List< T > list, params T[] items)
static double Min(this IEnumerable< double > values)
Laskee minimin.
Definition: ListHelpers.cs:17
static List< T > FindAll< T >(this IEnumerable< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:141
static T Find< T >(this List< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:169
static double Average(this IEnumerable< double > values)
Laskee keskiarvon.
Definition: ListHelpers.cs:51
static IEnumerable< K > FindAll< K, V >(this Dictionary< K, V >.KeyCollection keys, Predicate< K > pred)
Definition: ListHelpers.cs:203
static void RemoveAll< T >(this List< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:159
static T ArrayFind< T >(T[] array, Predicate< T > pred)
Definition: ListHelpers.cs:186
static IEnumerable< TOutput > ConvertAll< TInput, TOutput >(this IEnumerable< TInput > items, Converter< TInput, TOutput > converter)
Muuntaa kokoelman tietyn tyyppisiä olioita kokoelmaksi toisen tyyppisiä olioita.
Definition: ListHelpers.cs:126
2D-vektori.
Definition: Vector.cs:67