Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
PriorityQueue.c
Go to the documentation of this file.
1#include "../../Inc/Scheduler/PriorityQueue.h"
2
4 pq->size = 0;
5}
6
13void heapifyUp(PriorityQueue* pq, int index) {
14 while (index > 1 && pq->nodes[index / 2].priority > pq->nodes[index].priority) {
15 PQNode temp = pq->nodes[index];
16 pq->nodes[index] = pq->nodes[index / 2];
17 pq->nodes[index / 2] = temp;
18 index /= 2;
19 }
20}
21
22
29void heapifyDown(PriorityQueue* pq, int index) {
30 while (2 * index <= pq->size) {
31 int j = 2 * index;
32 if (j < pq->size && pq->nodes[j].priority > pq->nodes[j + 1].priority) j++;
33 if (pq->nodes[index].priority <= pq->nodes[j].priority) break;
34 PQNode temp = pq->nodes[index];
35 pq->nodes[index] = pq->nodes[j];
36 pq->nodes[j] = temp;
37 index = j;
38 }
39}
40
41bool PQPush(PriorityQueue* pq, Task task, int priority) {
42 if (PQIsFull(pq)) return false;
43 pq->nodes[++pq->size] = (PQNode){priority, task};
44 heapifyUp(pq, pq->size);
45 return true;
46}
47
48bool PQPop(PriorityQueue* pq, Task* task) {
49 if (PQIsEmpty(pq)) return false;
50 *task = pq->nodes[1].task;
51 pq->nodes[1] = pq->nodes[pq->size--];
52 heapifyDown(pq, 1);
53 return true;
54}
55
56bool PQPeek(PriorityQueue* pq, Task* task) {
57 if (PQIsEmpty(pq)) return false;
58 *task = pq->nodes[1].task;
59 return true;
60}
61
63 return pq->size == 0;
64}
65
67 return pq->size == MAX_SENSORS;
68}
bool PQPeek(PriorityQueue *pq, Task *task)
Peeks at the highest priority task in the priority queue without removing it.
Definition: PriorityQueue.c:56
bool PQIsEmpty(PriorityQueue *pq)
Checks if the priority queue is empty.
Definition: PriorityQueue.c:62
void heapifyDown(PriorityQueue *pq, int index)
Restores the heap property by moving the node at the given index down.
Definition: PriorityQueue.c:29
void heapifyUp(PriorityQueue *pq, int index)
Restores the heap property by moving the node at the given index up.
Definition: PriorityQueue.c:13
bool PQIsFull(PriorityQueue *pq)
Checks if the priority queue is full.
Definition: PriorityQueue.c:66
bool PQPop(PriorityQueue *pq, Task *task)
Pops the highest priority task from the priority queue.
Definition: PriorityQueue.c:48
bool PQPush(PriorityQueue *pq, Task task, int priority)
Pushes a task with a given priority onto the priority queue.
Definition: PriorityQueue.c:41
void PQInit(PriorityQueue *pq)
Initializes the priority queue.
Definition: PriorityQueue.c:3
#define MAX_SENSORS
Definition: PriorityQueue.h:8
int priority
Definition: PriorityQueue.h:11
Task task
Definition: PriorityQueue.h:12
PQNode nodes[MAX_SENSORS+1]
Definition: PriorityQueue.h:17
Definition: Task.h:8