Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 10 / Examples
diff options
context:
space:
mode:
Commit72300056a10209e483e7e499c7f03c207924bb82 (patch)
Author
 Angel Dimitriev <angel_dimitriev@abv.bg>
2022-05-11 13:17:28 -0400
Committer
 GitHub <noreply@github.com>
2022-05-11 13:17:28 -0400
Files7322eef2a2c7482018d6f83b5e02108a0d0a1b9a /Sem. 10/Examples
Object-oriented_programming_FMI-72300056a10209e483e7e499c7f03c207924bb82.tar
Object-oriented_programming_FMI-72300056a10209e483e7e499c7f03c207924bb82.tar.gz
Object-oriented_programming_FMI-72300056a10209e483e7e499c7f03c207924bb82.zip
Parent47d6fd52adf26180d68958c3c9f1ccc86f0c51ea (diff)
Create kPriorityQueue.hpp
1 files changed, 134 insertions, 0 deletions
-rw-r--r--Sem. 10/Examples/kPriorityQueue.hpp134
diff --git a/Sem. 10/Examples/kPriorityQueue.hpp b/Sem. 10/Examples/kPriorityQueue.hpp
new file mode 100644
index 0000000..9cc7494
--- /dev/null
+++ b/Sem. 10/Examples/kPriorityQueue.hpp
@@ -0,0 +1,134 @@
+#include <iostream>
+#include "Queue.hpp"
+#include <exception>
+#ifndef K_PRIORITY_QUEUE
+#define K_PRIORITY_QUEUE
+
+template<typename T>
+class kPriorityQueue
+{
+ size_t maxPriority;
+ MyQueue<T>* queues;
+ void free();
+ void copyFrom(const kPriorityQueue<T> &other);
+
+ size_t elementsCount = 0;
+
+public:
+ kPriorityQueue(size_t maxPriority);
+ kPriorityQueue(const kPriorityQueue<T>& other);
+ kPriorityQueue<T>& operator=(const kPriorityQueue<T>& other);
+ ~kPriorityQueue();
+
+ void push(const T& obj, size_t priority);
+ void push(T&& obj, size_t priority);
+ void pop();
+
+ const T& peek() const;
+ bool isEmpty() const;
+
+};
+
+template<typename T>
+void kPriorityQueue<T>::free()
+{
+ delete[] queues;
+}
+
+template<typename T>
+void kPriorityQueue<T>::copyFrom(const kPriorityQueue<T> &other)
+{
+ this->maxPriority = maxPriority;
+ queues = new MyQueue<T>[maxPriority];
+ for (size_t i = 0; i < maxPriority; i++)
+ queues[i] = other.queues[i];
+}
+
+template<typename T>
+kPriorityQueue<T>::kPriorityQueue(size_t maxPriority)
+{
+ if (maxPriority == 0)
+ throw std::logic_error("Cannot create priority queue with no priorities");
+
+ this->maxPriority = maxPriority;
+ queues = new MyQueue<T>[maxPriority];
+}
+
+template<typename T>
+kPriorityQueue<T>::kPriorityQueue(const kPriorityQueue<T> &other)
+{
+ copyFrom();
+}
+
+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()
+{
+ free();
+}
+
+template<typename T>
+void kPriorityQueue<T>::push(const T &obj, size_t priority)
+{
+ if (priority >= maxPriority)
+ throw std::logic_error("No such priority");
+ elementsCount++;
+ queues[priority].push(obj);
+}
+
+template<typename T>
+void kPriorityQueue<T>::push(T &&obj, size_t priority)
+{
+ if (priority >= maxPriority)
+ throw std::logic_error("No such priority");
+ elementsCount++;
+ queues[priority].push(std::move(obj));
+}
+
+template<typename T>
+void kPriorityQueue<T>::pop()
+{
+
+ if(isEmpty())
+ throw std::logic_error("Empty queue!");
+
+ for (int i = maxPriority - 1; i >= 0; i--)
+ {
+ if (!queues[i].isEmpty())
+ {
+ queues[i].pop();
+ elementsCount--;
+ return;
+ }
+ }
+}
+
+template<typename T>
+const T &kPriorityQueue<T>::peek() const
+{
+
+ for (int i = maxPriority - 1; i >= 0; i--)
+ {
+ if (!queues[i].isEmpty())
+ return queues[i].peek();
+ }
+
+}
+
+template<typename T>
+bool kPriorityQueue<T>::isEmpty() const
+{
+ return elementsCount == 0;
+}
+
+#endif