From 7fb9bebd463afc67909ae9220e38a6d3ca938c12 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 24 Apr 2014 09:46:52 +0200 Subject: util/{Const,Writable}Buffer: add front(), back(), pop_{front,back}(), shift() --- src/util/ConstBuffer.hxx | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'src/util/ConstBuffer.hxx') diff --git a/src/util/ConstBuffer.hxx b/src/util/ConstBuffer.hxx index bb114d44d..319102126 100644 --- a/src/util/ConstBuffer.hxx +++ b/src/util/ConstBuffer.hxx @@ -164,6 +164,65 @@ struct ConstBuffer { return data[i]; } + + /** + * Returns a reference to the first element. Buffer must not + * be empty. + */ +#ifdef NDEBUG + constexpr +#endif + reference_type front() const { +#ifndef NDEBUG + assert(!IsEmpty()); +#endif + return data[0]; + } + + /** + * Returns a reference to the last element. Buffer must not + * be empty. + */ +#ifdef NDEBUG + constexpr +#endif + reference_type back() const { +#ifndef NDEBUG + assert(!IsEmpty()); +#endif + return data[size - 1]; + } + + /** + * Remove the first element (by moving the head pointer, does + * not actually modify the buffer). Buffer must not be empty. + */ + void pop_front() { + assert(!IsEmpty()); + + ++data; + --size; + } + + /** + * Remove the last element (by moving the tail pointer, does + * not actually modify the buffer). Buffer must not be empty. + */ + void pop_back() { + assert(!IsEmpty()); + + --size; + } + + /** + * Remove the first element and return a reference to it. + * Buffer must not be empty. + */ + reference_type shift() { + reference_type result = front(); + pop_front(); + return result; + } }; #endif -- cgit v1.2.3