static inline void load_config(void) { /* FIXME */ if ((cfg = config_load("/etc/xcb/pd.conf"))) { char *cat = category_browse(cfg, NULL); while (cat) { if (!strcasecmp(cat, "pair")) { struct variable *var = variable_browse(cfg, cat); struct cpl *cpl = NULL; while (var) { if (!strcasecmp(var->name, "contract1")) { if (!strcasecmp(var->value, "")) break; if (cpl == NULL) { if (NEW(cpl) == NULL) break; cpl->contract2 = NULL; cpl->price1 = cpl->price2 = cpl->prevpd = -1.0; pthread_spin_init(&cpl->lock, 0); } cpl->contract1 = var->value; } else if (!strcasecmp(var->name, "contract2")) { if (!strcasecmp(var->value, "")) break; if (cpl == NULL) { if (NEW(cpl) == NULL) break; cpl->contract1 = NULL; cpl->price1 = cpl->price2 = cpl->prevpd = -1.0; pthread_spin_init(&cpl->lock, 0); } cpl->contract2 = var->value; } else xcb_log(XCB_LOG_WARNING, "Unknown variable '%s' in " "category '%s' of pd.conf", var->name, cat); var = var->next; } if (cpl && cpl->contract1 && cpl->contract2) { dlist_t dlist; if ((dlist = table_get_value(contracts, cpl->contract1)) == NULL) { dlist = dlist_new(NULL, NULL); table_insert(contracts, cpl->contract1, dlist); } dlist_insert_tail(dlist, cpl); if ((dlist = table_get_value(contracts, cpl->contract2)) == NULL) { dlist = dlist_new(NULL, NULL); table_insert(contracts, cpl->contract2, dlist); } dlist_insert_tail(dlist, cpl); dlist_insert_tail(pairs, cpl); } else if (cpl) FREE(cpl); } cat = category_browse(cfg, cat); } } }
/* * RscAddCallback: Called for each new resource that's loaded from a file. * Add given resource to table. */ bool RscAddCallback(char *fname, int res, char *string) { resource_type entry, r; entry = (resource_type) SafeMalloc(sizeof(resource_struct)); entry->idnum = res; entry->data = (char *) SafeMalloc(strlen(string) + 1); strcpy(entry->data, string); if (table_insert(t, entry, ResourceHash, ResourceCompare) != 0) { if (!ignore_duplicates) { ClientError(hInst, hMain, IDS_DUPRESOURCE, res, fname); FreeRsc(entry); } else { // Free existing resource r = (resource_type) table_lookup(t, &res, IdHash, IdResourceCompare); if (r != NULL) { table_delete_item(t, r, ResourceHash, ResourceCompare); FreeRsc(r); table_insert(t, entry, ResourceHash, ResourceCompare); } } } return true; }
/* * perform some basic tests */ static void basic(table_t *tab_p) { long key; int ret; void *data_p; (void)printf("Performing basic tests:\n"); (void)fflush(stdout); key = TEST_VALUE; ret = table_insert(tab_p, &key, sizeof(key), NULL, 0, &data_p, 0); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not store null pointer of size 0 in table: %s\n", table_strerror(ret)); exit(1); } /* try the replace */ ret = table_insert(tab_p, &key, sizeof(key), NULL, 0, &data_p, 1); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not store null pointer of size 0 in table: %s\n", table_strerror(ret)); exit(1); } /* try to insert without replace */ ret = table_insert(tab_p, &key, sizeof(key), NULL, 0, &data_p, 0); if (ret != TABLE_ERROR_OVERWRITE) { (void)printf("replaced a key in the table with no-overwrite flag: %s\n", table_strerror(ret)); exit(1); } /* delete the key */ ret = table_delete(tab_p, &key, sizeof(key), NULL, NULL); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not delete test key %ld: %s\n", key, table_strerror(ret)); exit(1); } /* test to make sure we can insert a NULL with size > 0 */ ret = table_insert(tab_p, &key, sizeof(key), NULL, 100, &data_p, 0); if (ret != TABLE_ERROR_NONE) { (void)printf("could not add NULL with size > 0: %s\n", table_strerror(ret)); exit(1); } ret = table_delete(tab_p, &key, sizeof(key), NULL, NULL); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not delete test key %ld: %s\n", key, table_strerror(ret)); exit(1); } }
void initialize_parser(void) { int i; st.globalvars = table_create(TABLESIZE); st.classvars = table_create(TABLESIZE); st.localvars = table_create(TABLESIZE); st.missingvars = table_create(TABLESIZE); /* Add function names to table of global identifiers */ for (i=0; i < numfuncs; i++) { id_type id = (id_type) SafeMalloc(sizeof(id_struct)); id->name = Functions[i].name; id->type = I_FUNCTION; id->idnum = i; /* For functions, idnum is just index in table, not a real id # */ if (table_insert(st.globalvars, (void *) id, id_hash, id_compare) != 0) simple_error("Duplicate built-in function name %s", id->name); } /* Add builtin identifiers to appropriate symbol tables */ for (i=0; i < numbuiltins; i++) switch (BuiltinIds[i].type) { case I_MISSING: if (table_insert(st.missingvars, (void *) &BuiltinIds[i], id_hash, id_compare) != 0) simple_error("Duplicate builtin identifier name %s", BuiltinIds[i].name); break; case I_PROPERTY: if (table_insert(st.globalvars, (void *) &BuiltinIds[i], id_hash, id_compare) != 0) simple_error("Duplicate builtin identifier name %s", BuiltinIds[i].name); break; default: simple_error("Bad type on builtin identifier %s", BuiltinIds[i].name); break; } st.maxid = IDBASE; /* Base for user-defined ids; builtins have lower #s */ st.maxresources = RESOURCEBASE; st.maxlocals = -1; /* So that first local is numbered 0 */ st.maxclassvars = -1; /* So that first class variable is numbered 0 */ // XXX not needed because of self #if 0 st.maxproperties = -1; /* So that first property is numbered 0 */ #endif st.recompile_list = NULL; st.constants = NULL; st.num_strings = 0; st.strings = NULL; st.override_classvars = NULL; }
static inline void load_config(void) { /* FIXME */ if ((cfg = config_load("/etc/xcb/sse50etf.conf"))) { char *cat = category_browse(cfg, NULL); struct variable *var; while (cat) { if (!strcasecmp(cat, "general")) { var = variable_browse(cfg, cat); while (var) { if (!strcasecmp(var->name, "CRU")) { if (strcmp(var->value, "")) cru = atoi(var->value); } else if (!strcasecmp(var->name, "ECC")) { if (strcmp(var->value, "")) ecc = atof(var->value); } else xcb_log(XCB_LOG_WARNING, "Unknown variable '%s' in " "category '%s' of sse50etf.conf", var->name, cat); var = var->next; } } else if (!strcasecmp(cat, "weights")) { var = variable_browse(cfg, cat); while (var) { struct wp *wp; if (NEW(wp)) { wp->weight = atof(var->value); wp->price = NAN; wp->type = 1; table_insert(contracts, var->name, wp); } var = var->next; } } else if (!strcasecmp(cat, "prices")) { var = variable_browse(cfg, cat); while (var) { struct wp *wp; if (NEW(wp)) { wp->weight = 1.0; wp->price = atof(var->value); wp->type = 2; table_insert(contracts, var->name, wp); } var = var->next; } } cat = category_browse(cfg, cat); } } }
/* build a table with worst keys. Insert keys that hash to the same table * location. Protects against invalid keys by wrapping around if the total * number of addresses times the table size is large. */ int build_worst(table_t *T, int table_size, int num_addr) { hashkey_t key = MAXID; int i, batches = 0, code; int probes = 0; int *ip; for (i = 0; i < num_addr; i++) { assert(MINID <= key && key <= MAXID); ip = (int *) malloc(sizeof(int)); *ip = key; code = table_insert(T, key, ip); if (code != 0) { printf("build of worst table failed: code (%d) index (%d) key (%u) batch (%d)\n", code, i, key, batches); exit(6); } if (key < MINID + table_size) { batches++; printf("batch %d\n", batches); key = MAXID - batches; } else key -= table_size; probes += table_stats(T); } return probes; }
int load_add_resource(char *resource_name, int resource_id) { id_type id = (id_type) SafeMalloc(sizeof(id_struct)); resource_type r = (resource_type) SafeMalloc(sizeof(resource_struct)); id->name = strdup(resource_name); id->idnum = resource_id; id->type = I_RESOURCE; id->ownernum = st.curclass; id->source = DBASE; r->lhs = id; // Have to load in resources from *rsc files for (int i = 0; i < sizeof(r->resource) / sizeof(r->resource[i]); i++) { r->resource[i] = NULL; } /* Add resource to resource list of current class */ if (current_class == NULL) database_error("Resource appears outside of class in database file"); current_class->resources = list_add_item(current_class->resources, (void *) r); /* OK if parameter already in table; just ignore return value */ table_insert(st.globalvars, (void *) id, id_hash, id_compare); return True; }
static inline void load_config(void) { /* FIXME */ if ((cfg = config_load("/etc/xcb/csi300.conf"))) { char *cat = category_browse(cfg, NULL); struct variable *var; while (cat) { if (!strcasecmp(cat, "weights")) { var = variable_browse(cfg, cat); while (var) { struct wp *wp; if (NEW(wp)) { wp->weight = atof(var->value); wp->price = NAN; table_insert(contracts, var->name, wp); } var = var->next; } } else if (!strcasecmp(cat, "divisor")) if ((var = variable_browse(cfg, cat)) && strcmp(var->value, "")) divisor = atof(var->value); cat = category_browse(cfg, cat); } } }
/* * RscAddCallback: Called for each new resource that's loaded from a file. * Add given resource to table. */ bool RscAddCallback(char *fname, int res, int lang_id, char *string) { resource_type r; // Check for existing resource first, since we might want to add // another language string to it. r = (resource_type) table_lookup(t, &res, IdHash, IdResourceCompare); if (r == NULL) { r = (resource_type) SafeMalloc(sizeof(resource_struct)); r->idnum = res; for (int i = 0; i < MAX_LANGUAGE_ID; i++) r->resource[i] = NULL; } else table_delete_item(t, r, ResourceHash, ResourceCompare); // Shouldn't be able to compile blakod with multiple strings // of the same language type. if (r->resource[lang_id]) SafeFree(r->resource[lang_id]); r->resource[lang_id] = (char *) SafeMalloc(strlen(string) + 1); strcpy(r->resource[lang_id], string); available_languages[lang_id] = True; // Replace the existing resource if we used it. table_insert(t, r, ResourceHash, ResourceCompare); return true; }
/* * add_parent_classvars: Add classvars of a class's superclasses to the * class's classvars. * cv_list is a list used just for holding classvars; we need a separate list * to handle classvars that are overridden with properties in a parent class. */ void add_parent_classvars(list_type cv_list, class_type base, class_type parent) { list_type cv; if (parent == NULL) { list_delete(cv_list); return; } for (cv = parent->classvars; cv != NULL; cv = cv->next) { classvar_type classvar = (classvar_type) (cv->data); /* Parser error recovery may have made a NULL property */ if (classvar != NULL) { // Increment classvar count even for classvars that are overridden by properties. // Only add non-overridden classvars to symbol table, though, so that references // to the name will map to the property, not the classvar. if (list_find_item(cv_list, classvar->id, id_compare) == NULL) { cv_list = list_add_item(cv_list, classvar->id); st.maxclassvars++; } // Insert to table will fail if property with same name is already there table_insert(st.classvars, (void *) classvar->id, id_hash, id_compare); } } add_parent_classvars(cv_list, base, parent->superclass); }
/* * add_parent_properties: Add properties of a class's superclasses to the * class's properties. */ void add_parent_properties(class_type base, class_type parent) { list_type p; if (parent == NULL) return; for (p = parent->properties; p != NULL; p = p->next) { property_type prop = (property_type) (p->data); /* Parser error recovery may have made a NULL property */ if (prop != NULL) { /* Add property id to base class's property table, using table_insert * so that original id #s are preserved. Only increment # of properties * if wasn't already in table. * If already in table, it's not an error, since a property might be * listed many times in this class's ancestors. */ if (table_insert(st.classvars, (void *) prop->id, id_hash, id_compare) == 0) st.maxproperties++; } } add_parent_properties(base, parent->superclass); }
/* build a table with random keys. The keys are generated with a uniform * distribution. */ int build_random(table_t *T, int table_size, int num_addr) { hashkey_t key; int i, range, code; int probes = 0; int *ip; range = MAXID - MINID + 1; for (i = 0; i < num_addr; i++) { key = (hashkey_t) (drand48() * range) + MINID; assert(MINID <= key && key <= MAXID); ip = (int *) malloc(sizeof(int)); *ip = key; code = table_insert(T, key, ip); if (code == 1) { i--; // since does not increase size of table // replaced. The chances should be very small printf("during random build generated duplicate key (%u) on trial (%d)\n", key, i); printf("this should be unlikely: if see more than a few you have a problem\n"); } else if (code != 0) { printf("build of random table failed code (%d) index (%d) key (%u)\n", code, i, key); exit(2); } probes += table_stats(T); } return probes; }
int load_add_message(char *message_name, int message_id) { id_type id = (id_type) SafeMalloc(sizeof(id_struct)); message_handler_type m = (message_handler_type) SafeMalloc(sizeof(message_handler_struct)); message_header_type h = (message_header_type) SafeMalloc(sizeof(message_header_struct)); id->name = strdup(message_name); id->idnum = message_id; id->type = I_MESSAGE; id->ownernum = st.curclass; id->source = DBASE; h->message_id = id; h->params = NULL; m->header = h; m->locals = NULL; m->body = NULL; /* Add message to message list of current class */ if (current_class == NULL) simple_error("Message appears outside of class in database file"); else current_class->messages = list_add_item(current_class->messages, (void *) m); current_message = h; st.curmessage = message_id; /* OK if message already in table; just ignore return value */ table_insert(st.globalvars, (void *) id, id_hash, id_compare); return True; }
/* build a table with sequential keys. The starting address is random. The * keys are are in adjacent table locations. */ int build_seq(table_t *T, int table_size, int num_addr) { hashkey_t key; int i, range, starting, code; int *ip; int probes = 0; range = MAXID - MINID + 1; starting = (int) (drand48() * range) + MINID; if (starting >= MAXID - table_size) starting -= table_size; for (i = starting; i < starting + num_addr; i++) { assert(MINID <= i && i <= MAXID); key = i; ip = (int *) malloc(sizeof(int)); *ip = i; code = table_insert(T, key, ip); if (code != 0) { printf("build of sequential table failed code (%d) index (%d) key (%u)\n", code, i - starting, key); exit(3); } probes += table_stats(T); } return probes; }
/* Return False on error, True on success */ int load_add_class(char *class_name, int class_id, int superclass_id, char *superclass_name) { /* Build up a class data structure for the new class. */ id_type id = (id_type) SafeMalloc(sizeof(id_struct)); id_type temp_id = (id_type) SafeMalloc(sizeof(id_struct)); class_type c = (class_type) SafeMalloc(sizeof(class_struct)); id->name = strdup(class_name); id->idnum = class_id; id->type = I_CLASS; id->source = DBASE; c->class_id = id; c->properties = c->messages = c->resources = c->classvars = NULL; c->is_new = False; /* Don't generate code for this class */ /* Store superclass id # in pointer for now. Id # will be converted to pointer * when build_superclasses below is called. */ c->superclass = (class_type)(intptr_t) superclass_id; /* Add to list of classes that have been read in */ st.classes = list_add_item(st.classes, (void *) c); current_class = c; st.curclass = class_id; current_message = NULL; /* Call table_insert instead of add_identifier so that our id # from * the database file is preserved. */ if (table_insert(st.globalvars, (void *) id, id_hash, id_compare) == 0) return True; else return False; }
//return new pRoot element* table_insert(element* pRoot, element* pBlock) { element* pCur = pRoot; if (element_size(pCur) == element_size(pBlock)) { //pCur->pBlock->pChild element_child(pBlock) = element_child(pCur); element_child(pCur) = pBlock; } else if (element_size(pCur) > element_size(pBlock)) { //pBlock //pCur element_sibling(pBlock) = pCur; pCur = pBlock; } else { //pCur //pBlock MY_ASSERT(element_size(pCur) < element_size(pBlock)); if (element_sibling(pCur) != NULL) { element_sibling(pCur) = table_insert(element_sibling(pCur), pBlock); } else { element_sibling(pCur) = pBlock; } } return pCur; };
/* FIXME */ void sall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; struct kvd *kvd; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); } dstr_free(res); //-------------------------------------------------------------------------------------------------------------------- // FIXME //-------------------------------------------------------------------------------------------------------------------- }
symbol_table *table_union(symbol_table *a, symbol_table *b) { symbol_table *ret = new_symbol_table(); closure *item; int i; closure *chain; start_table_iteration(b, item, i){ if (!leakedp(cheap_cdr(item)) || nilp(table_lookup(cheap_car(item), ret))) table_insert(cheap_car(item), second(item), ret); } end_table_iteration; start_table_iteration(a, item, i){ if (!leakedp(cheap_cdr(item)) || nilp(table_lookup(cheap_car(item), ret))) table_insert(cheap_car(item), second(item), ret); } end_table_iteration; return ret; }
param_type make_parameter(id_type id, expr_type e) { param_type p = (param_type) SafeMalloc(sizeof(param_struct)); if (e->type != E_CONSTANT) { action_error("Parameter can only be initialized to a constant"); return p; } lookup_id(id); /* Left-hand side must not have appeared before, except perhaps as a parameter */ switch (id->type) { case I_MISSING: /* The parameter has been referenced in a function call, but not declared anywhere * We should use the existent id # and remove the id from the missing list. * First we must make sure that the missing id is supposed to be a missing parameter. */ if (id->source != I_PARAMETER) { action_error("Parameter %s was referenced elsewhere with different type", id->name); break; } /* Insert directly into global table to preserve id # */ id->type = I_PARAMETER; id->source = COMPILE; table_insert(st.globalvars, (void *) id, id_hash, id_compare); /* Remove from missing list */ table_delete_item(st.missingvars, id, id_hash, id_compare); break; case I_UNDEFINED: /* New parameter # */ id->ownernum = st.curmessage; add_identifier(id, I_PARAMETER); break; case I_PARAMETER: /* Legal only if it hasn't yet appeared in this message */ if (id->ownernum == st.curmessage && id->source == COMPILE) action_error("Parameter %s appears twice", id->name); break; default: /* Other types indicate name already used */ action_error("Duplicate identifier %s", id->name); } p->lhs = id; p->rhs = e->value.constval; return p; }
symbol_table *closing_to_table(closure *a) { symbol_table *ret = new_symbol_table(); while(!nilp(a)){ if(nilp(table_lookup(cheap_car(cheap_car(a)), ret))){ table_insert(cheap_car(cheap_car(a)), second(cheap_car(a)), ret); } a = cheap_cdr(a); } return ret; }
/* Return False on error, True on success */ int load_add_class(char *class_name, int class_id, int superclass_id, char *superclass_name) { /* Build up a class data structure for the new class. */ id_type id = (id_type)SafeMalloc(sizeof(id_struct)); id_type temp_id = (id_type)SafeMalloc(sizeof(id_struct)); class_type c = (class_type)SafeMalloc(sizeof(class_struct)); // Adding new built-in object types will render existing kodbase.txt files // incompatible. This isn't a problem as a pre-existing kodbase.txt is only // required for reloading a live server, so a new one can be made. Check // for built-in class name/ID mismatches here and instruct the user to // delete kodbase.txt if this check fails. extern id_struct BuiltinIds[]; if ((strcmp(BuiltinIds[SETTINGS_CLASS].name, class_name) == 0 && class_id != SETTINGS_CLASS) || (strcmp(BuiltinIds[REALTIME_CLASS].name, class_name) == 0 && class_id != REALTIME_CLASS) || (strcmp(BuiltinIds[EVENTENGINE_CLASS].name, class_name) == 0 && class_id != EVENTENGINE_CLASS)) { database_error("Incompatible kodbase.txt. Delete the file and recompile."); return False; } id->name = strdup(class_name); id->idnum = class_id; id->type = I_CLASS; id->source = DBASE; c->class_id = id; c->properties = c->messages = c->resources = c->classvars = NULL; c->is_new = False; /* Don't generate code for this class */ /* Store superclass id # in pointer for now. Id # will be converted to pointer * when build_superclasses below is called. */ c->superclass = (class_type) superclass_id; /* Add to list of classes that have been read in */ st.classes = list_add_item(st.classes, (void *) c); current_class = c; st.curclass = class_id; current_message = NULL; /* Call table_insert instead of add_identifier so that our id # from * the database file is preserved. */ if (table_insert(st.globalvars, (void *) id, id_hash, id_compare) == 0) return True; else return False; }
void test_hash(int prime, int table_size, int elems_count) { struct HashTableParams params = GET_HASH_PARAMS(&prime, hash_func, 0, int); struct HashTable *table = allocate_table(table_size, params); for (int i = 0; i != elems_count; ++i) { for (int j = 0; j != i; ++j) { int value = j * j; assert(table_has(table, &value)); assert(table_insert(table, &value) == 1); } for (int j = i; j != elems_count; ++j) { int value = j * j; assert(!table_has(table, &value)); assert(!table_remove(table, &value)); } int value = i * i; assert(!table_insert(table, &value)); } for (int i = 0; i != elems_count; ++i) { for (int j = i; j != elems_count; ++j) { int value = j * j; assert(table_has(table, &value)); } int value = i * i; assert(table_remove(table, &value)); assert(!table_remove(table, &value)); for (int j = 0; j != i; ++j) { int value = j * j; assert(!table_has(table, &value)); } } deallocate_table(table); }
int load_add_external(char *name, int idnum, int type) { id_type id = (id_type) SafeMalloc(sizeof(id_struct)); /* Add unresolved externals to list of missing vars */ id->name = strdup(name); id->type = I_MISSING; id->idnum = idnum; id->source = type; if (table_insert(st.missingvars, (void *) id, id_hash, id_compare) == 0) return True; return False; }
/*-------Environment-------*/ pointer environment_make2(VM, pointer rib, pointer env) { pointer t; save(vm, rib); save(vm, env); t = table_alloc(vm); while (!AR_ISNIL(rib)) { table_insert(vm, t, caar(rib), cdar(rib)); rib = cdr(rib); } unsave(vm, 2); return tagged_alloc(vm, vm->s_environment, cons(t,AR_ISNIL(env)?vm->nil:rep(env))); }
/* build a table with folded keys. The starting address is random. The first * set of keys are sequential, and the second set hashes to the same table * locations as the first set. */ int build_fold(table_t *T, int table_size, int num_addr) { int i, range, starting, code; int probes = 0; int *ip; range = MAXID - MINID + 1; starting = (int) (drand48() * range) + MINID; if (starting <= MINID + table_size) starting += table_size; if (starting >= MAXID - table_size) starting -= table_size; for (i = starting; i > starting - num_addr/2; i--) { assert(MINID <= i && i <= MAXID); ip = (int *) malloc(sizeof(int)); *ip = i; code = table_insert(T, i, ip); if (code != 0) { printf("build of first phase of folded table failed code (%d) index (%d) key (%d)\n", code, i - starting, i); exit(4); } probes += table_stats(T); } for (i = starting + table_size; i > starting + table_size - (num_addr+1)/2; i--) { assert(MINID <= i && i <= MAXID); ip = (int *) malloc(sizeof(int)); *ip = i; code = table_insert(T, i, ip); if (code != 0) { printf("build of second phase of folded table failed code (%d) index (%d) key (%d)\n", code, i - starting, i); exit(5); } probes += table_stats(T); } return probes; }
void ChangeResource(ID res, char *value) { resource_type r, entry; // debug(("got new resource %d, type = %d, value = %s\n", res, type, value)); r = (resource_type) table_lookup(t, &res, IdHash, IdResourceCompare); if (r != NULL) { table_delete_item(t, r, ResourceHash, ResourceCompare); FreeRsc(r); } entry = (resource_type) SafeMalloc(sizeof(resource_struct)); entry->data = (char *) SafeMalloc(strlen(value) + 1); strcpy(entry->data, value); entry->idnum = res; table_insert(t, entry, ResourceHash, ResourceCompare); }
void test_2() { //generate_keys(30, 2); int i; enum { nNode = 50 }; int keys[nNode] = { 0x38, 0x85, 0x37, 0x55, 0x43, 0x86, 0x78, 0x23, 0x62, 0x57, 0x24, 0x02, 0x17, 0x34, 0x96, 0x22, 0x07, 0x89, 0x91, 0x12, 0x61, 0x05, 0x36, 0x09, 0x41, 0x00, 0x31, 0x88, 0x94, 0x04, 0x18, 0x97, 0x68, 0x35, 0x83, 0x53, 0x93, 0x64, 0x65, 0x03, 0x58, 0x60, 0x52, 0x15, 0x30, 0x06, 0x25, 0x32, 0x39, 0x63 }; queue q(nNode); element* pNode = NULL; element* pRoot = NULL; //init pRoot = element_crt(keys[0]); q.push(pRoot); //generate data for (i = 1; i < nNode;) { //add node pNode = element_crt(keys[i]); q.push(pNode); pRoot = table_insert(pRoot, pNode); i++; } //print int count = table_printf(pRoot); printf(" count %d\n", count); //clean for (pNode = (element*)q.pop(); pNode != NULL; pNode = (element*)q.pop()) { pRoot = table_remove(pRoot, pNode); count = table_printf(pRoot); printf(" count %d\n", count); element_del(pNode); } }
/* * strtab_insert: Copy string into table if not already present. Return * pointer to string in table. */ char * strtab_insert(STRTAB * strtab, char *str, ID_TYPE ** id) { char *s; BUFFER *buf; int size; if (strtab->index == NULL) return NULL; s = (char *) table_find(strtab->index, str, 0, 0); if (s) { if (id) *id = (ID_TYPE *) (s - IDSIZE); return s; } size = IDSIZE + strlen(str) + 1; if (strtab->size < size) { buf = (BUFFER *) malloc(sizeof *buf + size); CHECK_MALLOC(buf); buf->next = strtab->buf_list; strtab->buf_list = buf; strtab->buf_ptr = (char *) (buf->buf); strtab->size = size + sizeof buf->buf; } *(long *) strtab->buf_ptr = 0; strcpy(strtab->buf_ptr + IDSIZE, str); s = (char *) table_insert(strtab->index, strtab->buf_ptr + IDSIZE, 0); if (id) *id = (ID_TYPE *) (strtab->buf_ptr); size = (size + alignSize) & ~alignSize; strtab->buf_ptr += size; strtab->size -= size; upfix_exclude(strtab->upfix, str); return s; }
//----------------------------- void test_1() { element arr[] = { { 0x100 }, { 0x200 }, { 0x300 }, { 0x300 }, { 0x400 }, }; element* pRoot = &arr[0]; for (int i = 1; i < (sizeof(arr) / sizeof(arr[0])); i++) { pRoot = table_insert(pRoot, &arr[i]); printf("insert %d\n", i); table_printf(pRoot); } for (int i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++) { pRoot = table_remove(pRoot, &arr[i]); printf("remove %d\n", i); table_printf(pRoot); } }
int main () { table_t* tablePtr; long hash[] = {3, 1, 4, 1, 5, 9, 2, 6, 8, 7, -1}; long i; bool_t status = memory_init(1, 4, 2); assert(status); puts("Starting..."); tablePtr = table_alloc(8, NULL); for (i = 0; hash[i] >= 0; i++ ) { bool_t status = table_insert(tablePtr, (ulong_t)hash[i], (void*)&hash[i]) assert(status); printTable(tablePtr); puts(""); } for (i = 0; hash[i] >= 0; i++ ) { bool_t status = table_remove(tablePtr, (ulong_t)hash[i], (void*)&hash[i]) assert(status); printTable(tablePtr); puts(""); } table_free(tablePtr); puts("Done."); return 0; }