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