/* * Xournal++ * * A rudimentary circular buffer * * @author Xournal++ Team * https://github.com/xournalpp/xournalpp * * @license GNU GPLv2 or later */ template class CircularBuffer: private std::vector { public: CircularBuffer(size_t length): std::vector(length > 1 ? length : 1), length(length > 1 ? length : 1) {} ~CircularBuffer() = default; T front() { return (*this)[head]; } T back() { if (head == 0) { return (*this)[length - 1]; } return (*this)[head - 1]; } void push_front(const T& ev) { head++; head %= length; (*this)[head] = ev; } void assign(const T& ev) { for (T& e: *this) { e = ev; } } size_t size() { return length; } /** * Beware: begin and end are not related to the position of the head */ using std::vector::cbegin; using std::vector::begin; using std::vector::cend; using std::vector::end; private: const size_t length; size_t head = length - 1; };