2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2004-08-20 09:32:32 +00:00
|
|
|
#include "queue.h"
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void Stack_Clear(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) {
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i < q->data.stack.size; i++) free(q->data.stack.elements[i]);
|
|
|
|
}
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.stack.size = 0;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void Stack_Free(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
q->clear(q, free_values);
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.stack.elements);
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->freeq) free(q);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool Stack_Push(Queue* q, void* item, int priority)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->data.stack.size == q->data.stack.max_size) return false;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.stack.elements[q->data.stack.size++] = item;
|
2004-08-20 09:32:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void* Stack_Pop(Queue* q)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->data.stack.size == 0) return NULL;
|
|
|
|
return q->data.stack.elements[--q->data.stack.size];
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool Stack_Delete(Queue* q, void* item, int priority)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static Queue* init_stack(Queue* q, uint max_size)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
q->push = Stack_Push;
|
|
|
|
q->pop = Stack_Pop;
|
|
|
|
q->del = Stack_Delete;
|
|
|
|
q->clear = Stack_Clear;
|
|
|
|
q->free = Stack_Free;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.stack.max_size = max_size;
|
|
|
|
q->data.stack.size = 0;
|
2006-02-11 15:05:56 +00:00
|
|
|
q->data.stack.elements = malloc(max_size * sizeof(*q->data.stack.elements));
|
2004-08-20 09:32:32 +00:00
|
|
|
q->freeq = false;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
Queue* new_Stack(uint max_size)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
Queue* q = malloc(sizeof(*q));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
init_stack(q, max_size);
|
|
|
|
q->freeq = true;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fifo
|
|
|
|
*/
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void Fifo_Clear(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
if (free_values) {
|
2006-02-11 15:05:56 +00:00
|
|
|
uint head = q->data.fifo.head;
|
|
|
|
uint tail = q->data.fifo.tail; /* cache for speed */
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
while (head != tail) {
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.fifo.elements[tail]);
|
|
|
|
tail = (tail + 1) % q->data.fifo.max_size;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
q->data.fifo.head = 0;
|
|
|
|
q->data.fifo.tail = 0;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void Fifo_Free(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
q->clear(q, free_values);
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.fifo.elements);
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->freeq) free(q);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool Fifo_Push(Queue* q, void* item, int priority)
|
|
|
|
{
|
2004-08-22 14:44:03 +00:00
|
|
|
uint next = (q->data.fifo.head + 1) % q->data.fifo.max_size;
|
2004-08-20 09:32:32 +00:00
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
if (next == q->data.fifo.tail) return false;
|
|
|
|
q->data.fifo.elements[q->data.fifo.head] = item;
|
2004-08-20 09:32:32 +00:00
|
|
|
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.fifo.head = next;
|
2004-08-20 09:32:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void* Fifo_Pop(Queue* q)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
void* result;
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->data.fifo.head == q->data.fifo.tail) return NULL;
|
|
|
|
result = q->data.fifo.elements[q->data.fifo.tail];
|
2004-08-20 09:32:32 +00:00
|
|
|
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.fifo.tail = (q->data.fifo.tail + 1) % q->data.fifo.max_size;
|
2004-08-20 09:32:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool Fifo_Delete(Queue* q, void* item, int priority)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static Queue* init_fifo(Queue* q, uint max_size)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
q->push = Fifo_Push;
|
|
|
|
q->pop = Fifo_Pop;
|
|
|
|
q->del = Fifo_Delete;
|
|
|
|
q->clear = Fifo_Clear;
|
|
|
|
q->free = Fifo_Free;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.fifo.max_size = max_size;
|
|
|
|
q->data.fifo.head = 0;
|
|
|
|
q->data.fifo.tail = 0;
|
2006-02-11 15:05:56 +00:00
|
|
|
q->data.fifo.elements = malloc(max_size * sizeof(*q->data.fifo.elements));
|
2004-08-20 09:32:32 +00:00
|
|
|
q->freeq = false;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
Queue* new_Fifo(uint max_size)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
Queue* q = malloc(sizeof(*q));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
init_fifo(q, max_size);
|
|
|
|
q->freeq = true;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insertion Sorter
|
|
|
|
*/
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void InsSort_Clear(Queue* q, bool free_values)
|
|
|
|
{
|
2004-08-22 14:44:03 +00:00
|
|
|
InsSortNode* node = q->data.inssort.first;
|
2004-08-20 09:32:32 +00:00
|
|
|
InsSortNode* prev;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
while (node != NULL) {
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) free(node->item);
|
2004-08-20 09:32:32 +00:00
|
|
|
prev = node;
|
|
|
|
node = node->next;
|
|
|
|
free(prev);
|
|
|
|
}
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.inssort.first = NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void InsSort_Free(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
q->clear(q, free_values);
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->freeq) free(q);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool InsSort_Push(Queue* q, void* item, int priority)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
InsSortNode* newnode = malloc(sizeof(*newnode));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
if (newnode == NULL) return false;
|
|
|
|
newnode->item = item;
|
|
|
|
newnode->priority = priority;
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->data.inssort.first == NULL ||
|
|
|
|
q->data.inssort.first->priority >= priority) {
|
2004-08-22 14:44:03 +00:00
|
|
|
newnode->next = q->data.inssort.first;
|
|
|
|
q->data.inssort.first = newnode;
|
2004-08-20 09:32:32 +00:00
|
|
|
} else {
|
2004-08-22 14:44:03 +00:00
|
|
|
InsSortNode* node = q->data.inssort.first;
|
2006-02-01 09:08:25 +00:00
|
|
|
while (node != NULL) {
|
2004-08-20 09:32:32 +00:00
|
|
|
if (node->next == NULL || node->next->priority >= priority) {
|
|
|
|
newnode->next = node->next;
|
|
|
|
node->next = newnode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void* InsSort_Pop(Queue* q)
|
|
|
|
{
|
2004-08-22 14:44:03 +00:00
|
|
|
InsSortNode* node = q->data.inssort.first;
|
2004-08-20 09:32:32 +00:00
|
|
|
void* result;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
if (node == NULL) return NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
result = node->item;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.inssort.first = q->data.inssort.first->next;
|
2006-02-11 15:05:56 +00:00
|
|
|
assert(q->data.inssort.first == NULL || q->data.inssort.first->priority >= node->priority);
|
2004-08-20 09:32:32 +00:00
|
|
|
free(node);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool InsSort_Delete(Queue* q, void* item, int priority)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void init_InsSort(Queue* q)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
q->push = InsSort_Push;
|
|
|
|
q->pop = InsSort_Pop;
|
|
|
|
q->del = InsSort_Delete;
|
|
|
|
q->clear = InsSort_Clear;
|
|
|
|
q->free = InsSort_Free;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.inssort.first = NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
q->freeq = false;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
Queue* new_InsSort(void)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
Queue* q = malloc(sizeof(*q));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
init_InsSort(q);
|
|
|
|
q->freeq = true;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Binary Heap
|
|
|
|
* For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BINARY_HEAP_BLOCKSIZE (1 << BINARY_HEAP_BLOCKSIZE_BITS)
|
2006-02-11 15:05:56 +00:00
|
|
|
#define BINARY_HEAP_BLOCKSIZE_MASK (BINARY_HEAP_BLOCKSIZE - 1)
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
// To make our life easy, we make the next define
|
|
|
|
// Because Binary Heaps works with array from 1 to n,
|
|
|
|
// and C with array from 0 to n-1, and we don't like typing
|
2006-02-11 15:05:56 +00:00
|
|
|
// q->data.binaryheap.elements[i - 1] every time, we use this define.
|
|
|
|
#define BIN_HEAP_ARR(i) q->data.binaryheap.elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
2004-08-20 09:32:32 +00:00
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void BinaryHeap_Clear(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
/* Free all items if needed and free all but the first blocks of memory */
|
|
|
|
uint i;
|
|
|
|
uint j;
|
|
|
|
|
|
|
|
for (i = 0; i < q->data.binaryheap.blocks; i++) {
|
2004-08-22 14:44:03 +00:00
|
|
|
if (q->data.binaryheap.elements[i] == NULL) {
|
2004-08-20 09:32:32 +00:00
|
|
|
/* No more allocated blocks */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* For every allocated block */
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) {
|
|
|
|
for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
|
2004-08-20 09:32:32 +00:00
|
|
|
/* For every element in the block */
|
2006-02-11 15:05:56 +00:00
|
|
|
if ((q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
|
|
|
|
(q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == j) {
|
2004-08-20 09:32:32 +00:00
|
|
|
break; /* We're past the last element */
|
2006-02-11 15:05:56 +00:00
|
|
|
}
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.binaryheap.elements[i][j].item);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
}
|
2004-08-20 09:32:32 +00:00
|
|
|
if (i != 0) {
|
|
|
|
/* Leave the first block of memory alone */
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.binaryheap.elements[i]);
|
|
|
|
q->data.binaryheap.elements[i] = NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.size = 0;
|
|
|
|
q->data.binaryheap.blocks = 1;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void BinaryHeap_Free(Queue* q, bool free_values)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
uint i;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
q->clear(q, free_values);
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i < q->data.binaryheap.blocks; i++) {
|
|
|
|
if (q->data.binaryheap.elements[i] == NULL) break;
|
2004-08-22 14:44:03 +00:00
|
|
|
free(q->data.binaryheap.elements[i]);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
2005-12-27 20:44:42 +00:00
|
|
|
free(q->data.binaryheap.elements);
|
2006-02-11 15:05:56 +00:00
|
|
|
if (q->freeq) free(q);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool BinaryHeap_Push(Queue* q, void* item, int priority)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifdef QUEUE_DEBUG
|
|
|
|
printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->data.binaryheap.size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (q->data.binaryheap.size == q->data.binaryheap.max_size) return false;
|
2004-08-22 14:44:03 +00:00
|
|
|
assert(q->data.binaryheap.size < q->data.binaryheap.max_size);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-22 14:44:03 +00:00
|
|
|
if (q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
|
2004-08-20 09:32:32 +00:00
|
|
|
/* The currently allocated blocks are full, allocate a new one */
|
2004-08-22 14:44:03 +00:00
|
|
|
assert((q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
|
2006-02-11 15:05:56 +00:00
|
|
|
q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] = malloc(BINARY_HEAP_BLOCKSIZE * sizeof(*q->data.binaryheap.elements[0]));
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.blocks++;
|
2004-08-20 09:32:32 +00:00
|
|
|
#ifdef QUEUE_DEBUG
|
2006-02-11 15:05:56 +00:00
|
|
|
printf("[BinaryHeap] Increasing size of elements to %d nodes\n", q->data.binaryheap.blocks * BINARY_HEAP_BLOCKSIZE);
|
2004-08-20 09:32:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the item at the end of the array
|
2006-02-11 15:05:56 +00:00
|
|
|
BIN_HEAP_ARR(q->data.binaryheap.size + 1).priority = priority;
|
|
|
|
BIN_HEAP_ARR(q->data.binaryheap.size + 1).item = item;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.size++;
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
// Now we are going to check where it belongs. As long as the parent is
|
|
|
|
// bigger, we switch with the parent
|
|
|
|
{
|
|
|
|
BinaryHeapNode temp;
|
2006-02-11 15:05:56 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
2004-08-22 14:44:03 +00:00
|
|
|
i = q->data.binaryheap.size;
|
2004-08-20 09:32:32 +00:00
|
|
|
while (i > 1) {
|
|
|
|
// Get the parent of this object (divide by 2)
|
|
|
|
j = i / 2;
|
|
|
|
// Is the parent bigger then the current, switch them
|
|
|
|
if (BIN_HEAP_ARR(i).priority <= BIN_HEAP_ARR(j).priority) {
|
|
|
|
temp = BIN_HEAP_ARR(j);
|
|
|
|
BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
|
|
|
|
BIN_HEAP_ARR(i) = temp;
|
|
|
|
i = j;
|
|
|
|
} else {
|
|
|
|
// It is not, we're done!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool BinaryHeap_Delete(Queue* q, void* item, int priority)
|
2004-08-20 09:32:32 +00:00
|
|
|
{
|
|
|
|
uint i = 0;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
#ifdef QUEUE_DEBUG
|
|
|
|
printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->data.binaryheap.size);
|
|
|
|
#endif
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
// First, we try to find the item..
|
|
|
|
do {
|
2006-02-11 15:05:56 +00:00
|
|
|
if (BIN_HEAP_ARR(i + 1).item == item) break;
|
2004-08-20 09:32:32 +00:00
|
|
|
i++;
|
2004-08-22 14:44:03 +00:00
|
|
|
} while (i < q->data.binaryheap.size);
|
2004-08-20 09:32:32 +00:00
|
|
|
// We did not find the item, so we return false
|
2004-08-22 14:44:03 +00:00
|
|
|
if (i == q->data.binaryheap.size) return false;
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
// Now we put the last item over the current item while decreasing the size of the elements
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.size--;
|
2006-02-11 15:05:56 +00:00
|
|
|
BIN_HEAP_ARR(i + 1) = BIN_HEAP_ARR(q->data.binaryheap.size + 1);
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
// Now the only thing we have to do, is resort it..
|
|
|
|
// On place i there is the item to be sorted.. let's start there
|
|
|
|
{
|
|
|
|
uint j;
|
|
|
|
BinaryHeapNode temp;
|
2006-02-11 15:05:56 +00:00
|
|
|
/* Because of the fact that Binary Heap uses array from 1 to n, we need to
|
|
|
|
* increase i by 1
|
|
|
|
*/
|
2004-08-20 09:32:32 +00:00
|
|
|
i++;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
j = i;
|
|
|
|
// Check if we have 2 childs
|
2006-02-11 15:05:56 +00:00
|
|
|
if (2 * j + 1 <= q->data.binaryheap.size) {
|
2004-12-29 13:13:29 +00:00
|
|
|
// Is this child smaller than the parent?
|
2006-02-11 15:05:56 +00:00
|
|
|
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
|
2004-08-20 09:32:32 +00:00
|
|
|
// Yes, we _need_ to use i here, not j, because we want to have the smallest child
|
|
|
|
// This way we get that straight away!
|
2006-02-11 15:05:56 +00:00
|
|
|
if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1;
|
2004-08-20 09:32:32 +00:00
|
|
|
// Do we have one child?
|
2006-02-11 15:05:56 +00:00
|
|
|
} else if (2 * j <= q->data.binaryheap.size) {
|
|
|
|
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2004-12-29 13:13:29 +00:00
|
|
|
// One of our childs is smaller than we are, switch
|
2004-08-20 09:32:32 +00:00
|
|
|
if (i != j) {
|
|
|
|
temp = BIN_HEAP_ARR(j);
|
|
|
|
BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
|
|
|
|
BIN_HEAP_ARR(i) = temp;
|
|
|
|
} else {
|
|
|
|
// None of our childs is smaller, so we stay here.. stop :)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void* BinaryHeap_Pop(Queue* q)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
void* result;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
#ifdef QUEUE_DEBUG
|
|
|
|
printf("[BinaryHeap] Popping an element. There are %d elements left\n", q->data.binaryheap.size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (q->data.binaryheap.size == 0) return NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
// The best item is always on top, so give that as result
|
|
|
|
result = BIN_HEAP_ARR(1).item;
|
2006-02-11 15:05:56 +00:00
|
|
|
// And now we should get rid of this item...
|
|
|
|
BinaryHeap_Delete(q, BIN_HEAP_ARR(1).item, BIN_HEAP_ARR(1).priority);
|
2004-08-20 09:32:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_BinaryHeap(Queue* q, uint max_size)
|
|
|
|
{
|
2006-02-11 15:05:56 +00:00
|
|
|
assert(q != NULL);
|
2004-08-20 09:32:32 +00:00
|
|
|
q->push = BinaryHeap_Push;
|
|
|
|
q->pop = BinaryHeap_Pop;
|
|
|
|
q->del = BinaryHeap_Delete;
|
|
|
|
q->clear = BinaryHeap_Clear;
|
|
|
|
q->free = BinaryHeap_Free;
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.max_size = max_size;
|
|
|
|
q->data.binaryheap.size = 0;
|
2004-08-20 09:32:32 +00:00
|
|
|
// We malloc memory in block of BINARY_HEAP_BLOCKSIZE
|
|
|
|
// It autosizes when it runs out of memory
|
2006-02-11 15:05:56 +00:00
|
|
|
q->data.binaryheap.elements = calloc((max_size - 1) / BINARY_HEAP_BLOCKSIZE + 1, sizeof(*q->data.binaryheap.elements));
|
|
|
|
q->data.binaryheap.elements[0] = malloc(BINARY_HEAP_BLOCKSIZE * sizeof(*q->data.binaryheap.elements[0]));
|
2004-08-22 14:44:03 +00:00
|
|
|
q->data.binaryheap.blocks = 1;
|
2004-08-20 09:32:32 +00:00
|
|
|
q->freeq = false;
|
|
|
|
#ifdef QUEUE_DEBUG
|
2006-02-11 15:05:56 +00:00
|
|
|
printf("[BinaryHeap] Initial size of elements is %d nodes\n", BINARY_HEAP_BLOCKSIZE);
|
2004-08-20 09:32:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
Queue* new_BinaryHeap(uint max_size)
|
|
|
|
{
|
|
|
|
Queue* q = malloc(sizeof(*q));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
init_BinaryHeap(q, max_size);
|
|
|
|
q->freeq = true;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because we don't want anyone else to bother with our defines
|
|
|
|
#undef BIN_HEAP_ARR
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash
|
|
|
|
*/
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void init_Hash(Hash* h, Hash_HashProc* hash, uint num_buckets)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Allocate space for the Hash, the buckets and the bucket flags */
|
2005-04-07 19:19:16 +00:00
|
|
|
uint i;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
assert(h != NULL);
|
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Allocated hash: %p", h);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
h->hash = hash;
|
|
|
|
h->size = 0;
|
|
|
|
h->num_buckets = num_buckets;
|
2006-02-11 15:05:56 +00:00
|
|
|
h->buckets = malloc(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use)));
|
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Buckets = %p", h->buckets);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
h->buckets_in_use = (bool*)(h->buckets + num_buckets);
|
|
|
|
h->freeh = false;
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i < num_buckets; i++) h->buckets_in_use[i] = false;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
Hash* new_Hash(Hash_HashProc* hash, int num_buckets)
|
|
|
|
{
|
|
|
|
Hash* h = malloc(sizeof(*h));
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
init_Hash(h, hash, num_buckets);
|
|
|
|
h->freeh = true;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void delete_Hash(Hash* h, bool free_values)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
uint i;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Iterate all buckets */
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i < h->num_buckets; i++) {
|
2004-08-20 09:32:32 +00:00
|
|
|
if (h->buckets_in_use[i]) {
|
|
|
|
HashNode* node;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Free the first value */
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) free(h->buckets[i].value);
|
2004-08-20 09:32:32 +00:00
|
|
|
node = h->buckets[i].next;
|
|
|
|
while (node != NULL) {
|
|
|
|
HashNode* prev = node;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
node = node->next;
|
|
|
|
/* Free the value */
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) free(prev->value);
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Free the node */
|
|
|
|
free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(h->buckets);
|
|
|
|
/* No need to free buckets_in_use, it is always allocated in one
|
|
|
|
* malloc with buckets */
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Freeing Hash: %p", h);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
|
|
|
if (h->freeh) free(h);
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-12-14 06:20:23 +00:00
|
|
|
#ifdef HASH_STATS
|
2006-02-11 15:05:56 +00:00
|
|
|
static void stat_Hash(const Hash* h)
|
2005-04-07 19:19:16 +00:00
|
|
|
{
|
|
|
|
uint used_buckets = 0;
|
|
|
|
uint max_collision = 0;
|
|
|
|
uint max_usage = 0;
|
|
|
|
uint usage[200];
|
2005-04-07 19:33:32 +00:00
|
|
|
uint i;
|
2005-04-07 19:19:16 +00:00
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i < lengthof(usage); i++) usage[i] = 0;
|
|
|
|
for (i = 0; i < h->num_buckets; i++) {
|
|
|
|
uint collision = 0;
|
2005-04-07 19:19:16 +00:00
|
|
|
if (h->buckets_in_use[i]) {
|
2006-02-11 15:05:56 +00:00
|
|
|
const HashNode* node;
|
|
|
|
|
2005-04-07 19:19:16 +00:00
|
|
|
used_buckets++;
|
2006-02-11 15:05:56 +00:00
|
|
|
for (node = &h->buckets[i]; node != NULL; node = node->next) collision++;
|
2005-04-07 19:19:16 +00:00
|
|
|
if (collision > max_collision) max_collision = collision;
|
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
if (collision >= lengthof(usage)) collision = lengthof(usage) - 1;
|
2005-04-07 19:19:16 +00:00
|
|
|
usage[collision]++;
|
2006-02-11 15:05:56 +00:00
|
|
|
if (collision > 0 && usage[collision] >= max_usage) {
|
|
|
|
max_usage = usage[collision];
|
|
|
|
}
|
2005-04-07 19:19:16 +00:00
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
printf(
|
|
|
|
"---\n"
|
|
|
|
"Hash size: %d\n"
|
|
|
|
"Nodes used: %d\n"
|
|
|
|
"Non empty buckets: %d\n"
|
|
|
|
"Max collision: %d\n",
|
|
|
|
h->num_buckets, h->size, used_buckets, max_collision
|
|
|
|
);
|
2005-04-07 19:19:16 +00:00
|
|
|
printf("{ ");
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i <= max_collision; i++) {
|
|
|
|
if (usage[i] > 0) {
|
2005-04-07 19:19:16 +00:00
|
|
|
printf("%d:%d ", i, usage[i]);
|
2006-02-11 15:05:56 +00:00
|
|
|
#if 0
|
|
|
|
if (i > 0) {
|
2005-04-07 19:33:32 +00:00
|
|
|
uint j;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
for (j = 0; j < usage[i] * 160 / 800; j++) putchar('#');
|
2005-04-07 19:33:32 +00:00
|
|
|
}
|
2005-04-07 19:19:16 +00:00
|
|
|
printf("\n");
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2005-04-07 19:19:16 +00:00
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
}
|
2005-04-07 19:19:16 +00:00
|
|
|
printf ("}\n");
|
|
|
|
}
|
2005-12-14 06:20:23 +00:00
|
|
|
#endif
|
2005-04-07 19:19:16 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
void clear_Hash(Hash* h, bool free_values)
|
|
|
|
{
|
|
|
|
uint i;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2005-04-07 19:19:16 +00:00
|
|
|
#ifdef HASH_STATS
|
2006-02-11 15:05:56 +00:00
|
|
|
if (h->size > 2000) stat_Hash(h);
|
2005-04-07 19:19:16 +00:00
|
|
|
#endif
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Iterate all buckets */
|
2006-02-11 15:05:56 +00:00
|
|
|
for (i = 0; i < h->num_buckets; i++) {
|
2004-08-20 09:32:32 +00:00
|
|
|
if (h->buckets_in_use[i]) {
|
2006-02-11 15:05:56 +00:00
|
|
|
HashNode* node;
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
h->buckets_in_use[i] = false;
|
|
|
|
/* Free the first value */
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) free(h->buckets[i].value);
|
2004-08-20 09:32:32 +00:00
|
|
|
node = h->buckets[i].next;
|
|
|
|
while (node != NULL) {
|
|
|
|
HashNode* prev = node;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
node = node->next;
|
2006-02-11 15:05:56 +00:00
|
|
|
if (free_values) free(prev->value);
|
2004-08-20 09:32:32 +00:00
|
|
|
free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finds the node that that saves this key pair. If it is not
|
|
|
|
* found, returns NULL. If it is found, *prev is set to the
|
|
|
|
* node before the one found, or if the node found was the first in the bucket
|
|
|
|
* to NULL. If it is not found, *prev is set to the last HashNode in the
|
|
|
|
* bucket, or NULL if it is empty. prev can also be NULL, in which case it is
|
|
|
|
* not used for output.
|
|
|
|
*/
|
2006-02-11 15:05:56 +00:00
|
|
|
static HashNode* Hash_FindNode(const Hash* h, uint key1, uint key2, HashNode** prev_out)
|
2005-01-22 22:47:58 +00:00
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
uint hash = h->hash(key1, key2);
|
|
|
|
HashNode* result = NULL;
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Looking for %u, %u", key1, key2);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Check if the bucket is empty */
|
|
|
|
if (!h->buckets_in_use[hash]) {
|
2006-02-11 15:05:56 +00:00
|
|
|
if (prev_out != NULL) *prev_out = NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
result = NULL;
|
|
|
|
/* Check the first node specially */
|
|
|
|
} else if (h->buckets[hash].key1 == key1 && h->buckets[hash].key2 == key2) {
|
|
|
|
/* Save the value */
|
|
|
|
result = h->buckets + hash;
|
2006-02-11 15:05:56 +00:00
|
|
|
if (prev_out != NULL) *prev_out = NULL;
|
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Found in first node: %p", result);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
/* Check all other nodes */
|
|
|
|
} else {
|
|
|
|
HashNode* prev = h->buckets + hash;
|
2006-02-11 15:05:56 +00:00
|
|
|
HashNode* node;
|
|
|
|
|
|
|
|
for (node = prev->next; node != NULL; node = node->next) {
|
2004-08-20 09:32:32 +00:00
|
|
|
if (node->key1 == key1 && node->key2 == key2) {
|
|
|
|
/* Found it */
|
|
|
|
result = node;
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Found in other node: %p", result);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = node;
|
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
if (prev_out != NULL) *prev_out = prev;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifdef HASH_DEBUG
|
|
|
|
if (result == NULL) debug("Not found");
|
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void* Hash_Delete(Hash* h, uint key1, uint key2)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
void* result;
|
|
|
|
HashNode* prev; /* Used as output var for below function call */
|
|
|
|
HashNode* node = Hash_FindNode(h, key1, key2, &prev);
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
/* not found */
|
|
|
|
result = NULL;
|
|
|
|
} else if (prev == NULL) {
|
|
|
|
/* It is in the first node, we can't free that one, so we free
|
|
|
|
* the next one instead (if there is any)*/
|
|
|
|
/* Save the value */
|
|
|
|
result = node->value;
|
|
|
|
if (node->next != NULL) {
|
|
|
|
HashNode* next = node->next;
|
|
|
|
/* Copy the second to the first */
|
|
|
|
*node = *next;
|
|
|
|
/* Free the second */
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifndef NOFREE
|
2004-08-20 09:32:32 +00:00
|
|
|
free(next);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
} else {
|
|
|
|
/* This was the last in this bucket */
|
|
|
|
/* Mark it as empty */
|
|
|
|
uint hash = h->hash(key1, key2);
|
|
|
|
h->buckets_in_use[hash] = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* It is in another node */
|
|
|
|
/* Save the value */
|
|
|
|
result = node->value;
|
|
|
|
/* Link previous and next nodes */
|
|
|
|
prev->next = node->next;
|
|
|
|
/* Free the node */
|
2006-02-11 15:05:56 +00:00
|
|
|
#ifndef NOFREE
|
2004-08-20 09:32:32 +00:00
|
|
|
free(node);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
2006-02-11 15:05:56 +00:00
|
|
|
if (result != NULL) h->size--;
|
2004-08-20 09:32:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void* Hash_Set(Hash* h, uint key1, uint key2, void* value)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
HashNode* prev;
|
|
|
|
HashNode* node = Hash_FindNode(h, key1, key2, &prev);
|
2006-02-11 15:05:56 +00:00
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
if (node != NULL) {
|
|
|
|
/* Found it */
|
2006-02-11 15:05:56 +00:00
|
|
|
void* result = node->value;
|
|
|
|
|
2004-08-20 09:32:32 +00:00
|
|
|
node->value = value;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* It is not yet present, let's add it */
|
|
|
|
if (prev == NULL) {
|
|
|
|
/* The bucket is still empty */
|
|
|
|
uint hash = h->hash(key1, key2);
|
|
|
|
h->buckets_in_use[hash] = true;
|
|
|
|
node = h->buckets + hash;
|
|
|
|
} else {
|
|
|
|
/* Add it after prev */
|
2006-02-11 15:05:56 +00:00
|
|
|
node = malloc(sizeof(*node));
|
2004-08-20 09:32:32 +00:00
|
|
|
prev->next = node;
|
|
|
|
}
|
|
|
|
node->next = NULL;
|
|
|
|
node->key1 = key1;
|
|
|
|
node->key2 = key2;
|
|
|
|
node->value = value;
|
|
|
|
h->size++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
void* Hash_Get(const Hash* h, uint key1, uint key2)
|
|
|
|
{
|
2004-08-20 09:32:32 +00:00
|
|
|
HashNode* node = Hash_FindNode(h, key1, key2, NULL);
|
2006-02-11 15:05:56 +00:00
|
|
|
|
|
|
|
#ifdef HASH_DEBUG
|
2004-08-20 09:32:32 +00:00
|
|
|
debug("Found node: %p", node);
|
2006-02-11 15:05:56 +00:00
|
|
|
#endif
|
|
|
|
return (node != NULL) ? node->value : NULL;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 15:05:56 +00:00
|
|
|
uint Hash_Size(const Hash* h)
|
|
|
|
{
|
|
|
|
return h->size;
|
2004-08-20 09:32:32 +00:00
|
|
|
}
|