int zhash_rename (zhash_t *self, char *old_key, char *new_key) { item_t *item; item_t *new_item; item = s_item_lookup (self, old_key); if (item) { s_item_destroy (self, item, FALSE); new_item = s_item_lookup (self, new_key); if (new_item == NULL) { free (item->key); item->key = _strdup (new_key); item->index = self->cached_index; item->next = self->items [self->cached_index]; self->items [self->cached_index] = item; self->size++; return 0; } else return -1; } else return -1; }
int zhash_rename (zhash_t *self, const void *old_key, const void *new_key) { item_t *old_item = s_item_lookup (self, old_key); item_t *new_item = s_item_lookup (self, new_key); if (old_item && !new_item) { s_item_destroy (self, old_item, false); if (self->key_destructor) (self->key_destructor)((void **) &old_item->key); if (self->key_duplicator) old_item->key = (self->key_duplicator)(new_key); else old_item->key = new_key; old_item->index = self->cached_index; old_item->next = self->items [self->cached_index]; self->items [self->cached_index] = old_item; self->size++; self->cursor_item = old_item; self->cursor_key = old_item->key; return 0; } else return -1; }
static item_t * s_item_insert (zhash_t *self, const char *key, void *value) { // Check that item does not already exist in hash table // Leaves self->cached_index with calculated hash item item_t *item = s_item_lookup (self, key); if (item == NULL) { item = (item_t *) zmalloc (sizeof (item_t)); if (!item) return NULL; // If necessary, take duplicate of item (string) value if (self->autofree) value = strdup ((char *) value); item->value = value; item->key = strdup (key); item->index = self->cached_index; // Insert into start of bucket list item->next = self->items [self->cached_index]; self->items [self->cached_index] = item; self->size++; } else item = NULL; // Signal duplicate insertion return item; }
int zhash_rename (zhash_t *self, const char *old_key, const char *new_key) { item_t *old_item = s_item_lookup (self, old_key); item_t *new_item = s_item_lookup (self, new_key); if (old_item && !new_item) { s_item_destroy (self, old_item, false); free (old_item->key); old_item->key = strdup (new_key); old_item->index = self->cached_index; old_item->next = self->items [self->cached_index]; self->items [self->cached_index] = old_item; self->size++; return 0; } else return -1; }
void zhash_delete (zhash_t *self, const char *key) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) s_item_destroy (self, item, true); }
void * zhash_lookup (zhash_t *self, const char *key) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) return item->value; else return NULL; }
void zhash_delete (zhash_t *self, char *key) { item_t *item; assert (self); assert (key); item = s_item_lookup (self, key); if (item) s_item_destroy (self, item, TRUE); }
void * zhash_freefn (zhash_t *self, const char *key, zhash_free_fn *free_fn) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) { item->free_fn = free_fn; return item->value; } else return NULL; }
void * zhash_lookup (zhash_t *self, const void *key) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) { self->cursor_item = item; self->cursor_key = item->key; return item->value; } else return NULL; }
void zhash_update (zhash_t *self, char *key, void *value) { item_t *item; assert (self); assert (key); item = s_item_lookup (self, key); if (item) { if (item->free_fn) (item->free_fn) (item->value); item->value = value; } else zhash_insert (self, key, value); }
void zhash_update (zhash_t *self, const char *key, void *value) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) { if (item->free_fn) (item->free_fn) (item->value); else if (self->autofree) free (item->value); // If necessary, take duplicate of item (string) value if (self->autofree) value = strdup ((char *) value); item->value = value; } else zhash_insert (self, key, value); }
void zhash_update (zhash_t *self, const void *key, void *value) { assert (self); assert (key); item_t *item = s_item_lookup (self, key); if (item) { if (self->destructor) (self->destructor)(&item->value); else if (item->free_fn) (item->free_fn)(item->value); // If necessary, take duplicate of item value if (self->duplicator) item->value = (self->duplicator)(value); else item->value = value; } else zhash_insert (self, key, value); }
static item_t * s_item_insert (zhash_t *self, const void *key, void *value) { // Check that item does not already exist in hash table // Leaves self->cached_index with calculated hash item item_t *item = s_item_lookup (self, key); if (item == NULL) { item = (item_t *) zmalloc (sizeof (item_t)); if (!item) return NULL; // If necessary, take duplicate of item key if (self->key_duplicator) item->key = (self->key_duplicator)((void *) key); else item->key = key; // If necessary, take duplicate of item value if (self->duplicator) item->value = (self->duplicator)(value); else item->value = value; item->index = self->cached_index; // Insert into start of bucket list item->next = self->items [self->cached_index]; self->items [self->cached_index] = item; self->size++; self->cursor_item = item; self->cursor_key = item->key; } else item = NULL; // Signal duplicate insertion return item; }