Example #1
0
/*
 * sgs_map_destroy()
 */
void sgs_map_destroy(sgs_linked_list_map *map) {
    sgs_map_clear(map);
    free(map);
}
Example #2
0
/*
 * function: main()
 */
int main(int argc, char *argv[]) {
    int lookup_key;
    char str1[] = "foobar";
    char str2[] = "chicken soup";
    const void *pbuf;
    int *pkey;
    int *pkey2;
    int result;
    sgs_map *map;
    
    map = sgs_map_create(compare_ints);
    
    if (map == NULL) {
        fprintf(stderr, "Could not create sgs_map.\n");
        exit(-1);
    }
    
    pkey = malloc(sizeof(int));
    *pkey = 100;
    pbuf = sgs_map_get(map, pkey);
    print_get(*pkey, pbuf);
    
    result = sgs_map_put(map, pkey, str1);
    printf("Added element {%d, %s}.  result=%d\n", *pkey, str1, result);
    
    pbuf = sgs_map_get(map, pkey);
    print_get(*pkey, pbuf);
    
    pkey2 = malloc(sizeof(int));
    *pkey2 = 200;
    pbuf = sgs_map_get(map, pkey2);
    print_get(*pkey2, pbuf);
    
    result = sgs_map_put(map, pkey2, str2);
    printf("Added element {%d, %s}.  result=%d\n", *pkey2, str2, result);
    
    pbuf = sgs_map_get(map, pkey2);
    print_get(*pkey2, pbuf);
    
    lookup_key = 100;
    pbuf = sgs_map_get(map, &lookup_key);
    print_get(lookup_key, pbuf);
    
    lookup_key = 300;
    result = sgs_map_remove(map, &lookup_key);
    printf("REMOVE(%d) == %d\n", lookup_key, result);
    
    lookup_key = 100;
    result = sgs_map_remove(map, &lookup_key);
    printf("REMOVE(%d) == %d\n", lookup_key, result);
    
    lookup_key = 100;
    pbuf = sgs_map_get(map, &lookup_key);
    print_get(lookup_key, pbuf);
    
    lookup_key = 200;
    pbuf = sgs_map_get(map, &lookup_key);
    print_get(lookup_key, pbuf);
  
    sgs_map_clear(map);
    printf("EMPTY()\n");
  
    lookup_key = 100;
    pbuf = sgs_map_get(map, &lookup_key);
    print_get(lookup_key, pbuf);
  
    lookup_key = 200;
    pbuf = sgs_map_get(map, &lookup_key);
    print_get(lookup_key, pbuf);
  
    sgs_map_destroy(map);
    free(pkey2);
    free(pkey);
  
    printf("Goodbye!\n");
  
    return 0;
}