Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions
PriorityQueue.c File Reference
#include "../../Inc/Scheduler/PriorityQueue.h"
Include dependency graph for PriorityQueue.c:

Go to the source code of this file.

Functions

void PQInit (PriorityQueue *pq)
 Initializes the priority queue. More...
 
void heapifyUp (PriorityQueue *pq, int index)
 Restores the heap property by moving the node at the given index up. More...
 
void heapifyDown (PriorityQueue *pq, int index)
 Restores the heap property by moving the node at the given index down. More...
 
bool PQPush (PriorityQueue *pq, Task task, int priority)
 Pushes a task with a given priority onto the priority queue. More...
 
bool PQPop (PriorityQueue *pq, Task *task)
 Pops the highest priority task from the priority queue. More...
 
bool PQPeek (PriorityQueue *pq, Task *task)
 Peeks at the highest priority task in the priority queue without removing it. More...
 
bool PQIsEmpty (PriorityQueue *pq)
 Checks if the priority queue is empty. More...
 
bool PQIsFull (PriorityQueue *pq)
 Checks if the priority queue is full. More...
 

Function Documentation

◆ heapifyDown()

void heapifyDown ( PriorityQueue pq,
int  index 
)

Restores the heap property by moving the node at the given index down.

Parameters
pqPointer to the PriorityQueue structure.
indexThe index of the node to move down.

Definition at line 29 of file PriorityQueue.c.

29 {
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}
int priority
Definition: PriorityQueue.h:11
PQNode nodes[MAX_SENSORS+1]
Definition: PriorityQueue.h:17
Here is the caller graph for this function:

◆ heapifyUp()

void heapifyUp ( PriorityQueue pq,
int  index 
)

Restores the heap property by moving the node at the given index up.

Parameters
pqPointer to the PriorityQueue structure.
indexThe index of the node to move up.

Definition at line 13 of file PriorityQueue.c.

13 {
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}
Here is the caller graph for this function:

◆ PQInit()

void PQInit ( PriorityQueue pq)

Initializes the priority queue.

Parameters
pqPointer to the PriorityQueue structure to initialize.

Definition at line 3 of file PriorityQueue.c.

3 {
4 pq->size = 0;
5}
Here is the caller graph for this function:

◆ PQIsEmpty()

bool PQIsEmpty ( PriorityQueue pq)

Checks if the priority queue is empty.

Parameters
pqPointer to the PriorityQueue structure.
Returns
True if the queue is empty, false otherwise.

Definition at line 62 of file PriorityQueue.c.

62 {
63 return pq->size == 0;
64}
Here is the caller graph for this function:

◆ PQIsFull()

bool PQIsFull ( PriorityQueue pq)

Checks if the priority queue is full.

Parameters
pqPointer to the PriorityQueue structure.
Returns
True if the queue is full, false otherwise.

Definition at line 66 of file PriorityQueue.c.

66 {
67 return pq->size == MAX_SENSORS;
68}
#define MAX_SENSORS
Definition: PriorityQueue.h:8
Here is the caller graph for this function:

◆ PQPeek()

bool PQPeek ( PriorityQueue pq,
Task task 
)

Peeks at the highest priority task in the priority queue without removing it.

Parameters
pqPointer to the PriorityQueue structure.
taskPointer to the Task structure to store the peeked task.
Returns
True if a task was successfully peeked, false if the queue is empty.

Definition at line 56 of file PriorityQueue.c.

56 {
57 if (PQIsEmpty(pq)) return false;
58 *task = pq->nodes[1].task;
59 return true;
60}
bool PQIsEmpty(PriorityQueue *pq)
Checks if the priority queue is empty.
Definition: PriorityQueue.c:62
Task task
Definition: PriorityQueue.h:12
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PQPop()

bool PQPop ( PriorityQueue pq,
Task task 
)

Pops the highest priority task from the priority queue.

Parameters
pqPointer to the PriorityQueue structure.
taskPointer to the Task structure to store the popped task.
Returns
True if a task was successfully popped, false if the queue is empty.

Definition at line 48 of file PriorityQueue.c.

48 {
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}
void heapifyDown(PriorityQueue *pq, int index)
Restores the heap property by moving the node at the given index down.
Definition: PriorityQueue.c:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PQPush()

bool PQPush ( PriorityQueue pq,
Task  task,
int  priority 
)

Pushes a task with a given priority onto the priority queue.

Parameters
pqPointer to the PriorityQueue structure.
taskThe task to push onto the queue.
priorityThe priority of the task.
Returns
True if the task was successfully pushed, false if the queue is full.

Definition at line 41 of file PriorityQueue.c.

41 {
42 if (PQIsFull(pq)) return false;
43 pq->nodes[++pq->size] = (PQNode){priority, task};
44 heapifyUp(pq, pq->size);
45 return true;
46}
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
Here is the call graph for this function:
Here is the caller graph for this function: