| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "tree.h" |
| 7 |
#include "config.h" |
| 8 |
#include "dynabuf.h" |
| 9 |
#include "global.h" |
| 10 |
#include "platform.h" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
static hash_t make_hash(struct ronode *node) { |
| 19 |
register char byte; |
| 20 |
register const char *read; |
| 21 |
register hash_t tmp = 0; |
| 22 |
|
| 23 |
read = node->id_string; |
| 24 |
while ((byte = *read++)) |
| 25 |
tmp = ((tmp << 7) | (tmp >> (8 * sizeof(hash_t) - 7))) ^ byte; |
| 26 |
node->hash_value = tmp; |
| 27 |
return tmp; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
static void add_node_to_tree(struct ronode **tree, struct ronode *node_to_add) |
| 32 |
{ |
| 33 |
hash_t hash; |
| 34 |
|
| 35 |
|
| 36 |
hash = make_hash(node_to_add); |
| 37 |
|
| 38 |
while (*tree) { |
| 39 |
|
| 40 |
if (hash > (*tree)->hash_value) |
| 41 |
tree = &((*tree)->greater_than); |
| 42 |
else |
| 43 |
tree = &((*tree)->less_than_or_equal); |
| 44 |
} |
| 45 |
*tree = node_to_add; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void tree_from_list(struct ronode **tree, struct ronode *table_to_add) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
while (table_to_add->hash_value) |
| 59 |
add_node_to_tree(tree, table_to_add++); |
| 60 |
add_node_to_tree(tree, table_to_add); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
int tree_easy_scan(struct ronode *tree, void **node_body, struct dynabuf *dyna_buf) |
| 69 |
{ |
| 70 |
struct ronode wanted; |
| 71 |
const char *p1, |
| 72 |
*p2; |
| 73 |
char b1, |
| 74 |
b2; |
| 75 |
hash_t hash; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if (tree->greater_than == NULL) |
| 81 |
tree_from_list(&tree->greater_than, tree + 1); |
| 82 |
tree = tree->greater_than; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
wanted.id_string = dyna_buf->buffer; |
| 87 |
hash = make_hash(&wanted); |
| 88 |
while (tree) { |
| 89 |
|
| 90 |
if (hash > tree->hash_value) { |
| 91 |
|
| 92 |
|
| 93 |
tree = tree->greater_than; |
| 94 |
continue; |
| 95 |
} |
| 96 |
if (hash == tree->hash_value) { |
| 97 |
p1 = wanted.id_string; |
| 98 |
p2 = tree->id_string; |
| 99 |
do { |
| 100 |
b1 = *p1++; |
| 101 |
b2 = *p2++; |
| 102 |
} while ((b1 == b2) && b1); |
| 103 |
if (b1 == b2) { |
| 104 |
|
| 105 |
*node_body = tree->body; |
| 106 |
return TRUE; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
tree = tree->less_than_or_equal; |
| 112 |
} |
| 113 |
return FALSE ; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
int tree_hard_scan(struct rwnode **result, struct rwnode **forest, int id_number, boolean create) |
| 125 |
{ |
| 126 |
struct ronode wanted; |
| 127 |
struct rwnode **current_node; |
| 128 |
struct rwnode *new_leaf_node; |
| 129 |
const char *p1, |
| 130 |
*p2; |
| 131 |
char b1, |
| 132 |
b2; |
| 133 |
hash_t byte_hash; |
| 134 |
|
| 135 |
wanted.id_string = GLOBALDYNABUF_CURRENT; |
| 136 |
|
| 137 |
byte_hash = make_hash(&wanted) ^ id_number; |
| 138 |
wanted.hash_value = byte_hash; |
| 139 |
PLATFORM_UINT2CHAR(byte_hash); |
| 140 |
|
| 141 |
current_node = &(forest[byte_hash]); |
| 142 |
while (*current_node) { |
| 143 |
|
| 144 |
if (wanted.hash_value > (*current_node)->hash_value) { |
| 145 |
|
| 146 |
|
| 147 |
current_node = &((*current_node)->greater_than); |
| 148 |
continue; |
| 149 |
} |
| 150 |
if (wanted.hash_value == (*current_node)->hash_value) { |
| 151 |
if (id_number == (*current_node)->id_number) { |
| 152 |
p1 = wanted.id_string; |
| 153 |
p2 = (*current_node)->id_string; |
| 154 |
do { |
| 155 |
b1 = *p1++; |
| 156 |
b2 = *p2++; |
| 157 |
} while ((b1 == b2) && b1); |
| 158 |
if (b1 == b2) { |
| 159 |
|
| 160 |
*result = *current_node; |
| 161 |
|
| 162 |
|
| 163 |
return FALSE; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
current_node = &((*current_node)->less_than_or_equal); |
| 170 |
} |
| 171 |
|
| 172 |
if (!create) { |
| 173 |
*result = NULL; |
| 174 |
return FALSE; |
| 175 |
} |
| 176 |
|
| 177 |
new_leaf_node = safe_malloc(sizeof(*new_leaf_node)); |
| 178 |
new_leaf_node->greater_than = NULL; |
| 179 |
new_leaf_node->less_than_or_equal = NULL; |
| 180 |
new_leaf_node->hash_value = wanted.hash_value; |
| 181 |
new_leaf_node->id_number = id_number; |
| 182 |
new_leaf_node->id_string = dynabuf_get_copy(GlobalDynaBuf); |
| 183 |
|
| 184 |
*current_node = new_leaf_node; |
| 185 |
|
| 186 |
*result = new_leaf_node; |
| 187 |
return TRUE; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static void dump_tree(struct rwnode *node, int id_number, void (*fn)(struct rwnode *, FILE *), FILE *env) |
| 193 |
{ |
| 194 |
|
| 195 |
if (node->id_number == id_number) |
| 196 |
fn(node, env); |
| 197 |
if (node->greater_than) |
| 198 |
dump_tree(node->greater_than, id_number, fn, env); |
| 199 |
if (node->less_than_or_equal) |
| 200 |
dump_tree(node->less_than_or_equal, id_number, fn, env); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void tree_dump_forest(struct rwnode **forest, int id_number, void (*fn)(struct rwnode *, FILE *), FILE *env) |
| 205 |
{ |
| 206 |
int ii; |
| 207 |
|
| 208 |
for (ii = 255; ii >= 0; --ii) { |
| 209 |
if (*forest) |
| 210 |
dump_tree(*forest, id_number, fn, env); |
| 211 |
++forest; |
| 212 |
} |
| 213 |
} |