|
|
@ -14,82 +14,6 @@
|
|
|
|
#include "queue.h"
|
|
|
|
#include "queue.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Insertion Sorter
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void InsSort_Clear(Queue *q, bool free_values)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
InsSortNode *node = q->data.inssort.first;
|
|
|
|
|
|
|
|
InsSortNode *prev;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
|
|
|
if (free_values) free(node->item);
|
|
|
|
|
|
|
|
prev = node;
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
free(prev);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
q->data.inssort.first = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void InsSort_Free(Queue *q, bool free_values)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
q->clear(q, free_values);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool InsSort_Push(Queue *q, void *item, int priority)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
InsSortNode *newnode = MallocT<InsSortNode>(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newnode->item = item;
|
|
|
|
|
|
|
|
newnode->priority = priority;
|
|
|
|
|
|
|
|
if (q->data.inssort.first == NULL ||
|
|
|
|
|
|
|
|
q->data.inssort.first->priority >= priority) {
|
|
|
|
|
|
|
|
newnode->next = q->data.inssort.first;
|
|
|
|
|
|
|
|
q->data.inssort.first = newnode;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
InsSortNode *node = q->data.inssort.first;
|
|
|
|
|
|
|
|
while (node != NULL) {
|
|
|
|
|
|
|
|
if (node->next == NULL || node->next->priority >= priority) {
|
|
|
|
|
|
|
|
newnode->next = node->next;
|
|
|
|
|
|
|
|
node->next = newnode;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void *InsSort_Pop(Queue *q)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
InsSortNode *node = q->data.inssort.first;
|
|
|
|
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (node == NULL) return NULL;
|
|
|
|
|
|
|
|
result = node->item;
|
|
|
|
|
|
|
|
q->data.inssort.first = q->data.inssort.first->next;
|
|
|
|
|
|
|
|
assert(q->data.inssort.first == NULL || q->data.inssort.first->priority >= node->priority);
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool InsSort_Delete(Queue *q, void *item, int priority)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void init_InsSort(Queue *q)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
q->push = InsSort_Push;
|
|
|
|
|
|
|
|
q->pop = InsSort_Pop;
|
|
|
|
|
|
|
|
q->del = InsSort_Delete;
|
|
|
|
|
|
|
|
q->clear = InsSort_Clear;
|
|
|
|
|
|
|
|
q->free = InsSort_Free;
|
|
|
|
|
|
|
|
q->data.inssort.first = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Binary Heap
|
|
|
|
* Binary Heap
|
|
|
|
* For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
|
|
|
|
* For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
|
|
|
|