unsigned long get_index(MMspacetable *st, char *file, unsigned long line, void *ptr) { unsigned long i; // iterate over all elements for (i = 0; i < st->number; i++) { // if we find what we're looking for, // we can return. If not, note that // i now has the value of st's capacity if (ptr == st->blocks[i].block) return i; } // if the element wasn't included, and also isn't a null pointer, // something went wrong if (ptr != NULL) { fprintf(stderr, "Invalid pointer! %s line %lu\n", file, line); exit(EXIT_FAILURE); } // the block we were looking for isn't in the table // so we need to enlarge it enlarge_table(st); // i is still st's previous capacity, // therefore the first free index of st return i; }
void hash_add(Hash_Table *t, Label to_add) { int size = t->size; Label *table = t->table; int key = find_place(t, to_add.name); if (table[key].name == NULL) { table[key] = to_add; } else { free(to_add.name); } if (t->elements >= 0.8 * size) { enlarge_table(t); } }