-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnmanagedCollection.cs
209 lines (175 loc) · 5.68 KB
/
UnmanagedCollection.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
public unsafe class UnmanagedCollection<T> : IList<T>, IDisposable where T : unmanaged
{
// public getters
public bool IsReadOnly => false;
public int DataSizeInBytes => dataSizeInElements_ * elementSize_;
public int UsedSizeInBytes => Count * elementSize_;
// public getter with setter
public T* Data { get; private set; }
public IntPtr DataIntPtr => (IntPtr)Data;
public int Count { get; private set; } = 0;
// private fields
private int dataSizeInElements_;
// readonly fields
#if DEBUG
readonly
#endif
private float overflowMult_;
#if DEBUG
readonly
#endif
private int elementSize_;
public UnmanagedCollection(int startingBufferSize = 8, float overflowMult = 1.5f)
{
if ((int)(startingBufferSize * overflowMult) <= startingBufferSize)
throw new ArithmeticException("Overflow multiplier doesn't increase size. Try increasing it.");
overflowMult_ = overflowMult;
elementSize_ = sizeof(T);
dataSizeInElements_ = startingBufferSize;
Data = (T*)Marshal.AllocHGlobal(DataSizeInBytes);
}
public void Dispose()
{
Marshal.FreeHGlobal((IntPtr)Data);
GC.SuppressFinalize(this);
}
~UnmanagedCollection()
{
Marshal.FreeHGlobal((IntPtr)Data);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
{
if (Count + 1 > dataSizeInElements_)
GrowMemoryBlock(GetNextBlockSize());
Data[Count] = item;
Count++;
}
public void AddRange(UnmanagedCollection<T> unmanagedCollection)
{
AssureSize(Count + unmanagedCollection.Count);
for (int i = 0; i < unmanagedCollection.Count; i++)
Data[Count + i] = unmanagedCollection.Data[i];
Count += unmanagedCollection.Count;
}
public void AddRange(IList<T> collection)
{
AssureSize(Count + collection.Count);
for (int i = 0; i < collection.Count; i++)
Data[Count + i] = collection[i];
Count += collection.Count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AssureSize(int sizeInElements)
{
if (sizeInElements > dataSizeInElements_)
{
var nextAccomodatingSize = GetNextBlockSize();
while (nextAccomodatingSize < sizeInElements)
nextAccomodatingSize = GetNextBlockSize();
GrowMemoryBlock(nextAccomodatingSize);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetNextBlockSize()
{
return (int)(dataSizeInElements_ * overflowMult_);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GrowMemoryBlock(int newElementCount)
{
var newDataSize = elementSize_ * newElementCount;
var newData = (T*)Marshal.AllocHGlobal(newDataSize);
Buffer.MemoryCopy(Data, newData, DataSizeInBytes, DataSizeInBytes);
Marshal.FreeHGlobal((IntPtr)Data);
dataSizeInElements_ = newElementCount;
Data = newData;
}
public void Clear()
{
Count = 0;
}
public int IndexOf(T item)
{
for (int i = 0; i < Count; i++)
if (Data[i].Equals(item))
return i;
return -1;
}
public void Insert(int index, T item)
{
AssureSize(Count+1);
var trailingSize = (Count - index) * elementSize_;
Buffer.MemoryCopy(&Data[index], &Data[index + 1], trailingSize, trailingSize);
Data[index] = item;
Count++;
}
/// <summary>This is slow. Use RemoveAtFast() if you don't need stable order</summary>
public void RemoveAt(int index)
{
var trailingSize = (Count - index) * elementSize_;
Buffer.MemoryCopy(&Data[index + 1], &Data[index], trailingSize, trailingSize);
Count--;
}
/// <summary>Removes element at index without preserving order (very fast)</summary>
public void RemoveAtFast(int index)
{
Buffer.MemoryCopy(&Data[Count - 1], &Data[index], elementSize_, elementSize_);
Count--;
}
public T this[int index]
{
set => Data[index] = value;
get => Data[index];
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Count; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public unsafe void FastForeach(Action<T> loopAction)
{
for (int i = 0; i < Count; i++)
loopAction(Data[i]);
}
public bool Contains(T item)
{
for (int i = 0; i < Count; i++)
if (Data[i].Equals(item))
return true;
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
if (arrayIndex + Count > array.Length)
throw new IndexOutOfRangeException("Array to copy to doesn't have enough space");
fixed (T* manArrDataPtr = &array[arrayIndex])
{
Buffer.MemoryCopy(Data, manArrDataPtr, UsedSizeInBytes, UsedSizeInBytes);
}
}
public void CopyTo(IntPtr memAddr)
{
Buffer.MemoryCopy(Data, (void*)memAddr, UsedSizeInBytes, UsedSizeInBytes);
}
public bool Remove(T item)
{
var index = IndexOf(item);
if (index < 0) return false;
RemoveAt(index); return true;
}
public void TrimExcess()
{
//todo shrink buffer but not beyond a value that doesn't increase when multiplied by overflowMult_
throw new NotImplementedException();
}
}