Skip to content

Commit f1980cb

Browse files
author
LoneWandererProductions
committed
merge some parts of the interface
1 parent 7579a83 commit f1980cb

6 files changed

Lines changed: 266 additions & 28 deletions

File tree

CommonExtendedObjectsTests/IntListTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ public void AddPopPeekBehavior()
2929
list.Add(20);
3030
list.Add(30);
3131

32-
Assert.AreEqual(3, list.Count);
32+
Assert.AreEqual(3, list.Length);
3333
Assert.AreEqual(30, list.Peek());
3434

3535
var popped = list.Pop();
3636
Assert.AreEqual(30, popped);
37-
Assert.AreEqual(2, list.Count);
37+
Assert.AreEqual(2, list.Length);
3838
Assert.AreEqual(20, list.Peek());
3939

4040
list[0] = 100;
4141
Assert.AreEqual(100, list[0]);
4242

4343
list.Clear();
44-
Assert.AreEqual(0, list.Count);
44+
Assert.AreEqual(0, list.Length);
4545
}
4646

4747
/// <summary>
@@ -78,7 +78,7 @@ public void CapacityResizesCorrectly()
7878
list.Add(i);
7979
}
8080

81-
Assert.AreEqual(1000, list.Count);
81+
Assert.AreEqual(1000, list.Length);
8282
Assert.AreEqual(999, list[999]);
8383
}
8484

ExtendedSystemObjects/IUnmanagedArray.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ public interface IUnmanagedArray<T> : IDisposable
3333
/// The <see cref="T"/>.
3434
/// </value>
3535
/// <param name="index">The index.</param>
36-
/// <returns></returns>
36+
/// <returns>Value at intex</returns>
3737
T this[int index] { get; set; }
3838

39+
/// <summary>
40+
/// Removes at.
41+
/// </summary>
42+
/// <param name="index">The index.</param>
43+
void RemoveAt(int index);
44+
3945
/// <summary>
4046
/// Resizes the specified new size.
4147
/// </summary>

ExtendedSystemObjects/IntArray.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ public IntArray(int size)
6262
/// </summary>
6363
public int Length { get; private set; }
6464

65-
/// <summary>
66-
/// Gets or sets the <see cref="T" /> at the specified index.
67-
/// </summary>
68-
/// <value>
69-
/// The <see cref="T" />.
70-
/// </value>
71-
/// <param name="i">The i.</param>
72-
/// <returns></returns>
73-
/// <exception cref="System.IndexOutOfRangeException"></exception>
7465
public int this[int i]
7566
{
7667
[MethodImpl(MethodImplOptions.AggressiveInlining)]

ExtendedSystemObjects/IntList.cs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace ExtendedSystemObjects
2323
/// Designed for scenarios where manual memory management is needed.
2424
/// </summary>
2525
/// <seealso cref="T:System.IDisposable" />
26-
public sealed unsafe class IntList : IDisposable
26+
public sealed unsafe class IntList : IUnmanagedArray<int>, IDisposable
2727
{
2828
/// <summary>
2929
/// The buffer
@@ -59,7 +59,7 @@ public IntList(int initialCapacity = 16)
5959
/// <summary>
6060
/// Gets the number of elements contained in the <see cref="IntList" />.
6161
/// </summary>
62-
public int Count { get; private set; }
62+
public int Length { get; private set; }
6363

6464
/// <summary>
6565
/// Gets or sets the element at the specified index.
@@ -72,7 +72,7 @@ public int this[int i]
7272
get
7373
{
7474
#if DEBUG
75-
if (i < 0 || i >= Count)
75+
if (i < 0 || i >= Length)
7676
{
7777
throw new IndexOutOfRangeException();
7878
}
@@ -82,7 +82,7 @@ public int this[int i]
8282
set
8383
{
8484
#if DEBUG
85-
if (i < 0 || i >= Count)
85+
if (i < 0 || i >= Length)
8686
{
8787
throw new IndexOutOfRangeException();
8888
}
@@ -106,8 +106,8 @@ public void Push(int value)
106106
/// <param name="value">The integer value to add.</param>
107107
public void Add(int value)
108108
{
109-
EnsureCapacity(Count + 1);
110-
_ptr[Count++] = value;
109+
EnsureCapacity(Length + 1);
110+
_ptr[Length++] = value;
111111
}
112112

113113
/// <summary>
@@ -117,12 +117,12 @@ public void Add(int value)
117117
/// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception>
118118
public int Pop()
119119
{
120-
if (Count == 0)
120+
if (Length == 0)
121121
{
122122
throw new InvalidOperationException("Stack empty");
123123
}
124124

125-
return _ptr[--Count];
125+
return _ptr[--Length];
126126
}
127127

128128
/// <summary>
@@ -132,20 +132,81 @@ public int Pop()
132132
/// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception>
133133
public int Peek()
134134
{
135-
if (Count == 0)
135+
if (Length == 0)
136136
{
137137
throw new InvalidOperationException("Stack empty");
138138
}
139139

140-
return _ptr[Count - 1];
140+
return _ptr[Length - 1];
141+
}
142+
143+
/// <summary>
144+
/// Inserts at.
145+
/// </summary>
146+
/// <param name="index">The index.</param>
147+
/// <param name="value">The value.</param>
148+
/// <param name="count">The count.</param>
149+
/// <exception cref="System.ArgumentOutOfRangeException">index</exception>
150+
public void InsertAt(int index, int value, int count = 1)
151+
{
152+
#if DEBUG
153+
if (index < 0 || index > Length) throw new ArgumentOutOfRangeException(nameof(index));
154+
#endif
155+
if (count <= 0) return;
156+
157+
EnsureCapacity(Length + count);
158+
159+
// Shift elements to the right
160+
Buffer.MemoryCopy(_ptr + index, _ptr + index + count, (_capacity - index - count) * sizeof(int), (Length - index) * sizeof(int));
161+
162+
// Fill with value
163+
for (int i = 0; i < count; i++)
164+
_ptr[index + i] = value;
165+
166+
Length += count;
167+
}
168+
169+
/// <summary>
170+
/// Removes at.
171+
/// </summary>
172+
/// <param name="index">The index.</param>
173+
/// <exception cref="System.ArgumentOutOfRangeException">index</exception>
174+
public void RemoveAt(int index)
175+
{
176+
#if DEBUG
177+
if (index < 0 || index >= Length) throw new ArgumentOutOfRangeException(nameof(index));
178+
#endif
179+
if (index < Length - 1)
180+
{
181+
Buffer.MemoryCopy(_ptr + index + 1, _ptr + index, (Length - index - 1) * sizeof(int), (Length - index - 1) * sizeof(int));
182+
}
183+
184+
Length--;
185+
}
186+
187+
/// <summary>
188+
/// Resizes the specified new size.
189+
/// </summary>
190+
/// <param name="newSize">The new size.</param>
191+
/// <exception cref="System.ArgumentOutOfRangeException">newSize</exception>
192+
public void Resize(int newSize)
193+
{
194+
if (newSize < 0) throw new ArgumentOutOfRangeException(nameof(newSize));
195+
EnsureCapacity(newSize);
196+
Length = newSize;
141197
}
142198

143199
/// <summary>
144200
/// Removes all elements from the list. The capacity remains unchanged.
145201
/// </summary>
146202
public void Clear()
147203
{
148-
Count = 0;
204+
Length = 0;
205+
206+
for (var i = 0; i < Length; i++)
207+
{
208+
_ptr[i] = 0;
209+
}
149210
}
150211

151212
/// <summary>
@@ -155,7 +216,7 @@ public void Clear()
155216
/// <returns>A <see cref="Span{Int32}" /> representing the list's contents.</returns>
156217
public Span<int> AsSpan()
157218
{
158-
return new Span<int>((void*)_buffer, Count);
219+
return new Span<int>((void*)_buffer, Length);
159220
}
160221

161222
/// <inheritdoc />
@@ -193,7 +254,7 @@ private void Dispose(bool disposing)
193254
_buffer = IntPtr.Zero;
194255
_ptr = null;
195256
_capacity = 0;
196-
Count = 0;
257+
Length = 0;
197258
}
198259

199260
// If you had managed disposable members and disposing is true,

ExtendedSystemObjects/UnmanagedArray.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T> where T : unma
3636
/// </summary>
3737
private int _capacity;
3838

39-
4039
/// <summary>
4140
/// The disposed
4241
/// </summary>
@@ -96,6 +95,72 @@ public T this[int index]
9695
}
9796
}
9897

98+
/// <summary>
99+
/// Inserts at.
100+
/// </summary>
101+
/// <param name="index">The index.</param>
102+
/// <param name="value">The value.</param>
103+
/// <param name="count">The count.</param>
104+
/// <exception cref="System.ArgumentOutOfRangeException">
105+
/// index
106+
/// or
107+
/// count
108+
/// </exception>
109+
public void InsertAt(int index, T value, int count = 1)
110+
{
111+
if (index < 0 || index > Length)
112+
throw new ArgumentOutOfRangeException(nameof(index));
113+
if (count < 0)
114+
throw new ArgumentOutOfRangeException(nameof(count));
115+
if (count == 0)
116+
return;
117+
118+
EnsureCapacity(Length + count);
119+
120+
int elementsToShift = Length - index;
121+
if (elementsToShift > 0)
122+
{
123+
// Shift existing elements right by 'count'
124+
Buffer.MemoryCopy(
125+
_ptr + index,
126+
_ptr + index + count,
127+
(_capacity - index - count) * sizeof(T),
128+
elementsToShift * sizeof(T));
129+
}
130+
131+
// Fill inserted region with 'value'
132+
for (int i = 0; i < count; i++)
133+
{
134+
_ptr[index + i] = value;
135+
}
136+
137+
Length += count;
138+
}
139+
140+
/// <summary>
141+
/// Removes at.
142+
/// </summary>
143+
/// <param name="index">The index.</param>
144+
/// <exception cref="System.ArgumentOutOfRangeException">index</exception>
145+
public void RemoveAt(int index)
146+
{
147+
if (index < 0 || index >= Length)
148+
throw new ArgumentOutOfRangeException(nameof(index));
149+
150+
int elementsToShift = Length - index - 1;
151+
if (elementsToShift > 0)
152+
{
153+
// Shift elements left by one to overwrite removed item
154+
Buffer.MemoryCopy(
155+
_ptr + index + 1,
156+
_ptr + index,
157+
(_capacity - index - 1) * sizeof(T),
158+
elementsToShift * sizeof(T));
159+
}
160+
161+
Length--;
162+
}
163+
99164
/// <summary>
100165
/// Resizes the internal array to the specified new size.
101166
/// Contents will be preserved up to the minimum of old and new size.

0 commit comments

Comments
 (0)