1
1
using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . Linq ;
2
5
3
6
namespace DataStructures . Lists
4
7
{
5
- public class CircularBuffer < T >
6
- {
8
+ public class CircularBuffer < T > : IEnumerable < T > , ICollection < T > where T : IComparable < T > {
7
9
private T [ ] _circularBuffer ;
8
10
private int _end ;
9
11
private int _start ;
12
+ private int _length { get => _circularBuffer . Length - 1 ; }
13
+ private readonly T _default ;
10
14
private static readonly int _defaultBufferLength = 10 ;
11
15
12
16
/// <summary>
13
17
/// Controls whether data should be overridden when it is continously inserted without reading
14
18
/// </summary>
15
19
public bool CanOverride { get ; }
16
20
public bool IsEmpty { get => _end == _start ; }
17
- public int Length { get => _circularBuffer . Length - 1 ; }
18
21
public bool IsFilledUp { get => ( ( _end + 1 ) % _circularBuffer . Length == _start ) && ! _circularBuffer [ _start ] . Equals ( _circularBuffer [ _end ] ) ; }
19
22
23
+
20
24
/// <summary>
21
25
/// Initializes a circular buffer with initial length of 10
22
26
/// </summary>
@@ -35,6 +39,7 @@ public CircularBuffer(int length, bool canOverride=true)
35
39
throw new ArgumentOutOfRangeException ( "length can not be zero or negative" ) ;
36
40
}
37
41
_circularBuffer = new T [ length + 1 ] ;
42
+ _default = _circularBuffer [ length ] ;
38
43
_end = 0 ;
39
44
_start = 0 ;
40
45
CanOverride = canOverride ;
@@ -81,6 +86,106 @@ public T Pop()
81
86
_start = ( _start + 1 ) % _circularBuffer . Length ;
82
87
return result ;
83
88
}
89
+
90
+ #region IEnumerable Implementation
91
+ public IEnumerator < T > GetEnumerator ( )
92
+ {
93
+ for ( int i = 0 ; i < Count ; i ++ ) {
94
+ yield return _circularBuffer [ i ] ;
95
+ }
96
+ }
97
+
98
+ IEnumerator IEnumerable . GetEnumerator ( )
99
+ {
100
+ return GetEnumerator ( ) ;
101
+ }
102
+ #endregion
103
+
104
+ #region ICollection Implementation
105
+ /// <summary>
106
+ /// Returns the number of elements.
107
+ /// </summary>
108
+ public int Count
109
+ {
110
+ get { return _length ; }
111
+ }
112
+ /// <summary>
113
+ /// Checks whether this collection is readonly
114
+ /// </summary>
115
+ public bool IsReadOnly
116
+ {
117
+ get { return false ; }
118
+ }
119
+ /// <summary>
120
+ /// Clears this instance
121
+ /// </summary>
122
+ public void Clear ( )
123
+ {
124
+ for ( int i = 0 ; i < _circularBuffer . Length ; i ++ )
125
+ {
126
+ _circularBuffer [ i ] = _default ;
127
+ }
128
+ }
129
+ /// <summary>
130
+ /// Checks whether the buffer contains an item
131
+ /// </summary>
132
+ public bool Contains ( T item )
133
+ {
134
+ return _circularBuffer . Contains ( item ) ;
135
+ }
136
+ /// <summary>
137
+ /// Copies this buffer to an array
138
+ /// </summary>
139
+ public void CopyTo ( T [ ] array , int arrayIndex )
140
+ {
141
+ if ( array == null )
142
+ {
143
+ throw new ArgumentNullException ( "array can not be null" ) ;
144
+ }
145
+
146
+ if ( array . Length == 0 || arrayIndex >= array . Length || arrayIndex < 0 ) {
147
+ throw new IndexOutOfRangeException ( ) ;
148
+ }
149
+
150
+ // Get enumerator
151
+ var enumarator = GetEnumerator ( ) ;
152
+
153
+ // Copy elements if there is any in the buffer and if the index is within the valid range
154
+ while ( arrayIndex < array . Length )
155
+ {
156
+ if ( enumarator . MoveNext ( ) )
157
+ {
158
+ array [ arrayIndex ] = enumarator . Current ;
159
+ arrayIndex ++ ;
160
+ }
161
+ else
162
+ {
163
+ break ;
164
+ }
165
+ }
166
+ }
167
+ /// <summary>
168
+ /// Removes an item from the buffer
169
+ /// </summary>
170
+ public bool Remove ( T item )
171
+ {
172
+ if ( Contains ( item ) )
173
+ {
174
+ for ( int i = 0 ; i < _circularBuffer . Length ; i ++ )
175
+ {
176
+ if ( item . Equals ( _circularBuffer [ i ] ) )
177
+ {
178
+ _circularBuffer [ i ] = _default ;
179
+ }
180
+ }
181
+ return true ;
182
+ }
183
+ else
184
+ {
185
+ return false ;
186
+ }
187
+ }
188
+ #endregion
84
189
}
85
190
86
191
public class CircularBufferFullException : Exception
0 commit comments