-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListExtensions.cs
executable file
·69 lines (63 loc) · 1.85 KB
/
ListExtensions.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
63
64
65
66
67
68
69
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace UberTools
{
static public class ListExtensions
{
public static void Resize<T>(this List<T> list, int size, T defaultValue)
{
int count = list.Count;
if(size < count)
{
list.RemoveRange(size, count - size);
}
else if(size > count)
{
//Prevent multiple capacity changes
if(size > list.Capacity)
{
list.Capacity = size;
}
list.AddRange(Enumerable.Repeat(defaultValue, size - count));
}
}
public static void Shuffle<T>(this IList<T> list)
{
for (int i = 0; i < list.Count - 1; i++)
{
int pos = UnityEngine.Random.Range(i, list.Count);
T temp = list[i];
list[i] = list[pos];
list[pos] = temp;
}
}
public static bool RemoveFirst<T>(this IList<T> list, Predicate<T> match)
{
var itemCount = list.Count;
for (int i = 0; i < itemCount - 1; ++i)
{
if (match(list[i]) == true)
{
list.RemoveAt(i);
return true;
}
}
return false;
}
public static List<T> RemoveDuplicates<T>(this List<T> list)
{
var newList = new List<T>();
foreach(var item in list)
{
if(!newList.Contains(item))
{
newList.Add(item);
}
}
return newList;
}
}
}