-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDList.cs
77 lines (69 loc) · 1.67 KB
/
DList.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
using System.Collections;
using System.Collections.Generic;
using System;
public class DItem : IDItem
{
public bool destroyed { get; set; }
}
public interface IDItem
{
bool destroyed { get; set; }
}
/// <summary>
/// 提供延迟删除List元素的功能
/// </summary>
/// <typeparam name="T"></typeparam>
public class DList<T> : List<T> where T:IDItem
{
bool m_RemoveDirty = false;
bool m_AddDirty = false;
List<T> delayAddList = new List<T>();
public void DelayAdd(T item)
{
delayAddList.Add(item);
m_AddDirty = true;
}
public void DelayRemove(T item)
{
item.destroyed = true;
m_RemoveDirty = true;
}
public void DelayRemoveAt(int index)
{
Remove(this[index]);
}
public void ApplyDelayCommands()
{
if (this.m_RemoveDirty)
{
int count = this.Count;
int i = 0, j = 0;
while (i < count)
{
if (this[i].destroyed)
{
j = i + 1;
while (j < count)
{
if (!this[j].destroyed)
{
this[i] = this[j];
i++;
}
j++;
}
this.RemoveRange(i, this.Count - i);
break;
}
i++;
}
this.m_RemoveDirty = false;
}
if (this.m_AddDirty)
{
this.AddRange(delayAddList);
this.delayAddList.Clear();
this.m_AddDirty = false;
}
}
}