-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStructureLibrary.cs
85 lines (70 loc) · 2.42 KB
/
DataStructureLibrary.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
using System;
using System.Collections.Generic;
namespace Neo.Utility {
public class DataStructureLibrary<T> : IDisposable {
//TODO: Should make this a struct at some point
public class CheckoutSlip : IDisposable {
public T Value {
get;
protected set;
}
public CheckoutSlip(T data)
{
Value = data;
}
public static implicit operator T(CheckoutSlip slip) => slip.Value;
public void Dispose()
{
Instance.Return(this);
}
public override string ToString()
{
return Value.ToString();
}
}
public readonly static DataStructureLibrary<T> Instance = new DataStructureLibrary<T>();
protected LinkedList<T> m_Available = new LinkedList<T>();
protected LinkedList<T> m_CheckedOut = new LinkedList<T>();
public CheckoutSlip CheckOut(params object[] constructorArgs)
{
if (m_Available.Count <= 0)
{
T inst = (T)Activator.CreateInstance(typeof(T), constructorArgs);
System.Diagnostics.Debug.Assert(inst != null);
m_Available.AddLast(inst);
}
LinkedListNode<T> node = m_Available.Last;
m_Available.Remove(node.Value);
m_CheckedOut.AddLast(node);
return new CheckoutSlip(node.Value);
}
protected void Return(CheckoutSlip slip)
{
LinkedListNode<T> node = m_CheckedOut.Find(slip.Value);
if (node == null)
{
slip = null;
return;
}
m_CheckedOut.Remove(node);
m_Available.AddLast(node);
}
// Hiding the contructor
protected DataStructureLibrary()
{
}
public void Dispose()
{
System.Diagnostics.Debug.Assert(m_CheckedOut.Count <= 0);
Type tType = typeof(T);
var methodInfo = tType.GetMethod("Dispose", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (methodInfo != null)
{
foreach (var node in m_Available)
{
methodInfo.Invoke(node, null);
}
}
}
}
}