Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
73 lines (59 loc) · 1.62 KB

File metadata and controls

73 lines (59 loc) · 1.62 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright Heikki Berg 2017 - 2018
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(CDSP_CIRCULAR_BUFFER)
#define CDSP_CIRCULAR_BUFFER
#include <algorithm>
#include <vector>
namespace cdsp
{
template<typename T>
class circular_buffer
{
public:
/// Ctor
explicit circular_buffer(std::size_t size = 1) :
m_buffer(size, T{}),
m_last(0)
{
};
~circular_buffer() = default;
/// Resizes the buffer
void resize(std::size_t size);
/// Clears the contents of the circular buffer
void clear();
/// Replaces the oldest value in the circular buffer and steps forward
void push_back(T const& data);
/// Returns reference to the value, 0 index being latest, 1 second latest
T& operator[](std::size_t index);
private:
std::vector<T> m_buffer;
/// Points to the latest inserted data sample
std::size_t m_last;
};
template<class T>
void circular_buffer<T>::resize(std::size_t size)
{
m_buffer.resize(size);
std::fill(m_buffer.begin(), m_buffer.end(), T{});
m_last = 0;
}
template<class T>
void circular_buffer<T>::clear()
{
std::fill(m_buffer.begin(), m_buffer.end(), T{});
}
template<class T>
T& circular_buffer<T>::operator[](std::size_t index)
{
return m_buffer[(m_last + m_buffer.size() - index) % m_buffer.size()];
}
template<class T>
void circular_buffer<T>::push_back(T const& data)
{
m_last = (m_last + 1) % m_buffer.size();
m_buffer[m_last] = data;
}
} //namespace cdsp
#endif //CDSP_CIRCULAR_BUFFER
Morty Proxy This is a proxified and sanitized view of the page, visit original site.