Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9752ef451f | |||
| 42cb09f3bd |
+22
-16
@@ -75,30 +75,36 @@ void destroyDiskQueue(DiskQueue *queue) {
|
||||
free(queue);
|
||||
}
|
||||
|
||||
int calcDistance(int position, int destination) {
|
||||
if (position > destination) {
|
||||
return destination + (9999 + 1 - position);
|
||||
} else {
|
||||
return destination - position;
|
||||
}
|
||||
}
|
||||
|
||||
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *closest = NULL;
|
||||
DiskRequest *next = NULL;
|
||||
DiskRequest *first = NULL;
|
||||
while (current != NULL) {
|
||||
if (current->time <= time) { // Filter out requests that haven't come in yet
|
||||
if (closest == NULL) { // If there is no closest yet, set it to the current
|
||||
closest = current;
|
||||
if (first == NULL) {
|
||||
first = current;
|
||||
} else {
|
||||
int closestDistance = calcDistance(position, closest->position); // Distance from the closest to the current position, negative means down (bad)
|
||||
int currentDistance = calcDistance(position, current->position); // Distance from the current to the current position, negative means down (bad
|
||||
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
||||
closest = current;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return closest;
|
||||
// If we didn't find a request that is above the current position, return the first as a fallback
|
||||
if (next == NULL) {
|
||||
return first;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
@@ -26,5 +26,3 @@ DiskRequest *dequeue(DiskQueue *queue);
|
||||
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue);
|
||||
|
||||
int calcDistance(int position, int destination);
|
||||
|
||||
Reference in New Issue
Block a user