void init_pred_table(pred_table_t *pred_table){ int32_t size = INIT_PRED_TABLE_SIZE; if (size >= MAXSIZE(sizeof(pred_entry_t), 0)){ out_of_memory(); } pred_table->evpred_tbl.size = size; pred_table->evpred_tbl.num_preds = 1; pred_table->evpred_tbl.entries = (pred_entry_t*) safe_malloc(size*sizeof(pred_entry_t)); pred_table->pred_tbl.size = size; pred_table->pred_tbl.num_preds = 1; pred_table->pred_tbl.entries = (pred_entry_t*) safe_malloc(size*sizeof(pred_entry_t)); init_stbl(&(pred_table->pred_name_index), size); stbl_add(&(pred_table->pred_name_index), truepred, 0); pred_table->pred_tbl.entries[0].arity = 0; pred_table->pred_tbl.entries[0].name = truepred; pred_table->pred_tbl.entries[0].signature = NULL; pred_table->pred_tbl.entries[0].size_atoms = 0; pred_table->pred_tbl.entries[0].num_atoms = 0; pred_table->pred_tbl.entries[0].atoms = NULL; pred_table->evpred_tbl.entries[0].arity = 0; pred_table->evpred_tbl.entries[0].name = truepred; pred_table->evpred_tbl.entries[0].signature = NULL; pred_table->evpred_tbl.entries[0].size_atoms = 0; pred_table->evpred_tbl.entries[0].num_atoms = 0; pred_table->evpred_tbl.entries[0].atoms = NULL; }
/* * Initialize: empty symbol table * - all symbols have scope 0 (unassigned) */ static void init(void) { uint32_t i; init_stbl(&sym_table, 2); // use a small initial size to trigger resizing stbl_set_finalizer(&sym_table, finalize); for (i=0; i<NSYMBOLS; i++) { scope[i] = 0; } }
void init_var_table(var_table_t *var_table){ int32_t size = INIT_VAR_TABLE_SIZE; if (size >= MAXSIZE(sizeof(var_entry_t), 0)){ out_of_memory(); } var_table->size = size; var_table->num_vars = 0; var_table->entries = (var_entry_t*) safe_malloc(size*sizeof(var_entry_t)); init_stbl(&(var_table->var_name_index), 0); }
void init_const_table(const_table_t *const_table){ int32_t size = INIT_CONST_TABLE_SIZE; if (size >= MAXSIZE(sizeof(const_entry_t), 0)){ out_of_memory(); } const_table->size = size; const_table->num_consts = 0; const_table->entries = (const_entry_t*) safe_malloc(size*sizeof(const_entry_t)); init_stbl(&(const_table->const_name_index), 0); }
void init_sort_table(sort_table_t *sort_table){ int32_t size = INIT_SORT_TABLE_SIZE; if (size >= MAXSIZE(sizeof(sort_entry_t), 0)){ out_of_memory(); } sort_table->size = size; sort_table->num_sorts = 0; sort_table->entries = (sort_entry_t*)safe_malloc( size*sizeof(sort_entry_t)); /* this is not really needed */ //for (i=0; i<size; i++){ // sort_table->entries[i].cardinality = 0; // sort_table->entries[i].name = NULL; //} init_stbl(&(sort_table->sort_name_index), 0); }
int main(int argc, char *argv[]) { uint32_t i, j, n; double runtime, memused; int32_t x, *val; if (argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); fprintf(stderr, " filename must contain a set of strings, one per line, less than 100 char long\n"); fflush(stderr); exit(1); } words_from_file(argv[1]); init_stbl(&sym_table, 0); val = (int32_t *) malloc(n_words * sizeof(int32_t)); if (val == NULL) { fprintf(stderr, "Failed to allocate array val\n"); exit(1); } for (i=0; i<n_words; i++) { x = stbl_find(&sym_table, words[i]); if (x < 0) { stbl_add(&sym_table, words[i], i); val[i] = i; } else { val[i] = x; } } printf("\n--- checking ---\n"); for (i=0; i<n_words; i++) { x = stbl_find(&sym_table, words[i]); if (x != val[i]) { printf("*** Error: %s, val = %"PRId32", should be %"PRId32" ***\n", words[i], x, val[i]); fflush(stdout); exit(1); } } printf("\n*** Added %"PRIu32" words from %s ***\n", n_words, argv[1]); // repeated additions of the same symbols with multiple lookups // warning: this code does not work (may give a false alarm) // if the input file contains duplicates. n = (n_words < 200) ? n_words : 200; printf("\n*** Repeated symbol addition ***\n"); runtime = get_cpu_time(); for (i=0; i<10000; i++) { for (j=0; j<n; j++) { stbl_add(&sym_table, words[j], new_val(i, j)); } for (j=0; j<n; j++) { x = stbl_find(&sym_table, words[j]); if (x != new_val(i, j)) { printf("*** Error: %s, val = %"PRId32", should be %"PRId32" ***\n", words[j], x, new_val(i, j)); fflush(stdout); exit(1); } } for (j=0; j<n; j++) { x = stbl_find(&sym_table, words[j]); if (x != new_val(i, j)) { printf("*** Error: %s, val = %"PRId32", should be %"PRId32" ***\n", words[j], x, new_val(i, j)); fflush(stdout); exit(1); } } for (j=0; j<n_words; j++) { x = stbl_find(&sym_table, words[j]); } for (j=0; j<n; j++) { x = stbl_find(&sym_table, words[j]); if (x != new_val(i, j)) { printf("*** Error: %s, val = %"PRId32", should be %"PRId32" ***\n", words[j], x, new_val(i, j)); fflush(stdout); exit(1); } } for (j=0; j<n_words; j++) { x = stbl_find(&sym_table, words[j]); } } runtime = get_cpu_time() - runtime; memused = mem_size() / (1024 * 1024); printf("Adding 10000 times the same %"PRIu32" words + repeated lookups\n", n); printf("Runtime: %.4f s\n", runtime); printf("Table size: %"PRIu32" (nelems = %"PRIu32", ndeleted = %"PRIu32")\n", sym_table.size, sym_table.nelems, sym_table.ndeleted); if (memused > 0) { printf("Memory used: %.2f MB\n", memused); } clear_words(); free(val); delete_stbl(&sym_table); return 0; }