This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
/
PooledLinkedList.cs
336 lines (291 loc) · 11.2 KB
/
PooledLinkedList.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using Archon.SwissArmyLib.Pooling;
namespace Archon.SwissArmyLib.Collections
{
/// <summary>
/// A wrapper for <see cref="LinkedList{T}" /> that recycles its <see cref="LinkedListNode{T}" /> instances to reduce
/// GC allocations.
/// </summary>
/// <typeparam name="T">Type of items the list should contain.</typeparam>
public class PooledLinkedList<T> : ICollection<T>
{
private readonly LinkedList<T> _list;
private readonly IPool<LinkedListNode<T>> _pool;
/// <summary>
/// Initializes a new empty PooledLinkedList<T> with its own node pool.
/// </summary>
public PooledLinkedList()
: this(new Pool<LinkedListNode<T>>(() => new LinkedListNode<T>(default(T))))
{
}
/// <summary>
/// Initializes a new empty PooledLinkedList<T> that uses a specified node pool.
/// </summary>
/// <param name="nodePool">The pool that should be used spawning/despawning new nodes.</param>
public PooledLinkedList(IPool<LinkedListNode<T>> nodePool)
{
if (nodePool == null)
throw new ArgumentNullException("nodePool");
_list = new LinkedList<T>();
_pool = nodePool;
}
/// <summary>
/// Initializes a new PooledLinkedList<T> with its own node pool and the contents of the specified
/// <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="collection">A collection of items to fill with PooledLinkedList<T> with.</param>
public PooledLinkedList(IEnumerable<T> collection)
: this(collection, new Pool<LinkedListNode<T>>(() => new LinkedListNode<T>(default(T))))
{
}
/// <summary>
/// Initializes a new PooledLinkedList<T> with a custom node pool and the contents of the specified
/// <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="collection">A collection of items to fill with PooledLinkedList<T> with.</param>
/// <param name="nodePool">The pool that should be used spawning/despawning new nodes.</param>
public PooledLinkedList(IEnumerable<T> collection, IPool<LinkedListNode<T>> nodePool)
: this(nodePool)
{
if (collection == null)
throw new ArgumentNullException("collection");
foreach (var item in collection)
AddLast(item);
}
/// <summary>
/// Gets the <see cref="LinkedList{T}" /> instance that is wrapped.
/// </summary>
public LinkedList<T> BackingList
{
get { return _list; }
}
/// <summary>
/// Gets the object pool used for storing unused nodes.
/// </summary>
public IPool<LinkedListNode<T>> Pool
{
get { return _pool; }
}
/// <summary>
/// Gets the first node of the PooledLinkedList<T>.
/// </summary>
public LinkedListNode<T> First
{
get { return _list.First; }
}
/// <summary>
/// Gets the last node of the PooledLinkedList<T>.
/// </summary>
public LinkedListNode<T> Last
{
get { return _list.Last; }
}
/// <summary>
/// Gets the number of nodes contained in the PooledLinkedList<T>.
/// </summary>
public int Count
{
get { return _list.Count; }
}
/// <summary>
/// Removes all nodes from the PooledLinkedList<T>.
/// </summary>
public void Clear()
{
var current = First;
while (current != null)
{
var temp = current;
current = current.Next;
_pool.Despawn(temp);
}
_list.Clear();
}
/// <summary>
/// Determines whether a value is in the PooledLinkedList<T>.
/// </summary>
/// <param name="item">The item to check for.</param>
/// <returns>True if the list contains the item, otherwise false.</returns>
public bool Contains(T item)
{
return _list.Contains(item);
}
/// <summary>
/// Copies the entire PooledLinkedList<T> to a compatible one-dimensional Array, starting at the specified index
/// of the target array.
/// </summary>
/// <param name="array">The array to copy to.</param>
/// <param name="arrayIndex">The index to start at.</param>
public void CopyTo(T[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
/// <summary>
/// Removes the first occurrence of the specified value from the PooledLinkedList<T>.
/// </summary>
/// <param name="item">The item to remove.</param>
/// <returns>True if found and removed, otherwise false.</returns>
public bool Remove(T item)
{
var node = _list.Find(item);
if (node != null)
{
_list.Remove(node);
_pool.Despawn(node);
return true;
}
return false;
}
/// <summary>
/// Returns an enumerator that iterates through the PooledLinkedList<T>.
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _list).GetEnumerator();
}
void ICollection<T>.Add(T item)
{
AddLast(item);
}
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
/// <summary>
/// Adds a new node containing the specified value after the specified existing node in the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node that <paramref name="value" /> should be added after.</param>
/// <param name="value">The value to add.</param>
/// <returns>The added node.</returns>
public LinkedListNode<T> AddAfter(LinkedListNode<T> node, T value)
{
var newNode = _pool.Spawn();
newNode.Value = value;
AddAfter(node, newNode);
return newNode;
}
/// <summary>
/// Adds the specified new node after the specified existing node in the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node that <paramref name="newNode" /> should be added after.</param>
/// <param name="newNode">The node to add.</param>
public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode)
{
_list.AddAfter(node, newNode);
}
/// <summary>
/// Adds a new node containing the specified value before the specified existing node in the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node that <paramref name="value" /> should be added before.</param>
/// <param name="value">The value to add.</param>
/// <returns>The added node.</returns>
public LinkedListNode<T> AddBefore(LinkedListNode<T> node, T value)
{
var newNode = _pool.Spawn();
newNode.Value = value;
AddBefore(node, newNode);
return newNode;
}
/// <summary>
/// Adds the specified new node before the specified existing node in the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node that <paramref name="newNode" /> should be added before.</param>
/// <param name="newNode">The node to add.</param>
public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
{
_list.AddBefore(node, newNode);
}
/// <summary>
/// Adds a new node containing the specified value at the start of the PooledLinkedList<T>.
/// </summary>
/// <param name="value">The value to add.</param>
/// <returns>The node that was added.</returns>
public LinkedListNode<T> AddFirst(T value)
{
var newNode = _pool.Spawn();
newNode.Value = value;
AddFirst(newNode);
return newNode;
}
/// <summary>
/// Adds the specified new node at the start of the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node to add.</param>
public void AddFirst(LinkedListNode<T> node)
{
_list.AddFirst(node);
}
/// <summary>
/// Adds a new node containing the specified value at the end of the PooledLinkedList<T>.
/// </summary>
/// <param name="value">The value to add.</param>
/// <returns>The node that was added.</returns>
public LinkedListNode<T> AddLast(T value)
{
var newNode = _pool.Spawn();
newNode.Value = value;
AddLast(newNode);
return newNode;
}
/// <summary>
/// Adds the specified new node at the end of the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node to add.</param>
public void AddLast(LinkedListNode<T> node)
{
_list.AddLast(node);
}
/// <summary>
/// Finds the first node that contains the specified value.
/// </summary>
/// <param name="value">The value to search for.</param>
/// <returns>The found node or null if none were found.</returns>
public LinkedListNode<T> Find(T value)
{
return _list.Find(value);
}
/// <summary>
/// Finds the last node that contains the specified value.
/// </summary>
/// <param name="value">The value to search for.</param>
/// <returns>The found node or null if none were found.</returns>
public LinkedListNode<T> FindLast(T value)
{
return _list.FindLast(value);
}
/// <summary>
/// Removes the specified node from the PooledLinkedList<T>.
/// </summary>
/// <param name="node">The node to remove.</param>
public void Remove(LinkedListNode<T> node)
{
_list.Remove(node);
_pool.Despawn(node);
}
/// <summary>
/// Removes the node at the start of the PooledLinkedList<T>.
/// </summary>
public void RemoveFirst()
{
var first = First;
_list.RemoveFirst();
_pool.Despawn(first);
}
/// <summary>
/// Removes the node at the end of the PooledLinkedList<T>.
/// </summary>
public void RemoveLast()
{
var last = Last;
_list.RemoveLast();
_pool.Despawn(last);
}
}
}