Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashmap_put and hashmap_remove may need to return old values #25

Open
xuning0 opened this issue Apr 25, 2022 · 2 comments
Open

hashmap_put and hashmap_remove may need to return old values #25

xuning0 opened this issue Apr 25, 2022 · 2 comments

Comments

@xuning0
Copy link

xuning0 commented Apr 25, 2022

Only in this way can it be convenient to release the old value on heap.

@strayLuo
Copy link

strayLuo commented Aug 2, 2022

void* hashmap_remove_and_get_value(struct HashmapS* const m, 
    const char* const key,
    const unsigned len)
{
    void* element = HASHMAP_NULL;

    unsigned int i;
    unsigned int curr;

    /* Find key */
    curr = hashmap_hash_helper_int_helper(m, key, len);
    /* Linear probing, if necessary */
    for (i = 0; i < HASHMAP_MAX_CHAIN_LENGTH; i++) {
        if (m->data[curr].in_use) {
            if (hashmap_match_helper(&m->data[curr], key, len)) {
                element = m->data[curr].data;
                //free(m->data[curr].key); //the key should be freed
                memset(&m->data[curr], 0, sizeof(struct HashmapElementS));

                /* Reduce the size */
                m->size--;
                break;
            }
        }

        curr = (curr + 1) % m->table_size;
    }
    return element;
}

@strayLuo
Copy link

strayLuo commented Aug 2, 2022

你看看这个是否符合需求,但是可能违背了此项目的设计。key由外界申请,并由外界释放。
Look at this to see if it meets the requirements, but it may go against the design of this repository. key is malloced by user and freed by user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants