author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9111 | 48ce04029fe4 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8130
diff
changeset
|
3 |
/** @file queue.cpp Implementation of the Queue/Hash. */ |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
4 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1662
diff
changeset
|
6 |
#include "openttd.h" |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
7 |
#include "queue.h" |
8130
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8037
diff
changeset
|
8 |
#include "core/alloc_func.hpp" |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
9 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
10 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
11 |
/* |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
12 |
* Insertion Sorter |
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 |
|
1095 | 15 |
static void InsSort_Clear(Queue* q, bool free_values) |
16 |
{ |
|
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
17 |
InsSortNode* node = q->data.inssort.first; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
18 |
InsSortNode* prev; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
19 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
20 |
while (node != NULL) { |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
21 |
if (free_values) free(node->item); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
22 |
prev = node; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
23 |
node = node->next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
24 |
free(prev); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
25 |
} |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
26 |
q->data.inssort.first = NULL; |
84
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 |
|
1095 | 29 |
static void InsSort_Free(Queue* q, bool free_values) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
30 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
31 |
q->clear(q, free_values); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
32 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
33 |
|
1095 | 34 |
static bool InsSort_Push(Queue* q, void* item, int priority) |
35 |
{ |
|
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
36 |
InsSortNode* newnode = MallocT<InsSortNode>(1); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
37 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
38 |
if (newnode == NULL) return false; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
39 |
newnode->item = item; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
40 |
newnode->priority = priority; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
41 |
if (q->data.inssort.first == NULL || |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
42 |
q->data.inssort.first->priority >= priority) { |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
43 |
newnode->next = q->data.inssort.first; |
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
44 |
q->data.inssort.first = newnode; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
45 |
} else { |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
46 |
InsSortNode* node = q->data.inssort.first; |
2953
4d933ef9a41f
(svn r3512) Yet more whitespace fixes (mostly by Rubidium)
peter1138
parents:
2799
diff
changeset
|
47 |
while (node != NULL) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
48 |
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
|
49 |
newnode->next = node->next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
50 |
node->next = newnode; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
51 |
break; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
52 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
53 |
node = node->next; |
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 |
return true; |
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 |
|
1095 | 59 |
static void* InsSort_Pop(Queue* q) |
60 |
{ |
|
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
61 |
InsSortNode* node = q->data.inssort.first; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
62 |
void* result; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
63 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
64 |
if (node == NULL) return NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
65 |
result = node->item; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
66 |
q->data.inssort.first = q->data.inssort.first->next; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
67 |
assert(q->data.inssort.first == NULL || q->data.inssort.first->priority >= node->priority); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
68 |
free(node); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
69 |
return result; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
70 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
71 |
|
1095 | 72 |
static bool InsSort_Delete(Queue* q, void* item, int priority) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
73 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
74 |
return false; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
75 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
76 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
77 |
void init_InsSort(Queue* q) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
78 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
79 |
q->push = InsSort_Push; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
80 |
q->pop = InsSort_Pop; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
81 |
q->del = InsSort_Delete; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
82 |
q->clear = InsSort_Clear; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
83 |
q->free = InsSort_Free; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
84 |
q->data.inssort.first = NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
85 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
86 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
87 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
88 |
/* |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
89 |
* Binary Heap |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
90 |
* 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
|
91 |
*/ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
92 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
93 |
#define BINARY_HEAP_BLOCKSIZE (1 << BINARY_HEAP_BLOCKSIZE_BITS) |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
94 |
#define BINARY_HEAP_BLOCKSIZE_MASK (BINARY_HEAP_BLOCKSIZE - 1) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
95 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
96 |
/* To make our life easy, we make the next define |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
97 |
* Because Binary Heaps works with array from 1 to n, |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
98 |
* and C with array from 0 to n-1, and we don't like typing |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
99 |
* q->data.binaryheap.elements[i - 1] every time, we use this define. */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
100 |
#define BIN_HEAP_ARR(i) q->data.binaryheap.elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK] |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
101 |
|
1095 | 102 |
static void BinaryHeap_Clear(Queue* q, bool free_values) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
103 |
{ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
104 |
/* Free all items if needed and free all but the first blocks of memory */ |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
105 |
uint i; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
106 |
uint j; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
107 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
108 |
for (i = 0; i < q->data.binaryheap.blocks; i++) { |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
109 |
if (q->data.binaryheap.elements[i] == NULL) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
110 |
/* No more allocated blocks */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
111 |
break; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
112 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
113 |
/* For every allocated block */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
114 |
if (free_values) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
115 |
for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
116 |
/* For every element in the block */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
117 |
if ((q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS) == i && |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
118 |
(q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == j) { |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
119 |
break; // We're past the last element |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
120 |
} |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
121 |
free(q->data.binaryheap.elements[i][j].item); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
122 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
123 |
} |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
124 |
if (i != 0) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
125 |
/* Leave the first block of memory alone */ |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
126 |
free(q->data.binaryheap.elements[i]); |
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
127 |
q->data.binaryheap.elements[i] = NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
128 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
129 |
} |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
130 |
q->data.binaryheap.size = 0; |
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
131 |
q->data.binaryheap.blocks = 1; |
84
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 |
|
1095 | 134 |
static void BinaryHeap_Free(Queue* q, bool free_values) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
135 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
136 |
uint i; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
137 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
138 |
q->clear(q, free_values); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
139 |
for (i = 0; i < q->data.binaryheap.blocks; i++) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
140 |
if (q->data.binaryheap.elements[i] == NULL) break; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
141 |
free(q->data.binaryheap.elements[i]); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
142 |
} |
2799
739481fa6cb9
(svn r3347) Plug a memory leak (Found by Valgrind using Truelight ... or was it the other way round?)
tron
parents:
2752
diff
changeset
|
143 |
free(q->data.binaryheap.elements); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
144 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
145 |
|
1095 | 146 |
static bool BinaryHeap_Push(Queue* q, void* item, int priority) |
147 |
{ |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
148 |
#ifdef QUEUE_DEBUG |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
149 |
printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->data.binaryheap.size); |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
150 |
#endif |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
151 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
152 |
if (q->data.binaryheap.size == q->data.binaryheap.max_size) return false; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
153 |
assert(q->data.binaryheap.size < q->data.binaryheap.max_size); |
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
107
diff
changeset
|
154 |
|
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
155 |
if (q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
156 |
/* The currently allocated blocks are full, allocate a new one */ |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
157 |
assert((q->data.binaryheap.size & BINARY_HEAP_BLOCKSIZE_MASK) == 0); |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
158 |
q->data.binaryheap.elements[q->data.binaryheap.size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE); |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
159 |
q->data.binaryheap.blocks++; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
160 |
#ifdef QUEUE_DEBUG |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
161 |
printf("[BinaryHeap] Increasing size of elements to %d nodes\n", q->data.binaryheap.blocks * BINARY_HEAP_BLOCKSIZE); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
162 |
#endif |
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 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
165 |
/* Add the item at the end of the array */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
166 |
BIN_HEAP_ARR(q->data.binaryheap.size + 1).priority = priority; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
167 |
BIN_HEAP_ARR(q->data.binaryheap.size + 1).item = item; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
168 |
q->data.binaryheap.size++; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
169 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
170 |
/* Now we are going to check where it belongs. As long as the parent is |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
171 |
* bigger, we switch with the parent */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
172 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
173 |
BinaryHeapNode temp; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
174 |
int i; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
175 |
int j; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
176 |
|
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
177 |
i = q->data.binaryheap.size; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
178 |
while (i > 1) { |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
179 |
/* Get the parent of this object (divide by 2) */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
180 |
j = i / 2; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
181 |
/* Is the parent bigger then the current, switch them */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
182 |
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
|
183 |
temp = BIN_HEAP_ARR(j); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
184 |
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
|
185 |
BIN_HEAP_ARR(i) = temp; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
186 |
i = j; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
187 |
} else { |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
188 |
/* It is not, we're done! */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
189 |
break; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
190 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
191 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
192 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
193 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
194 |
return true; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
195 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
196 |
|
1095 | 197 |
static bool BinaryHeap_Delete(Queue* q, void* item, int priority) |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
198 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
199 |
uint i = 0; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
200 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
201 |
#ifdef QUEUE_DEBUG |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
202 |
printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->data.binaryheap.size); |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
203 |
#endif |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
204 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
205 |
/* First, we try to find the item.. */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
206 |
do { |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
207 |
if (BIN_HEAP_ARR(i + 1).item == item) break; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
208 |
i++; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
209 |
} while (i < q->data.binaryheap.size); |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
210 |
/* We did not find the item, so we return false */ |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
211 |
if (i == q->data.binaryheap.size) return false; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
212 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
213 |
/* Now we put the last item over the current item while decreasing the size of the elements */ |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
214 |
q->data.binaryheap.size--; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
215 |
BIN_HEAP_ARR(i + 1) = BIN_HEAP_ARR(q->data.binaryheap.size + 1); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
216 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
217 |
/* Now the only thing we have to do, is resort it.. |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
218 |
* On place i there is the item to be sorted.. let's start there */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
219 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
220 |
uint j; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
221 |
BinaryHeapNode temp; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
222 |
/* Because of the fact that Binary Heap uses array from 1 to n, we need to |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
223 |
* increase i by 1 |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
224 |
*/ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
225 |
i++; |
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 |
for (;;) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
228 |
j = i; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
229 |
/* Check if we have 2 childs */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
230 |
if (2 * j + 1 <= q->data.binaryheap.size) { |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
231 |
/* Is this child smaller than the parent? */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
232 |
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
233 |
/* Yes, we _need_ to use i here, not j, because we want to have the smallest child |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
234 |
* This way we get that straight away! */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
235 |
if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
236 |
/* Do we have one child? */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
237 |
} else if (2 * j <= q->data.binaryheap.size) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
238 |
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
239 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
240 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
241 |
/* One of our childs is smaller than we are, switch */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
242 |
if (i != j) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
243 |
temp = BIN_HEAP_ARR(j); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
244 |
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
|
245 |
BIN_HEAP_ARR(i) = temp; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
246 |
} else { |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
247 |
/* None of our childs is smaller, so we stay here.. stop :) */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
248 |
break; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
249 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
250 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
251 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
252 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
253 |
return true; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
254 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
255 |
|
1095 | 256 |
static void* BinaryHeap_Pop(Queue* q) |
257 |
{ |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
258 |
void* result; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
259 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
260 |
#ifdef QUEUE_DEBUG |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
261 |
printf("[BinaryHeap] Popping an element. There are %d elements left\n", q->data.binaryheap.size); |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
262 |
#endif |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
263 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
264 |
if (q->data.binaryheap.size == 0) return NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
265 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
266 |
/* The best item is always on top, so give that as result */ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
267 |
result = BIN_HEAP_ARR(1).item; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
268 |
/* And now we should get rid of this item... */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
269 |
BinaryHeap_Delete(q, BIN_HEAP_ARR(1).item, BIN_HEAP_ARR(1).priority); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
270 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
271 |
return result; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
272 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
273 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
274 |
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
|
275 |
{ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
276 |
assert(q != NULL); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
277 |
q->push = BinaryHeap_Push; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
278 |
q->pop = BinaryHeap_Pop; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
279 |
q->del = BinaryHeap_Delete; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
280 |
q->clear = BinaryHeap_Clear; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
281 |
q->free = BinaryHeap_Free; |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
282 |
q->data.binaryheap.max_size = max_size; |
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
283 |
q->data.binaryheap.size = 0; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
284 |
/* We malloc memory in block of BINARY_HEAP_BLOCKSIZE |
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
285 |
* It autosizes when it runs out of memory */ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
286 |
q->data.binaryheap.elements = CallocT<BinaryHeapNode*>((max_size - 1) / BINARY_HEAP_BLOCKSIZE + 1); |
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
287 |
q->data.binaryheap.elements[0] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE); |
107
c72381c27546
(svn r108) -Fix: anon-union problems on GCC2 compilers
truelight
parents:
84
diff
changeset
|
288 |
q->data.binaryheap.blocks = 1; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
289 |
#ifdef QUEUE_DEBUG |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
290 |
printf("[BinaryHeap] Initial size of elements is %d nodes\n", BINARY_HEAP_BLOCKSIZE); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
291 |
#endif |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
292 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
293 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
294 |
// 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
|
295 |
#undef BIN_HEAP_ARR |
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 |
/* |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
298 |
* Hash |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
299 |
*/ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
300 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
301 |
void init_Hash(Hash* h, Hash_HashProc* hash, uint num_buckets) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
302 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
303 |
/* Allocate space for the Hash, the buckets and the bucket flags */ |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
304 |
uint i; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
305 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
306 |
assert(h != NULL); |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
307 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
308 |
debug("Allocated hash: %p", h); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
309 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
310 |
h->hash = hash; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
311 |
h->size = 0; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
312 |
h->num_buckets = num_buckets; |
8037
8aa4ace04383
(svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents:
6352
diff
changeset
|
313 |
h->buckets = (HashNode*)MallocT<byte>(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use))); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
314 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
315 |
debug("Buckets = %p", h->buckets); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
316 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
317 |
h->buckets_in_use = (bool*)(h->buckets + num_buckets); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
318 |
for (i = 0; i < num_buckets; i++) h->buckets_in_use[i] = false; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
319 |
} |
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 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
322 |
void delete_Hash(Hash* h, bool free_values) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
323 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
324 |
uint i; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
325 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
326 |
/* Iterate all buckets */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
327 |
for (i = 0; i < h->num_buckets; i++) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
328 |
if (h->buckets_in_use[i]) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
329 |
HashNode* node; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
330 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
331 |
/* Free the first value */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
332 |
if (free_values) free(h->buckets[i].value); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
333 |
node = h->buckets[i].next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
334 |
while (node != NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
335 |
HashNode* prev = node; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
336 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
337 |
node = node->next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
338 |
/* Free the value */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
339 |
if (free_values) free(prev->value); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
340 |
/* Free the node */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
341 |
free(prev); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
342 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
343 |
} |
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 |
free(h->buckets); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
346 |
/* 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
|
347 |
* malloc with buckets */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
348 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
349 |
debug("Freeing Hash: %p", h); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
350 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
351 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
352 |
|
2752 | 353 |
#ifdef HASH_STATS |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
354 |
static void stat_Hash(const Hash* h) |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
355 |
{ |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
356 |
uint used_buckets = 0; |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
357 |
uint max_collision = 0; |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
358 |
uint max_usage = 0; |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
359 |
uint usage[200]; |
1662
beb197794c14
(svn r2166) Fixed two warnings in the last commit.
matthijs
parents:
1661
diff
changeset
|
360 |
uint i; |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
361 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
362 |
for (i = 0; i < lengthof(usage); i++) usage[i] = 0; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
363 |
for (i = 0; i < h->num_buckets; i++) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
364 |
uint collision = 0; |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
365 |
if (h->buckets_in_use[i]) { |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
366 |
const HashNode* node; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
367 |
|
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
368 |
used_buckets++; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
369 |
for (node = &h->buckets[i]; node != NULL; node = node->next) collision++; |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
370 |
if (collision > max_collision) max_collision = collision; |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
371 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
372 |
if (collision >= lengthof(usage)) collision = lengthof(usage) - 1; |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
373 |
usage[collision]++; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
374 |
if (collision > 0 && usage[collision] >= max_usage) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
375 |
max_usage = usage[collision]; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
376 |
} |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
377 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
378 |
printf( |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
379 |
"---\n" |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
380 |
"Hash size: %d\n" |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
381 |
"Nodes used: %d\n" |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
382 |
"Non empty buckets: %d\n" |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
383 |
"Max collision: %d\n", |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
384 |
h->num_buckets, h->size, used_buckets, max_collision |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
385 |
); |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
386 |
printf("{ "); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
387 |
for (i = 0; i <= max_collision; i++) { |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
388 |
if (usage[i] > 0) { |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
389 |
printf("%d:%d ", i, usage[i]); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
390 |
#if 0 |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
391 |
if (i > 0) { |
1662
beb197794c14
(svn r2166) Fixed two warnings in the last commit.
matthijs
parents:
1661
diff
changeset
|
392 |
uint j; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
393 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
394 |
for (j = 0; j < usage[i] * 160 / 800; j++) putchar('#'); |
1662
beb197794c14
(svn r2166) Fixed two warnings in the last commit.
matthijs
parents:
1661
diff
changeset
|
395 |
} |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
396 |
printf("\n"); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
397 |
#endif |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
398 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
399 |
} |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
400 |
printf ("}\n"); |
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
401 |
} |
2752 | 402 |
#endif |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
403 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
404 |
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
|
405 |
{ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
406 |
uint i; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
407 |
|
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
408 |
#ifdef HASH_STATS |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
409 |
if (h->size > 2000) stat_Hash(h); |
1661
f3799f2c84fa
(svn r2165) - Codechange: [NPF] Properly enummed NPF hash size, it is easily changable now.
matthijs
parents:
1247
diff
changeset
|
410 |
#endif |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
411 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
412 |
/* Iterate all buckets */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
413 |
for (i = 0; i < h->num_buckets; i++) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
414 |
if (h->buckets_in_use[i]) { |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
415 |
HashNode* node; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
416 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
417 |
h->buckets_in_use[i] = false; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
418 |
/* Free the first value */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
419 |
if (free_values) free(h->buckets[i].value); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
420 |
node = h->buckets[i].next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
421 |
while (node != NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
422 |
HashNode* prev = node; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
423 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
424 |
node = node->next; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
425 |
if (free_values) free(prev->value); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
426 |
free(prev); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
427 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
428 |
} |
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 |
h->size = 0; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
431 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
432 |
|
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
433 |
/** Finds the node that that saves this key pair. If it is not |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
434 |
* 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
|
435 |
* 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
|
436 |
* 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
|
437 |
* 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
|
438 |
* not used for output. |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
439 |
*/ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
440 |
static HashNode* Hash_FindNode(const Hash* h, uint key1, uint key2, HashNode** prev_out) |
1095 | 441 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
442 |
uint hash = h->hash(key1, key2); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
443 |
HashNode* result = NULL; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
444 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
445 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
446 |
debug("Looking for %u, %u", key1, key2); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
447 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
448 |
/* Check if the bucket is empty */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
449 |
if (!h->buckets_in_use[hash]) { |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
450 |
if (prev_out != NULL) *prev_out = NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
451 |
result = NULL; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
452 |
/* Check the first node specially */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
453 |
} 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
|
454 |
/* Save the value */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
455 |
result = h->buckets + hash; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
456 |
if (prev_out != NULL) *prev_out = NULL; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
457 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
458 |
debug("Found in first node: %p", result); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
459 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
460 |
/* Check all other nodes */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
461 |
} else { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
462 |
HashNode* prev = h->buckets + hash; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
463 |
HashNode* node; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
464 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
465 |
for (node = prev->next; node != NULL; node = node->next) { |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
466 |
if (node->key1 == key1 && node->key2 == key2) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
467 |
/* Found it */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
468 |
result = node; |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
469 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
470 |
debug("Found in other node: %p", result); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
471 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
472 |
break; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
473 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
474 |
prev = node; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
475 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
476 |
if (prev_out != NULL) *prev_out = prev; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
477 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
478 |
#ifdef HASH_DEBUG |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
479 |
if (result == NULL) debug("Not found"); |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
480 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
481 |
return result; |
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 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
484 |
void* Hash_Delete(Hash* h, uint key1, uint key2) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
485 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
486 |
void* result; |
6352
938ab8f48e5d
(svn r9391) -Documentation : correct Doxygen of comments and @file inclusion. Time for P and Q files
belugas
parents:
6105
diff
changeset
|
487 |
HashNode* prev; // Used as output var for below function call |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
488 |
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
|
489 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
490 |
if (node == NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
491 |
/* not found */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
492 |
result = NULL; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
493 |
} else if (prev == NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
494 |
/* 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
|
495 |
* 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
|
496 |
/* Save the value */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
497 |
result = node->value; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
498 |
if (node->next != NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
499 |
HashNode* next = node->next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
500 |
/* Copy the second to the first */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
501 |
*node = *next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
502 |
/* Free the second */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
503 |
#ifndef NOFREE |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
504 |
free(next); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
505 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
506 |
} else { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
507 |
/* This was the last in this bucket */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
508 |
/* Mark it as empty */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
509 |
uint hash = h->hash(key1, key2); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
510 |
h->buckets_in_use[hash] = false; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
511 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
512 |
} else { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
513 |
/* It is in another node */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
514 |
/* Save the value */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
515 |
result = node->value; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
516 |
/* Link previous and next nodes */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
517 |
prev->next = node->next; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
518 |
/* Free the node */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
519 |
#ifndef NOFREE |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
520 |
free(node); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
521 |
#endif |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
522 |
} |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
523 |
if (result != NULL) h->size--; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
524 |
return result; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
525 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
526 |
|
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
527 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
528 |
void* Hash_Set(Hash* h, uint key1, uint key2, void* value) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
529 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
530 |
HashNode* prev; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
531 |
HashNode* node = Hash_FindNode(h, key1, key2, &prev); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
532 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
533 |
if (node != NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
534 |
/* Found it */ |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
535 |
void* result = node->value; |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
536 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
537 |
node->value = value; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
538 |
return result; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
539 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
540 |
/* 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
|
541 |
if (prev == NULL) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
542 |
/* The bucket is still empty */ |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
543 |
uint hash = h->hash(key1, key2); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
544 |
h->buckets_in_use[hash] = true; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
545 |
node = h->buckets + hash; |
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 |
/* Add it after prev */ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
548 |
node = MallocT<HashNode>(1); |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
549 |
prev->next = node; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
550 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
551 |
node->next = NULL; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
552 |
node->key1 = key1; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
553 |
node->key2 = key2; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
554 |
node->value = value; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
555 |
h->size++; |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
556 |
return NULL; |
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 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
559 |
void* Hash_Get(const Hash* h, uint key1, uint key2) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
560 |
{ |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
561 |
HashNode* node = Hash_FindNode(h, key1, key2, NULL); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
562 |
|
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
563 |
#ifdef HASH_DEBUG |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
564 |
debug("Found node: %p", node); |
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
565 |
#endif |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
566 |
return (node != NULL) ? node->value : NULL; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
567 |
} |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
568 |
|
3012
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
569 |
uint Hash_Size(const Hash* h) |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
570 |
{ |
5e79c183a4bf
(svn r3592) Miscellaneous smaller changes, most notably replacing sizeof(type) by sizeof(*variable)
tron
parents:
3010
diff
changeset
|
571 |
return h->size; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
diff
changeset
|
572 |
} |