queue.c
author matthijs
Wed, 22 Mar 2006 22:26:16 +0000
branch0.4.5
changeset 9958 bed516c67d61
parent 2799 542aae5b7805
child 2953 310f58987d7b
permissions -rw-r--r--
(svn r4041) [Debian] Change next version number to 0.4.6 instead of 0.4.5.1.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2026
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2026
diff changeset
     2
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1662
diff changeset
     4
#include "openttd.h"
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     5
#include "queue.h"
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     6
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
     7
static void Stack_Clear(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     8
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     9
	uint i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    10
	if (free_values)
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    11
		for (i=0;i<q->data.stack.size;i++)
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    12
			free(q->data.stack.elements[i]);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    13
	q->data.stack.size = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    14
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    15
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    16
static void Stack_Free(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    17
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    18
	q->clear(q, free_values);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    19
	free(q->data.stack.elements);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    20
	if (q->freeq)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    21
		free(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    22
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    23
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    24
static bool Stack_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    25
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    26
	if (q->data.stack.size == q->data.stack.max_size)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    27
		return false;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    28
	q->data.stack.elements[q->data.stack.size++] = item;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    29
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    30
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    31
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    32
static void* Stack_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    33
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    34
	void* result;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    35
	if (q->data.stack.size == 0)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    36
		return NULL;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    37
	result = q->data.stack.elements[--q->data.stack.size];
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    38
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    39
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    40
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    41
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    42
static bool Stack_Delete(Queue* q, void* item, int priority)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    43
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    44
	return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    45
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    46
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    47
static Queue* init_stack(Queue* q, uint max_size)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    48
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    49
	q->push = Stack_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    50
	q->pop = Stack_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    51
	q->del = Stack_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    52
	q->clear = Stack_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    53
	q->free = Stack_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    54
	q->data.stack.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    55
	q->data.stack.size = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    56
	q->data.stack.elements = malloc(max_size * sizeof(void*));
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    57
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    58
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    59
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    60
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    61
Queue* new_Stack(uint max_size)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    62
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    63
	Queue* q = malloc(sizeof(Queue));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    64
	init_stack(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    65
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    66
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    67
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    68
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    69
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    70
 * Fifo
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    71
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    72
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    73
static void Fifo_Clear(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    74
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    75
	uint head, tail;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    76
	if (free_values) {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    77
		head = q->data.fifo.head;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    78
		tail = q->data.fifo.tail; /* cache for speed */
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    79
		while (head != tail) {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    80
			free(q->data.fifo.elements[tail]);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    81
			tail = (tail + 1) % q->data.fifo.max_size;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    82
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    83
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    84
	q->data.fifo.head = q->data.fifo.tail = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    85
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    86
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    87
static void Fifo_Free(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    88
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    89
	q->clear(q, free_values);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    90
	free(q->data.fifo.elements);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    91
	if (q->freeq)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    92
		free(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    93
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    94
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    95
static bool Fifo_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    96
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    97
	uint next = (q->data.fifo.head + 1) % q->data.fifo.max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    98
	if (next == q->data.fifo.tail)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    99
		return false;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   100
	q->data.fifo.elements[q->data.fifo.head] = item;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   101
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   102
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   103
	q->data.fifo.head = next;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   104
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   105
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   106
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   107
static void* Fifo_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   108
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   109
	void* result;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   110
	if (q->data.fifo.head == q->data.fifo.tail)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   111
		return NULL;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   112
	result = q->data.fifo.elements[q->data.fifo.tail];
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   113
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   114
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   115
	q->data.fifo.tail = (q->data.fifo.tail + 1) % q->data.fifo.max_size;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   116
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   117
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   118
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   119
static bool Fifo_Delete(Queue* q, void* item, int priority)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   120
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   121
	return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   122
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   123
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   124
static Queue* init_fifo(Queue* q, uint max_size)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   125
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   126
	q->push = Fifo_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   127
	q->pop = Fifo_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   128
	q->del = Fifo_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   129
	q->clear = Fifo_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   130
	q->free = Fifo_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   131
	q->data.fifo.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   132
	q->data.fifo.head = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   133
	q->data.fifo.tail = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   134
	q->data.fifo.elements = malloc(max_size * sizeof(void*));
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   135
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   136
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   137
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   138
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   139
Queue* new_Fifo(uint max_size)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   140
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   141
	Queue* q = malloc(sizeof(Queue));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   142
	init_fifo(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   143
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   144
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   145
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   146
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   147
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   148
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   149
 * Insertion Sorter
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   150
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   151
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   152
static void InsSort_Clear(Queue* q, bool free_values)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   153
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   154
	InsSortNode* node = q->data.inssort.first;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   155
	InsSortNode* prev;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   156
	while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   157
		if (free_values)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   158
			free(node->item);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   159
		prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   160
		node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   161
		free(prev);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 107
diff changeset
   162
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   163
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   164
	q->data.inssort.first = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   165
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   166
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   167
static void InsSort_Free(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   168
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   169
	q->clear(q, free_values);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   170
	if (q->freeq)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   171
		free(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   172
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   173
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   174
static bool InsSort_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   175
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   176
	InsSortNode* newnode = malloc(sizeof(InsSortNode));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   177
	if (newnode == NULL) return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   178
	newnode->item = item;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   179
	newnode->priority = priority;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   180
	if (q->data.inssort.first == NULL || q->data.inssort.first->priority >= priority) {
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   181
		newnode->next = q->data.inssort.first;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   182
		q->data.inssort.first = newnode;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   183
	} else {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   184
		InsSortNode* node = q->data.inssort.first;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   185
		while( node != NULL ) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   186
			if (node->next == NULL || node->next->priority >= priority) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   187
				newnode->next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   188
				node->next = newnode;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   189
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   190
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   191
			node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   192
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   193
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   194
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   195
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   196
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   197
static void* InsSort_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   198
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   199
	InsSortNode* node = q->data.inssort.first;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   200
	void* result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   201
	if (node == NULL)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   202
		return NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   203
	result = node->item;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   204
	q->data.inssort.first = q->data.inssort.first->next;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   205
	if (q->data.inssort.first)
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   206
		assert(q->data.inssort.first->priority >= node->priority);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   207
	free(node);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   208
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   209
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   210
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   211
static bool InsSort_Delete(Queue* q, void* item, int priority)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   212
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   213
	return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   214
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   215
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   216
void init_InsSort(Queue* q) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   217
	q->push = InsSort_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   218
	q->pop = InsSort_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   219
	q->del = InsSort_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   220
	q->clear = InsSort_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   221
	q->free = InsSort_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   222
	q->data.inssort.first = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   223
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   224
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   225
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 826
diff changeset
   226
Queue* new_InsSort(void)
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 826
diff changeset
   227
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   228
	Queue* q = malloc(sizeof(Queue));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   229
	init_InsSort(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   230
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   231
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   232
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   233
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   234
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   235
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   236
 * Binary Heap
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   237
 * For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   238
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   239
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   240
#define BINARY_HEAP_BLOCKSIZE (1 << BINARY_HEAP_BLOCKSIZE_BITS)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   241
#define BINARY_HEAP_BLOCKSIZE_MASK (BINARY_HEAP_BLOCKSIZE-1)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   242
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   243
// To make our life easy, we make the next define
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   244
//  Because Binary Heaps works with array from 1 to n,
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   245
//  and C with array from 0 to n-1, and we don't like typing
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   246
//  q->data.binaryheap.elements[i-1] every time, we use this define.
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   247
#define BIN_HEAP_ARR(i) q->data.binaryheap.elements[((i)-1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i)-1) & BINARY_HEAP_BLOCKSIZE_MASK]
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   248
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   249
static void BinaryHeap_Clear(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   250
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   251
	/* Free all items if needed and free all but the first blocks of
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   252
	 * memory */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   253
	uint i,j;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   254
	for (i=0;i<q->data.binaryheap.blocks;i++) {
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   255
		if (q->data.binaryheap.elements[i] == NULL) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   256
			/* No more allocated blocks */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   257
			break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   258
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   259
		/* For every allocated block */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   260
		if (free_values)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   261
			for (j=0;j<(1<<BINARY_HEAP_BLOCKSIZE_BITS);j++) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   262
				/* For every element in the block */
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   263
				if ((q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS) == i
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   264
					&& (q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == j)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   265
					break; /* We're past the last element */
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   266
				free(q->data.binaryheap.elements[i][j].item);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   267
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   268
		if (i != 0) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   269
			/* Leave the first block of memory alone */
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   270
			free(q->data.binaryheap.elements[i]);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   271
			q->data.binaryheap.elements[i] = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   272
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   273
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   274
	q->data.binaryheap.size = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   275
	q->data.binaryheap.blocks = 1;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   276
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   277
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   278
static void BinaryHeap_Free(Queue* q, bool free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   279
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   280
	uint i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   281
	q->clear(q, free_values);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   282
	for (i=0;i<q->data.binaryheap.blocks;i++) {
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   283
		if (q->data.binaryheap.elements[i] == NULL)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   284
			break;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   285
		free(q->data.binaryheap.elements[i]);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   286
	}
2799
542aae5b7805 (svn r3347) Plug a memory leak (Found by Valgrind using Truelight ... or was it the other way round?)
tron
parents: 2752
diff changeset
   287
	free(q->data.binaryheap.elements);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   288
	if (q->freeq)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   289
		free(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   290
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   291
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   292
static bool BinaryHeap_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   293
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   294
	#ifdef QUEUE_DEBUG
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   295
			printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->data.binaryheap.size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   296
	#endif
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   297
	if (q->data.binaryheap.size == q->data.binaryheap.max_size)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   298
		return false;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   299
	assert(q->data.binaryheap.size < q->data.binaryheap.max_size);
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 107
diff changeset
   300
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   301
	if (q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   302
		/* The currently allocated blocks are full, allocate a new one */
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   303
		assert((q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   304
		q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] = malloc(BINARY_HEAP_BLOCKSIZE * sizeof(BinaryHeapNode));
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   305
		q->data.binaryheap.blocks++;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   306
#ifdef QUEUE_DEBUG
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   307
		printf("[BinaryHeap] Increasing size of elements to %d nodes\n",q->data.binaryheap.blocks *  BINARY_HEAP_BLOCKSIZE);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   308
#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   309
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   310
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   311
	// Add the item at the end of the array
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   312
	BIN_HEAP_ARR(q->data.binaryheap.size+1).priority = priority;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   313
	BIN_HEAP_ARR(q->data.binaryheap.size+1).item = item;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   314
	q->data.binaryheap.size++;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   315
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   316
	// Now we are going to check where it belongs. As long as the parent is
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   317
	// bigger, we switch with the parent
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   318
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   319
		int i, j;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   320
		BinaryHeapNode temp;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   321
		i = q->data.binaryheap.size;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   322
		while (i > 1) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   323
			// Get the parent of this object (divide by 2)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   324
			j = i / 2;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   325
			// Is the parent bigger then the current, switch them
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   326
			if (BIN_HEAP_ARR(i).priority <= BIN_HEAP_ARR(j).priority) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   327
				temp = BIN_HEAP_ARR(j);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   328
				BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   329
				BIN_HEAP_ARR(i) = temp;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   330
				i = j;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   331
			} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   332
				// It is not, we're done!
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   333
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   334
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   335
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   336
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   337
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   338
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   339
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   340
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   341
static bool BinaryHeap_Delete(Queue* q, void* item, int priority)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   342
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   343
	#ifdef QUEUE_DEBUG
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   344
			printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->data.binaryheap.size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   345
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   346
	uint i = 0;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   347
	// First, we try to find the item..
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   348
	do {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   349
		if (BIN_HEAP_ARR(i+1).item == item) break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   350
		i++;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   351
	} while (i < q->data.binaryheap.size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   352
	// We did not find the item, so we return false
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   353
	if (i == q->data.binaryheap.size) return false;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   354
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   355
	// Now we put the last item over the current item while decreasing the size of the elements
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   356
	q->data.binaryheap.size--;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   357
	BIN_HEAP_ARR(i+1) = BIN_HEAP_ARR(q->data.binaryheap.size+1);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   358
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   359
	// Now the only thing we have to do, is resort it..
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   360
	// On place i there is the item to be sorted.. let's start there
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   361
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   362
		uint j;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   363
		BinaryHeapNode temp;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   364
		// Because of the fast that Binary Heap uses array from 1 to n, we need to increase
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   365
		//   i with 1
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   366
		i++;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   367
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   368
		for (;;) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   369
			j = i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   370
			// Check if we have 2 childs
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   371
			if (2*j+1 <= q->data.binaryheap.size) {
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 201
diff changeset
   372
				// Is this child smaller than the parent?
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   373
				if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2*j).priority) {i = 2*j; }
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   374
				// Yes, we _need_ to use i here, not j, because we want to have the smallest child
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   375
				//  This way we get that straight away!
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   376
				if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2*j+1).priority) { i = 2*j+1; }
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   377
			// Do we have one child?
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   378
			} else if (2*j <= q->data.binaryheap.size) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   379
				if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2*j).priority) { i = 2*j; }
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   380
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   381
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 201
diff changeset
   382
			// One of our childs is smaller than we are, switch
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   383
			if (i != j) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   384
				temp = BIN_HEAP_ARR(j);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   385
				BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   386
				BIN_HEAP_ARR(i) = temp;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   387
			} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   388
				// None of our childs is smaller, so we stay here.. stop :)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   389
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   390
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   391
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   392
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   393
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   394
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   395
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   396
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   397
static void* BinaryHeap_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   398
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   399
	#ifdef QUEUE_DEBUG
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   400
			printf("[BinaryHeap] Popping an element. There are %d elements left\n", q->data.binaryheap.size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   401
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   402
	void* result;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   403
	if (q->data.binaryheap.size == 0)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   404
		return NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   405
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   406
	// The best item is always on top, so give that as result
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   407
	result = BIN_HEAP_ARR(1).item;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   408
	// And now we should get ride of this item...
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   409
	BinaryHeap_Delete(q,BIN_HEAP_ARR(1).item, BIN_HEAP_ARR(1).priority);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   410
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   411
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   412
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   413
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   414
void init_BinaryHeap(Queue* q, uint max_size)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   415
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   416
	assert(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   417
	q->push = BinaryHeap_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   418
	q->pop = BinaryHeap_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   419
	q->del = BinaryHeap_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   420
	q->clear = BinaryHeap_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   421
	q->free = BinaryHeap_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   422
	q->data.binaryheap.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   423
	q->data.binaryheap.size = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   424
	// We malloc memory in block of BINARY_HEAP_BLOCKSIZE
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   425
	//   It autosizes when it runs out of memory
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1095
diff changeset
   426
	q->data.binaryheap.elements = calloc(1, ((max_size - 1) / BINARY_HEAP_BLOCKSIZE*sizeof(BinaryHeapNode)) + 1);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   427
	q->data.binaryheap.elements[0] = malloc(BINARY_HEAP_BLOCKSIZE * sizeof(BinaryHeapNode));
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   428
	q->data.binaryheap.blocks = 1;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   429
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   430
#ifdef QUEUE_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   431
		printf("[BinaryHeap] Initial size of elements is %d nodes\n",(1024));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   432
#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   433
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   434
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   435
Queue* new_BinaryHeap(uint max_size) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   436
	Queue* q = malloc(sizeof(Queue));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   437
	init_BinaryHeap(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   438
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   439
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   440
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   441
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   442
// Because we don't want anyone else to bother with our defines
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   443
#undef BIN_HEAP_ARR
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   444
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   445
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   446
 * Hash
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   447
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   448
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   449
void init_Hash(Hash* h, Hash_HashProc* hash, uint num_buckets) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   450
	/* Allocate space for the Hash, the buckets and the bucket flags */
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   451
	uint i;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   452
	assert(h);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   453
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   454
	debug("Allocated hash: %p", h);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   455
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   456
	h->hash = hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   457
	h->size = 0;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   458
	h->num_buckets = num_buckets;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   459
	h->buckets = malloc(num_buckets * (sizeof(HashNode) + sizeof(bool)));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   460
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   461
	debug("Buckets = %p", h->buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   462
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   463
	h->buckets_in_use = (bool*)(h->buckets + num_buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   464
	h->freeh = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   465
	for (i=0;i<num_buckets;i++)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   466
		h->buckets_in_use[i] = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   467
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   468
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   469
Hash* new_Hash(Hash_HashProc* hash, int num_buckets) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   470
	Hash* h = malloc(sizeof(Hash));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   471
	init_Hash(h, hash, num_buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   472
	h->freeh = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   473
	return h;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   474
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   475
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   476
void delete_Hash(Hash* h, bool free_values) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   477
	uint i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   478
	/* Iterate all buckets */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   479
	for (i=0;i<h->num_buckets;i++)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   480
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   481
		if (h->buckets_in_use[i]) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   482
			HashNode* node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   483
			/* Free the first value */
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 107
diff changeset
   484
			if (free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   485
				free(h->buckets[i].value);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   486
			node = h->buckets[i].next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   487
			while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   488
				HashNode* prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   489
				node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   490
				/* Free the value */
201
c40d343115f8 (svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents: 107
diff changeset
   491
				if (free_values)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   492
					free(prev->value);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   493
				/* Free the node */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   494
				free(prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   495
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   496
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   497
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   498
	free(h->buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   499
	/* No need to free buckets_in_use, it is always allocated in one
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   500
	 * malloc with buckets */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   501
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   502
	debug("Freeing Hash: %p", h);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   503
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   504
	if (h->freeh)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   505
		free(h);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   506
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   507
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2186
diff changeset
   508
#ifdef HASH_STATS
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2186
diff changeset
   509
static void stat_Hash(Hash* h)
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   510
{
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   511
	uint used_buckets = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   512
	uint max_collision = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   513
	uint max_usage = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   514
	uint usage[200];
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   515
	uint i;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   516
	uint collision;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   517
	HashNode* node;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   518
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   519
	for (i=0;i<200;i++) usage[i] = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   520
	for (i=0;i<h->num_buckets;i++) {
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   521
		collision = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   522
		if (h->buckets_in_use[i]) {
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   523
			used_buckets++;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   524
			node = &h->buckets[i];
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   525
			while (node != NULL) {
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   526
				collision++;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   527
				node = node->next;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   528
			}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   529
			if (collision > max_collision) max_collision = collision;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   530
		}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   531
		if (collision > 199) collision = 199;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   532
		usage[collision]++;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   533
		if (collision >0 && usage[collision] >= max_usage) max_usage = usage[collision];
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   534
	}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   535
	printf("---\nHash size: %d\nNodes used: %d\nNon empty buckets: %d\nMax collision: %d\n", h->num_buckets, h->size, used_buckets, max_collision);
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   536
	printf("{ ");
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   537
	for (i=0;i<=max_collision;i++)
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   538
		if (usage[i]) {
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   539
			printf("%d:%d ", i, usage[i]);
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   540
/*
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1891
diff changeset
   541
			if (i>0){
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   542
				uint j;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   543
				for (j=0;j<(usage[i] * 160 / 800);j++)
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   544
					printf("#");
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   545
			}
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   546
			printf("\n");
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   547
			*/
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   548
		}
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   549
	printf ("}\n");
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   550
}
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2186
diff changeset
   551
#endif
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   552
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   553
void clear_Hash(Hash* h, bool free_values)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   554
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   555
	uint i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   556
	HashNode* node;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   557
#ifdef HASH_STATS
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   558
	if (h->size > 2000)
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   559
		stat_Hash(h);
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   560
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   561
	/* Iterate all buckets */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   562
	for (i=0;i<h->num_buckets;i++)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   563
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   564
		if (h->buckets_in_use[i]) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   565
			h->buckets_in_use[i] = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   566
			/* Free the first value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   567
			if (free_values)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   568
				free(h->buckets[i].value);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   569
			node = h->buckets[i].next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   570
			while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   571
				HashNode* prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   572
				node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   573
				if (free_values)
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1891
diff changeset
   574
					free(prev->value);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   575
				free(prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   576
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   577
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   578
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   579
	h->size = 0;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   580
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   581
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   582
/* Finds the node that that saves this key pair. If it is not
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   583
 * found, returns NULL. If it is found, *prev is set to the
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   584
 * node before the one found, or if the node found was the first in the bucket
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   585
 * to NULL. If it is not found, *prev is set to the last HashNode in the
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   586
 * bucket, or NULL if it is empty. prev can also be NULL, in which case it is
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   587
 * not used for output.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   588
 */
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   589
static HashNode* Hash_FindNode(Hash* h, uint key1, uint key2, HashNode** prev_out)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   590
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   591
	uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   592
	HashNode* result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   593
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   594
	debug("Looking for %u, %u", key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   595
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   596
	/* Check if the bucket is empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   597
	if (!h->buckets_in_use[hash]) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   598
		if (prev_out)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   599
			*prev_out = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   600
		result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   601
	/* Check the first node specially */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   602
	} else if (h->buckets[hash].key1 == key1 && h->buckets[hash].key2 == key2) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   603
		/* Save the value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   604
		result = h->buckets + hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   605
		if (prev_out)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   606
			*prev_out = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   607
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   608
		debug("Found in first node: %p", result);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   609
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   610
	/* Check all other nodes */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   611
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   612
		HashNode* prev = h->buckets + hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   613
		HashNode* node = prev->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   614
		while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   615
			if (node->key1 == key1 && node->key2 == key2) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   616
				/* Found it */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   617
				result = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   618
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   619
				debug("Found in other node: %p", result);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   620
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   621
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   622
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   623
			prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   624
			node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   625
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   626
		if (prev_out)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   627
			*prev_out = prev;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   628
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   629
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   630
	if (result == NULL)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   631
		debug("Not found");
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   632
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   633
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   634
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   635
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   636
void* Hash_Delete(Hash* h, uint key1, uint key2) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   637
	void* result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   638
	HashNode* prev; /* Used as output var for below function call */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   639
	HashNode* node = Hash_FindNode(h, key1, key2, &prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   640
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   641
	if (node == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   642
		/* not found */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   643
		result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   644
	} else if (prev == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   645
		/* It is in the first node, we can't free that one, so we free
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   646
		 * the next one instead (if there is any)*/
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   647
		/* Save the value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   648
		result = node->value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   649
		if (node->next != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   650
			HashNode* next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   651
			/* Copy the second to the first */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   652
			*node = *next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   653
			/* Free the second */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   654
		#ifndef NOFREE
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   655
			free(next);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   656
		#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   657
		} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   658
			/* This was the last in this bucket */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   659
			/* Mark it as empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   660
			uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   661
			h->buckets_in_use[hash] = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   662
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   663
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   664
		/* It is in another node */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   665
		/* Save the value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   666
		result = node->value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   667
		/* Link previous and next nodes */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   668
		prev->next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   669
		/* Free the node */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   670
		#ifndef NOFREE
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   671
		free(node);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   672
		#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   673
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   674
	if (result != NULL)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   675
		h->size--;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   676
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   677
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   678
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   679
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   680
void* Hash_Set(Hash* h, uint key1, uint key2, void* value) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   681
	HashNode* prev;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   682
	HashNode* node = Hash_FindNode(h, key1, key2, &prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   683
	void* result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   684
	if (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   685
		/* Found it */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   686
		result = node->value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   687
		node->value = value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   688
		return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   689
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   690
	/* It is not yet present, let's add it */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   691
	if (prev == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   692
		/* The bucket is still empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   693
		uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   694
		h->buckets_in_use[hash] = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   695
		node = h->buckets + hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   696
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   697
		/* Add it after prev */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   698
		node = malloc(sizeof(HashNode));
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   699
		prev->next = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   700
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   701
	node->next = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   702
	node->key1 = key1;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   703
	node->key2 = key2;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   704
	node->value = value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   705
	h->size++;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   706
	return NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   707
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   708
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   709
void* Hash_Get(Hash* h, uint key1, uint key2) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   710
	HashNode* node = Hash_FindNode(h, key1, key2, NULL);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   711
	#ifdef HASH_DEBUG
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   712
	debug("Found node: %p", node);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   713
	#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   714
	if (node == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   715
		/* Node not found */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   716
		return NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   717
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   718
		return node->value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   719
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   720
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   721
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   722
uint Hash_Size(Hash* h) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   723
    return h->size;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   724
}