This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed
src/System.Collections/src/System/Collections/Generic Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ public void Enqueue(T item)
213
213
}
214
214
215
215
_array [ _tail ] = item ;
216
- _tail = ( _tail + 1 ) % _array . Length ;
216
+ MoveNext ( ref _tail ) ;
217
217
_size ++ ;
218
218
_version ++ ;
219
219
}
@@ -249,7 +249,7 @@ public T Dequeue()
249
249
250
250
T removed = _array [ _head ] ;
251
251
_array [ _head ] = default ( T ) ;
252
- _head = ( _head + 1 ) % _array . Length ;
252
+ MoveNext ( ref _head ) ;
253
253
_size -- ;
254
254
_version ++ ;
255
255
return removed ;
@@ -289,13 +289,13 @@ public bool Contains(T item)
289
289
{
290
290
return true ;
291
291
}
292
- index = ( index + 1 ) % _array . Length ;
292
+ MoveNext ( ref index ) ;
293
293
}
294
294
295
295
return false ;
296
296
}
297
297
298
- internal T GetElement ( int i )
298
+ private T GetElement ( int i )
299
299
{
300
300
return _array [ ( _head + i ) % _array . Length ] ;
301
301
}
@@ -324,7 +324,6 @@ public T[] ToArray()
324
324
return arr ;
325
325
}
326
326
327
-
328
327
// PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity
329
328
// must be >= _size.
330
329
private void SetCapacity ( int capacity )
@@ -349,6 +348,15 @@ private void SetCapacity(int capacity)
349
348
_version ++ ;
350
349
}
351
350
351
+ // Increments the index wrapping it if necessary.
352
+ private void MoveNext ( ref int index )
353
+ {
354
+ // It is tempting to use the remainder operator here but it is actually much slower
355
+ // than a simple comparison and a rarely taken branch.
356
+ int tmp = index + 1 ;
357
+ index = ( tmp == _array . Length ) ? 0 : tmp ;
358
+ }
359
+
352
360
public void TrimExcess ( )
353
361
{
354
362
int threshold = ( int ) ( ( ( double ) _array . Length ) * 0.9 ) ;
You can’t perform that action at this time.
0 commit comments