-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListExtensions.cs
254 lines (236 loc) · 5.86 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using UnityEngine;
using System.Collections.Generic;
public static class ListExtensions
{
public static bool AddUnique<T>(this List<T> list, T item)
{
for (int i = 0; i < list.Count; i++)
{
if (Equals(list[i], item))
{
return false;
}
}
list.Add(item);
return true;
}
public static void RemoveAtSwapBack<T>(this List<T> list, int index)
{
var lastIndex = list.Count - 1;
if (index == lastIndex)
{
list.RemoveAt(lastIndex);
}
else
{
list[index] = list[lastIndex];
list.RemoveAt(lastIndex);
}
}
public static bool RemoveSwapBack<T>(this List<T> list, T item)
{
for (int i = 0; i < list.Count; i++)
{
if (Equals(item, list[i]))
{
RemoveAtSwapBack(list, i);
return true;
}
}
return false;
}
public static bool AreAllSame<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null) throw new System.ArgumentNullException(nameof(enumerable));
using (var enumerator = enumerable.GetEnumerator())
{
var toCompare = default(T);
if (enumerator.MoveNext())
{
toCompare = enumerator.Current;
}
while (enumerator.MoveNext())
{
if (toCompare != null && !toCompare.Equals(enumerator.Current))
{
return false;
}
}
}
return true;
}
public static List<T> GetShuffledList<T>(this List<T> sourceList)
{
var unshuffledList = new List<T>(sourceList);
var shuffledList = new List<T>();
while (unshuffledList.Count > 0)
{
var randomIndex = UnityEngine.Random.Range(0, unshuffledList.Count);
shuffledList.Add(unshuffledList[randomIndex]);
unshuffledList.RemoveAtSwapBack(randomIndex);
}
return shuffledList;
}
public static bool ContainsAny<T>(this List<T> list, List<T> match)
{
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < match.Count; j++)
{
if (list[i].Equals(match[j]))
{
return true;
}
}
}
return false;
}
public static int IndexOfByID<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
return i;
}
}
return -1;
}
public static bool ContainsByID<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
return true;
}
}
return false;
}
public static bool AddUniqueByID<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
return false;
}
}
list.Add(obj);
return true;
}
public static bool RemoveByID<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
list.RemoveAt(i);
return true;
}
}
return false;
}
public static bool RemoveByIDSwapBack<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
list.RemoveAtSwapBack(i);
return true;
}
}
return false;
}
public static int RemoveAllByID<T>(this List<T> list, T obj) where T : Object
{
var id = obj.GetInstanceID();
var count = 0;
for (int i = 0; i < list.Count; i++)
{
if (id == list[i].GetInstanceID())
{
list.RemoveAt(i);
count++;
i--;
}
}
return count;
}
public static T RandomItem<T>(this List<T> list)
{
if (list.Count == 0)
{
return default(T);
}
return list[Random.Range(0, list.Count)];
}
public static void Swap<T>(this List<T> array, int first, int second)
{
var temp = array[first];
array[first] = array[second];
array[second] = temp;
}
public static int CheckEmptyItems<T>(this List<T> list)
{
int removedCount = 0;
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null)
{
list.RemoveAt(i);
i--;
removedCount++;
}
}
return removedCount;
}
public static int RemoveAll<T>(this List<T> list, T item)
{
var removed = 0;
for (int i = list.Count - 1; i >= 0; i--)
{
if (item.Equals(list[i]))
{
list.RemoveAt(i);
removed++;
}
}
return removed;
}
public static int RemoveAll<T>(this List<T> list, List<T> items)
{
var removed = 0;
for (int i = list.Count - 1; i >= 0; i--)
{
if (items.Contains(list[i]))
{
list.RemoveAt(i);
removed++;
}
}
return removed;
}
public static bool Replace<T>(this List<T> list, T item, T replacement)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Equals(item))
{
list[i] = replacement;
return true;
}
}
return false;
}
public static T LastItem<T>(this List<T> list)
{
return list[list.Count - 1];
}
}