55namespace Advanced . Algorithms . DataStructures
66{
77 /// <summary>
8- /// A self expanding array (dynamic array) aka array vector
8+ /// A self expanding array implementation.
99 /// </summary>
10- /// <typeparam name="T"></typeparam>
10+ /// <typeparam name="T">The datatype of this ArrayList. </typeparam>
1111 public class ArrayList < T > : IEnumerable < T >
1212 {
1313 private readonly int initialArraySize ;
1414 private int arraySize ;
15-
1615 private T [ ] array ;
1716
18- private int currentEndPosition ;
19- public int Length => currentEndPosition ;
17+ public int Length { get ; private set ; }
2018
21- //constructor init
19+ /// <summary>
20+ /// Constructor.
21+ /// TimeComplexity: O(1) if initial is empty otherwise O(n).
22+ /// </summary>
23+ /// <param name="initalArraySize">The initial array size.</param>
24+ /// <param name="initial">Initial values if any.</param>
2225 public ArrayList ( int initalArraySize = 2 , IEnumerable < T > initial = null )
2326 {
2427 if ( initalArraySize < 2 )
@@ -42,24 +45,25 @@ public ArrayList(int initalArraySize = 2, IEnumerable<T> initial = null)
4245 }
4346
4447 /// <summary>
45- /// Overloaded constructor
48+ /// Constructor.
49+ /// TimeComplexity: O(1) if initial is empty otherwise O(n).
4650 /// </summary>
47- /// <param name="initial"></param>
51+ /// <param name="initial">Initial values if any. </param>
4852 public ArrayList ( IEnumerable < T > initial )
4953 : this ( 2 , initial ) { }
5054
5155 /// <summary>
52- /// Expose indexer
56+ /// Indexed access to array.
57+ /// Time Complexity: O(1).
5358 /// </summary>
54- /// <param name="index"></param>
59+ /// <param name="index">The index to write or read. </param>
5560 /// <returns></returns>
5661 public T this [ int index ]
5762 {
5863 get => itemAt ( index ) ;
5964 set => setItem ( index , value ) ;
6065 }
6166
62- //O(1)
6367 private T itemAt ( int i )
6468 {
6569 if ( i >= Length )
@@ -68,51 +72,51 @@ private T itemAt(int i)
6872 return array [ i ] ;
6973 }
7074
71- //O(1) amortized
75+ /// <summary>
76+ /// Add a new item to this array list.
77+ /// Time complexity: O(1) amortized.
78+ /// </summary>
79+ /// <param name="item"></param>
7280 public void Add ( T item )
7381 {
7482 grow ( ) ;
7583
76- array [ currentEndPosition ] = item ;
77- currentEndPosition ++ ;
84+ array [ Length ] = item ;
85+ Length ++ ;
7886 }
7987
8088 /// <summary>
8189 /// Insert element at specified index
90+ /// Time complexity: O(1) amortized.
8291 /// </summary>
83- /// <param name="index"></ param>
84- /// <param name="item"></param>
92+ /// <param name="index">The index to insert at.< param>
93+ /// <param name="item">The item to insert. </param>
8594 public void InsertAt ( int index , T item )
8695 {
8796 grow ( ) ;
8897
8998 shift ( index ) ;
9099
91100 array [ index ] = item ;
92- currentEndPosition ++ ;
101+ Length ++ ;
93102 }
94103
95104 /// <summary>
96- /// shift the position of elements right by one starting at this index
97- /// create a blank field at index
105+ /// Shift the position of elements right by one starting at this index.
106+ /// Creates a blank field at index.
98107 /// </summary>
99- /// <param name="index"></param>
100108 private void shift ( int index )
101109 {
102110 Array . Copy ( array , index , array , index + 1 , Length - index ) ;
103111 }
104112
105- /// <summary>
106- /// empty the list
107- /// </summary>
108113 internal void Clear ( )
109114 {
110115 arraySize = initialArraySize ;
111116 array = new T [ arraySize ] ;
112- currentEndPosition = 0 ;
117+ Length = 0 ;
113118 }
114119
115- //O(1)
116120 private void setItem ( int i , T item )
117121 {
118122 if ( i >= Length )
@@ -121,8 +125,12 @@ private void setItem(int i, T item)
121125 array [ i ] = item ;
122126 }
123127
124- //O(n)
125- public void RemoveItem ( int i )
128+ /// <summary>
129+ /// Remove the item at given index.
130+ /// Time complexity: O(1) amortized.
131+ /// </summary>
132+ /// <param name="i">The index to remove at.</param>
133+ public void RemoveAt ( int i )
126134 {
127135 if ( i >= Length )
128136 throw new System . Exception ( "Index exeeds array size" ) ;
@@ -133,18 +141,14 @@ public void RemoveItem(int i)
133141 array [ j ] = array [ j + 1 ] ;
134142 }
135143
136- currentEndPosition -- ;
144+ Length -- ;
137145
138146 shrink ( ) ;
139147 }
140148
141-
142- /// <summary>
143- /// Grow array if needed
144- /// </summary>
145149 private void grow ( )
146150 {
147- if ( currentEndPosition != arraySize )
151+ if ( Length != arraySize )
148152 {
149153 return ;
150154 }
@@ -153,16 +157,13 @@ private void grow()
153157 arraySize *= 2 ;
154158
155159 var biggerArray = new T [ arraySize ] ;
156- Array . Copy ( array , 0 , biggerArray , 0 , currentEndPosition ) ;
160+ Array . Copy ( array , 0 , biggerArray , 0 , Length ) ;
157161 array = biggerArray ;
158162 }
159163
160- /// <summary>
161- /// Shrink array if needed
162- /// </summary>
163164 private void shrink ( )
164165 {
165- if ( currentEndPosition != arraySize / 2 || arraySize == initialArraySize )
166+ if ( Length != arraySize / 2 || arraySize == initialArraySize )
166167 {
167168 return ;
168169 }
@@ -171,42 +172,10 @@ private void shrink()
171172 arraySize /= 2 ;
172173
173174 var smallerArray = new T [ arraySize ] ;
174- Array . Copy ( array , 0 , smallerArray , 0 , currentEndPosition ) ;
175+ Array . Copy ( array , 0 , smallerArray , 0 , Length ) ;
175176 array = smallerArray ;
176177 }
177178
178-
179- /// <summary>
180- /// Returns as an array
181- /// </summary>
182- /// <returns></returns>
183- public T [ ] ToArray ( )
184- {
185- var result = new T [ Length ] ;
186-
187- var i = 0 ;
188- foreach ( var item in this )
189- {
190- result [ i ] = item ;
191- i ++ ;
192- }
193-
194- return result ;
195- }
196-
197- /// <summary>
198- /// Add's the given array to the end
199- /// </summary>
200- /// <param name="array"></param>
201- public void AddRange ( T [ ] array )
202- {
203- foreach ( var item in array )
204- {
205- Add ( item ) ;
206- }
207- }
208-
209- //Implementation for the GetEnumerator method.
210179 IEnumerator IEnumerable . GetEnumerator ( )
211180 {
212181 return GetEnumerator ( ) ;
@@ -218,8 +187,7 @@ public IEnumerator<T> GetEnumerator()
218187 }
219188 }
220189
221- // implement IEnumerator.
222- public class ArrayListEnumerator < T > : IEnumerator < T >
190+ internal class ArrayListEnumerator < T > : IEnumerator < T >
223191 {
224192 private T [ ] array ;
225193
@@ -228,7 +196,7 @@ public class ArrayListEnumerator<T> : IEnumerator<T>
228196 private int position = - 1 ;
229197 private int length ;
230198
231- public ArrayListEnumerator ( T [ ] list , int length )
199+ internal ArrayListEnumerator ( T [ ] list , int length )
232200 {
233201 this . length = length ;
234202 array = list ;
@@ -265,8 +233,6 @@ public T Current
265233 public void Dispose ( )
266234 {
267235 array = null ;
268- length = 0 ;
269- position = - 1 ;
270236 }
271237 }
272238
0 commit comments