|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities.DataStructures;
namespace Signum.Utilities
{
public static class ListExtensions
{
public static List<T> Extract<T>(this IList<T> list, Func<T, bool> condition)
{
List<T> result = new List<T>();
int i = 0;
while (i < list.Count)
{
if (condition(list[i]))
{
result.Add(list[i]);
list.RemoveAt(i);
}
else
{
i++;
}
}
return result;
}
public static void Sort<T, A>(this List<T> list, Func<T, A> keySelector)
{
list.Sort(new LambdaComparer<T, A>(keySelector));
}
public static void RemoveAll<T>(this IList<T> list, Func<T, bool> condition)
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (condition(list[i]))
list.RemoveAt(i);
}
}
public static void AddRange<T>(this IList<T> list, IEnumerable<T> elements)
{
foreach (var item in elements)
{
list.Add(item);
}
}
public static void AddRange<T>(this IList<T> list, params T[] elements)
{
foreach (var item in elements)
{
list.Add(item);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...