[RingBugffer]\n- added operators\n- Remove get() and put()
This commit is contained in:
@@ -30,6 +30,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include "../../dsp_types.hpp"
|
#include "../../dsp_types.hpp"
|
||||||
|
|
||||||
namespace dsp {
|
namespace dsp {
|
||||||
@@ -37,8 +38,8 @@ namespace dsp {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class RingBuffer
|
class RingBuffer
|
||||||
{
|
{
|
||||||
uint32_t m_index;
|
uint m_index;
|
||||||
uint32_t m_mask;
|
uint m_mask;
|
||||||
size_t m_capacity;
|
size_t m_capacity;
|
||||||
vector<T> m_buffer;
|
vector<T> m_buffer;
|
||||||
|
|
||||||
@@ -55,6 +56,12 @@ class RingBuffer
|
|||||||
{
|
{
|
||||||
return m_buffer.capacity();
|
return m_buffer.capacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
m_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void resize(int capacity, T value=T())
|
void resize(int capacity, T value=T())
|
||||||
{
|
{
|
||||||
m_capacity = 1;
|
m_capacity = 1;
|
||||||
@@ -68,17 +75,40 @@ class RingBuffer
|
|||||||
m_index = 0;
|
m_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void put(T sample)
|
T & operator[](int offset)
|
||||||
{
|
{
|
||||||
m_index = (m_index + 1) & m_mask;
|
return m_buffer[(m_index + (uint)offset) & m_mask];
|
||||||
m_buffer[m_index] = sample;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T get(uint32_t delay = 0)
|
const T & operator[](int offset) const
|
||||||
{
|
{
|
||||||
uint32_t i = (m_index - delay) & m_mask;
|
return m_buffer[(m_index + (uint)offset) & m_mask];
|
||||||
return m_buffer[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RingBuffer & operator ++()
|
||||||
|
{
|
||||||
|
++m_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
RingBuffer & operator +=(int i)
|
||||||
|
{
|
||||||
|
m_index += (uint)i;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
RingBuffer & operator --()
|
||||||
|
{
|
||||||
|
--m_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
RingBuffer & operator -=(int i)
|
||||||
|
{
|
||||||
|
m_index -= (uint)i;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,16 @@ int main()
|
|||||||
{
|
{
|
||||||
dsp::RingBuffer<int> buffer(10);
|
dsp::RingBuffer<int> buffer(10);
|
||||||
printf("Buffer capacity = %zu\n", buffer.capacity());
|
printf("Buffer capacity = %zu\n", buffer.capacity());
|
||||||
for (int i; i < 10; i++)
|
for (int i=0; i < 10; i++)
|
||||||
{
|
{
|
||||||
buffer.put(i+1);
|
buffer[i] = i+1;
|
||||||
int v = buffer.get(2);
|
}
|
||||||
printf("i=%d: v=%d\n", i, v);
|
buffer += 5;
|
||||||
|
|
||||||
|
for (int i=-5; i < 5; i++)
|
||||||
|
{
|
||||||
|
int v = buffer[i];
|
||||||
|
printf("i=%d: v=%d\n", i, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user