1 #ifndef INCLUDE_AL_BUFFER_HPP
2 #define INCLUDE_AL_BUFFER_HPP
61 template <
class T,
class Alloc=std::allocator<T> >
62 class Buffer :
protected Alloc{
67 explicit Buffer(
int size=0)
68 : mElems(size), mSize(size)
73 Buffer(
int size,
int capacity)
74 : mElems(capacity), mSize(size)
80 int capacity()
const {
return mElems.size(); }
81 int size()
const {
return mSize; }
82 const T * elems()
const {
return &mElems[0]; }
83 T * elems(){
return &mElems[0]; }
87 T& operator[](
int i){
return mElems[i]; }
90 const T& operator[](
int i)
const {
return mElems[i]; }
97 void assign(
int n,
const T& v){ mElems.assign(n,v); }
100 T& last(){
return mElems[size()-1]; }
101 const T& last()
const {
return mElems[size()-1]; }
104 void reset(){ mSize=0; }
122 if(capacity() < n) resize(n);
127 void append(
const T& v,
double growFactor=2){
130 if(size() >= capacity()){
133 const T vsafecopy = v;
134 mElems.resize((size() ? size() : 4)*growFactor);
135 super::construct(elems()+size(), vsafecopy);
138 super::construct(elems()+size(), v);
143 void push_back(
const T& v,
double growFactor=2) { append(v, growFactor); }
149 void append(
const Buffer<T>& src){
150 append(src.elems(), src.size());
154 void append(
const T * src,
int len){
155 int oldsize = size();
157 std::copy(src, src + len, mElems.begin() + oldsize);
161 void repeatLast(){ append(last()); }
169 template <
int n,
bool dup>
172 const int Nd = dup ? n : 1;
173 for(
int i=size()/n-1; i>=0; --i){
174 const T& v = (*this)[i];
175 for(
int j=0; j<Nd; ++j) Alloc::construct(elems()+n*i+j, v);
180 std::vector<T, Alloc> mElems;
183 void setSize(
int n){ mSize=n; }
194 template <
class T,
class Alloc = std::allocator<T> >
207 size_t size()
const {
return mElems.size(); }
210 int pos()
const {
return mPos; }
213 int fill()
const {
return mFill; }
229 if (mFill <
size()) ++mFill;
234 return mElems[
pos()];
238 void write(
const T& v) { Alloc::construct(&
next(), v); }
252 return mElems[wrapOnce(from -
dist,
size())];
273 if (mPos >= n) mPos = n - 1;
277 std::vector<T, Alloc> mElems;
282 static int wrapOnce(
int v,
int max) {
283 if (v < 0)
return v +
max;
284 if (v >=
max)
return v -
max;
297 template <
int N,
class T>
304 static int size() {
return N; }
307 const T*
elems()
const {
return &mElems[0]; }
320 for (
int i = N - 1; i > 0; --i) mElems[i] = mElems[i - 1];
326 for (
int i = 0; i < N; ++i) mElems[i] = v;
330 void zero() { memset(mElems, 0, N *
sizeof(T)); }
void reset()
Set write position to start of array and zero fill amount.
const T & readFrom(int from, int dist) const
Get reference to older element relative to some newer element (read-only)
void write(const T &v)
Write new element.
T & read(int i)
Get reference to element relative to newest element.
int pos() const
Get absolute index of most recently written element.
T & next()
Obtain next element in buffer.
int fill() const
Get fill amount of buffer.
const T & operator[](int i) const
Get element at absolute index (read-only)
size_t size() const
Get number of elements.
const T & read(int i) const
Get reference to element relative to newest element (read-only)
T & operator[](int i)
Get element at absolute index.
RingBuffer(unsigned size, const T &v=T())
void resize(int n, const T &v=T())
Resize buffer.
RingBuffer()
Default constructor; does not allocate memory.
Constant size shift buffer.
const T & operator[](int i) const
Get reference to element at index (read-only)
const T * elems() const
Get pointer to elements (read-only)
void assign(const T &v)
Set all elements to argument.
static int size()
Get number of elements.
void operator()(const T &v)
Push new element onto buffer. Newest element is at index 0.
T * elems()
Get pointer to elements.
void zero()
Zero bytes of all elements.
ShiftBuffer(const T &v=T())
T & operator[](int i)
Get reference to element at index.
T max(const T &v1, const T &v2, const T &v3)
T dist(const Vec< N, T > &a, const Vec< N, U > &b)
Returns distance between two vectors.