int HashmapExercise(int verbose, struct cfg *cfg, char *args[]) { struct hashmap *h; int rate = 0, i, n = 0; int count = atoi(args[0]); int interval = atoi(args[1]); iter_t riter; iter_t iter; char *str; struct allocator *al = NULL; char *mem; clock_t t0; int chkpnt = 0; struct allocator *hal; if ((mem = malloc(0xFFFFFF)) == NULL || (al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL || (h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } hal = AL(h); /* if ((h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) { AMSG(""); return -1; } */ srand(0); t0 = clock(); if (verbose) fprintf(stderr, "\n time count size mem\n"); hashmap_iterate(h, &iter); rate_iterate(&riter); for (i = 0; i < count; i++) { if ((i % interval) == 0) { if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); rate = rate_next(&riter); } if (rand() % 10 < rate) { str = allocator_alloc(NULL, 8, 0); sprintf(str, "%07d", n++); if (hashmap_put(h, str, str) == -1) { AMSG(""); return -1; } else { /*fputc('+', stderr); */ tcase_printf(verbose, "put %s %d\r", str, hashmap_size(h)); } } else { if (hashmap_is_empty(h)) { /*fputc('0', stderr); */ tcase_printf(verbose, "hashmap empty\r"); } else { str = hashmap_next(h, &iter); if (str) { char *data; tcase_printf(verbose, "get %s %d\r", str, hashmap_size(h)); if (hashmap_remove(h, (void **)&str, (void **)&data) == -1) { AMSG(""); return -1; } /*fputc('-', stderr); */ tcase_printf(verbose, "rem %s %d\r", str, hashmap_size(h)); allocator_free(NULL, str); } else { /*fputc('x', stderr); */ tcase_printf(verbose, "nothing left to iterate over\r"); hashmap_iterate(h, &iter); } } } } if (verbose) fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total); hashmap_del(h, allocator_free, NULL, NULL); /* free(mem); */ if (verbose) fprintf(stderr, "bytes outstanding from allocator: %d\n", hal->alloc_total - hal->free_total); cfg = NULL; return 0; }
void myHashMapRemove(MyHashMap*obj, int key){ hashmap_del(hm, key); }
void hashmap_idel(hashmap *hm, unsigned int key) { hashmap_del(hm, (char*)&key, sizeof(unsigned int)); }
void hashmap_sdel(hashmap *hm, const char *key) { hashmap_del(hm, key, strlen(key)+1); }
int handle_result(uint64_t con_id, struct message_request *request, char *pluginkey, struct api_error *error) { uint64_t callid; array *meta = NULL; struct message_object args_object; char * targetpluginkey; if (!error || !request) return (-1); /* check params size */ if (request->params.size != 2) { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. Invalid params size"); return (-1); } if (request->params.obj[0].type == OBJECT_TYPE_ARRAY) meta = &request->params.obj[0].data.params; else { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. meta params has wrong type"); return (-1); } if (!meta) { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. meta params is NULL"); return (-1); } /* meta = [callid]*/ if (meta->size != 1) { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. Invalid meta params size"); return (-1); } /* extract meta information */ if (meta->obj[0].type != OBJECT_TYPE_UINT) { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. meta elements have wrong type"); return (-1); } callid = meta->obj[0].data.uinteger; if (request->params.obj[1].type != OBJECT_TYPE_ARRAY) { error_set(error, API_ERROR_TYPE_VALIDATION, "Error dispatching result API request. function string has wrong type"); return (-1); } args_object = request->params.obj[1]; targetpluginkey = hashmap_get(uint64_t, ptr_t)(callids, callid); if (!targetpluginkey) { error_set(error, API_ERROR_TYPE_VALIDATION, "Failed to find target's key associated with given callid."); return (-1); } if (api_result(targetpluginkey, callid, args_object, con_id, request->msgid, error) == -1) { if (false == error->isset) error_set(error, API_ERROR_TYPE_VALIDATION, "Error executing result API request."); return (-1); } hashmap_del(uint64_t, ptr_t)(callids, callid); return (0); }
int HashmapCount(int verbose, struct cfg *cfg, char *args[]) { struct test t; char *mem = NULL; int ret = 0, fd = 0, data; int useal = atoi(args[0]); struct stat st; void *mm; if (useal) { if ((mem = malloc(0xFFFFFF)) == NULL || (t.al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL) { AMSG(""); ret = -1; goto err; } } else { t.al = NULL; } if ((t.words = hashmap_new(hash_str, (cmp_fn)strcmp, NULL, t.al)) == NULL || (fd = open(args[1], 0)) == -1 || fstat(fd, &st) == -1) { AMSG(""); ret = -1; goto err; } if ((t.src = mm = mmap(NULL, (int)st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) == NULL) { AMSG(""); ret = -1; goto err; } t.slim = t.src + st.st_size; if (load_words(&t) == -1) { AMSG(""); ret = -1; goto err; } if ((data = (int)hashmap_get(t.words, args[2])) == 0 || atoi(args[3]) != data || (data = (int)hashmap_get(t.words, args[4])) == 0 || atoi(args[5]) != data) { errno = ERANGE; PMNF(errno, ": %d != %d", atoi(args[3]), data); ret = -1; goto err; } if (verbose) print_values(t.words); err: munmap(mm, st.st_size); if (fd) close(fd); hashmap_del(t.words, NULL, NULL, NULL); free(mem); cfg = NULL; return ret; }