/** * 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; }
SDB_API SdbListIter *ls_insert(SdbList *list, int n, void *data) { SdbListIter *it, *item; int i; if (list) { if (!list->head || !n) { return ls_prepend (list, data); } for (it = list->head, i = 0; it && it->data; it = it->n, i++) { if (i == n) { item = R_NEW0 (SdbListIter); if (!item) { return NULL; } item->data = data; item->n = it; item->p = it->p; if (it->p) { it->p->n = item; } it->p = item; list->length++; list->sorted = false; return item; } } } return ls_append (list, data); }