-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.cc
More file actions
executable file
·36 lines (30 loc) · 928 Bytes
/
priority_queue.cc
File metadata and controls
executable file
·36 lines (30 loc) · 928 Bytes
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
#include <stdexcept>
#include "priorities.h"
#include "priority_queue.h"
PriorityQueue::PriorityQueue() : queue(PRIORITYQ_NQUEUE), bitmap(PRIORITYQ_NQUEUE), size_(0) {}
void PriorityQueue::addTask(Task* task, int priority) {
priority = priority - PRIORITYQ_MIN_PRIORITY;
queue[priority].push_back(task);
size_++;
bitmap[priority] = 1;
}
Task* PriorityQueue::getNextTask() {
for (int priority = 0; priority < PRIORITYQ_NQUEUE; priority++) {
if (bitmap[priority]) {
Task* task = queue[priority].front();
queue[priority].pop_front();
size_--;
if (queue[priority].empty()) {
bitmap[priority] = 0;
}
return task;
}
}
throw std::runtime_error("Error: Invalid access to empty priority queue");
}
bool PriorityQueue::empty() {
return size_ == 0;
}
size_t PriorityQueue::size() {
return size_;
}