1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
// ==============================================================
/* */
// File : struct_man.c
// Purpose :
/* */
// Institute of Microbiology (Technical University Munich)
// http://www.arb-home.de/
/* */
// ==============================================================
#include <aisc.h>
#include <struct_man.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// AISC_MKPT_PROMOTE:struct aisc_hash_node;
// ---------------------
// hash tables
#define CORE
#define HASH_SIZE 103123
#define TRF_HASH_SIZE 103123
struct aisc_hash_node {
char *key;
long data;
aisc_hash_node *next;
};
static aisc_hash_node **aisc_init_hash(int size) {
struct aisc_hash_node **tab;
tab = (aisc_hash_node **) calloc(sizeof(aisc_hash_node *), size);
tab[0] = (aisc_hash_node *) calloc(sizeof(aisc_hash_node), 1);
tab[0]->data = size;
tab[0]->key = (char *)strdup("len_of_hash_table_(c) oliver_strunk 1.3.93");
return tab;
}
static int aisc_hash(const char *key, int size) {
unsigned int i, len, x;
len = strlen(key);
x = 0;
for (i=0; i<len; i++) {
x = x<<2 ^ key[i];
}
x = x%size;
return x;
}
static void aisc_free_key(aisc_hash_node **table, char *key) {
if (table && table[0]) {
long size = table[0]->data;
long i = aisc_hash(key, (int)size);
aisc_hash_node *hn, *hhn;
for (hn = hhn = table[i]; hn; hn = hn->next) {
if (strcmp(key, hn->key)) {
hhn = hn;
continue;
}
if (hn != hhn)
hhn->next = hn->next;
else {
table[i] = hhn->next;
}
free(hn->key);
free(hn);
break;
}
}
}
static void aisc_free_hash(aisc_hash_node **table) {
long end = table[0]->data;
for (long i=0; i<end; i++) {
aisc_hash_node *hn, *hnn;
for (hn = table[i]; hn; hn=hnn) {
hnn = hn->next;
free(hn->key);
free(hn);
}
}
free(table);
}
static void aisc_insert_hash(aisc_hash_node **table, char *key, long data) {
long size = table[0]->data;
long i = aisc_hash(key, (int)size);
aisc_hash_node *hnl = 0;
aisc_hash_node *hn;
for (hn=table[i]; hn; hn=hn->next) {
hnl = hn;
if (strcmp(key, hn->key) == 0) {
hn->data = data;
return;
}
}
hn = (aisc_hash_node *)calloc(sizeof(aisc_hash_node), 1);
hn->key = (char *)strdup(key);
hn->data = data;
if (hnl) {
hnl->next = hn;
}
else {
table[i] = hn;
}
}
long aisc_read_hash(aisc_hash_node **table, const char *key) {
if (table && table[0]) {
long size = table[0]->data;
long i = aisc_hash(key, (int)size);
for (aisc_hash_node *hn=table[i]; hn; hn=hn->next) {
if (strcmp(key, hn->key) == 0) return hn->data;
}
}
return 0;
}
// ----------------------
// link control
const char *aisc_link(dllpublic_ext *father, dllheader_ext *object) {
if (!object) {
CORE;
return "Object is (NULL)";
}
if (object->mh.parent) {
CORE;
return "Object already linked";
}
if (!father) {
CORE;
return "Parent is (NULL)";
}
if (father->key != object->mh.key) {
CORE;
return "Parent key doesn't match Object key";
}
if (object->mh.ident) {
if (strlen(object->mh.ident) <= 0) {
CORE;
return "Too short ident";
}
if (father->hash) {
if (aisc_read_hash((aisc_hash_node **)father->hash, object->mh.ident)) {
CORE;
return "Object already in list";
}
else {
aisc_insert_hash((aisc_hash_node **)father->hash, object->mh.ident, (long)object);
}
}
else {
father->hash = (long)aisc_init_hash(HASH_SIZE);
aisc_insert_hash((aisc_hash_node **)father->hash, object->mh.ident, (long)object);
}
}
object->next = object->previous = NULL;
if (!father->first) {
father->cnt = 1;
father->first = object;
father->last = object;
}
else {
father->cnt++;
object->previous = father->last;
father->last->next = object;
father->last = object;
}
object->mh.parent = father;
return 0;
}
const char *aisc_unlink(dllheader_ext *object) {
dllpublic_ext *father = (dllpublic_ext *)object->mh.parent;
if (!father) {
CORE;
return "Object not linked";
}
if (father->hash) {
aisc_free_key((aisc_hash_node **)father->hash, object->mh.ident);
}
if (father->cnt <= 0) {
CORE;
return "Parent count is 0";
}
if (object->previous) {
if (object->previous->next != object) {
CORE;
return "Fatal Error: Object is a copy, not original";
}
object->previous->next = object->next;
}
else {
father->first = object->next;
}
if (object->next) {
object->next->previous = object->previous;
}
else {
father->last = object->previous;
}
object->mh.parent = NULL;
object->previous = NULL;
object->next = NULL;
father->cnt--;
if (!father->cnt) {
if (father->hash) {
aisc_free_hash((aisc_hash_node **)father->hash);
father->hash = 0;
}
}
return 0;
}
long aisc_find_lib(dllpublic_ext *parent, char *ident)
{
if (!parent->hash) return 0;
if (!ident) return 0;
return aisc_read_hash((aisc_hash_node **)parent->hash, ident);
}
struct trf_dest_struct {
struct trf_dest_struct *next;
long *dest;
};
struct trf_struct {
struct trf_struct *next;
long new_item;
long old;
struct trf_dest_struct *dests;
};
static int trf_hash(long p)
{
return (p+(p>>8))&(TRF_HASH_SIZE-1);
}
static int trf_level = 0;
static struct trf_struct **trf_sp = 0;
void trf_create(long old, long new_item) {
long i;
struct trf_struct *ts;
struct trf_dest_struct *tds, *ntds;
if (!trf_sp) return;
i = trf_hash(old);
for (ts = trf_sp[i]; ts; ts = ts->next) {
if (ts->old == old) {
if (ts->new_item && (ts->new_item != new_item)) {
GBK_terminate("ERROR IN trf_create");
}
else {
ts->new_item = new_item;
for (tds = ts->dests; tds; tds = ntds) {
*tds->dest = new_item;
ntds = tds->next;
free(tds);
}
}
return;
}
}
ts = (struct trf_struct *)calloc(sizeof(struct trf_struct), 1);
ts->next = trf_sp[i];
trf_sp[i] = ts;
ts->new_item = new_item;
ts->old = old;
}
void trf_link(long old, long *dest)
{
long i;
struct trf_struct *ts, *fts;
struct trf_dest_struct *tds;
if (!trf_sp) return;
i = trf_hash(old);
fts = 0;
for (ts = trf_sp[i]; ts; ts = ts->next) {
if (ts->old == old) { fts = ts; break; }
}
if (!fts) {
ts = (struct trf_struct *)calloc(sizeof(struct trf_struct), 1);
ts->next = trf_sp[i];
trf_sp[i] = ts;
ts->old = old;
fts = ts;
}
tds = (struct trf_dest_struct *)calloc(sizeof(struct trf_dest_struct), 1);
tds->next = fts->dests;
fts->dests = tds;
tds->dest = dest;
}
void trf_begin() {
if (trf_level==0) {
trf_sp = (struct trf_struct **)calloc(sizeof(struct trf_struct *), TRF_HASH_SIZE);
}
trf_level ++;
}
void trf_commit(int errors) {
// if errors == 1 then print errors and CORE
struct trf_dest_struct *tds, *ntds;
struct trf_struct *ts, *nts;
trf_level--;
if (!trf_level) {
for (int i = 0; i < TRF_HASH_SIZE; i++) {
for (ts = trf_sp[i]; ts; ts = nts) {
if (errors) {
if (ts->dests) {
GBK_terminate("ERROR IN trf_commit");
}
}
else {
for (tds = ts->dests; tds; tds = ntds) {
ntds = tds->next;
free(tds);
}
}
nts = ts->next;
free(ts);
}
}
free(trf_sp);
trf_sp = 0;
}
}
|