Skip to content

Commit 0719b39

Browse files
committed
Re-Implemented methods in ICollection interface based on Suggestions
1 parent 1e83529 commit 0719b39

File tree

2 files changed

+80
-42
lines changed

2 files changed

+80
-42
lines changed

DataStructures/Lists/CircularBuffer.cs

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,67 @@
55

66
namespace DataStructures.Lists
77
{
8-
public class CircularBuffer<T> : IEnumerable<T> , ICollection<T> where T : IComparable<T> {
8+
public class CircularBuffer<T> : IEnumerable<T>, ICollection<T> where T : IComparable<T>
9+
{
910
private T[] _circularBuffer;
1011
private int _end;
1112
private int _start;
12-
private int _length { get => _circularBuffer.Length - 1; }
1313
private readonly T _default;
1414
private static readonly int _defaultBufferLength = 10;
1515

1616
/// <summary>
17-
/// Controls whether data should be overridden when it is continously inserted without reading
17+
/// Returns the length of the buffer
18+
/// </summary>
19+
public int Length
20+
{
21+
get
22+
{
23+
return _circularBuffer.Length - 1;
24+
}
25+
}
26+
27+
/// <summary>
28+
/// Checks if no element is inserted into the buffer
1829
/// </summary>
19-
public bool CanOverride { get; }
20-
public bool IsEmpty { get => _end == _start; }
21-
public bool IsFilledUp { get => ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]); }
30+
public bool IsEmpty
31+
{
32+
get
33+
{
34+
return _count == 0;
35+
}
36+
}
2237

38+
/// <summary>
39+
/// Checks if the buffer is filled up
40+
/// </summary>
41+
public bool IsFilledUp
42+
{
43+
get
44+
{
45+
return ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]);
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Controls whether data should be overridden when it is continously inserted without reading
51+
/// </summary>
52+
public bool CanOverride
53+
{
54+
get;
55+
}
2356

2457
/// <summary>
2558
/// Initializes a circular buffer with initial length of 10
2659
/// </summary>
27-
public CircularBuffer(bool canOverride=true) : this(_defaultBufferLength, canOverride)
60+
public CircularBuffer(bool canOverride = true) : this(_defaultBufferLength, canOverride)
2861
{
2962
}
3063

3164
/// <summary>
3265
/// Initializes a circular buffer with given length
3366
/// </summary>
3467
/// <param name="length">The length of the buffer</param>
35-
public CircularBuffer(int length, bool canOverride=true)
68+
public CircularBuffer(int length, bool canOverride = true)
3669
{
3770
if (length < 1)
3871
{
@@ -51,18 +84,11 @@ public CircularBuffer(int length, bool canOverride=true)
5184
/// <param name="value">value to be added to the buffer</param>
5285
public void Add(T value)
5386
{
54-
if (CanOverride)
55-
{
56-
innerInsert(value);
57-
}
58-
else
87+
if (CanOverride==false && IsFilledUp==true)
5988
{
60-
if (IsFilledUp)
61-
{
62-
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
63-
}
64-
innerInsert(value);
89+
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
6590
}
91+
innerInsert(value);
6692
}
6793

6894
// Inserts data into the buffer without checking if it is full
@@ -74,6 +100,9 @@ private void innerInsert(T value)
74100
{
75101
_start = (_start + 1) % _circularBuffer.Length;
76102
}
103+
104+
// Count should not be greater than the length of the buffer when overriding
105+
_count = _count < Length ? ++_count : _count;
77106
}
78107

79108
/// <summary>
@@ -84,13 +113,16 @@ public T Pop()
84113
var result = _circularBuffer[_start];
85114
_circularBuffer[_start] = _circularBuffer[_end];
86115
_start = (_start + 1) % _circularBuffer.Length;
116+
//Count should not go below Zero when poping an empty buffer.
117+
_count = _count > 0 ? --_count : _count;
87118
return result;
88119
}
89120

90121
#region IEnumerable Implementation
91122
public IEnumerator<T> GetEnumerator()
92123
{
93-
for (int i = 0; i < Count; i++) {
124+
for (int i = 0; i < Count; i++)
125+
{
94126
yield return _circularBuffer[i];
95127
}
96128
}
@@ -102,29 +134,34 @@ IEnumerator IEnumerable.GetEnumerator()
102134
#endregion
103135

104136
#region ICollection Implementation
137+
private int _count;
105138
/// <summary>
106139
/// Returns the number of elements.
107140
/// </summary>
108141
public int Count
109142
{
110-
get { return _length; }
143+
get
144+
{
145+
return _count;
146+
}
111147
}
112148
/// <summary>
113149
/// Checks whether this collection is readonly
114150
/// </summary>
115151
public bool IsReadOnly
116152
{
117-
get { return false; }
153+
get
154+
{
155+
return false;
156+
}
118157
}
119158
/// <summary>
120159
/// Clears this instance
121160
/// </summary>
122161
public void Clear()
123162
{
124-
for (int i = 0; i < _circularBuffer.Length; i++)
125-
{
126-
_circularBuffer[i] = _default;
127-
}
163+
_count = 0;
164+
_circularBuffer = new T[Length + 1];
128165
}
129166
/// <summary>
130167
/// Checks whether the buffer contains an item
@@ -143,10 +180,11 @@ public void CopyTo(T[] array, int arrayIndex)
143180
throw new ArgumentNullException("array can not be null");
144181
}
145182

146-
if (array.Length == 0 || arrayIndex >= array.Length || arrayIndex < 0) {
183+
if (array.Length == 0 || arrayIndex >= array.Length || arrayIndex < 0)
184+
{
147185
throw new IndexOutOfRangeException();
148186
}
149-
187+
150188
// Get enumerator
151189
var enumarator = GetEnumerator();
152190

@@ -169,26 +207,23 @@ public void CopyTo(T[] array, int arrayIndex)
169207
/// </summary>
170208
public bool Remove(T item)
171209
{
172-
if (Contains(item))
210+
var result = false;
211+
for (int i = 0; i < _circularBuffer.Length; i++)
173212
{
174-
for (int i = 0; i < _circularBuffer.Length; i++)
213+
if (item.Equals(_circularBuffer[i]))
175214
{
176-
if (item.Equals(_circularBuffer[i]))
177-
{
178-
_circularBuffer[i] = _default;
179-
}
215+
_circularBuffer[i] = _default;
216+
--_count;
217+
result = true;
180218
}
181-
return true;
182-
}
183-
else
184-
{
185-
return false;
186219
}
220+
return result;
221+
187222
}
188223
#endregion
189224
}
190225

191-
public class CircularBufferFullException: Exception
226+
public class CircularBufferFullException : Exception
192227
{
193228
public CircularBufferFullException(string message) : base(message)
194229
{

UnitTest/DataStructuresTests/CircularBufferTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CircularBufferTest
1111
public static void SetsFixedLength()
1212
{
1313
var circularBuffer = new CircularBuffer<byte>(3);
14-
var length = circularBuffer.Count;
14+
var length = circularBuffer.Length;
1515
Assert.Equal(3, length);
1616
}
1717

@@ -37,7 +37,7 @@ public static void ChecksIsFilledProperty()
3737
public static void InitializesWithDefaultLengthOf10()
3838
{
3939
var circularBuffer = new CircularBuffer<byte>();
40-
var length = circularBuffer.Count;
40+
var length = circularBuffer.Length;
4141

4242
Assert.Equal(10, length);
4343
}
@@ -159,12 +159,15 @@ public static void TestingICollectionImplementation()
159159
//Testing Remove
160160
Assert.True(circularBuffer.Remove(3));
161161
Assert.False(circularBuffer.Remove(14));
162-
162+
163+
//Testing Count
164+
Assert.Equal(2, circularBuffer.Count);
163165
//Testing clear
164166
circularBuffer.Clear();
165167
Assert.Equal(0, circularBuffer.Pop());
166168
Assert.Equal(0, circularBuffer.Pop());
167169
Assert.Equal(0, circularBuffer.Pop());
170+
Assert.Empty(circularBuffer);
168171
}
169172
}
170173
}

0 commit comments

Comments
 (0)