Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EnumerableExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 namespace SiliconStudio.Core.Extensions
9 {
10  public static class EnumerableExtensions
11  {
12  /// <summary>
13  /// Tells whether a sequence is null or empty.
14  /// </summary>
15  /// <param name="source">The source sequence.</param>
16  /// <returns>Returns true if the sequence is null or empty, false if it is not null and contains at least one element.</returns>
17  public static bool IsNullOrEmpty(this IEnumerable source)
18  {
19  if (source == null)
20  return true;
21 
22  IEnumerator enumerator = source.GetEnumerator();
23  if (enumerator == null)
24  throw new ArgumentException("Invalid 'source' IEnumerable.");
25 
26  return enumerator.MoveNext() == false;
27  }
28 
29  /// <summary>
30  /// Execute an action for each (casted) item of the given enumerable.
31  /// </summary>
32  /// <typeparam name="T">Type of the item value in the enumerable.</typeparam>
33  /// <param name="source">Input enumerable to work on.</param>
34  /// <param name="action">Action performed for each item in the enumerable.</param>
35  /// <remarks>This extension method do not yield. It acts just like a foreach statement, and performs a cast to a typed enumerable in the middle.</remarks>
36  public static void ForEach<T>(this IEnumerable source, Action<T> action)
37  {
38  source.Cast<T>().ForEach(action);
39  }
40 
41  /// <summary>
42  /// Execute an action for each item of the given enumerable.
43  /// </summary>
44  /// <typeparam name="T">Type of the item value in the enumerable.</typeparam>
45  /// <param name="source">Input enumerable to work on.</param>
46  /// <param name="action">Action performed for each item in the enumerable.</param>
47  /// <remarks>This extension method do not yield. It acts just like a foreach statement.</remarks>
48  public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
49  {
50  foreach (T item in source)
51  action(item);
52  }
53 
54  /// <summary>
55  /// An <see cref="IEnumerable{T}"/> extension method that searches for the first match and returns its index.
56  /// </summary>
57  /// <typeparam name="T">Generic type parameter.</typeparam>
58  /// <param name="source">Input enumerable to work on.</param>
59  /// <param name="predicate">The predicate.</param>
60  /// <returns>The index of the first element matching.</returns>
61  public static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
62  {
63  int index = 0;
64  foreach (T item in source)
65  {
66  if (predicate(item))
67  return index;
68  index++;
69  }
70  return -1;
71  }
72 
73  /// <summary>
74  /// An <see cref="IEnumerable{T}"/> extension method that searches for the last match and returns its index.
75  /// </summary>
76  /// <typeparam name="T">Generic type parameter.</typeparam>
77  /// <param name="source">Input enumerable to work on.</param>
78  /// <param name="predicate">The predicate.</param>
79  /// <returns>The index of the last element matching.</returns>
80  public static int LastIndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
81  {
82  var list = source as IList<T>;
83  if (list != null)
84  {
85  // Faster search for lists.
86  for (int i = list.Count - 1; i >= 0; --i)
87  {
88  if (predicate(list[i]))
89  return i;
90  }
91  return -1;
92  }
93  int index = 0;
94  int lastIndex = -1;
95  foreach (T item in source)
96  {
97  if (predicate(item))
98  lastIndex = index;
99  index++;
100  }
101  return lastIndex;
102  }
103 
104  /// <summary>
105  /// Enumerates the linked list nodes.
106  /// </summary>
107  /// <typeparam name="T"></typeparam>
108  /// <param name="list">The linked list.</param>
109  /// <returns></returns>
110  internal static IEnumerable<LinkedListNode<T>> EnumerateNodes<T>(this LinkedList<T> list)
111  {
112  var node = list.First;
113  while (node != null)
114  {
115  yield return node;
116  node = node.Next;
117  }
118  }
119  }
120 }
static bool IsNullOrEmpty(this IEnumerable source)
Tells whether a sequence is null or empty.