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