]> git.tdb.fi Git - netvis.git/blob - source/ringbuffer.h
Add a traffic history graph
[netvis.git] / source / ringbuffer.h
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #ifndef RINGBUFFER_H_
9 #define RINGBUFFER_H_
10
11 #include <vector>
12
13 template<typename T>
14 class RingBuffer
15 {
16 private:
17         unsigned sz;
18         std::vector<T> data;
19         unsigned pos;
20
21 public:
22         RingBuffer(unsigned s): sz(s), data(sz), pos(0) { }
23         void push(const T &v) { data[pos=(pos+1)%sz] = v; }
24         const T &get(unsigned i) const { return data[(pos+sz-i)%sz]; }
25         unsigned size() const { return sz; }
26 };
27
28 #endif