-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool1.cs
More file actions
267 lines (250 loc) · 8.39 KB
/
ObjectPool1.cs
File metadata and controls
267 lines (250 loc) · 8.39 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
using ObjectPool.Interface;
using System;
using System.Collections.Generic;
namespace ObjectPool
{
/// <summary>
/// Object pool that does not rely on any property of the internal components, beside their ID.
/// </summary>
/// <typeparam name="T">Any type that inherits from IComponent.</typeparam>
public class ObjectPool1<T> : IObjectPool<T>
where T : IComponent
{
/// <summary>
/// A native container that will hold all elements in the pool.
/// </summary>
private readonly List<T> pool = null;
/// <summary>
/// The index of the last element used by a third party.
/// </summary>
private int indexOfLastObjectUsed = -1;
/// <summary>
/// Stores the last lookup direction in the pool.
/// </summary>
private bool isMovingForward = true;
/// <summary>
/// Object pool that does not rely on any property of the internal components, beside their ID.
/// </summary>
public ObjectPool1()
{
pool = new List<T>();
}
/// <summary>
/// Adds an element to the pool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Add(T value)
{
if (value != null)
{
T component = FindObjectByID(value.ID);
if (component == null)
{
pool.Add(value);
return true;
}
}
return false;
}
/// <summary>
/// Removes an element from the pool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Remove(T value)
{
if (value != null)
{
//Check if the object is in the pool
int indexOfValue = pool.IndexOf(value);
if(indexOfValue >= 0)
{
//If the requested element was the last object in use
if (indexOfValue == indexOfLastObjectUsed)
{
//If moving forward, go back one element, otherwise advance one.
if(isMovingForward)
{
//Decrement the index, pointing to the previous element
indexOfLastObjectUsed--;
}
else
{
//Increment the index, pointing to the next element
indexOfLastObjectUsed++;
}
}
return pool.Remove(value);
}
}
return false;
}
/// <summary>
/// Gets the next element of the pool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetNext(out T value)
{
value = default;
isMovingForward = true;
//Checks if there is an object selected
if (indexOfLastObjectUsed >= 0)
{
//Checks if the current element is the last in the container
if(indexOfLastObjectUsed == pool.Count - 1)
{
//If yes, the returns the first object
indexOfLastObjectUsed = 0;
}
//Else, will try to pick the next element
else
{
int nextIndex = indexOfLastObjectUsed + 1;
if (nextIndex < pool.Count)
{
indexOfLastObjectUsed = nextIndex;
}
//If the next index is greater than the last index of the pool, return first element
else
{
indexOfLastObjectUsed = 0;
}
}
value = pool[indexOfLastObjectUsed];
}
else
{
//Returns first element case none had been picked before.
if(pool.Count > 0)
{
value = pool[0];
indexOfLastObjectUsed = 0;
}
}
return indexOfLastObjectUsed >= 0;
}
/// <summary>
/// Gets the previous element of the pool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetPrevious(out T value)
{
value = default;
isMovingForward = false;
//Checks if there is an object selected
if (indexOfLastObjectUsed >= 0)
{
//Checks if the current element is the first in the container
if (indexOfLastObjectUsed == 0)
{
//If yes, the returns the last object
indexOfLastObjectUsed = pool.Count - 1;
}
//Else, will try to pick the previous element
else
{
int previous = indexOfLastObjectUsed - 1;
if (previous >= 0)
{
indexOfLastObjectUsed = previous;
}
//If the next index is greater than the last index of the pool, return first element
else
{
indexOfLastObjectUsed = pool.Count - 1;
}
}
value = pool[indexOfLastObjectUsed];
}
else
{
//Returns first element case none had been picked before.
if (pool.Count > 0)
{
indexOfLastObjectUsed = pool.Count - 1;
value = pool[indexOfLastObjectUsed];
}
}
return indexOfLastObjectUsed >= 0;
}
/// <summary>
/// Locate an object inside the pool.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T FindObjectByID(uint id)
{
T component = default;
for (int i = 0; i < pool.Count; i++)
{
T _component = pool[i];
if (_component != null && _component.ID == id)
{
component = _component;
break;
}
}
return component;
}
/// <summary>
/// Locate an object inside the pool.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public T FindObjectByName(string name)
{
T component = default;
for (int i = 0; i < pool.Count; i++)
{
T _component = pool[i];
if (_component != null && _component.Name == name)
{
component = _component;
break;
}
}
return component;
}
/// <summary>
/// Retursn the size of the pool.
/// </summary>
/// <returns></returns>
public int GetCount() => pool.Count;
/// <summary>
/// Returns the number of elements of type T1 in the pool.
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <returns></returns>
public int GetCount<T1>() where T1 : IComponent
{
int count = 0;
for(int i = 0; i < pool.Count; i++)
{
T _component = pool[i];
if(_component != null && _component is T1)
{
count++;
}
}
return count;
}
/// <summary>
/// Releases internal resources.
/// </summary>
public void Dispose()
{
for (int i = 0; i < pool.Count; i++)
{
T _component = pool[i];
if (_component != null && typeof(IDisposable).IsAssignableFrom(_component.GetType()))
{
(_component as IDisposable).Dispose();
}
}
pool.Clear();
}
}
}