Skip to content

Commit ceda970

Browse files
author
LoneWandererProductions
committed
improve unmanaged array
1 parent 9a92a4d commit ceda970

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

ExtendedSystemObjects/ImmutableLookupMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ IEnumerator IEnumerable.GetEnumerator()
121121
/// Retrieves the value associated with the specified key.
122122
/// </summary>
123123
/// <param name="key">The key.</param>
124-
/// <returns></returns>
124+
/// <returns>Requested Value from Key</returns>
125125
/// <exception cref="KeyNotFoundException">The key {key} was not found in the lookup map.</exception>
126126
public TValue Get(TKey key)
127127
{

ExtendedSystemObjects/IntArray.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ public void EnsureCapacity(int minCapacity)
326326
}
327327

328328
var newCapacity = Capacity == 0 ? 4 : Capacity;
329+
329330
while (newCapacity < minCapacity)
330331
{
331332
newCapacity *= 2;

ExtendedSystemObjects/UnmanagedArray.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IEnumerable<T
3333
private IntPtr _buffer;
3434

3535
/// <summary>
36-
/// The capacity
36+
/// The capacity of the current Array.
3737
/// </summary>
38-
private int _capacity;
38+
/// <value>
39+
/// The capacity.
40+
/// </value>
41+
public int Capacity { get; private set; }
3942

4043
/// <summary>
4144
/// The disposed
@@ -53,7 +56,7 @@ public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IEnumerable<T
5356
/// <param name="size">The size.</param>
5457
public UnmanagedArray(int size)
5558
{
56-
_capacity = size;
59+
Capacity = size;
5760
Length = size;
5861
_buffer = Marshal.AllocHGlobal(size * sizeof(T));
5962
_ptr = (T*)_buffer;
@@ -123,7 +126,7 @@ public void RemoveAt(int index)
123126
Buffer.MemoryCopy(
124127
_ptr + index + 1,
125128
_ptr + index,
126-
(_capacity - index - 1) * sizeof(T),
129+
(Capacity - index - 1) * sizeof(T),
127130
elementsToShift * sizeof(T));
128131
}
129132

@@ -162,15 +165,32 @@ IEnumerator IEnumerable.GetEnumerator()
162165
/// <param name="newSize">The new size of the array.</param>
163166
public void Resize(int newSize)
164167
{
165-
_buffer = Marshal.ReAllocHGlobal(_buffer, (IntPtr)(newSize * sizeof(T)));
166-
_ptr = (T*)_buffer;
167-
_capacity = newSize;
168+
if (newSize < 0)
169+
throw new ArgumentOutOfRangeException(nameof(newSize));
170+
171+
if (newSize == Capacity)
172+
return;
173+
174+
var newBuffer = Marshal.ReAllocHGlobal(_buffer, (IntPtr)(newSize * sizeof(T)));
175+
var newPtr = (T*)newBuffer;
176+
177+
if (newSize > Capacity)
178+
{
179+
var newRegion = new Span<T>(newPtr + Capacity, newSize - Capacity);
180+
newRegion.Clear();
181+
}
182+
183+
_buffer = newBuffer;
184+
_ptr = newPtr;
185+
Capacity = newSize;
186+
168187
if (Length > newSize)
169188
{
170189
Length = newSize;
171190
}
172191
}
173192

193+
174194
/// <inheritdoc />
175195
/// <summary>
176196
/// Clears the array by setting all elements to zero.
@@ -228,7 +248,7 @@ public void InsertAt(int index, T value, int count = 1)
228248
Buffer.MemoryCopy(
229249
_ptr + index,
230250
_ptr + index + count,
231-
(_capacity - index - count) * sizeof(T),
251+
(Capacity - index - count) * sizeof(T),
232252
elementsToShift * sizeof(T));
233253
}
234254

@@ -247,12 +267,12 @@ public void InsertAt(int index, T value, int count = 1)
247267
/// <param name="minCapacity">The minimum capacity.</param>
248268
public void EnsureCapacity(int minCapacity)
249269
{
250-
if (minCapacity <= _capacity)
270+
if (minCapacity <= Capacity)
251271
{
252272
return;
253273
}
254274

255-
var newCapacity = _capacity == 0 ? 4 : _capacity;
275+
var newCapacity = Capacity == 0 ? 4 : Capacity;
256276
while (newCapacity < minCapacity)
257277
{
258278
newCapacity *= 2;
@@ -299,7 +319,7 @@ private void Dispose(bool disposing)
299319
_buffer = IntPtr.Zero;
300320
_ptr = null;
301321
Length = 0;
302-
_capacity = 0;
322+
Capacity = 0;
303323
}
304324

305325
_disposed = true;

0 commit comments

Comments
 (0)