src/queue.cpp
author celestar
Mon, 15 Jan 2007 20:14:06 +0000
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5860 7fdc9b423ba1
permissions -rw-r--r--
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
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"
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
     6
#include "helpers.hpp"
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
     7
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
     8
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
     9
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    10
	if (free_values) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    11
		uint i;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    12
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    13
		for (i = 0; i < q->data.stack.size; i++) free(q->data.stack.elements[i]);
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    14
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    15
	q->data.stack.size = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    16
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    17
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    18
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
    19
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    20
	q->clear(q, free_values);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    21
	free(q->data.stack.elements);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    22
	if (q->freeq) free(q);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    23
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    24
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    25
static bool Stack_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    26
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    27
	if (q->data.stack.size == q->data.stack.max_size) 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
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    34
	if (q->data.stack.size == 0) return NULL;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    35
	return 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
    36
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    37
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    38
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
    39
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    40
	return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    41
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    42
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    43
static Queue* init_stack(Queue* q, uint max_size)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    44
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    45
	q->push = Stack_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    46
	q->pop = Stack_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    47
	q->del = Stack_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    48
	q->clear = Stack_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    49
	q->free = Stack_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    50
	q->data.stack.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    51
	q->data.stack.size = 0;
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
    52
	MallocT(&q->data.stack.elements, max_size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    53
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    54
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    55
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    56
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    57
Queue* new_Stack(uint max_size)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    58
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
    59
	Queue* q;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
    60
	MallocT(&q, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    61
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    62
	init_stack(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    63
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    64
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    65
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    66
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
 * Fifo
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
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    71
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
    72
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    73
	if (free_values) {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    74
		uint head = q->data.fifo.head;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    75
		uint tail = q->data.fifo.tail; /* cache for speed */
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    76
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    77
		while (head != tail) {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    78
			free(q->data.fifo.elements[tail]);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    79
			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
    80
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    81
	}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    82
	q->data.fifo.head = 0;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    83
	q->data.fifo.tail = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    84
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    85
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    86
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
    87
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    88
	q->clear(q, free_values);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    89
	free(q->data.fifo.elements);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    90
	if (q->freeq) free(q);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    91
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
    92
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    93
static bool Fifo_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
    94
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    95
	uint next = (q->data.fifo.head + 1) % q->data.fifo.max_size;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    96
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
    97
	if (next == q->data.fifo.tail) return false;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
    98
	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
    99
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   100
	q->data.fifo.head = next;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   101
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   102
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   103
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   104
static void* Fifo_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   105
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   106
	void* result;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   107
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   108
	if (q->data.fifo.head == q->data.fifo.tail) return NULL;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   109
	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
   110
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   111
	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
   112
	return result;
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
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   115
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
   116
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   117
	return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   118
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   119
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   120
static Queue* init_fifo(Queue* q, uint max_size)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   121
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   122
	q->push = Fifo_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   123
	q->pop = Fifo_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   124
	q->del = Fifo_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   125
	q->clear = Fifo_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   126
	q->free = Fifo_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   127
	q->data.fifo.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   128
	q->data.fifo.head = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   129
	q->data.fifo.tail = 0;
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   130
	MallocT(&q->data.fifo.elements, max_size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   131
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   132
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   133
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   134
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   135
Queue* new_Fifo(uint max_size)
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   136
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   137
	Queue* q;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   138
	MallocT(&q, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   139
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   140
	init_fifo(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   141
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   142
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   143
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   144
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
 * Insertion Sorter
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
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   150
static void InsSort_Clear(Queue* q, bool free_values)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   151
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   152
	InsSortNode* node = q->data.inssort.first;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   153
	InsSortNode* prev;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   154
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   155
	while (node != NULL) {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   156
		if (free_values) free(node->item);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   157
		prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   158
		node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   159
		free(prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   160
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   161
	q->data.inssort.first = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   162
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   163
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   164
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
   165
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   166
	q->clear(q, free_values);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   167
	if (q->freeq) free(q);
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
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   170
static bool InsSort_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   171
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   172
	InsSortNode* newnode;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   173
	MallocT(&newnode, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   174
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   175
	if (newnode == NULL) return false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   176
	newnode->item = item;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   177
	newnode->priority = priority;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   178
	if (q->data.inssort.first == NULL ||
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   179
			q->data.inssort.first->priority >= priority) {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   180
		newnode->next = q->data.inssort.first;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   181
		q->data.inssort.first = newnode;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   182
	} else {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   183
		InsSortNode* node = q->data.inssort.first;
2953
310f58987d7b (svn r3512) Yet more whitespace fixes (mostly by Rubidium)
peter1138
parents: 2799
diff changeset
   184
		while (node != NULL) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   185
			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
   186
				newnode->next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   187
				node->next = newnode;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   188
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   189
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   190
			node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   191
		}
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
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   194
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   195
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   196
static void* InsSort_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   197
{
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   198
	InsSortNode* node = q->data.inssort.first;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   199
	void* result;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   200
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   201
	if (node == NULL) return NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   202
	result = node->item;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   203
	q->data.inssort.first = q->data.inssort.first->next;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   204
	assert(q->data.inssort.first == NULL || 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
   205
	free(node);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   206
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   207
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   208
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   209
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
   210
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   211
	return false;
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
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   214
void init_InsSort(Queue* q)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   215
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   216
	q->push = InsSort_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   217
	q->pop = InsSort_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   218
	q->del = InsSort_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   219
	q->clear = InsSort_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   220
	q->free = InsSort_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   221
	q->data.inssort.first = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   222
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   223
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   224
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
   225
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
   226
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   227
	Queue* q;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   228
	MallocT(&q, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   229
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   230
	init_InsSort(q);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   231
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   232
	return q;
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
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   237
 * Binary Heap
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   238
 * 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
   239
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   240
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   241
#define BINARY_HEAP_BLOCKSIZE (1 << BINARY_HEAP_BLOCKSIZE_BITS)
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   242
#define BINARY_HEAP_BLOCKSIZE_MASK (BINARY_HEAP_BLOCKSIZE - 1)
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   243
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   244
// 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
   245
//  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
   246
//  and C with array from 0 to n-1, and we don't like typing
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   247
//  q->data.binaryheap.elements[i - 1] every time, we use this define.
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   248
#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
   249
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   250
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
   251
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   252
	/* Free all items if needed and free all but the first blocks of memory */
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   253
	uint i;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   254
	uint j;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   255
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   256
	for (i = 0; i < q->data.binaryheap.blocks; i++) {
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   257
		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
   258
			/* No more allocated blocks */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   259
			break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   260
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   261
		/* For every allocated block */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   262
		if (free_values) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   263
			for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   264
				/* For every element in the block */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   265
				if ((q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   266
						(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
   267
					break; /* We're past the last element */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   268
				}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   269
				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
   270
			}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   271
		}
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   272
		if (i != 0) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   273
			/* Leave the first block of memory alone */
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   274
			free(q->data.binaryheap.elements[i]);
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   275
			q->data.binaryheap.elements[i] = NULL;
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
	}
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   278
	q->data.binaryheap.size = 0;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   279
	q->data.binaryheap.blocks = 1;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   280
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   281
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   282
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
   283
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   284
	uint i;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   285
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   286
	q->clear(q, free_values);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   287
	for (i = 0; i < q->data.binaryheap.blocks; i++) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   288
		if (q->data.binaryheap.elements[i] == NULL) break;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   289
		free(q->data.binaryheap.elements[i]);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   290
	}
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
   291
	free(q->data.binaryheap.elements);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   292
	if (q->freeq) free(q);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   293
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   294
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   295
static bool BinaryHeap_Push(Queue* q, void* item, int priority)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   296
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   297
#ifdef QUEUE_DEBUG
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   298
	printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->data.binaryheap.size);
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   299
#endif
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   300
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   301
	if (q->data.binaryheap.size == q->data.binaryheap.max_size) return false;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   302
	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
   303
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   304
	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
   305
		/* 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
   306
		assert((q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   307
		MallocT(&q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS], BINARY_HEAP_BLOCKSIZE);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   308
		q->data.binaryheap.blocks++;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   309
#ifdef QUEUE_DEBUG
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   310
		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
   311
#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   312
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   313
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   314
	// Add the item at the end of the array
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   315
	BIN_HEAP_ARR(q->data.binaryheap.size + 1).priority = priority;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   316
	BIN_HEAP_ARR(q->data.binaryheap.size + 1).item = item;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   317
	q->data.binaryheap.size++;
84
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
	// 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
   320
	// bigger, we switch with the parent
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   321
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   322
		BinaryHeapNode temp;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   323
		int i;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   324
		int j;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   325
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   326
		i = q->data.binaryheap.size;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   327
		while (i > 1) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   328
			// 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
   329
			j = i / 2;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   330
			// 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
   331
			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
   332
				temp = BIN_HEAP_ARR(j);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   333
				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
   334
				BIN_HEAP_ARR(i) = temp;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   335
				i = j;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   336
			} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   337
				// It is not, we're done!
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   338
				break;
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
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   341
	}
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
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   344
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   345
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   346
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
   347
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   348
	uint i = 0;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   349
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   350
#ifdef QUEUE_DEBUG
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   351
	printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->data.binaryheap.size);
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   352
#endif
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   353
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   354
	// First, we try to find the item..
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   355
	do {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   356
		if (BIN_HEAP_ARR(i + 1).item == item) break;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   357
		i++;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   358
	} while (i < q->data.binaryheap.size);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   359
	// 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
   360
	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
   361
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   362
	// 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
   363
	q->data.binaryheap.size--;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   364
	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
   365
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   366
	// 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
   367
	// 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
   368
	{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   369
		uint j;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   370
		BinaryHeapNode temp;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   371
		/* Because of the fact that Binary Heap uses array from 1 to n, we need to
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   372
		 * increase i by 1
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   373
		 */
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   374
		i++;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   375
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   376
		for (;;) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   377
			j = i;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   378
			// Check if we have 2 childs
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   379
			if (2 * j + 1 <= q->data.binaryheap.size) {
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 201
diff changeset
   380
				// Is this child smaller than the parent?
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   381
				if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   382
				// 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
   383
				//  This way we get that straight away!
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   384
				if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   385
			// Do we have one child?
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   386
			} else if (2 * j <= q->data.binaryheap.size) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   387
				if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   388
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   389
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 201
diff changeset
   390
			// 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
   391
			if (i != j) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   392
				temp = BIN_HEAP_ARR(j);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   393
				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
   394
				BIN_HEAP_ARR(i) = temp;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   395
			} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   396
				// 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
   397
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   398
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   399
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   400
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   401
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   402
	return true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   403
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   404
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   405
static void* BinaryHeap_Pop(Queue* q)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   406
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   407
	void* result;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   408
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   409
#ifdef QUEUE_DEBUG
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   410
	printf("[BinaryHeap] Popping an element. There are %d elements left\n", q->data.binaryheap.size);
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   411
#endif
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   412
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   413
	if (q->data.binaryheap.size == 0) return NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   414
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   415
	// 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
   416
	result = BIN_HEAP_ARR(1).item;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   417
	// And now we should get rid of this item...
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   418
	BinaryHeap_Delete(q, BIN_HEAP_ARR(1).item, BIN_HEAP_ARR(1).priority);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   419
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   420
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   421
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   422
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   423
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
   424
{
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   425
	assert(q != NULL);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   426
	q->push = BinaryHeap_Push;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   427
	q->pop = BinaryHeap_Pop;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   428
	q->del = BinaryHeap_Delete;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   429
	q->clear = BinaryHeap_Clear;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   430
	q->free = BinaryHeap_Free;
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   431
	q->data.binaryheap.max_size = max_size;
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   432
	q->data.binaryheap.size = 0;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   433
	// 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
   434
	//   It autosizes when it runs out of memory
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   435
	CallocT(&q->data.binaryheap.elements, (max_size - 1) / BINARY_HEAP_BLOCKSIZE + 1);
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   436
	MallocT(&q->data.binaryheap.elements[0], BINARY_HEAP_BLOCKSIZE);
107
c72381c27546 (svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents: 84
diff changeset
   437
	q->data.binaryheap.blocks = 1;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   438
	q->freeq = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   439
#ifdef QUEUE_DEBUG
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   440
	printf("[BinaryHeap] Initial size of elements is %d nodes\n", BINARY_HEAP_BLOCKSIZE);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   441
#endif
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   442
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   443
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   444
Queue* new_BinaryHeap(uint max_size)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   445
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   446
	Queue* q;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   447
	MallocT(&q, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   448
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   449
	init_BinaryHeap(q, max_size);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   450
	q->freeq = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   451
	return q;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   452
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   453
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   454
// 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
   455
#undef BIN_HEAP_ARR
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   456
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   457
/*
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   458
 * Hash
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   459
 */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   460
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   461
void init_Hash(Hash* h, Hash_HashProc* hash, uint num_buckets)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   462
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   463
	/* 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
   464
	uint i;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   465
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   466
	assert(h != NULL);
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   467
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   468
	debug("Allocated hash: %p", h);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   469
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   470
	h->hash = hash;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   471
	h->size = 0;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   472
	h->num_buckets = num_buckets;
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   473
	h->buckets = (HashNode*)malloc(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use)));
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   474
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   475
	debug("Buckets = %p", h->buckets);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   476
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   477
	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
   478
	h->freeh = false;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   479
	for (i = 0; i < num_buckets; i++) h->buckets_in_use[i] = false;
84
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
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   482
Hash* new_Hash(Hash_HashProc* hash, int num_buckets)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   483
{
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   484
	Hash* h;
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   485
	MallocT(&h, 1);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   486
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   487
	init_Hash(h, hash, num_buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   488
	h->freeh = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   489
	return h;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   490
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   491
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   492
void delete_Hash(Hash* h, bool free_values)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   493
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   494
	uint i;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   495
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   496
	/* Iterate all buckets */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   497
	for (i = 0; i < h->num_buckets; i++) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   498
		if (h->buckets_in_use[i]) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   499
			HashNode* node;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   500
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   501
			/* Free the first value */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   502
			if (free_values) free(h->buckets[i].value);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   503
			node = h->buckets[i].next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   504
			while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   505
				HashNode* prev = node;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   506
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   507
				node = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   508
				/* Free the value */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   509
				if (free_values) free(prev->value);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   510
				/* Free the node */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   511
				free(prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   512
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   513
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   514
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   515
	free(h->buckets);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   516
	/* 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
   517
	 * malloc with buckets */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   518
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   519
	debug("Freeing Hash: %p", h);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   520
#endif
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   521
	if (h->freeh) free(h);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   522
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   523
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2186
diff changeset
   524
#ifdef HASH_STATS
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   525
static void stat_Hash(const Hash* h)
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   526
{
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   527
	uint used_buckets = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   528
	uint max_collision = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   529
	uint max_usage = 0;
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   530
	uint usage[200];
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   531
	uint i;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   532
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   533
	for (i = 0; i < lengthof(usage); i++) usage[i] = 0;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   534
	for (i = 0; i < h->num_buckets; i++) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   535
		uint collision = 0;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   536
		if (h->buckets_in_use[i]) {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   537
			const HashNode* node;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   538
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   539
			used_buckets++;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   540
			for (node = &h->buckets[i]; node != NULL; node = node->next) collision++;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   541
			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
   542
		}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   543
		if (collision >= lengthof(usage)) collision = lengthof(usage) - 1;
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   544
		usage[collision]++;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   545
		if (collision > 0 && usage[collision] >= max_usage) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   546
			max_usage = usage[collision];
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   547
		}
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   548
	}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   549
	printf(
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   550
		"---\n"
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   551
		"Hash size: %d\n"
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   552
		"Nodes used: %d\n"
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   553
		"Non empty buckets: %d\n"
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   554
		"Max collision: %d\n",
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   555
		h->num_buckets, h->size, used_buckets, max_collision
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   556
	);
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   557
	printf("{ ");
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   558
	for (i = 0; i <= max_collision; i++) {
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   559
		if (usage[i] > 0) {
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   560
			printf("%d:%d ", i, usage[i]);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   561
#if 0
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   562
			if (i > 0) {
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   563
				uint j;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   564
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   565
				for (j = 0; j < usage[i] * 160 / 800; j++) putchar('#');
1662
9f8c03dbc1bd (svn r2166) Fixed two warnings in the last commit.
matthijs
parents: 1661
diff changeset
   566
			}
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   567
			printf("\n");
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   568
#endif
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   569
		}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   570
	}
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   571
	printf ("}\n");
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   572
}
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2186
diff changeset
   573
#endif
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   574
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   575
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
   576
{
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   577
	uint i;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   578
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   579
#ifdef HASH_STATS
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   580
	if (h->size > 2000) stat_Hash(h);
1661
6af0c4416154 (svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents: 1247
diff changeset
   581
#endif
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   582
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   583
	/* Iterate all buckets */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   584
	for (i = 0; i < h->num_buckets; i++) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   585
		if (h->buckets_in_use[i]) {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   586
			HashNode* node;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   587
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   588
			h->buckets_in_use[i] = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   589
			/* Free the first value */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   590
			if (free_values) free(h->buckets[i].value);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   591
			node = h->buckets[i].next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   592
			while (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   593
				HashNode* prev = node;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   594
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   595
				node = node->next;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   596
				if (free_values) free(prev->value);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   597
				free(prev);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   598
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   599
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   600
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   601
	h->size = 0;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   602
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   603
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   604
/* 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
   605
 * 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
   606
 * 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
   607
 * 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
   608
 * 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
   609
 * not used for output.
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   610
 */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   611
static HashNode* Hash_FindNode(const Hash* h, uint key1, uint key2, HashNode** prev_out)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   612
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   613
	uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   614
	HashNode* result = NULL;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   615
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   616
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   617
	debug("Looking for %u, %u", key1, key2);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   618
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   619
	/* Check if the bucket is empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   620
	if (!h->buckets_in_use[hash]) {
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   621
		if (prev_out != NULL) *prev_out = NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   622
		result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   623
	/* Check the first node specially */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   624
	} 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
   625
		/* Save the value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   626
		result = h->buckets + hash;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   627
		if (prev_out != NULL) *prev_out = NULL;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   628
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   629
		debug("Found in first node: %p", result);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   630
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   631
	/* Check all other nodes */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   632
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   633
		HashNode* prev = h->buckets + hash;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   634
		HashNode* node;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   635
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   636
		for (node = prev->next; node != NULL; node = node->next) {
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   637
			if (node->key1 == key1 && node->key2 == key2) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   638
				/* Found it */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   639
				result = node;
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   640
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   641
				debug("Found in other node: %p", result);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   642
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   643
				break;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   644
			}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   645
			prev = node;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   646
		}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   647
		if (prev_out != NULL) *prev_out = prev;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   648
	}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   649
#ifdef HASH_DEBUG
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   650
	if (result == NULL) debug("Not found");
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   651
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   652
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   653
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   654
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   655
void* Hash_Delete(Hash* h, uint key1, uint key2)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   656
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   657
	void* result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   658
	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
   659
	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
   660
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   661
	if (node == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   662
		/* not found */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   663
		result = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   664
	} else if (prev == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   665
		/* 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
   666
		 * 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
   667
		/* Save the value */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   668
		result = node->value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   669
		if (node->next != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   670
			HashNode* next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   671
			/* Copy the second to the first */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   672
			*node = *next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   673
			/* Free the second */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   674
#ifndef NOFREE
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   675
			free(next);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   676
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   677
		} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   678
			/* This was the last in this bucket */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   679
			/* Mark it as empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   680
			uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   681
			h->buckets_in_use[hash] = false;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   682
		}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   683
	} else {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   684
		/* It is in another node */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   685
		/* Save the value */
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
		/* Link previous and next nodes */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   688
		prev->next = node->next;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   689
		/* Free the node */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   690
#ifndef NOFREE
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   691
		free(node);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   692
#endif
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   693
	}
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   694
	if (result != NULL) h->size--;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   695
	return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   696
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   697
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   698
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   699
void* Hash_Set(Hash* h, uint key1, uint key2, void* value)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   700
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   701
	HashNode* prev;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   702
	HashNode* node = Hash_FindNode(h, key1, key2, &prev);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   703
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   704
	if (node != NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   705
		/* Found it */
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   706
		void* result = node->value;
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   707
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   708
		node->value = value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   709
		return result;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   710
	}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   711
	/* 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
   712
	if (prev == NULL) {
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   713
		/* The bucket is still empty */
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   714
		uint hash = h->hash(key1, key2);
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   715
		h->buckets_in_use[hash] = true;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   716
		node = h->buckets + hash;
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
		/* Add it after prev */
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
   719
		MallocT(&node, 1);
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   720
		prev->next = node;
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
	node->next = NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   723
	node->key1 = key1;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   724
	node->key2 = key2;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   725
	node->value = value;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   726
	h->size++;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   727
	return NULL;
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   728
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   729
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   730
void* Hash_Get(const Hash* h, uint key1, uint key2)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   731
{
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   732
	HashNode* node = Hash_FindNode(h, key1, key2, NULL);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   733
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   734
#ifdef HASH_DEBUG
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   735
	debug("Found node: %p", node);
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   736
#endif
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   737
	return (node != NULL) ? node->value : NULL;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   738
}
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   739
3012
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   740
uint Hash_Size(const Hash* h)
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   741
{
e6d8dd948cb4 (svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents: 3010
diff changeset
   742
	return h->size;
84
1e0721c29bad (svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff changeset
   743
}