diff options
author | Max Kellermann <max@duempel.org> | 2014-04-24 09:46:52 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-04-24 09:50:19 +0200 |
commit | 7fb9bebd463afc67909ae9220e38a6d3ca938c12 (patch) | |
tree | 2d1a794a04c4e257cf53e03ee5ede127dc8a51ba | |
parent | 3b8a9dd6ecc8f8bb0674e215fa59dfc8d3ad0c2a (diff) | |
download | mpd-7fb9bebd463afc67909ae9220e38a6d3ca938c12.tar.gz mpd-7fb9bebd463afc67909ae9220e38a6d3ca938c12.tar.xz mpd-7fb9bebd463afc67909ae9220e38a6d3ca938c12.zip |
util/{Const,Writable}Buffer: add front(), back(), pop_{front,back}(), shift()
-rw-r--r-- | src/util/ConstBuffer.hxx | 59 | ||||
-rw-r--r-- | src/util/WritableBuffer.hxx | 59 |
2 files changed, 118 insertions, 0 deletions
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 diff --git a/src/util/WritableBuffer.hxx b/src/util/WritableBuffer.hxx index 1fe1b254f..f9e6d4a96 100644 --- a/src/util/WritableBuffer.hxx +++ b/src/util/WritableBuffer.hxx @@ -158,6 +158,65 @@ struct WritableBuffer { 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 |