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

Map size increaded inserting the same key twice #8

Open
vdenotaris opened this issue Aug 17, 2015 · 0 comments
Open

Map size increaded inserting the same key twice #8

vdenotaris opened this issue Aug 17, 2015 · 0 comments

Comments

@vdenotaris
Copy link

I just recognized that inserting the same key twice, the size of the map will be increased, even if inside the keyset there will be just one key (as expected).

#include <stdio.h>
#include <stdlib.h>

#include "../lib/c_hashmap/hashmap.h"
#define MAX (100)

typedef struct idp_s
{
    char entity_id[300];
    char *display_name;
    char *organization;
} idp_t;

int main(int argc, char *argv[]) {
    map_t map;
    idp_t *value, *value2;
    char key_string[MAX];
    int map_code, i;

    map = hashmap_new();
    value = malloc(sizeof(idp_t));
    snprintf(value->entity_id, MAX, "%s", "http://login.walterwhite.me/th/");
    value->display_name = "ciao";
    value->organization = "ciaone";

    map_code = hashmap_put(map, value->entity_id, value);
    if (map_code == MAP_OK)
        printf("Inserted\n");
    else
        printf("Error\n");

    value2 = malloc(sizeof(idp_t));
    snprintf(value2->entity_id, MAX, "%s", "http://login.walterwhite.me/th/");
    value2->display_name = "ciao2";
    value2->organization = "ciaone2";

    map_code = hashmap_put(map, value2->entity_id, value2);
    if (map_code == MAP_OK)
        printf("Inserted\n");
    else
        printf("Error\n");

    snprintf(key_string, MAX, "%s", "http://login.walterwhite.me/th/");
    map_code = hashmap_get(map, key_string, (void**)(&value));
    if (map_code == MAP_OK)
        printf("Found\n");
    else
        printf("Not found\n");

    printf("Size: %d\n", hashmap_length(map));
    print_keyset(map);

    return 0;
}

The output is:

Inserted
Inserted
Found
Size: 2
Table size: 256
key: http://login.walterwhite.me/th/

Note that I added print_keyset in hashmap.c as follows:

void print_keyset(map_t in) {
    int i;
    hashmap_map* m = (hashmap_map *) in;
    if(m != NULL) {
        /* Linear probing */
        printf("Table size: %d\n", m->table_size);
        for(i = 0; i< m->table_size; i++) {
            if(m->data[i].in_use != 0) {
                printf("key: %s\n", m->data[i].key);
            }
        }
    }
}
@vdenotaris vdenotaris changed the title Map size increaded inserting the same ket twice Map size increaded inserting the same key twice Aug 17, 2015
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

1 participant