diff --git a/BAN/include/BAN/PriorityQueue.h b/BAN/include/BAN/PriorityQueue.h new file mode 100644 index 00000000..bee03eb2 --- /dev/null +++ b/BAN/include/BAN/PriorityQueue.h @@ -0,0 +1,64 @@ +#pragma once + +#include "BAN/Errors.h" +#include +#include + +namespace BAN +{ + + template> + class PriorityQueue + { + public: + PriorityQueue() = default; + PriorityQueue(Comp comp) + : m_comp(comp) + { } + + ErrorOr push(const T& value) + { + TRY(m_data.push_back(value)); + push_heap(m_data.begin(), m_data.end()); + return {}; + } + + ErrorOr push(T&& value) + { + TRY(m_data.push_back(move(value))); + push_heap(m_data.begin(), m_data.end()); + return {}; + } + + template + ErrorOr emplace(Args&&... args) requires is_constructible_v + { + TRY(m_data.emplace_back(forward(args)...)); + push_heap(m_data.begin(), m_data.end()); + return {}; + } + + void pop() + { + pop_heap(m_data.begin(), m_data.end()); + m_data.pop_back(); + } + + BAN::ErrorOr reserve(Vector::size_type size) + { + return m_data.reserve(size); + } + + T& top() { return m_data.front(); } + const T& top() const { return m_data.front(); } + + bool empty() const { return m_data.empty(); } + Vector::size_type size() const { return m_data.size(); } + Vector::size_type capacity() const { return m_data.capacity(); } + + private: + Comp m_comp; + Vector m_data; + }; + +}