/** * Inserts the data with the given hash into the table. * * Note that insertion may rearrange the table on a resize or rehash, * so previously found hash_entries are no longer valid after this function. */ int ht_insert(SdbHash *ht, ut32 hash, void *data, SdbListIter *iter) { ut32 hash_address; if (ht->entries >= ht->max_entries) ht_rehash (ht, ht->size_index + 1); else if (ht->deleted_entries + ht->entries >= ht->max_entries) ht_rehash (ht, ht->size_index); hash_address = hash % ht->size; do { SdbHashEntry *entry = ht->table + hash_address; ut32 double_hash; if (!entry_is_present (entry)) { if (entry_is_deleted (entry)) ht->deleted_entries--; entry->hash = hash; entry->data = data; if (!rehash) entry->iter = ls_append (ht->list, data); else entry->iter = iter; ht->entries++; return 1; } double_hash = hash % ht->rehash; if (double_hash == 0) double_hash = 1; hash_address = (hash_address + double_hash) % ht->size; } while (hash_address != hash % ht->size); /* We could hit here if a required resize failed. An unchecked-malloc * application could ignore this result. */ return 0; }
bool ht_add(Hash_Table *ht, void *T) { if (ec_struct(ht, HTABLE_NAME) || ec_type(T, HTABLE_NAME)) return false; int position = ht_find_position(ht, T); Hash_Entry *entry = ht_create_hash_entry(T); ht->table[position] = entry; ht->occupied++; if ((float) ht->occupied / ht->table_size >= HT_LOAD_FACTOR) ht_rehash(ht); return true; }