Separate data structures into own file

This commit is contained in:
Isaac Shoebottom 2023-11-28 20:33:23 -04:00
parent ca2d32dc34
commit fb6ec1dc98
4 changed files with 144 additions and 125 deletions

104
Assignment8/code/disk.c Normal file
View File

@ -0,0 +1,104 @@
#include <stdlib.h>
#include <stdio.h>
#include "disk.h"
void destroyDiskQueue(DiskQueue *queue) {
free(queue->requests);
free(queue);
}
DiskQueue *sortQueueByPosition(DiskQueue *queue) {
qsort(queue->requests, queue->size, sizeof(DiskRequest), compareDiskRequestsByPosition);
return queue;
}
int compareDiskRequestsByPosition(const void *a, const void *b) {
DiskRequest *requestA = (DiskRequest *) a;
DiskRequest *requestB = (DiskRequest *) b;
return requestA->position - requestB->position;
}
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time) {
DiskQueue *sameTimeQueue = createDiskQueue();
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].time <= time) {
enqueue(sameTimeQueue, queue->requests[i]);
}
}
return sameTimeQueue;
}
DiskRequest dequeue(DiskQueue *queue) {
return delete(queue, peek(queue));
}
DiskRequest peek(DiskQueue *queue) {
return queue->requests[0];
}
DiskQueue *clone(DiskQueue *queue) {
DiskQueue *clone = createDiskQueue();
for (int i = 0; i < queue->size; i++) {
enqueue(clone, queue->requests[i]);
}
return clone;
}
DiskRequest delete(DiskQueue *queue, DiskRequest request) {
// Find the index of the request
int index = -1;
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].position == request.position && queue->requests[i].time == request.time) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Request not found in queue\n");
exit(EXIT_FAILURE);
}
// Shift all the elements after the index down one
for (int i = index; i < queue->size - 1; i++) {
queue->requests[i] = queue->requests[i + 1];
}
// Resize the queue
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size - 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size--;
return request;
}
void enqueue(DiskQueue *queue, DiskRequest request) {
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->requests[queue->size] = request;
queue->size++;
}
DiskRequest diskRequest(int position, int time) {
DiskRequest request;
request.position = position;
request.time = time;
return request;
}
DiskQueue *createDiskQueue() {
DiskQueue *queue = malloc(sizeof(DiskQueue));
if (queue == NULL) {
printf("Error allocating memory for queue\n");
exit(EXIT_FAILURE);
}
queue->requests = malloc(sizeof(DiskRequest));
if (queue->requests == NULL) {
printf("Error allocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size = 0;
return queue;
}

38
Assignment8/code/disk.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
typedef struct DiskRequest {
int position;
int time;
} DiskRequest;
typedef struct DiskQueue {
DiskRequest *requests;
int size;
} DiskQueue;
typedef enum DiskDirection {
UP, DOWN
} DiskDirection;
DiskQueue *createDiskQueue();
DiskRequest diskRequest(int position, int time);
void enqueue(DiskQueue *queue, DiskRequest request);
DiskRequest delete(DiskQueue *queue, DiskRequest request);
DiskQueue *clone(DiskQueue *queue);
DiskRequest peek(DiskQueue *queue);
DiskRequest dequeue(DiskQueue *queue);
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time);
int compareDiskRequestsByPosition(const void *a, const void *b);
DiskQueue *sortQueueByPosition(DiskQueue *queue);
void destroyDiskQueue(DiskQueue *queue);

View File

@ -1,129 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "disk.h"
// Data structures and helper functions
typedef struct DiskRequest {
int position;
int time;
} DiskRequest;
typedef struct DiskQueue {
DiskRequest *requests;
int size;
} DiskQueue;
DiskQueue *createDiskQueue() {
DiskQueue *queue = malloc(sizeof(DiskQueue));
if (queue == NULL) {
printf("Error allocating memory for queue\n");
exit(EXIT_FAILURE);
}
queue->requests = malloc(sizeof(DiskRequest));
if (queue->requests == NULL) {
printf("Error allocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size = 0;
return queue;
}
DiskRequest diskRequest(int position, int time) {
DiskRequest request;
request.position = position;
request.time = time;
return request;
}
void enqueue(DiskQueue *queue, DiskRequest request) {
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->requests[queue->size] = request;
queue->size++;
}
DiskRequest delete(DiskQueue *queue, DiskRequest request) {
// Find the index of the request
int index = -1;
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].position == request.position && queue->requests[i].time == request.time) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Request not found in queue\n");
exit(EXIT_FAILURE);
}
// Shift all the elements after the index down one
for (int i = index; i < queue->size - 1; i++) {
queue->requests[i] = queue->requests[i + 1];
}
// Resize the queue
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size - 1));
if (queue->requests == NULL) {
printf("Error reallocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size--;
return request;
}
DiskQueue* clone(DiskQueue *queue) {
DiskQueue *clone = createDiskQueue();
for (int i = 0; i < queue->size; i++) {
enqueue(clone, queue->requests[i]);
}
return clone;
}
DiskRequest peek(DiskQueue *queue) {
return queue->requests[0];
}
DiskRequest dequeue(DiskQueue *queue) {
return delete(queue, peek(queue));
}
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time) {
DiskQueue *sameTimeQueue = createDiskQueue();
for (int i = 0; i < queue->size; i++) {
if (queue->requests[i].time <= time) {
enqueue(sameTimeQueue, queue->requests[i]);
}
}
return sameTimeQueue;
}
// Compare function for disk requests by position
int compareDiskRequestsByPosition(const void *a, const void *b) {
DiskRequest *requestA = (DiskRequest *) a;
DiskRequest *requestB = (DiskRequest *) b;
return requestA->position - requestB->position;
}
// Sorts the queue by position, in place
DiskQueue *sortQueueByPosition(DiskQueue *queue) {
qsort(queue->requests, queue->size, sizeof(DiskRequest), compareDiskRequestsByPosition);
return queue;
}
void destroyDiskQueue(DiskQueue *queue) {
free(queue->requests);
free(queue);
}
typedef enum DiskDirection {
UP, DOWN
} DiskDirection;
// Global tracking variables // Global tracking variables
DiskDirection currentDirection = UP; DiskDirection currentDirection = UP;

View File

@ -6,7 +6,7 @@ clean:
rm -f ./a.out rm -f ./a.out
build: build:
gcc -g -O0 ./code/main.c -lm gcc -g -O0 ./code/main.c ./code/disk.c -lm
1: 1:
./a.out F < in/all_known_in_advance_spaces.in > student_out/f-in-advance.out && diff student_out/f-in-advance.out out/f-in-advance.out ./a.out F < in/all_known_in_advance_spaces.in > student_out/f-in-advance.out && diff student_out/f-in-advance.out out/f-in-advance.out
2: 2: