1
1
using System ;
2
2
3
- namespace DataStructures . Lists {
4
- public class CircularBuffer < T > {
3
+ namespace DataStructures . Lists
4
+ {
5
+ public class CircularBuffer < T >
6
+ {
5
7
/// <summary>
6
8
/// Initializes a circular buffer with initial length of 10
7
9
/// </summary>
8
- public CircularBuffer ( bool canOverride = true ) : this ( 10 , canOverride ) {
10
+ public CircularBuffer ( bool canOverride = true ) : this ( _defaultBufferLength , canOverride )
11
+ {
9
12
}
10
13
11
14
/// <summary>
12
15
/// Initializes a circular buffer with given length
13
16
/// </summary>
14
17
/// <param name="length">The length of the buffer</param>
15
- public CircularBuffer ( int length , bool canOverride = true ) {
16
- if ( length < 1 ) {
18
+ public CircularBuffer ( int length , bool canOverride = true )
19
+ {
20
+ if ( length < 1 )
21
+ {
17
22
throw new ArgumentOutOfRangeException ( "length can not be zero or negative" ) ;
18
23
}
19
24
_circularBuffer = new T [ length + 1 ] ;
@@ -26,62 +31,68 @@ public CircularBuffer(int length, bool canOverride=true) {
26
31
/// Writes value to the back of the buffer
27
32
/// </summary>
28
33
/// <param name="value">value to be added to the buffer</param>
29
- public void Write ( T value ) {
30
- if ( CanOverride ) {
34
+ public void Write ( T value )
35
+ {
36
+ if ( CanOverride )
37
+ {
31
38
InsertData ( value ) ;
32
39
}
33
- else {
40
+ else
41
+ {
34
42
DontOverrides ( value ) ;
35
43
}
36
44
}
37
45
38
- /// <summary>
39
- /// Inserts data into the buffer when it is not filled up
40
- /// </summary>
41
- /// <param name="value"></param>
42
- private void DontOverrides ( T value ) {
43
- if ( IsFilledUp ) {
44
- return ;
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") ;
45
52
}
46
53
InsertData ( value ) ;
47
54
}
48
55
49
- /// <summary>
50
- /// Inserts data into the buffer without checking if it is full
51
- /// </summary>
52
- /// <param name="value"></param>
53
- private void InsertData ( T value ) {
56
+ // Inserts data into the buffer without checking if it is full
57
+ private void InsertData ( T value )
58
+ {
54
59
_circularBuffer [ _end ] = value ;
55
60
_end = ( _end + 1 ) % _circularBuffer . Length ;
56
- if ( _end == _start ) {
61
+ if ( _end == _start )
62
+ {
57
63
_start = ( _start + 1 ) % _circularBuffer . Length ;
58
64
}
59
65
}
60
66
61
67
/// <summary>
62
68
/// Reads value from the front of the buffer
63
- /// <summay>
64
- /// Use the IsEmpty property to know when to read appropriately.
65
- /// </summay>
66
69
/// </summary>
67
- /// <returns></returns>
68
- public T Read ( ) {
70
+ public T Read ( )
71
+ {
69
72
var result = _circularBuffer [ _start ] ;
70
73
_circularBuffer [ _start ] = _circularBuffer [ _end ] ;
71
74
_start = ( _start + 1 ) % _circularBuffer . Length ;
72
75
return result ;
73
76
}
74
77
75
- public bool IsEmpty { get => _end == _start ; }
76
- public int Length { get => _circularBuffer . Length - 1 ; }
77
- public bool IsFilledUp { get => ( ( _end + 1 ) % _circularBuffer . Length == _start ) && ! _circularBuffer [ _start ] . Equals ( _circularBuffer [ _end ] ) ; }
78
78
/// <summary>
79
79
/// Controls whether data should be overridden when it is continously inserted without reading
80
80
/// </summary>
81
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 ] ) ; }
82
85
83
86
private T [ ] _circularBuffer ;
84
87
private int _end ;
85
88
private int _start ;
89
+ private static readonly int _defaultBufferLength = 10 ;
90
+ }
91
+
92
+ public class CircularBufferFullException : Exception
93
+ {
94
+ public CircularBufferFullException ( string message ) : base ( message )
95
+ {
96
+ }
86
97
}
87
98
}
0 commit comments