Compare commits
2 Commits
master
...
a8-alt-sol
Author | SHA1 | Date | |
---|---|---|---|
9752ef451f | |||
42cb09f3bd |
@ -57,10 +57,6 @@ void delete(DiskQueue *queue, DiskRequest request) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
DiskRequest peek(DiskQueue *queue) {
|
|
||||||
return *queue->head;
|
|
||||||
}
|
|
||||||
|
|
||||||
DiskRequest *dequeue(DiskQueue *queue) {
|
DiskRequest *dequeue(DiskQueue *queue) {
|
||||||
DiskRequest *head = queue->head;
|
DiskRequest *head = queue->head;
|
||||||
queue->head = head->next;
|
queue->head = head->next;
|
||||||
@ -81,26 +77,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) {
|
||||||
int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad)
|
first = current;
|
||||||
if (currentDistance > 0) { // If the current is up
|
}
|
||||||
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
}
|
||||||
closest = 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;
|
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;
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,6 @@ void enqueue(DiskQueue *queue, int time, int position);
|
|||||||
|
|
||||||
void delete(DiskQueue *queue, DiskRequest request);
|
void delete(DiskQueue *queue, DiskRequest request);
|
||||||
|
|
||||||
DiskRequest peek(DiskQueue *queue);
|
|
||||||
|
|
||||||
DiskRequest *dequeue(DiskQueue *queue);
|
DiskRequest *dequeue(DiskQueue *queue);
|
||||||
|
|
||||||
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
||||||
|
@ -68,7 +68,6 @@ void cscan(int start) {
|
|||||||
if (request != NULL) {
|
if (request != NULL) {
|
||||||
delete(queue, *request);
|
delete(queue, *request);
|
||||||
}
|
}
|
||||||
//printf("Movement: %i Time:%.1lf\n", movement, seekTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,4 +92,4 @@ int main(int argc, char **argv) {
|
|||||||
destroyDiskQueue(queue);
|
destroyDiskQueue(queue);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user