forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReusableItemList.cs
170 lines (137 loc) · 3.3 KB
/
ReusableItemList.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
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Collections.Generic;
namespace Microsoft.Xna.Framework
{
internal class ReusableItemList<T> : ICollection<T>, IEnumerator<T>
{
private readonly List<T> _list = new List<T>();
private int _listTop = 0;
private int _iteratorIndex;
#region ICollection<T> Members
public void Add(T item)
{
if (_list.Count > _listTop)
{
_list[_listTop] = item;
}
else
{
_list.Add(item);
}
_listTop++;
}
public void Sort(IComparer<T> comparison)
{
_list.Sort(comparison);
}
public T GetNewItem()
{
if (_listTop < _list.Count)
{
return _list[_listTop++];
}
else
{
// Damm...Mono fails in this!
//return (T) Activator.CreateInstance(typeof(T));
return default(T);
}
}
public T this[int index]
{
get
{
if (index >= _listTop)
throw new IndexOutOfRangeException();
return _list[index];
}
set
{
if (index >= _listTop)
throw new IndexOutOfRangeException();
_list[index] = value;
}
}
public void Clear()
{
_listTop = 0;
}
public void Reset()
{
Clear();
_list.Clear();
}
public bool Contains(T item)
{
return _list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_list.CopyTo(array,arrayIndex);
}
public int Count
{
get
{
return _listTop;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
_iteratorIndex = -1;
return this;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
_iteratorIndex = -1;
return this;
}
#endregion
#region IEnumerator<T> Members
public T Current
{
get
{
return _list[_iteratorIndex];
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
}
#endregion
#region IEnumerator Members
object System.Collections.IEnumerator.Current
{
get
{
return _list[_iteratorIndex];
}
}
public bool MoveNext()
{
_iteratorIndex++;
return (_iteratorIndex < _listTop);
}
#endregion
}
}