Jypeli  9
The simple game programming library
ListHelpers.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace 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
90  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 
123  public static IEnumerable<TOutput> ConvertAll<TInput, TOutput>( this IEnumerable<TInput> items, Converter<TInput, TOutput> converter )
124  {
125  // Huom/TK: ConvertAll<TOutput>-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
126 
127  List<TOutput> outList = new List<TOutput>();
128 
129  foreach ( TInput item in items )
130  {
131  outList.Add( converter( item ) );
132  }
133 
134  return outList;
135  }
136 
137  public static List<T> FindAll<T>( this IEnumerable<T> items, Predicate<T> pred )
138  {
139  // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
140 
141  List<T> outList = new List<T>();
142 
143  foreach ( var item in items )
144  {
145  if ( pred( item ) )
146  outList.Add( item );
147  }
148 
149  return outList;
150  }
151 
152  public static void AddItems<T>(this List<T> list, params T[] items) =>
153  list.AddRange(items);
154 
155  public static void RemoveAll<T>( this List<T> items, Predicate<T> pred )
156  {
157  // Huom/TK: RemoveAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
158 
159  foreach ( var item in items.FindAll( pred ) )
160  {
161  items.Remove( item );
162  }
163  }
164 
165  public static T Find<T>( this List<T> items, Predicate<T> pred )
166  {
167  // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
168 
169 #if WINDOWS
170  return items.Find( pred );
171 #else
172  foreach ( var item in items )
173  {
174  if ( pred( item ) )
175  return item;
176  }
177 
178  return default( T );
179 #endif
180  }
181 
182  public static T ArrayFind<T>( T[] array, Predicate<T> pred )
183  {
184  // Huom/TK: FindAll-metodi on jo olemassa, mutta sitä ei ole toteutettu X360/WP7-alustoille.
185 
186 #if WINDOWS
187  return Array.Find( array, pred );
188 #else
189  for (int i = 0; i < array.Length; i++)
190  {
191  if ( pred( array[i] ) )
192  return array[i];
193  }
194 
195  return default( T );
196 #endif
197  }
198 
199  public static IEnumerable<K> FindAll<K,V>( this Dictionary<K,V>.KeyCollection keys, Predicate<K> pred )
200  {
201  for ( int i = 0; i < keys.Count; i++ )
202  {
203  K key = keys.ElementAt( i );
204  if ( pred( key ) ) yield return key;
205  }
206  }
207 
208  public static void ForEach<T>( this T[] array, Action<T> action )
209  {
210  for ( int i = 0; i < array.Length; i++ )
211  {
212  action( array[i] );
213  }
214  }
215  }
216 }
Jypeli.ListHelpers.FindAll< K, V >
static IEnumerable< K > FindAll< K, V >(this Dictionary< K, V >.KeyCollection keys, Predicate< K > pred)
Definition: ListHelpers.cs:199
Jypeli
Definition: Automobile.cs:5
Jypeli.ListHelpers.Average
static double Average(this IEnumerable< double > values)
Laskee keskiarvon.
Definition: ListHelpers.cs:51
Jypeli.ListHelpers.Find< T >
static T Find< T >(this List< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:165
Jypeli.ListHelpers.FindAll< T >
static List< T > FindAll< T >(this IEnumerable< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:137
Jypeli.ListHelpers
Apufunktioita listojen ja muiden tietorakenteiden käyttöön.
Definition: ListHelpers.cs:11
Jypeli.ListHelpers.Max
static double Max(this IEnumerable< double > values)
Laskee maksimin.
Definition: ListHelpers.cs:34
Jypeli.ListHelpers.ForEach< T >
static void ForEach< T >(this T[] array, Action< T > action)
Definition: ListHelpers.cs:208
Jypeli.ListHelpers.RemoveAll< T >
static void RemoveAll< T >(this List< T > items, Predicate< T > pred)
Definition: ListHelpers.cs:155
Jypeli.ListHelpers.ConvertAll< TInput, TOutput >
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:123
Jypeli.ListHelpers.AddItems< T >
static void AddItems< T >(this List< T > list, params T[] items)
Jypeli.ListHelpers.Min
static double Min(this IEnumerable< double > values)
Laskee minimin.
Definition: ListHelpers.cs:17
Jypeli.ListHelpers.ArrayFind< T >
static T ArrayFind< T >(T[] array, Predicate< T > pred)
Definition: ListHelpers.cs:182
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29