-
-
Notifications
You must be signed in to change notification settings - Fork 955
Open
Description
I'm on the site: https://exercism.org/tracks/cpp/exercises/circular-buffer/edit
My .hh source code is:
#if !defined(CIRCULAR_BUFFER_H)
#define CIRCULAR_BUFFER_H
#include <vector>
#include <stdexcept>
using namespace std;
namespace circular_buffer {
template<typename T>
class circular_buffer
{
public:
int pos=0;
int writepos=0;
int len=0;
vector<T> elems;
int num;
circular_buffer(int num): elems(num)
{
num=num;
}
T read()
{
if (len==0)
throw domain_error("alma");
len--;
T ret= elems[pos];
pos=(pos+1)%num;
return ret;
}
void write(T a)
{
if (len==num)
throw domain_error("alma");
len++;
writepos=(writepos+1)%num;
elems[writepos]=a;
}
void clear()
{
len=0;
}
void overwrite(T a)
{
if (len==num)
{}
else
len++;
writepos=(writepos+1)%num;
elems[writepos]=a;
}
};
} // namespace circular_buffer
#endif // CIRCULAR_BUFFER_H
The .cc source code is the default
Metadata
Metadata
Assignees
Labels
No labels