Skip to content

Commit e07cc47

Browse files
committed
Implemented suggested modifications
1 parent 6a9e642 commit e07cc47

File tree

2 files changed

+59
-65
lines changed

2 files changed

+59
-65
lines changed

DataStructures/Lists/CircularBuffer.cs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ namespace DataStructures.Lists
44
{
55
public class CircularBuffer<T>
66
{
7+
private T[] _circularBuffer;
8+
private int _end;
9+
private int _start;
10+
private static readonly int _defaultBufferLength = 10;
11+
12+
/// <summary>
13+
/// Controls whether data should be overridden when it is continously inserted without reading
14+
/// </summary>
15+
public bool CanOverride { get; }
16+
public bool IsEmpty { get => _end == _start; }
17+
public int Length { get => _circularBuffer.Length - 1; }
18+
public bool IsFilledUp { get => ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]); }
19+
720
/// <summary>
821
/// Initializes a circular buffer with initial length of 10
922
/// </summary>
@@ -31,30 +44,24 @@ public CircularBuffer(int length, bool canOverride=true)
3144
/// Writes value to the back of the buffer
3245
/// </summary>
3346
/// <param name="value">value to be added to the buffer</param>
34-
public void Write(T value)
47+
public void Add(T value)
3548
{
3649
if (CanOverride)
3750
{
38-
InsertData(value);
51+
innerInsert(value);
3952
}
4053
else
4154
{
42-
DontOverrides(value);
55+
if (IsFilledUp)
56+
{
57+
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
58+
}
59+
innerInsert(value);
4360
}
4461
}
4562

46-
// Inserts data into the buffer when it is not filled up
47-
private void DontOverrides(T value)
48-
{
49-
if (IsFilledUp)
50-
{
51-
throw new CircularBufferFullException($"Circular Buffer is filled up. {value} can not be inserted");
52-
}
53-
InsertData(value);
54-
}
55-
5663
// Inserts data into the buffer without checking if it is full
57-
private void InsertData(T value)
64+
private void innerInsert(T value)
5865
{
5966
_circularBuffer[_end] = value;
6067
_end = (_end + 1) % _circularBuffer.Length;
@@ -65,28 +72,15 @@ private void InsertData(T value)
6572
}
6673

6774
/// <summary>
68-
/// Reads value from the front of the buffer
75+
/// Reads and removes the value in front of the buffer, and places the next value in front.
6976
/// </summary>
70-
public T Read()
77+
public T Pop()
7178
{
7279
var result = _circularBuffer[_start];
7380
_circularBuffer[_start] = _circularBuffer[_end];
7481
_start = (_start + 1) % _circularBuffer.Length;
7582
return result;
7683
}
77-
78-
/// <summary>
79-
/// Controls whether data should be overridden when it is continously inserted without reading
80-
/// </summary>
81-
public bool CanOverride { get; }
82-
public bool IsEmpty { get => _end == _start; }
83-
public int Length { get => _circularBuffer.Length - 1; }
84-
public bool IsFilledUp { get => ((_end + 1) % _circularBuffer.Length == _start) && !_circularBuffer[_start].Equals(_circularBuffer[_end]); }
85-
86-
private T[] _circularBuffer;
87-
private int _end;
88-
private int _start;
89-
private static readonly int _defaultBufferLength = 10;
9084
}
9185

9286
public class CircularBufferFullException: Exception

UnitTest/DataStructuresTests/CircularBufferTest.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public static void ChecksIsEmptyProperty()
2626
public static void ChecksIsFilledProperty()
2727
{
2828
var circularBuffer = new CircularBuffer<byte>(3, false);
29-
circularBuffer.Write(1);
30-
circularBuffer.Write(2);
31-
circularBuffer.Write(3);
29+
circularBuffer.Add(1);
30+
circularBuffer.Add(2);
31+
circularBuffer.Add(3);
3232

3333
Assert.True(circularBuffer.IsFilledUp);
3434
}
@@ -60,30 +60,30 @@ public static void ThrowsCircularBufferFullExceptionWhenInsertingInFullBuffer()
6060
Assert.Throws<CircularBufferFullException>(() =>
6161
{
6262
var circularBuffer = new CircularBuffer<byte>(3, false);
63-
circularBuffer.Write(1);
64-
circularBuffer.Write(2);
65-
circularBuffer.Write(3);
66-
circularBuffer.Write(4);
63+
circularBuffer.Add(1);
64+
circularBuffer.Add(2);
65+
circularBuffer.Add(3);
66+
circularBuffer.Add(4);
6767
});
6868
}
6969

7070
[Fact]
7171
public static void WritesAndReadsValue()
7272
{
7373
var circularBuffer = new CircularBuffer<byte>(4);
74-
circularBuffer.Write(13);
75-
circularBuffer.Write(43);
76-
circularBuffer.Write(23);
77-
circularBuffer.Write(2);
74+
circularBuffer.Add(13);
75+
circularBuffer.Add(43);
76+
circularBuffer.Add(23);
77+
circularBuffer.Add(2);
7878

79-
var result1 = circularBuffer.Read();
80-
var result2 = circularBuffer.Read();
81-
var result3 = circularBuffer.Read();
82-
var result4 = circularBuffer.Read();
83-
var result5 = circularBuffer.Read();
84-
var result6 = circularBuffer.Read();
85-
var result7 = circularBuffer.Read();
86-
var result8 = circularBuffer.Read();
79+
var result1 = circularBuffer.Pop();
80+
var result2 = circularBuffer.Pop();
81+
var result3 = circularBuffer.Pop();
82+
var result4 = circularBuffer.Pop();
83+
var result5 = circularBuffer.Pop();
84+
var result6 = circularBuffer.Pop();
85+
var result7 = circularBuffer.Pop();
86+
var result8 = circularBuffer.Pop();
8787

8888
Assert.Equal(13, result1);
8989
Assert.Equal(43, result2);
@@ -99,19 +99,19 @@ public static void WritesAndReadsValue()
9999
public static void TestingCantOverrideFunctionality()
100100
{
101101
var circularBuffer = new CircularBuffer<byte>(3, false);
102-
circularBuffer.Write(3);
103-
circularBuffer.Write(34);
104-
circularBuffer.Write(24);
102+
circularBuffer.Add(3);
103+
circularBuffer.Add(34);
104+
circularBuffer.Add(24);
105105
// if it doesn't override, then it will throw CircularBufferFullException
106106
Assert.Throws<CircularBufferFullException>(() =>
107107
{
108-
circularBuffer.Write(2);
108+
circularBuffer.Add(2);
109109
});
110110

111111
// Ensuring that it reads the appropriate values in the buffer.
112-
var result1 = circularBuffer.Read();
113-
var result2 = circularBuffer.Read();
114-
var result3 = circularBuffer.Read();
112+
var result1 = circularBuffer.Pop();
113+
var result2 = circularBuffer.Pop();
114+
var result3 = circularBuffer.Pop();
115115

116116
Assert.Equal(3, result1);
117117
Assert.Equal(34, result2);
@@ -122,17 +122,17 @@ public static void TestingCantOverrideFunctionality()
122122
public static void TestingWritingAndReadingSimultenouslyWithoutOverriding()
123123
{
124124
var circularBuffer = new CircularBuffer<byte>(3, false);
125-
circularBuffer.Write(3);
126-
circularBuffer.Write(34);
127-
circularBuffer.Write(24);
128-
var result1 = circularBuffer.Read();
129-
var result2 = circularBuffer.Read();
125+
circularBuffer.Add(3);
126+
circularBuffer.Add(34);
127+
circularBuffer.Add(24);
128+
var result1 = circularBuffer.Pop();
129+
var result2 = circularBuffer.Pop();
130130

131-
circularBuffer.Write(4);
132-
circularBuffer.Write(14);
133-
var result3 = circularBuffer.Read();
134-
var result4 = circularBuffer.Read();
135-
var result5 = circularBuffer.Read();
131+
circularBuffer.Add(4);
132+
circularBuffer.Add(14);
133+
var result3 = circularBuffer.Pop();
134+
var result4 = circularBuffer.Pop();
135+
var result5 = circularBuffer.Pop();
136136

137137
Assert.Equal(24, result3);
138138
Assert.Equal(4, result4);

0 commit comments

Comments
 (0)