Skip to content

Map size increaded inserting the same key twice #8

@vdenotaris

Description

@vdenotaris

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);
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions