]> git.tdb.fi Git - netvis.git/blobdiff - source/ringbuffer.h
Add a traffic history graph
[netvis.git] / source / ringbuffer.h
diff --git a/source/ringbuffer.h b/source/ringbuffer.h
new file mode 100644 (file)
index 0000000..c118019
--- /dev/null
@@ -0,0 +1,28 @@
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#ifndef RINGBUFFER_H_
+#define RINGBUFFER_H_
+
+#include <vector>
+
+template<typename T>
+class RingBuffer
+{
+private:
+       unsigned sz;
+       std::vector<T> data;
+       unsigned pos;
+
+public:
+       RingBuffer(unsigned s): sz(s), data(sz), pos(0) { }
+       void push(const T &v) { data[pos=(pos+1)%sz] = v; }
+       const T &get(unsigned i) const { return data[(pos+sz-i)%sz]; }
+       unsigned size() const { return sz; }
+};
+
+#endif