This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
EnumerableExtensions.cs
62 lines (54 loc) · 2.1 KB
/
EnumerableExtensions.cs
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
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bramble.Core;
using Amaranth.Util;
namespace Amaranth.Engine
{
public static class EnumerableExtensions
{
public static T GetAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
{
foreach (T item in collection)
{
if (item.Position.Equals(pos))
{
return item;
}
}
// no item here
return default(T);
}
/// <summary>
/// Gets all of the items in the collection at the given position.
/// </summary>
/// <typeparam name="T">The type of items in the collection. Must implement <see cref="IPosition"/>.</typeparam>
/// <param name="collection">The collection of items.</param>
/// <param name="pos">The position to look at.</param>
/// <returns>A list of all items in the collection at the given position.</returns>
public static IList<T> GetAllAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
{
List<T> items = new List<T>();
foreach (T item in collection)
{
if (item.Position.Equals(pos))
{
items.Add(item);
}
}
return items;
}
/// <summary>
/// Gets the number of the items in the collection at the given position.
/// </summary>
/// <typeparam name="T">The type of items in the collection. Must implement <see cref="IPosition"/>.</typeparam>
/// <param name="collection">The collection of items.</param>
/// <param name="pos">The position to look at.</param>
/// <returns>The number of items in the collection at the given position.</returns>
public static int CountAt<T>(this IEnumerable<T> collection, Vec pos) where T : IPosition
{
return collection.Count(item => item.Position == pos);
}
}
}