int main(int argc,char *argv[]) { int i; example_user_t *user, *users=NULL, *ausers=NULL; /* create elements */ for(i=0;i<10;i++) { user = (example_user_t*)malloc(sizeof(example_user_t)); user->id = i; HASH_ADD_INT(users,id,user); } for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) { printf("user %d\n", user->id); } printf("users count: %u\n", HASH_CNT(hh,users)); /* now select some users into ausers */ HASH_SELECT(ah,ausers,hh,users,EVENS); HASH_SRT(ah,ausers,idcmp); for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } printf("ausers count: %u\n", HASH_CNT(ah,ausers)); HASH_CLEAR(ah,ausers); printf("cleared ausers.\n"); printf("ausers count: %u\n", HASH_CNT(ah,ausers)); for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) { printf("auser %d\n", user->id); } printf("users count: %u\n", HASH_CNT(hh,users)); return 0; }
/* Initialize htable with the metrics known by the system */ int init_known_metrics() { /* Initialize PAPI library */ int retval; retval = PAPI_library_init(PAPI_VER_CURRENT); if (retval != PAPI_VER_CURRENT) { fprintf(stderr, "Error! PAPI_library_init %d\n",retval); PAPI_shutdown(); } /* Initialize custom metrics storage */ HASH_CLEAR(hh, callbacks_storage); for(int i = 0; i < available_metrics_no; ++i) { htable *new_pair = NULL; new_pair = malloc(sizeof(htable)); if (!new_pair) { fprintf(stderr, "can't alloc memory for the new pair\n"); exit(-1); } strcpy(new_pair->key, callbacks[i].alias); new_pair->value = callbacks[i].func; /* insert the new pair in callbacks_storage */ HASH_ADD_STR(callbacks_storage, key, new_pair); } return 0; }
static void MPIR_T_cat_env_finalize(void) { int i; cat_table_entry_t *cat; if (cat_table) { /* Free all entries */ for (i = 0; i < utarray_len(cat_table); i++) { cat = (cat_table_entry_t *)utarray_eltptr(cat_table, i); MPIU_Free((void *)cat->name); MPIU_Free((void *)cat->desc); utarray_free(cat->cvar_indices); utarray_free(cat->pvar_indices); utarray_free(cat->subcat_indices); } /* Free cat_table itself */ utarray_free(cat_table); cat_table = NULL; } if (cat_hash) { name2index_hash_t *current, *tmp; /* Free all entries */ HASH_ITER(hh, cat_hash, current, tmp) { HASH_DEL(cat_hash, current); MPIU_Free(current); } /* Free cat_hash itself */ HASH_CLEAR(hh, cat_hash); cat_hash = NULL; }
/* * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target) { struct my_struct { int key; int value; UT_hash_handle hh; }; int i = 0; struct my_struct *hash_table = NULL; struct my_struct data[numsSize]; int tmp_int = 0; struct my_struct *search_result = NULL; int *index_array = NULL; for (; i < numsSize; ++i) { tmp_int = target - nums[i]; if (hash_table != NULL) HASH_FIND_INT(hash_table, &tmp_int, search_result); if (search_result != NULL) { index_array = (int *) malloc(sizeof(int) * 2); if (index_array == NULL) return NULL; if (search_result->value > i) { index_array[0] = i; index_array[1] = search_result->value; } else { index_array[0] = search_result->value; index_array[1] = i; } HASH_CLEAR(hh, hash_table); return index_array; } data[i].key = nums[i]; data[i].value = i; HASH_ADD_INT(hash_table, key, &data[i]); } return NULL; }
void PancakeNetworkUnload() { PancakeFree(listenSockets); HASH_CLEAR(hh, architectures); }