Created
October 31, 2010 13:16
-
-
Save AndreaCrotti/656576 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef RINGBUFFER_H | |
| #define RINGBUFFER_H | |
| #include <deque> | |
| template<typename T> | |
| class RingBuffer | |
| { | |
| // TODO: see how to make it also iterable | |
| private: | |
| size_t max_size; | |
| std::deque<T> buffer; | |
| public: | |
| RingBuffer(size_t max_size); | |
| void push(T el); | |
| size_t size() { return buffer.size(); } | |
| T operator[](int index) { return buffer[index]; } | |
| // TODO: move in the implementation below if possible | |
| friend ostream& operator<<(ostream& s, const RingBuffer<T>& c) { | |
| s << c; | |
| return s; | |
| } | |
| }; | |
| template<typename T> | |
| RingBuffer<T>::RingBuffer(size_t max_size) : max_size(max_size) | |
| {} | |
| template<typename T> | |
| void RingBuffer<T>::push(T el) | |
| { | |
| if (buffer.size() == max_size) { | |
| buffer.pop_front(); | |
| } | |
| buffer.push_back(el); | |
| cout << "after push" << endl; | |
| } | |
| #endif /* RINGBUFFER_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment