@@ -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,
0 commit comments