Pass tests without introducing new function
This commit is contained in:
parent
64f9b63215
commit
42cb09f3bd
@ -81,26 +81,34 @@ void destroyDiskQueue(DiskQueue *queue) {
|
|||||||
|
|
||||||
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
||||||
DiskRequest *current = queue->head;
|
DiskRequest *current = queue->head;
|
||||||
DiskRequest *closest = NULL;
|
DiskRequest *next = NULL;
|
||||||
|
DiskRequest *first = NULL;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
if (current->time <= time) { // Filter out requests that haven't come in yet
|
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
|
if (first == NULL) {
|
||||||
closest = current;
|
first = current;
|
||||||
} else {
|
} else {
|
||||||
int closestDistance = closest->position - position; // Distance from the closest to the current position, negative means down (bad)
|
if (current->position < first->position) {
|
||||||
|
first = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad)
|
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 (currentDistance > 0) { // If the current is up
|
||||||
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
if (next == NULL) { // If there is no next yet, set it to the current
|
||||||
closest = 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;
|
current = current->next;
|
||||||
}
|
}
|
||||||
// Bad
|
// If we didn't find a request that is above the current position, return the first as a fallback
|
||||||
if (closest == NULL) {
|
if (next == NULL) {
|
||||||
return NULL;
|
return first;
|
||||||
}
|
}
|
||||||
return closest;
|
return next;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user