@@ -128,12 +128,21 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
128
128
129
129
public void CopyTo ( Array array , int index )
130
130
{
131
- Array . Copy ( backing , start , array , index , size ) ;
131
+ bool contiguous = end > start ;
132
+ if ( contiguous )
133
+ {
134
+ Array . Copy ( backing , start , array , index , size ) ;
135
+ }
136
+ else
137
+ {
138
+ Array . Copy ( backing , start , array , index , backing . Length - start ) ;
139
+ Array . Copy ( backing , 0 , array , index + backing . Length - start , end ) ;
140
+ }
132
141
}
133
142
134
143
public void CopyTo ( T [ ] array , int index )
135
144
{
136
- Array . Copy ( backing , start , array , index , size ) ;
145
+ CopyTo ( ( Array ) array , index ) ;
137
146
}
138
147
139
148
public T this [ int index ]
@@ -164,8 +173,24 @@ public bool Contains(T value)
164
173
165
174
public void Insert ( int index , T value )
166
175
{
167
- Array . Copy ( backing , index , backing , index + 1 , size - index ) ;
168
- backing [ index ] = value ;
176
+ if ( index < 0 || index >= size )
177
+ throw new ArgumentOutOfRangeException ( nameof ( index ) , index , "Index has to be within the bounds of the collection" ) ;
178
+ if ( size == backing . Length )
179
+ resize ( size * GrowthMultiplier ) ;
180
+
181
+ int actualIndex = ( start + index ) % backing . Length ;
182
+ bool contiguous = end > actualIndex ;
183
+ if ( contiguous )
184
+ {
185
+ Array . Copy ( backing , actualIndex , backing , actualIndex + 1 , size - actualIndex ) ;
186
+ }
187
+ else
188
+ {
189
+ Array . Copy ( backing , 0 , backing , 1 , end ) ;
190
+ backing [ 0 ] = backing [ backing . Length - 1 ] ;
191
+ Array . Copy ( backing , actualIndex , backing , actualIndex + 1 , backing . Length - actualIndex - 1 ) ;
192
+ }
193
+ backing [ actualIndex ] = value ;
169
194
end = ( end + 1 ) % backing . Length ;
170
195
size ++ ;
171
196
}
@@ -194,6 +219,12 @@ public void RemoveAt(int index)
194
219
if ( index < 0 || index >= size )
195
220
throw new ArgumentOutOfRangeException ( nameof ( index ) , index , "Index has to be within the bounds of the collection" ) ;
196
221
222
+ if ( index == 0 )
223
+ {
224
+ PopFront ( ) ;
225
+ return ;
226
+ }
227
+
197
228
int actualIndex = ( start + index ) % backing . Length ;
198
229
int copyCount = Math . Min ( size - index - 1 , backing . Length - actualIndex - 1 ) ;
199
230
Array . Copy ( backing , actualIndex + 1 , backing , actualIndex , copyCount ) ;
0 commit comments