Skip to content

Commit

Permalink
Merge pull request #18 from coopsdev/fix-push-checks
Browse files Browse the repository at this point in the history
Reset data structure to clean state on pop()
  • Loading branch information
cooperlarson authored Aug 6, 2024
2 parents 25c1e81 + 70d3875 commit d218770
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions include/BoundedPriorityDeque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <vector>

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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];
}

Expand All @@ -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];
}

Expand Down Expand Up @@ -477,4 +483,4 @@ class BoundedPriorityDeque : public BoundedPriorityDequeBase<K, V> {
BoundedPriorityDequeBase<K, V>(capacity), comparator(comp) {}
};

#endif // SERVER_BOUNDED_PRIORITY_DEQUE_BASE_H
#endif // BOUNDED_PRIORITY_DEQUE_H

0 comments on commit d218770

Please sign in to comment.