Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 10 / ex2 / kPriorityQueue.hpp
diff options
context:
space:
mode:
Commit2a8bf5b484ae5eb05bbaaeaf613089dc4a15f73a (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-05 11:29:29 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-05 11:29:29 +0300
Files861c90c8cfdacda2ed11ae5b5e92b30e734b27db /Year 1/Object-oriented Programming/Solutions Sem. 10/ex2/kPriorityQueue.hpp
uni-projects-2a8bf5b484ae5eb05bbaaeaf613089dc4a15f73a.tar
uni-projects-2a8bf5b484ae5eb05bbaaeaf613089dc4a15f73a.tar.gz
uni-projects-2a8bf5b484ae5eb05bbaaeaf613089dc4a15f73a.zip
Parent169cbc9a9a36ef2d0c71d35bc2086a061982df95 (diff)
Solved dimitriev hw (sem 10, ex2)
1 files changed, 140 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 10/ex2/kPriorityQueue.hpp140
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 10/ex2/kPriorityQueue.hpp b/Year 1/Object-oriented Programming/Solutions Sem. 10/ex2/kPriorityQueue.hpp
new file mode 100644
index 0000000..14cdcc4
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 10/ex2/kPriorityQueue.hpp
@@ -0,0 +1,140 @@
+#include "Queue.hpp"
+#include <stdexcept>
+
+template <typename T>
+class kPriorityQueue
+{
+ MyQueue<T>* priorities;
+ size_t size;
+ size_t largestPriority; // за да може peek да се изпълни за константно време
+
+ void free();
+ void copyFrom(const kPriorityQueue<T>& other);
+public:
+ kPriorityQueue(size_t k);
+
+ void push(const T& obj, size_t kth);
+ void push(T&& obj, size_t kth);
+ void pop();
+
+ const T& peek() const;
+ bool isEmpty() const;
+
+ kPriorityQueue();
+ kPriorityQueue(const kPriorityQueue<T>& other);
+ kPriorityQueue<T>& operator=(const kPriorityQueue<T>& other);
+ ~kPriorityQueue();
+};
+
+template <typename T>
+kPriorityQueue<T>::kPriorityQueue(size_t k)
+{
+ size = k;
+ priorities = new MyQueue<T>[size];
+ largestPriority = size - 1;
+}
+
+
+template <typename T>
+void kPriorityQueue<T>::push(const T& obj, size_t kth)
+{
+ if (kth >= size)
+ throw std::invalid_argument("Error! No such priority!");
+
+ priorities[kth].push(obj);
+ if (kth > largestPriority)
+ largestPriority = kth;
+}
+
+template <typename T>
+void kPriorityQueue<T>::push(T&& obj, size_t kth)
+{
+ if (kth >= size)
+ throw std::invalid_argument("Error! No such priority!");
+
+ priorities[kth].push(obj);
+ if (kth > largestPriority)
+ largestPriority = kth;
+}
+
+template <typename T>
+void kPriorityQueue<T>::pop()
+{
+ if (isEmpty())
+ throw std::logic_error("Empty priority queue!");
+
+ bool success = false;
+ while (!success)
+ {
+ try
+ {
+ priorities[largestPriority].pop();
+ success = true;
+
+ if (priorities[largestPriority].isEmpty())
+ largestPriority--;
+ }
+ catch (std::logic_error e)
+ {
+ largestPriority--;
+ }
+ }
+}
+
+
+template <typename T>
+const T& kPriorityQueue<T>::peek() const
+{
+ return priorities[largestPriority].peek();
+}
+
+template <typename T>
+bool kPriorityQueue<T>::isEmpty() const
+{
+ return size == 0;
+}
+
+
+template <typename T>
+kPriorityQueue<T>::kPriorityQueue() : kPriorityQueue(1)
+{}
+
+template <typename T>
+kPriorityQueue<T>::kPriorityQueue(const kPriorityQueue<T>& other)
+{
+ copyFrom(other);
+}
+
+template <typename T>
+kPriorityQueue<T>& kPriorityQueue<T>::operator=(const kPriorityQueue<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+kPriorityQueue<T>::~kPriorityQueue<T>()
+{
+ free();
+}
+
+
+template <typename T>
+void kPriorityQueue<T>::free()
+{
+ delete[] priorities;
+}
+
+template <typename T>
+void kPriorityQueue<T>::copyFrom(const kPriorityQueue<T>& other)
+{
+ priorities = new MyQueue<T>[other.size];
+ for (size_t i = 0; i < other.size; i++)
+ priorities[i] = other.priorities[i];
+ size = other.size;
+ largestPriority = other.largestPriority;
+}