aboutsummaryrefslogtreecommitdiffstats
path: root/priority_queue.hpp
blob: 794638fa62a0bd8d475f575c1ad382542fa4b7e0 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef PRIORITY_QUEUE_HPP
#define PRIORITY_QUEUE_HPP

#include <iostream>

template<typename T>
class PriorityQueue {
public:
	// creates a new object of type T
	PriorityQueue();
	// deletes the array priority_array so that there are no memory leaks
	~PriorityQueue();

	// pushes element to the queue which is sorted by smallest element first
	void push(const T& element);

	// pops the first element from the queue and removes it while returning
	// it
	T pop();

	// returns the lowest priority item
	T get_first();

	// check if item is in queue
	bool check_item(const T& item);

	// gets index of item
	int get_index(const T& item);

	// removes item from queue
	void remove_item(const T& item);

	void clear();

	// return element at position;
	const T& at(const unsigned int& i) const;

	const T& operator[](const unsigned int& i) const;
private:
	// size of the current queue
	unsigned int size;
	// capacity of the queue, this will just increase in multiples of 2
	unsigned int capacity;
	// array that will represent the priority queue
	T *priority_array;

	// resize the queue's array accordingly by some factor
	void resize_queue(double factor);

	// insert and element into the queue
	// this assumes that the capacity is larger than the size
	void insert_queue(const T& element, const unsigned int& loc);

	// removes an element from the queue
	T remove_queue(const unsigned int& loc);
};

template<typename T>
PriorityQueue<T>::PriorityQueue() : size(0), capacity(1) {
	priority_array = new T;
}

template<typename T>
PriorityQueue<T>::~PriorityQueue() {
	delete[] priority_array;
}

template<typename T>
void PriorityQueue<T>::push(const T& element) {
	unsigned int insert_loc = 0;

	if(size == capacity)
		resize_queue(2);

	while(insert_loc < size && element > priority_array[insert_loc])
		++insert_loc;

	insert_queue(element, insert_loc);
}

template<typename T>
T PriorityQueue<T>::pop() {
	if(!size)
		throw "Priority Queue is empty, no item to pop";
	else if(size == capacity / 2)
		resize_queue(0.5);

	T tmp = remove_queue(0);
	return tmp;
}

template<typename T>
T PriorityQueue<T>::get_first() {
	return priority_array[0];
}

template<typename T>
bool PriorityQueue<T>::check_item(const T& item) {
	for(unsigned int i = 0; i < size; ++i)
		if(priority_array[i] == item)
			return true;
	return false;
}

template<typename T>
int PriorityQueue<T>::get_index(const T& item) {
	for(unsigned int i = 0; i < size; ++i)
		if(priority_array[i] == item)
			return i;
	return -1;
}

template<typename T>
void PriorityQueue<T>::remove_item(const T& item) {
	remove_queue(get_index(item));
}

template<typename T>
void PriorityQueue<T>::clear() {
	T *tmp = new T;

	size = 0;
	capacity = 1;
	delete[] priority_array;
	priority_array = tmp;
}

template<typename T>
const T& PriorityQueue<T>::at(const unsigned int& i) const {
	return priority_array[i];
}

template<typename T>
const T& PriorityQueue<T>::operator[](const unsigned int& i) const {
	return priority_array[i];
}

template<typename T>
void PriorityQueue<T>::resize_queue(double factor) {
	capacity = (double)capacity * factor;
	T *tmp_array = new T[capacity];

	for(unsigned int i = 0; i < size; ++i)
		tmp_array[i] = priority_array[i];

	delete[] priority_array;
	priority_array = tmp_array;
}

template<typename T>
void PriorityQueue<T>::insert_queue(const T& element, const unsigned int& loc) {
	T *tmp_array = new T[capacity];

	for(unsigned int i = 0; i < size + 1; ++i)
		if(i == loc)
			tmp_array[i] = element;
		else if(i < loc)
			tmp_array[i] = priority_array[i];
		else
			tmp_array[i] = priority_array[i - 1];

	size++;

	delete[] priority_array;
	priority_array = tmp_array;
}

template<typename T>
T PriorityQueue<T>::remove_queue(const unsigned int& loc) {
	T element;

	T *tmp_array = new T[capacity];

	for(unsigned int i = 0; i < size; ++i)
		if(i == loc)
			element = priority_array[i];
		else if(i < loc)
			tmp_array[i] = priority_array[i];
		else
			tmp_array[i - 1] = priority_array[i];

	size--;

	delete[] priority_array;
	priority_array = tmp_array;

	return element;
}

#endif // PRIORITY_QUEUE_HPP