-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircle_buffer.h
More file actions
104 lines (88 loc) · 1.45 KB
/
Copy pathcircle_buffer.h
File metadata and controls
104 lines (88 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __CIRCLE_BUFFER_H__
#define __CIRCLE_BUFFER_H__
#include <cstdint>
#include <stdexcept>
template <typename T>
class circle_buffer
{
public:
explicit circle_buffer(uint64_t sz = 50000):size_(sz), wr_index(0), rd_index(0)
{
if( sz < 2 )
{
throw std::runtime_error("too few elements, size must grater than 1");
}
array = new T[sz + 1];
}
~circle_buffer()
{
delete [] array;
array = NULL;
}
public:
bool push_back(const T& val)
{
if (full())
return false;
array[wr_index] = val;
wr_index = inc(wr_index);
return true;
}
bool pop_front(T* val = 0)
{
if(empty() || val == NULL)
return false;
*val = array[rd_index];
rd_index = inc(rd_index);
return true;
}
T* get_write_index_memory()
{
if(full())
return NULL;
return &array[wr_index];
}
bool put_write_index_memory()
{
if(full())
return false;
wr_index = inc(wr_index);
}
T* get_read_index_memory()
{
if(empty())
return NULL;
return &array[rd_index];
}
bool put_read_index_memory()
{
if(empty())
return false;
rd_index = inc(rd_index);
return true;
}
size_t size()
{
int n = wr_index - rd_index;
return n < 0? n + size_ : n;
}
bool empty() const
{
return wr_index == rd_index;
}
bool full () const
{
return inc(wr_index) == rd_index;
}
private:
uint64_t inc(uint64_t n) const
{
return ++n % (size_ + 1);
}
private:
T* array;
const uint64_t size_;
uint64_t wr_index;
uint64_t rd_index;
};
#endif