Skip to content

Commit 1f69cb4

Browse files
author
LoneWandererProductions
committed
add some wip
1 parent f0801e6 commit 1f69cb4

6 files changed

Lines changed: 81 additions & 33 deletions

File tree

ExtendedSystemObjects/Helper/UnmanagedMemoryHelper.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,34 @@ internal static void Free(void* ptr)
8181
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8282
internal static void Clear<T>(T* buffer, int count) where T : unmanaged
8383
{
84+
<<<<<<< HEAD
85+
unsafe
86+
{
87+
Clear((T*)buffer, count);
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Clears a block of unmanaged memory using a typed pointer.
93+
/// </summary>
94+
/// <typeparam name="T">The unmanaged value type used in the memory block.</typeparam>
95+
/// <param name="ptr">The PTR.</param>
96+
/// <param name="count">The count.</param>
97+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98+
internal static unsafe void Clear<T>(T* ptr, int count) where T : unmanaged
99+
{
100+
new Span<T>(ptr, count).Clear();
101+
}
102+
103+
/// <summary>
104+
/// Shifts the right. Adding data at index.
105+
=======
84106
NativeMemory.Clear(buffer, (nuint)count * (nuint)sizeof(T));
85107
}
86108

87109
/// <summary>
88110
/// Shifts the right. Adding data at index.
111+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
89112
/// </summary>
90113
/// <typeparam name="T">Generic Parameter</typeparam>
91114
/// <param name="ptr">The PTR.</param>

ExtendedSystemObjects/UnmanagedArray.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ public UnmanagedArray(int size)
5454
Capacity = size;
5555
Length = size;
5656

57+
<<<<<<< HEAD
58+
// Fixed the initialization assignment bug here
59+
_ptr = (T*)UnmanagedMemoryHelper.Allocate<T>(size);
60+
UnmanagedMemoryHelper.Clear<T>(_ptr, size);
61+
=======
5762
_ptr = (T*)UnmanagedMemoryHelper.Allocate<T>(size);
5863
UnmanagedMemoryHelper.Clear(_ptr, size);
64+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
5965
}
6066

6167
/// <summary>
@@ -192,13 +198,27 @@ public void Resize(int newSize)
192198

193199
if (newSize == Capacity) return;
194200

201+
<<<<<<< HEAD
202+
// Reallocate directly using the cast pointer
203+
var newBuffer = UnmanagedMemoryHelper.Reallocate<T>((IntPtr)_ptr, newSize);
204+
var newPtr = (T*)newBuffer;
205+
206+
if (newSize > Capacity)
207+
{
208+
// Utilize the new pointer Clear overload cleanly
209+
UnmanagedMemoryHelper.Clear<T>(newPtr + Capacity, newSize - Capacity);
210+
}
211+
212+
_ptr = newPtr;
213+
=======
195214
_ptr = UnmanagedMemoryHelper.Reallocate(_ptr, newSize);
196215

197216
if (newSize > Capacity)
198217
{
199218
UnmanagedMemoryHelper.Clear(_ptr + Capacity, newSize - Capacity);
200219
}
201220

221+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
202222
Capacity = newSize;
203223

204224
if (Length > newSize)
@@ -214,7 +234,11 @@ public void Resize(int newSize)
214234
public void Clear()
215235
{
216236
EnsureNotDisposed();
237+
<<<<<<< HEAD
238+
UnmanagedMemoryHelper.Clear<T>(_ptr, Length);
239+
=======
217240
UnmanagedMemoryHelper.Clear(_ptr, Length);
241+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
218242
}
219243

220244
/// <inheritdoc />
@@ -316,8 +340,12 @@ private void Dispose(bool disposing)
316340

317341
if (_ptr != null)
318342
{
343+
<<<<<<< HEAD
344+
Marshal.FreeHGlobal((IntPtr)_ptr);
345+
=======
319346
// Standardized with tracking allocation layers safely
320347
UnmanagedMemoryHelper.Free(_ptr);
348+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
321349
_ptr = null;
322350
}
323351

ExtendedSystemObjects/UnmanagedIntArray.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,15 @@ public IEnumerator<int> GetEnumerator()
7979
}
8080

8181
/// <inheritdoc />
82-
/// <summary>
83-
/// Returns an enumerator that iterates through a collection.
84-
/// </summary>
85-
/// <returns>
86-
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
87-
/// </returns>
8882
IEnumerator IEnumerable.GetEnumerator()
8983
{
9084
return GetEnumerator();
9185
}
9286

9387
/// <inheritdoc />
94-
/// <summary>
95-
/// Gets the current number of elements in the array.
96-
/// </summary>
9788
public int Length { get; private set; }
9889

9990
/// <inheritdoc />
100-
/// <summary>
101-
/// Gets or sets the <see cref="!:T" /> at the specified index.
102-
/// </summary>
103-
/// <value>
104-
/// The <see cref="!:T" />.
105-
/// </value>
106-
/// <param name="i">The i.</param>
107-
/// <returns>Value at index.</returns>
10891
/// <exception cref="T:System.IndexOutOfRangeException"></exception>
10992
public int this[int i]
11093
{
@@ -135,10 +118,6 @@ public int this[int i]
135118
}
136119

137120
/// <inheritdoc />
138-
/// <summary>
139-
/// Resizes the internal buffer to the new capacity.
140-
/// If newSize is smaller than current Length, Length is reduced.
141-
/// </summary>
142121
public void Resize(int newSize)
143122
{
144123
ArgumentOutOfRangeException.ThrowIfNegative(newSize);
@@ -168,9 +147,6 @@ public void Resize(int newSize)
168147
}
169148

170149
/// <inheritdoc />
171-
/// <summary>
172-
/// Clears the array by setting all elements to zero.
173-
/// </summary>
174150
public void Clear()
175151
{
176152
// No casting, no IntPtr, just pure native clearing.
@@ -277,8 +253,12 @@ public int IndexOf(int value)
277253
}
278254

279255
/// <summary>
280-
/// Inserts 'count' copies of 'value' at the given index.
256+
/// Inserts 'count' copies of 'value' at the given index.
281257
/// </summary>
258+
/// <param name="index">The index.</param>
259+
/// <param name="value">The value.</param>
260+
/// <param name="count">The count.</param>
261+
/// <exception cref="System.ArgumentOutOfRangeException">index</exception>
282262
public void InsertAt(int index, int value, int count = 1)
283263
{
284264
if (index < 0 || index > Length)
@@ -302,8 +282,10 @@ public void InsertAt(int index, int value, int count = 1)
302282
}
303283

304284
/// <summary>
305-
/// Removes multiple elements efficiently, given sorted indices.
285+
/// Removes multiple elements efficiently, given sorted indices.
306286
/// </summary>
287+
/// <param name="indices">The indices.</param>
288+
/// <exception cref="System.IndexOutOfRangeException"></exception>
307289
public void RemoveMultiple(ReadOnlySpan<int> indices)
308290
{
309291
EnsureNotDisposed();
@@ -360,18 +342,20 @@ public void RemoveMultiple(ReadOnlySpan<int> indices)
360342
}
361343

362344
/// <summary>
363-
/// Returns a Span over the used portion of the array.
345+
/// Returns a Span over the used portion of the array.
364346
/// </summary>
347+
/// <returns>A span representing the used portion of the array.</returns>
365348
public Span<int> AsSpan()
366349
{
367350
EnsureNotDisposed();
368351
return new Span<int>(_ptr, Length);
369352
}
370353

371354
/// <summary>
372-
/// Ensures capacity to hold at least minCapacity elements.
373-
/// Grows capacity exponentially if needed.
355+
/// Ensures capacity to hold at least minCapacity elements.
356+
/// Grows capacity exponentially if needed.
374357
/// </summary>
358+
/// <param name="minCapacity">The minimum capacity.</param>
375359
public void EnsureCapacity(int minCapacity)
376360
{
377361
if (minCapacity <= Capacity)

ExtendedSystemObjects/UnmanagedIntList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ private void Dispose(bool disposing)
548548
}
549549

550550
/// <summary>
551-
/// Ensures the capacity of the internal buffer is at least the specified minimum size.
552-
/// Resizes the buffer if necessary by doubling its capacity or setting it to the minimum required size.
551+
/// Ensures the capacity of the internal buffer is at least the specified minimum size.
552+
/// Resizes the buffer if necessary by doubling its capacity or setting it to the minimum required size.
553553
/// </summary>
554554
/// <param name="min">The minimum capacity required.</param>
555555
[MethodImpl(MethodImplOptions.AggressiveInlining)]

ExtendedSystemObjects/UnmanagedList.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: ExtendedSystemObjects
44
* FILE: UnmanagedList.cs
5-
* PURPOSE:
5+
* PURPOSE: A dynamic array implementation that manages its own unmanaged memory, allowing for high-performance operations on large datasets without the overhead of garbage collection.
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

@@ -50,7 +50,7 @@ public sealed unsafe class UnmanagedList<T> : IUnmanagedArray<T>, IEnumerable<T>
5050
/// The <see cref="Span{T}"/>.
5151
/// </value>
5252
/// <param name="range">The range.</param>
53-
/// <returns></returns>
53+
/// <returns>A span representing the specified range of elements.</returns>
5454
public Span<T> this[Range range] => AsSpan()[range];
5555

5656
/// <summary>
@@ -389,8 +389,15 @@ public T Peek()
389389
return _ptr[Length - 1];
390390
}
391391

392+
/// <summary>
393+
/// Sorts this instance.
394+
/// </summary>
392395
public void Sort() => AsSpan().Sort();
393396

397+
/// <summary>
398+
/// Clones this instance.
399+
/// </summary>
400+
/// <returns>A new <see cref="UnmanagedList{T}"/> that is a copy of the current instance.</returns>
394401
public UnmanagedList<T> Clone()
395402
{
396403
var clone = new UnmanagedList<T>(Length);

ExtendedSystemObjects/UnmanagedMap.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
* Unlike typical dictionaries, entries are marked as deleted (tombstoned)
77
* and only physically removed during explicit compaction, improving
88
* insertion and deletion performance by avoiding frequent reallocations.
9+
<<<<<<< HEAD
10+
* Optimized for integer keys and unmanaged value types, with open addressing and linear probing.
11+
* Not thread-safe.
12+
* Second it is not good with bigger datatypes since it has no separate container for values and uses open addressing, so it is best used with small structs or primitive types as values.
13+
=======
14+
>>>>>>> f0801e635c3ba81f09f1576542ea953bd75ade08
915
* PROGRAMMER: Peter Geinitz (Wayfarer)
1016
*/
1117

0 commit comments

Comments
 (0)