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