Beispiel #1
0
void
zhash_destroy (zhash_t **self_p)
{
    assert (self_p);
    if (*self_p) {
        zhash_t *self = *self_p;
        uint index;
        for (index = 0; index < self->limit; index++) {
            //  Destroy all items in this hash bucket
            item_t *cur_item = self->items [index];
            while (cur_item) {
                item_t *next_item = cur_item->next;
                s_item_destroy (self, cur_item, true);
                cur_item = next_item;
            }
        }
        if (self->items)
            free (self->items);

        zlist_destroy (&self->comments);
        free (self->filename);
        free (self);
        *self_p = NULL;
    }
}
Beispiel #2
0
int
zhash_refresh (zhash_t *self)
{
    assert (self);

    if (self->filename) {
        if (  zsys_file_modified (self->filename) > self->modified
           && zsys_file_stable (self->filename)) {
            //  Empty the hash table; code is copied from zhash_destroy
            uint index;
            size_t limit = primes [self->prime_index];
            for (index = 0; index < limit; index++) {
                //  Destroy all items in this hash bucket
                item_t *cur_item = self->items [index];
                while (cur_item) {
                    item_t *next_item = cur_item->next;
                    s_item_destroy (self, cur_item, true);
                    cur_item = next_item;
                }
            }
            zhash_load (self, self->filename);
        }
    }
    return 0;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
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);
}
Beispiel #6
0
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);
}
Beispiel #7
0
static void
s_purge (zhash_t *self)
{
    uint index;
    size_t limit = primes [self->prime_index];

    for (index = 0; index < limit; index++) {
        //  Destroy all items in this hash bucket
        item_t *cur_item = self->items [index];
        while (cur_item) {
            item_t *next_item = cur_item->next;
            s_item_destroy (self, cur_item, true);
            cur_item = next_item;
        }
        self->items [index] = NULL;
    }
}
Beispiel #8
0
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;
}