void set_tbl (table * t, obj index, obj value){ int i; obj cur_key; for (i = 0 ; i < t->size ; i++){ cur_key = t->keys[i]; if (!APTR(cur_key) || !ASTR(cur_key)){ if (cur_key == index) break; } else if (ASTR(cur_key)) if (eq_str ((string *) cur_key, (string *) index)) break; } if (i == t->max_size){ t->max_size *= 2; t->keys = realloc (t->keys, sizeof (obj) * t->max_size); t->values = realloc (t->values, sizeof (obj) * t->max_size); } if (i == t->size) t->size++; t->keys[i] = index; t->values[i] = value; }
int main() { hash_table_t* people = hash_table_init(100); entry_t oliv = { "Pinon", "Olivier", "01 02 03 04", "Paris" }; entry_t pa = { "Durand", "Pierre-Alexandre", "33 04 10", "Lyon" }; printf("Does string equality work ? %d\n", eq_str(pa.name, "Durand")); hash_table_insert( people, &oliv ); hash_table_insert( people, &pa ); hash_table_print( people ); list_t test = hash_table_find_by_name(people, "Durand"); printf("%s\n ", "Is M.Durand here ?"); print_list(test); destroy_list(test); test = hash_table_find_by_surname(people, "Pierre-Alexandre"); printf("%s\n ", "Is Pierre-Alexandre here ?"); print_list(test); destroy_list(test); printf("hash map test done\n"); hash_table_print(people); hash_table_destroy( people ); return 0; };
obj tbl_lookup (table * t, obj index){ int i; obj cur_key; for (i = 0 ; i < t->size ; i++){ cur_key = t->keys[i]; if (!APTR(cur_key) || !ASTR(cur_key)){ if (cur_key == index) return t->values[i]; } else if (ASTR(cur_key)) if (eq_str ((string *) cur_key, (string *) index)) return t->values[i]; } return SYM2OBJ("nil"); }
static void pkg_add_file(struct pkg * slf, const char * fname){ if(ends_with(fname, ".h")){ slf->h_file = strdup(fname); }else if(ends_with(fname, ".c")){ slf->c_file = strdup(fname); }else{ panic("pkg.add_file only allows .h or .c files", __FILE__,__LINE__); } char ** imports = get_deps_from_fname(fname); if(imports){ int i; for(i = 0; imports[i]; i++){ if(! eq_str(imports[i], slf->name) ){ slf->deps->add(slf->deps, imports[i]); } } free_strings(imports); } };