-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemCollection.cs
More file actions
363 lines (340 loc) · 10.9 KB
/
ItemCollection.cs
File metadata and controls
363 lines (340 loc) · 10.9 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Events;
[Serializable]
public class ItemCollection<T> : List<T>
{
public UnityEvent<int, T> OnItemAdded = new();
public UnityEvent<T> OnItemRemoved = new();
public List<T> this[Range range]
{
get {
return GetRange(range.Start.Value, range.End.Value);
}
}
public int[] this[T item]
{
get{
List<int> indices = new();
for(int i = 0; i < Count; i++)
{
if(this[i].Equals(item))
indices.Add(i);
}
return indices.ToArray();
}
}
public int Max = -1;
/// <summary>
/// Adds the given Item to the ItemCollection.
/// Invokes the OnItemAdded event for the Item being added
/// </summary>
/// <param name="item"></param>
/// <returns>true if the Item could fit in the Collection</returns>
public new bool Add(T item)
{
if(Count < Max || Max == -1)
{
Add(item);
OnItemAdded.Invoke(Count - 1, item);
return true;
}
return false;
}
/// <summary>
/// Adds the given Items to the ItemCollection.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="items"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public new bool AddRange(IEnumerable<T> items)
{
if(Count + items.Count() < Max || Max == -1)
{
for(int i = 0; i < items.Count(); i++)
{
OnItemAdded.Invoke(Count + i, items.ElementAt(i));
}
AddRange(items);
return true;
}
return false;
}
/// <summary>
/// Adds the given Item n times to the ItemCollection.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="item"></param>
/// <param name="count"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool AddN(T item, int count)
{
if(Count + count < Max || Max == -1)
{
for(int i = 0; i < count; i++)
{
Add(item);
OnItemAdded.Invoke(Count - 1, item);
}
return true;
}
return false;
}
/// <summary>
/// Inserts the given Item at the given Index.
/// Invokes the OnItemAdded event for the Item being added
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns>true if the Item could fit in the Collection</returns>
public new bool Insert(int index, T item)
{
if(Count + 1 < Max || Max == -1)
{
base.Insert(index, item);
OnItemAdded.Invoke(index, item);
return true;
}
return false;
}
/// <summary>
/// Inserts the given Items at the given Index.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="index"></param>
/// <param name="items"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public new bool InsertRange(int index, IEnumerable<T> items)
{
if(Count + items.Count() < Max || Max == -1)
{
base.InsertRange(index, items);
for(int i = 0; i < items.Count(); i++)
{
OnItemAdded.Invoke(index + i, items.ElementAt(i));
}
return true;
}
return false;
}
/// <summary>
/// Replaces the Item at the given Index with the given Item
/// Invokes the OnItemRemoved event for the Item being replaced
/// Invokes the OnItemAdded event for the Item being added
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns>true if the Item could fit in the Collection</returns>
public bool Replace(int index, T item)
{
if(index < Max || Max == -1)
{
if(Count > index)
OnItemRemoved?.Invoke(this[index]);
if(Count <= index)
base.Insert(index, item);
else
{
this[index] = item;
}
OnItemAdded?.Invoke(index, item);
return true;
}
return false;
}
/// <summary>
/// Replaces the Items at the given Index with the given Items.
/// Invokes the OnItemRemoved event for each Item being replaced
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="index"></param>
/// <param name="items"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool ReplaceRange(int index, IEnumerable<T> items)
{
var replaced = false;
for(int i = 0; i < items.Count(); i++)
{
if(index + i < Max || Max == -1)
{
Replace(index + i, items.ElementAt(i));
replaced = true;
}
}
return replaced;
}
/// <summary>
/// Removes the Item from the ItemCollection, Invoking the OnItemRemoved event.
/// </summary>
/// <param name="item"></param>
public new void Remove(T item)
{
OnItemRemoved.Invoke(item);
base.Remove(item);
}
/// <summary>
/// Removes the Item at the given Index from the ItemCollection.
/// </summary>
/// <param name="index"></param>
public new void RemoveAt(int index)
{
Remove(this[index]);
}
/// <summary>
/// Removes the given Range of Items from the ItemCollection.
/// </summary>
/// <param name="range"></param>
public void RemoveRange(Range range)
{
Array.ForEach(this[range].ToArray(), x => Remove(x));
}
/// <summary>
/// Removes all of the specified Items from the ItemCollection.
/// </summary>
/// <param name="predicate"></param>
public new void RemoveAll(Predicate<T> predicate)
{
Array.ForEach(FindAll(predicate).ToArray(), x => Remove(x));
}
/// <summary>
/// Removes all of the specified Items from the ItemCollection.
/// </summary>
/// <param name="items"></param>
public void RemoveAll(IEnumerable<T> items)
{
Array.ForEach(items.ToArray(), x => Remove(x));
}
/// <summary>
/// Removes the first n of the specified Item from the ItemCollection.
/// </summary>
/// <param name="item"></param>
/// <param name="n"></param>
public void RemoveCount(T item, int n)
{
for(int i = 0; i < n; i++)
{
base.Remove(item);
}
}
/// <summary>
/// Removes all Items from the ItemCollection.
/// </summary>
public new void Clear()
{
ForEach(x => Remove(x));
}
/// <summary>
/// Transforms all Items from another ItemCollection into this one.
/// </summary>
/// <param name="other"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferAllFrom(ItemCollection<T> other)
{
if(Count + other.Count < Max || Max == -1)
{
AddRange(other);
other.Clear();
return true;
}
return false;
}
/// <summary>
/// Removes the Item from the other ItemCollection, by Index, and adds it to this one.
/// </summary>
/// <param name="other"></param>
/// <param name="index"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferFromOtherByIndex(ItemCollection<T> other, int index)
{
if(Count < Max || Max == -1)
{
Add(other[index]);
other.RemoveAt(index);
return true;
}
return false;
}
/// <summary>
/// Removes the Item from the other ItemCollection and adds it to this one.
/// If n is greater than 1, it will remove the first n items from the other ItemCollection.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="other"></param>
/// <param name="item"></param>
/// <param name="n"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferItemFromOther(ItemCollection<T> other, T item, int n = 1)
{
if(Count + n < Max || Max == -1)
{
var others = other.FindAll(x => x.Equals(item));
for(int i = 0; i < n; i++)
{
if(i < others.Count && Count < Max || Max == -1)
{
var first = other.First(x => x.Equals(item));
if(first != null)
{
other.Remove(first);
Add(first);
}
}
}
return true;
}
return false;
}
/// <summary>
/// Removes the Items from the other ItemCollection and adds them to this one.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="other"></param>
/// <param name="items"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferItemsFromOther(ItemCollection<T> other, IEnumerable<T> items)
{
if(Count + items.Count() < Max || Max == -1)
{
AddRange(other.FindAll(x => items.Contains(x)));
other.RemoveAll(x => items.Contains(x));
return true;
}
return false;
}
/// <summary>
/// Removes the Items from the other ItemCollection and adds them to this one.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="other"></param>
/// <param name="predicate"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferItemsFromOther(ItemCollection<T> other, Predicate<T> predicate)
{
var items = other.FindAll(predicate);
if(Count + items.Count() < Max || Max == -1)
{
AddRange(items);
other.RemoveAll(items);
return true;
}
return false;
}
/// <summary>
/// Removes the Items from the other ItemCollection and adds them to this one.
/// Invokes the OnItemAdded event for each Item being added
/// </summary>
/// <param name="other"></param>
/// <param name="range"></param>
/// <returns>true if the Items could fit in the Collection</returns>
public bool TransferRangeFromOther(ItemCollection<T> other, Range range)
{
if(Count + range.End.Value - range.Start.Value < Max || Max == -1)
{
AddRange(other[range]);
other.RemoveRange(range);
return true;
}
return false;
}
}