Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 10 / ex2 / kPriorityQueue.hpp
blob: 14cdcc4c335c8089c321a9e521947e55daff11ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
}