void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL) name = ""; name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof buf, first, rest)) { Set_T set; Table_T files; const char *id = Atom_string(buf); files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } set = Table_get(files, name); if (set == NULL) { set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } { int *p = &linenum; if (!Set_member(set, p)) { NEW(p); *p = linenum; Set_put(set, p); } } } }
void test_Atom_string(void) { const char *str; atom = Atom_string(msg); str = Atom_string(msg); CU_ASSERT_EQUAL(str, atom); CU_ASSERT_STRING_EQUAL(str, msg); }
struct gto * init_goto() { int i; struct gto *g = NULL; abstr_meta_var = Atom_string("_"); g = malloc(sizeof(*g)); g->ary = malloc(sizeof(int *)); g->ary[0] = malloc(128*sizeof(int)); g->ary_len = 1; for (i = 0; i < 128; ++i) g->ary[0][i] = FAIL; g->output = malloc(sizeof(*g->output)); g->output_len = 1; g->output->len = g->output->max = 0; g->output->out = NULL; g->max_node_count = 0; return g; }
void wf(char *name, FILE *fp) { Table_T table = Table_new(0, NULL, NULL); char buf[128]; while (getword(fp, buf, sizeof buf, first, rest)) { const char *word; int i, *count; for (i = 0; buf[i] != '\0'; i++) buf[i] = tolower(buf[i]); word = Atom_string(buf); count = Table_get(table, word); if (count) (*count)++; else { NEW(count); *count = 1; Table_put(table, word, count); } } if (name) printf("%s:\n", name); { int i; void **array = Table_toArray(table, NULL); qsort(array, Table_length(table), 2*sizeof (*array), compare); for (i = 0; array[i]; i += 2) printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]); FREE(array); } Table_map(table, vfree, NULL); Table_free(&table); }
T Umsections_new (const char *section, int (*error)(void *errstate, const char *message), void *errstate) { T Umsections = malloc(sizeof(*Umsections)); Umsections->sections = Table_new(TABLE_HINT, NULL, NULL); Seq_T temp; temp = Seq_new(SEQ_HINT); Seq_T temp2; temp2 = Seq_new(SEQ_HINT); Umsections->error = error; Umsections->errstate = errstate; Umsections->currentSection = temp; Umsections->sectionOrder = temp2; Seq_addlo(Umsections->sectionOrder, temp); Table_put(Umsections->sections, Atom_string(section), temp); /* Table_map((Umsections)->sections,applyFree,NULL); Table_free(&((Umsections)->sections)); Seq_free(&((Umsections)->sectionOrder)); // Seq_free(((Umsectinos)->currentSection)); free(Umsections); exit(1);*/ return Umsections; }
/* Umsections_new makes a new 'assembler' in the form of a Umsections_T. It * creates a new table and makes the first key value pair with the given * section. It also sets the assembler to emit to that section. */ Umsections_T Umsections_new(const char *section, int (*error)(void *errstate, const char *message), void *errstate) { Umsections_T assembler = malloc(sizeof(*assembler)); assert(assembler); Table_T table = Table_new(100, NULL, NULL); assert(table); Seq_T order = Seq_new(100); assert(order); Seq_addhi(order, (void *)section); Seq_T instructions = Seq_new(100); assert(instructions); Table_put(table, Atom_string(section), instructions); /* intializes the struct */ assembler->table = table; assembler->section = section; assembler->order = order; assembler->err_func = error; assembler->errstate = errstate; return assembler; }
void test_Atom_length(void) { atom = Atom_string(msg); CU_ASSERT_EQUAL(Atom_length(atom), strlen(msg)); atom = Atom_new(msg, strlen(msg) + 1); CU_ASSERT_NOT_EQUAL(Atom_length(atom), strlen(msg)); }
/*Inserts the value into the table under the corresponding fingerprint*/ void insert_atom(Table_T* table, char* fingerprint, char* name) { const char *key; const char *value; Set_T old_set; /*Turns fingerprint and name strings into Hanson atoms*/ key = Atom_string(fingerprint); value = Atom_string(name); old_set = Table_get(*table, key); if (old_set == NULL) { Set_T new_set = Set_new(1, NULL, NULL); Set_put(new_set, value); Table_put(*table, key, new_set); } else { Set_put(old_set, value); } }
void test_Atom_map(void) { Atom_reset(); atom = Atom_string(msg); CU_ASSERT_EQUAL(Atom_count(), 1); Atom_reset(); int i; for (i = 0; i < 10; i++) Atom_int(i); CU_ASSERT_EQUAL(Atom_count(), 10); }
void xref(const char *name, FILE *fp, Table_T identifiers){ char buf[128]; if (name == NULL){ name = ""; } name = Atom_string(name); linenum = 1; while (getword(fp, buf, sizeof(buf), first, rest)){ Set_T set; Table_T files; const char *id = Atom_string(buf); // files <- file table in identifiers associated with id files = Table_get(identifiers, id); if (files == NULL){ files = Table_new(0, NULL, NULL); Table_put(identifiers, id, files); } // set <- set in files associated with name set = Table_get(files, name); if (set == NULL){ set = Set_new(0, intcmp, inthash); Table_put(files, name, set); } // add linenum to set, if necessary { int *p = &linenum; if (!Set_member(set, p)){ NEW(p); *p = linenum; Set_put(set, p); } } } }
int main(int argc, char * argv[]) { int i, j ; char str[STRLENMAX]; size_t len; char *atom; int hit = 0; int miss = 0; clock_t s_new, f_new, s_len, f_len; printf("[test] %d atoms of maxsize = %d B\n",STRNUM, STRLENMAX); srand(time(NULL)); //prepare data for ( i=0; i< STRNUM ; i++ ) { len = (double) rand() / RAND_MAX * STRLENMAX - 1; strings[i].len = len; rand_str( strings[i].str, len); } //new atoms i = STRNUM; s_new = clock(); while( i-- > 0 ){ atom = (char *)Atom_string(strings[i].str); strings[i].atom = atom; } f_new = clock(); // hit len s_len = clock(); i = STRNUM; while( i-- > 0 ){ if (Atom_length(strings[i].atom) == strings[i].len) hit ++; else miss ++; } f_len = clock(); printf("new %f s\n",(float)(f_new - s_new) / CLOCKS_PER_SEC ); printf("len %f s\n",(float)(f_len- s_len) / CLOCKS_PER_SEC ); printf("hit %d / miss %d \n", hit, miss); return 0; }
void store_name(char* fprint_buffer, char* name_buf, T* table, L* name_list) { const char* fprint = Atom_string(fprint_buffer); const char* name = Atom_string(name_buf); char **name_arr = (char**) List_toArray(*name_list, NULL); for(int z = 0; name_arr[z]; z++) { if(name_arr[z] == name) { fprintf(stderr, "Duplicate name detected: Ignoring input\n"); free (name_arr); return; } } free(name_arr); *name_list = List_push(*name_list, (void*) name); L list = (L) Table_get(*table, fprint); if (list == NULL) { list = List_list((char*) name, NULL); } else { list = List_push(list, (void*) name); } Table_put(*table, fprint, list); }
void test_Atom_free(void) { Atom_reset(); atom = Atom_string(msg); Atom_free(atom); CU_ASSERT_EQUAL(Atom_count(), 0); int i; for (i = 0; i < 10000; i++) Atom_int(i); for (i = 0; i < 5000; i += 2) { atom = Atom_int(i); Atom_free(atom); } CU_ASSERT_EQUAL(Atom_count(), 7500); }
int addToTable( Set_T nameSet, Table_T fpTable, fpPair pair) { const char * nameAtom = Atom_string(pair.name); if( !Set_member( nameSet, nameAtom) ) { Set_put( nameSet, nameAtom); addToTableHelp( fpTable, pair ); return 1; } else{ free (pair.name); free (pair.fp); return 0; } }
unsigned test_table() { Table_T tbl = Table_new(10, NULL, NULL); struct Test t1 = { 10, "Luca"}; char* s1 = TestToStr(&t1); const char* a1 = Atom_string(s1); char * result; Table_put(tbl, a1, "Luca"); result = (char*) Table_get(tbl, a1); test_assert(strcmp(result, "Luca") == 0); Table_free(&tbl); FREE(s1); Atom_freeAll(); return TEST_SUCCESS; }
void addToTableHelp( Table_T fpTable, fpPair pair ) { List_T names = NULL; const char * fpAtom = Atom_string(pair.fp); // if the key does exist in the table // get the list already stored if( Table_get( fpTable, fpAtom ) != NULL ) { names = Table_get( fpTable, fpAtom ); } // pushes the the list names = List_push( names, pair.name); Table_put( fpTable, fpAtom, names ); free(pair.fp); // List_map( names , applyFree, NULL ); }
int main() { FILE *fp = fopen("test.txt", "r"); int c = 0; int i = 0; char s[512]; Table_T our_table = Table_new(10, NULL, NULL); char * name; const char *fingerprint; const char* temp; while((c=fgetc(fp)) != ' ' && c != EOF) //Fingerprint section { s[i]= c; i++; } s[i] = '\0'; //Here we have fingerprint in s fingerprint = Atom_string(s); temp = Atom_string(s); i = 0; while((c = getc(fp)) != '\n' && c != EOF) { s[i] = c; i++; } s[i] = '\0'; //Here we have name in s name = s; Table_put(our_table, fingerprint, name); while( c != EOF) { i = 0; while((c = fgetc(fp)) != ' ' && c != EOF) // Fingerprint section { s[i] = c; i++; } s[i] = '\0'; fingerprint = Atom_string(s); i = 0; while((c = getc(fp)) != '\n' && c != EOF) { s[i] = c; i++; } s[i] = '\0'; name = s; printf("FP: %s\n ", fingerprint); printf("NAME: %s\n", name); char * test = Table_put(our_table, fingerprint, name); printf("idk: %s\n", test); // printf("VALUE STORED IN FP: %s\n\n", (char *)Table_get(our_table, temp)); } // NEED TO DEAL WITH TABLE MAP AND IMPLEMENTING A LIST // Table_map(our_table, vfree, NULL); //printf("%s", fingerprint); // printf("%s", (char *)Table_get(our_table, fingerprint)); printf("%d", Table_length(our_table)); // Table_map(our_table, vfree, NULL); int length = Table_length(our_table); void **table = Table_toArray(our_table, NULL); for(int x = 0; x < 2*length; x++) { printf("%s ", (char *)table[x]); } Table_free(&our_table); return 0; }
char *string(const char *str) { return (char *)Atom_string(str); }
void Behavior::addTag( const char *tag ) { const char *atomizedTag = Atom_string( tag ); m_tags.push_back( atomizedTag ); }