void resizeHashTable(hashTable_t * ht) { // Find out what size table is next. int newsize = 0; for (int i=0; i<numOfSizes; i++) { if (hashTableSizes[i] == ht->size) { newsize = hashTableSizes[i+1]; break; } } // Remember the current values array. UINT64 * oldtable = ht->table; int size = ht->size; // Now reinit the hash table with a new table, etc. hashTableInit(ht, newsize); // Now iterate over all values and rehash them into the new table. for (int i=0; i<size; i++) { UINT64 value = oldtable[i]; if (isEmpty(value)) continue; if (isValue(value)) { hashTableInsert(ht, unmakeValue(value)); continue; } // We need to iterate through the nodes. hashNode_t * node = makePtr(value); while (true) { for (int j=0; j<HASHNODE_PAYLOAD_SIZE; j++) { value = node->values[j]; if (isEmpty(value)) break; hashTableInsert(ht, unmakeValue(value)); } if (node->next == NULL) break; node = node->next; } // We need to free up the nodes. // TODO move out of line. node->next = hashNodeFreeList; hashNodeFreeList = makePtr(oldtable[i]); } // Free up the oldtable. if (oldtable != NULL) free(oldtable); }
static void hashTableInitAllKeys(hashTableType *table, handType *hand, int ranks[NUM_RANKS], int curRank) { // initially choose a random response for AI unsigned long int response = 0; for (int i = 0; i < NUM_APPRECIABLE_RANKS; i++) response = response * 10 + randInt(0,1); hashTableInsert(table, hand, response); if (ranks[curRank] >= NUM_SUITS) { curRank += 1; } for (int i = curRank; i < NUM_APPRECIABLE_RANKS; i++) { hand->cards[hand->handSize].rank = i; hand->handSize += 1; ranks[i] += 1; handFindSum(hand); if (hand->sum > 21) { hand->handSize -= 1; ranks[i] -=1; break; } hashTableInitAllKeys(table, hand, ranks, i); hand->handSize -= 1; ranks[i] -= 1; } }
Component* GlobalFactory::addComponent(char* id, char* type, bool standardComponent) { Component* obj = (Component*)g_factory.construct(type, id); hashTableInsert(&idPool, hash32(id, (int)strlen(id)), obj); List *l = (List*)hashTableLookup(&typePool, hash32(id, (int)strlen(id))); if (l == NULL) { l = (List*)malloc(sizeof(List)); listInit(l); hashTableInsert(&typePool, hash32(id, (int)strlen(id)), l); } listAddElement(l, obj); return obj; }
int sem_imanager_req(){ int i, j; void *buf; unsigned int offset; unsigned int queue_count; unsigned int src_node; unsigned int seq_number; struct sem *find; struct req_node_info node; char *sem_name; if(g_group.coordinator.sem_id == g_group.node_id) return 1; //max 33 sem buf = mem_malloc(SEM_ISEM_SIZE); ((struct request_header*)buf)->msg_type = MSG_ISEM_MANAGER; sendRecv(g_group.coordinator.sem_id, buf, sizeof(struct request_header), buf, SEM_ISEM_SIZE); if(g_group.sem_table == NULL){ g_group.sem_table = hashTableCreate(SEM_HASH_SIZE); } src_node = ((struct isem_reply*)buf)->req.src_node; seq_number = ((struct isem_reply*)buf)->req.src_seq_number; offset = sizeof(struct isem_reply); for(i=0; i<((struct isem_reply*)buf)->sem_num; i++){ //sem name sem_name = malloc(((struct sem_info*)(buf+offset))->name_len); memcpy(sem_name, buf+offset+sizeof(struct sem_info), ((struct sem_info*)(buf+offset))->name_len); sem_name[((struct sem_info*)(buf+offset))->name_len] = 0; //search sem find = hashTableSearch(g_group.sem_table, ((struct sem_info*)(buf+offset))->hash_id, sem_name); if(find == NULL){ //create new find = createNewSem(((struct sem_info*)(buf+offset))->hash_id, sem_name, ((struct sem_info*)(buf+offset))->value); hashTableInsert(g_group.sem_table, (struct hashheader*)find); }else{ if(find->queue->use!=0){ while(queuePop(find->queue)!=NULL); } } //copy queue info queue_count = ((struct sem_info*)(buf+offset))->queue_count; offset += sizeof(struct sem_info)+strlen(sem_name); for(j=0; j< queue_count;j++){ node.id = ((struct req_node_info*)(buf+offset))->id; node.seq_number = ((struct req_node_info*)(buf+offset))->seq_number; queuePush(find->queue, (void*)&node); offset += sizeof(struct req_node_info); } } g_group.coordinator.sem_id = g_group.node_id; ((struct request_header*)buf)->msg_type = MSG_ISEM_READY; ((struct request_header*)buf)->seq_number = seq_number; sendTo(src_node ,buf, sizeof(struct request_header)); mem_free(buf); return 1; }
int main_imanager_req(){ int i, j; int index; void *buf; unsigned int offset; unsigned int users_count; unsigned int src_node; unsigned int seq_number; unsigned int node_id; struct sm_header *find; char *sm_name; if(g_group.coordinator.main_id == g_group.node_id) return 1; //max 33 mutex buf = mem_malloc(MAIN_IMAIN_SIZE); ((struct request_header*)buf)->msg_type = MSG_IMAIN_MANAGER; sendRecv(g_group.coordinator.main_id, buf, sizeof(struct request_header), buf, MSG_IMAIN_MANAGER); if(g_group.sm_table == NULL){ g_group.sm_table = hashTableCreate(SM_HASH_SIZE); } src_node = ((struct imain_reply*)buf)->req.src_node; seq_number = ((struct imain_reply*)buf)->req.src_seq_number; offset = sizeof(struct imain_reply); //copy share memory data for(i=0; i<((struct imain_reply*)buf)->sm_num; i++){ //mutex name sm_name = malloc(((struct main_sm_info*)(buf+offset))->name_len); memcpy(sm_name, buf+offset+sizeof(struct main_sm_info), ((struct main_sm_info*)(buf+offset))->name_len); sm_name[((struct main_sm_info*)(buf+offset))->name_len] = 0; printf("%s", sm_name); //search mutex find = hashTableSearch(g_group.sm_table, ((struct main_sm_info*)(buf+offset))->hash_id, sm_name); if(find == NULL){ //create new find = createSM(((struct main_sm_info*)(buf+offset))->hash_id, sm_name, ((struct main_sm_info*)(buf+offset))->home_node, ((struct main_sm_info*)(buf+offset))->size); hashTableInsert(g_group.sm_table, (struct hashheader*)find); } //copy find->count = ((struct main_sm_info*)(buf+offset))->count; //copy queue info users_count = ((struct main_sm_info*)(buf+offset))->users_count; offset += sizeof(struct main_sm_info)+strlen(sm_name); if(find->users == NULL) find->users = tableCreate(MAX_NODE_NUM, sizeof(unsigned int)); for(j=0; j< users_count; j++){ node_id = *((unsigned int*)(buf+offset)); if(searchNode(find->users, src_node)==-1){ index = tableGetEmpty(find->users); if(index == -1) return -1; tableAdd(find->users, (void*)&src_node, index); } offset += sizeof(unsigned int); } } g_group.coordinator.main_id = g_group.node_id; ((struct request_header*)buf)->msg_type = MSG_IMAIN_READY; ((struct request_header*)buf)->seq_number = seq_number; sendTo(src_node ,buf, sizeof(struct request_header)); mem_free(buf); return 1; }
int insertNewSem(unsigned int hash_id, char *name, unsigned int value){ struct sem *new_sem; new_sem = createNewSem(hash_id, name, value); hashTableInsert(g_group.sem_table, (struct hashheader*)new_sem); return 1; }
void Fsm::addStateWithInit(uint32_t stateId, Callback * callback, Callback * initCallback) { PrivateFsmState * newState = new PrivateFsmState(initCallback, callback); hashTableInsert(&mStates, stateId, newState ); }