2023-11-28 20:33:23 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "disk.h"
|
|
|
|
|
|
|
|
|
2023-11-28 21:02:49 -04:00
|
|
|
DiskQueue *createDiskQueue() {
|
|
|
|
DiskQueue *queue = malloc(sizeof(DiskQueue));
|
|
|
|
if (queue == NULL) {
|
|
|
|
printf("Error allocating memory for queue\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
queue->head = NULL;
|
|
|
|
queue->size = 0;
|
2023-11-28 20:33:23 -04:00
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2023-11-28 21:02:49 -04:00
|
|
|
void enqueue(DiskQueue *queue, int time, int position) {
|
|
|
|
DiskRequest *request = malloc(sizeof(DiskRequest));
|
|
|
|
if (request == NULL) {
|
|
|
|
printf("Error allocating memory for request\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
request->time = time;
|
|
|
|
request->position = position;
|
|
|
|
request->next = NULL;
|
2023-11-28 20:33:23 -04:00
|
|
|
|
2023-11-28 21:02:49 -04:00
|
|
|
if (queue->head == NULL) {
|
|
|
|
queue->head = request;
|
|
|
|
} else {
|
|
|
|
DiskRequest *current = queue->head;
|
|
|
|
while (current->next != NULL) {
|
|
|
|
current = current->next;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
2023-11-28 21:02:49 -04:00
|
|
|
current->next = request;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
2023-11-28 21:02:49 -04:00
|
|
|
queue->size++;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
|
|
|
|
2023-12-02 21:30:51 -04:00
|
|
|
void delete(DiskQueue *queue, DiskRequest request) {
|
2023-11-28 21:02:49 -04:00
|
|
|
DiskRequest *current = queue->head;
|
|
|
|
DiskRequest *previous = NULL;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (current->position == request.position && current->time == request.time) {
|
|
|
|
if (previous == NULL) {
|
|
|
|
queue->head = current->next;
|
|
|
|
} else {
|
|
|
|
previous->next = current->next;
|
|
|
|
}
|
|
|
|
queue->size--;
|
2023-12-02 21:55:24 -04:00
|
|
|
free(current);
|
2023-12-02 21:30:51 -04:00
|
|
|
return;
|
2023-11-28 21:02:49 -04:00
|
|
|
}
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
printf("Error: Request not found\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
|
|
|
|
2023-12-02 21:55:24 -04:00
|
|
|
DiskRequest *dequeue(DiskQueue *queue) {
|
2023-11-28 21:02:49 -04:00
|
|
|
DiskRequest *head = queue->head;
|
|
|
|
queue->head = head->next;
|
2023-11-28 20:33:23 -04:00
|
|
|
queue->size--;
|
2023-12-02 21:55:24 -04:00
|
|
|
return head;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
|
|
|
|
2023-11-28 21:02:49 -04:00
|
|
|
void destroyDiskQueue(DiskQueue *queue) {
|
|
|
|
DiskRequest *current = queue->head;
|
|
|
|
DiskRequest *next = NULL;
|
|
|
|
while (current != NULL) {
|
|
|
|
next = current->next;
|
|
|
|
free(current);
|
|
|
|
current = next;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
2023-11-28 21:02:49 -04:00
|
|
|
free(queue);
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
|
|
|
|
2023-11-28 21:02:49 -04:00
|
|
|
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
|
|
|
DiskRequest *current = queue->head;
|
2023-12-02 23:12:22 -04:00
|
|
|
DiskRequest *next = NULL;
|
|
|
|
DiskRequest *first = NULL;
|
2023-11-28 21:02:49 -04:00
|
|
|
while (current != NULL) {
|
2023-12-02 21:30:51 -04:00
|
|
|
if (current->time <= time) { // Filter out requests that haven't come in yet
|
2023-12-02 23:12:22 -04:00
|
|
|
if (first == NULL) {
|
|
|
|
first = current;
|
2023-11-28 21:02:49 -04:00
|
|
|
} else {
|
2023-12-02 23:12:22 -04:00
|
|
|
if (current->position < first->position) {
|
|
|
|
first = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad)
|
|
|
|
if (currentDistance > 0) { // If the current is up
|
|
|
|
if (next == NULL) { // If there is no next yet, set it to the current
|
|
|
|
next = current;
|
|
|
|
} else {
|
|
|
|
int nextDistance = next->position - position; // Distance from the next to the current position, negative means down (bad)
|
|
|
|
if (currentDistance < nextDistance) { // If the current is closer than the next
|
|
|
|
next = current;
|
2023-11-28 21:31:01 -04:00
|
|
|
}
|
2023-11-28 21:02:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current = current->next;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
2023-12-02 23:12:22 -04:00
|
|
|
// If we didn't find a request that is above the current position, return the first as a fallback
|
|
|
|
if (next == NULL) {
|
|
|
|
return first;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|
2023-12-02 23:12:22 -04:00
|
|
|
return next;
|
2023-11-28 20:33:23 -04:00
|
|
|
}
|