-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListPool.cs
72 lines (59 loc) · 1.79 KB
/
ListPool.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
using System;
using System.Collections.Generic;
namespace Binocle
{
/// <summary>
/// simple static class that can be used to pool Lists
/// </summary>
public static class ListPool<T>
{
static Queue<List<T>> _objectQueue = new Queue<List<T>>();
/// <summary>
/// warms up the cache filling it with a max of cacheCount objects
/// </summary>
/// <param name="cacheCount">new cache count</param>
public static void warmCache(int cacheCount)
{
cacheCount -= _objectQueue.Count;
if (cacheCount > 0)
{
for (var i = 0; i < cacheCount; i++)
_objectQueue.Enqueue(new List<T>());
}
}
/// <summary>
/// trims the cache down to cacheCount items
/// </summary>
/// <param name="cacheCount">Cache count.</param>
public static void trimCache(int cacheCount)
{
while (cacheCount > _objectQueue.Count)
_objectQueue.Dequeue();
}
/// <summary>
/// clears out the cache
/// </summary>
public static void clearCache()
{
_objectQueue.Clear();
}
/// <summary>
/// pops an item off the stack if available creating a new item as necessary
/// </summary>
public static List<T> obtain()
{
if (_objectQueue.Count > 0)
return _objectQueue.Dequeue();
return new List<T>();
}
/// <summary>
/// pushes an item back on the stack
/// </summary>
/// <param name="obj">Object.</param>
public static void free(List<T> obj)
{
_objectQueue.Enqueue(obj);
obj.Clear();
}
}
}