list extensions
You can visit http://www.the-barn.org/codebucket.php?id=72 to view this snippet directly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 | using System;
using System.Collections.Generic;
namespace Incah.Core
{
public static class ListExtensions
{
public static IEnumerable<T> Reversed<T>(this List<T> list)
{
for (int index = list.Count - 1; index >= 0; --index)
yield return list[index];
}
public static T GetNormalized<T>(this List<T> list, int index)
{
return (index %= list.Count) < 0
? list[list.Count + index]
: list[index];
}
public static T GetNextItem<T>(this List<T> list, T item)
{
int index = list.IndexOf(item) + 1;
return list.GetNormalized(index);
}
public static T GetPreviousItem<T>(this List<T> list, T item)
{
int index = Math.Max(0, list.IndexOf(item)) - 1;
return list.GetNormalized(index);
}
public static IEnumerable<F> Find<F, T>(this List<T> list)
{
foreach (object value in list)
{
if (value is F)
yield return (F)value;
}
}
public static IEnumerable<F> Find<F, T>(this List<T> list, Predicate<F> predicate)
{
foreach (F value in list.Find<F, T>())
{
if (predicate(value))
yield return value;
}
}
public static IEnumerable<F> Find<F, T>(this List<T> list, Func<F, bool> predicate)
{
foreach (F value in list.Find<F, T>())
{
if (predicate(value))
yield return value;
}
}
}
}
|
- Posted on 09.02.2010 at 11:12 PM by stoffle
- Language: C#