33--********************************************************************/
44
55using System ;
6- using System . Collections . Generic ;
76using System . Diagnostics ;
87using System . Diagnostics . CodeAnalysis ;
98
@@ -12,63 +11,41 @@ namespace Microsoft.PowerShell
1211 [ ExcludeFromCodeCoverage ]
1312 internal sealed class QueueDebugView < T >
1413 {
15- private readonly HistoryQueue < T > _queue ;
14+ private readonly RingBuffer < T > _queue ;
1615
1716 [ DebuggerBrowsable ( DebuggerBrowsableState . RootHidden ) ]
1817 public T [ ] Items => this . _queue . ToArray ( ) ;
1918
20- public QueueDebugView ( HistoryQueue < T > queue )
19+ public QueueDebugView ( RingBuffer < T > queue )
2120 {
2221 this . _queue = queue ?? throw new ArgumentNullException ( nameof ( queue ) ) ;
2322 }
2423 }
2524
2625 [ DebuggerDisplay ( "Count = {" + nameof ( Count ) + "}" ) ]
2726 [ DebuggerTypeProxy ( typeof ( QueueDebugView < > ) ) ]
28- internal class HistoryQueue < T >
27+ internal class RingBuffer < T >
2928 {
3029 private readonly T [ ] _array ;
3130 private int _head ;
3231 private int _tail ;
3332
34- public HistoryQueue ( int capacity )
33+ public int Capacity => _array . Length ;
34+ public int Count { get ; private set ; }
35+
36+ public RingBuffer ( int capacity )
3537 {
36- Debug . Assert ( capacity > 0 ) ;
38+ ArgumentOutOfRangeException . ThrowIfLessThanOrEqual ( capacity , 0 ) ;
3739 _array = new T [ capacity ] ;
3840 _head = _tail = Count = 0 ;
3941 }
4042
4143 public void Clear ( )
4244 {
43- for ( int i = 0 ; i < Count ; i ++ )
44- {
45- this [ i ] = default ( T ) ;
46- }
45+ Array . Clear ( _array ) ;
4746 _head = _tail = Count = 0 ;
4847 }
4948
50- public bool Contains ( T item )
51- {
52- return IndexOf ( item ) != - 1 ;
53- }
54-
55- public int Count { get ; private set ; }
56-
57- public int IndexOf ( T item )
58- {
59- // REVIEW: should we use case insensitive here?
60- var eqComparer = EqualityComparer < T > . Default ;
61- for ( int i = 0 ; i < Count ; i ++ )
62- {
63- if ( eqComparer . Equals ( this [ i ] , item ) )
64- {
65- return i ;
66- }
67- }
68-
69- return - 1 ;
70- }
71-
7249 public void Enqueue ( T item )
7350 {
7451 if ( Count == _array . Length )
@@ -82,10 +59,10 @@ public void Enqueue(T item)
8259
8360 public T Dequeue ( )
8461 {
85- Debug . Assert ( Count > 0 ) ;
62+ if ( Count == 0 ) throw new InvalidOperationException ( "RingBuffer is empty" ) ;
8663
8764 T obj = _array [ _head ] ;
88- _array [ _head ] = default ( T ) ;
65+ _array [ _head ] = default ;
8966 _head = ( _head + 1 ) % _array . Length ;
9067 Count -= 1 ;
9168 return obj ;
@@ -113,14 +90,10 @@ public T[] ToArray()
11390 public T this [ int index ]
11491 {
11592 get
116- {
117- Debug . Assert ( index >= 0 && index < Count ) ;
118- return _array [ ( _head + index ) % _array . Length ] ;
119- }
120- set
12193 {
122- Debug . Assert ( index >= 0 && index < Count ) ;
123- _array [ ( _head + index ) % _array . Length ] = value ;
94+ ArgumentOutOfRangeException . ThrowIfNegative ( index ) ;
95+ ArgumentOutOfRangeException . ThrowIfGreaterThanOrEqual ( index , Count ) ;
96+ return _array [ ( _head + index ) % _array . Length ] ;
12497 }
12598 }
12699 }
0 commit comments