From 669ffb5f3a56f28cbe6ba3397c06b5993f59c15b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 19 Aug 2016 23:18:46 -0400 Subject: [PATCH] intrusive_list: Add pop_back(), pop_front(), front(), and back() member functions --- src/common/intrusive_list.h | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/common/intrusive_list.h b/src/common/intrusive_list.h index 9308c255..1b24bb05 100644 --- a/src/common/intrusive_list.h +++ b/src/common/intrusive_list.h @@ -87,6 +87,24 @@ public: insert(end(), node); } + /** + * Erases the node at the front of the list. + * @note Must not be called on an empty list. + */ + void pop_front() { + DEBUG_ASSERT(!empty()); + erase(begin()); + } + + /** + * Erases the node at the back of the list. + * @note Must not be called on an empty list. + */ + void pop_back() { + DEBUG_ASSERT(!empty()); + erase(--end()); + } + /** * Removes node from list * @param node Node to remove from list. @@ -103,6 +121,42 @@ public: return root->next == root.get(); } + /** + * Retrieves a reference to the node at the front of the list. + * @note Must not be called on an empty list. + */ + reference front() { + DEBUG_ASSERT(!empty()); + return *begin(); + } + + /** + * Retrieves a constant reference to the node at the front of the list. + * @note Must not be called on an empty list. + */ + const_reference front() const { + DEBUG_ASSERT(!empty()); + return *begin(); + } + + /** + * Retrieves a reference to the node at the back of the list. + * @note Must not be called on an empty list. + */ + reference back() { + DEBUG_ASSERT(!empty()); + return *--end(); + } + + /** + * Retrieves a constant reference to the node at the back of the list. + * @note Must not be called on an empty list. + */ + const_reference back() const { + DEBUG_ASSERT(!empty()); + return *--end(); + } + // Iterator interface iterator begin() { return iterator(root.get(), root->next); } const_iterator begin() const { return const_iterator(root.get(), root->next); }