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