From 8dbde31db3a9626eba294bf1f6c1ca31a70e8551 Mon Sep 17 00:00:00 2001 From: Cooper Larson Date: Tue, 6 Aug 2024 13:31:26 -0600 Subject: [PATCH 1/2] clear() on pop last item --- include/BoundedPriorityDeque.hpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/include/BoundedPriorityDeque.hpp b/include/BoundedPriorityDeque.hpp index 49bae55..7a80a19 100644 --- a/include/BoundedPriorityDeque.hpp +++ b/include/BoundedPriorityDeque.hpp @@ -180,12 +180,20 @@ class BoundedPriorityDequeBase { ++_size; } + /** + * @brief Internal method with no return val + */ + void _popTop() { + _head = nextIndex(_head); + if (--_size == 0) clear(); + } + /** * @brief Internal method with no return val */ void _popBottom() { _tail = prevIndex(_tail); - --_size; + if (--_size == 0) clear(); } public: @@ -306,8 +314,7 @@ class BoundedPriorityDequeBase { if (empty()) throw std::runtime_error("Attempted to pop from empty BoundedPriorityDeque"); #endif auto index = _head; - _head = nextIndex(_head); - --_size; + _popTop(); return _buffer[index]; } @@ -321,8 +328,7 @@ class BoundedPriorityDequeBase { if (empty()) throw std::runtime_error("Attempted to pop from empty BoundedPriorityDeque"); #endif auto index = _tail; - _tail = prevIndex(_tail); - --_size; + _popBottom(); return _buffer[index]; } From 70d38753bbcdaaa81d0d4324073f49d182289357 Mon Sep 17 00:00:00 2001 From: Cooper Larson Date: Tue, 6 Aug 2024 14:30:34 -0600 Subject: [PATCH 2/2] cleanup header defs --- include/BoundedPriorityDeque.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/BoundedPriorityDeque.hpp b/include/BoundedPriorityDeque.hpp index 7a80a19..cb97a91 100644 --- a/include/BoundedPriorityDeque.hpp +++ b/include/BoundedPriorityDeque.hpp @@ -2,8 +2,8 @@ // Created by Cooper Larson on 7/30/24. // -#ifndef SERVER_BOUNDED_PRIORITY_DEQUE_BASE_H -#define SERVER_BOUNDED_PRIORITY_DEQUE_BASE_H +#ifndef BOUNDED_PRIORITY_DEQUE_H +#define BOUNDED_PRIORITY_DEQUE_H #include @@ -483,4 +483,4 @@ class BoundedPriorityDeque : public BoundedPriorityDequeBase { BoundedPriorityDequeBase(capacity), comparator(comp) {} }; -#endif // SERVER_BOUNDED_PRIORITY_DEQUE_BASE_H +#endif // BOUNDED_PRIORITY_DEQUE_H