int main(int argc, char *argv[]){ char op[3]; int v; int c; hashtable_t *hashtable = ht_create(64); ht_set(hashtable, (char*)&"mov", 0 ,2); ht_set(hashtable, (char*)&"lod", 1, 2); ht_set(hashtable, (char*)&"add", 2, 2); while (1){ scanf("%s", &op); v = ht_get(hashtable, (char*)&op); c = ht_get_count(hashtable, (char*)&op); printf("code: %d, count: %d\n", v, c); } //FILE *program; printf("%Opening [%s]\n", argv[1]); //program = fopen(argv[1],"w+"); //fclose(program); return 0; }
int test_add_to_same_key(){ hashtable_t *ht = ht_create( 65536, free_jambo ); ht_set( ht, "key1", "blorg" ); ht_set( ht, "key1", "bloff" ); ht_set( ht, "key1", "gladd" ); ht_set( ht, "key1", "grutt" ); ht_set( ht, "key1", "twerky" ); ht_set( ht, "key1", "lennart" ); ht_set( ht, "key2", "sverker" ); ht_set( ht, "key3", "Rogge" ); ht_set( ht, "key4", "Swutt" ); check( strcmp( ht_get(ht, "key1"), "lennart") == 0 ); check( strcmp( ht_get(ht, "key2"), "sverker") == 0); check( strcmp( ht_get(ht, "key3"), "Rogge") == 0); check( strcmp( ht_get(ht, "key4"), "Swutt") == 0); check(ht_size(ht) == 4); ht_destroy(ht); return 0; }
int main(int argc, char **argv) { hashtable_t *hashtable = ht_new(65536); ht_set(hashtable, "key1", "inky"); ht_set(hashtable, "key2", "pinky"); ht_set(hashtable, "key3", "blinky"); ht_set(hashtable, "key4", "floyd"); printf("%s\n", ht_get(hashtable, "key1")); printf("%s\n", ht_get(hashtable, "key2")); printf("%s\n", ht_get(hashtable, "key3")); printf("%s\n", ht_get(hashtable, "key4")); return 0; }
static void vfs_zipfile_init_pathmap(VFSNode *node) { VFSZipFileData *zdata = node->data1; VFSZipFileTLS *tls = vfs_zipfile_get_tls(node, true); zip_int64_t num = zip_get_num_entries(tls->zip, 0); ht_create(&zdata->pathmap); for(zip_int64_t i = 0; i < num; ++i) { const char *original = zip_get_name(tls->zip, i, 0); char normalized[strlen(original) + 1]; vfs_path_normalize(original, normalized); if(*normalized) { char *c = strchr(normalized, 0) - 1; if(*c == '/') { *c = 0; } } if(strcmp(original, normalized)) { ht_set(&zdata->pathmap, normalized, i); } } }
/** * Test that half fills a moderately sized hash table with * entries, then drains it. Hash function used here is the * Jenkins hash (which is the default). */ TEST(hashy_tests, test_jenkins_hash) { size_t ht_size = 1024; std::string skey = "key_", svalue = "value_"; std::stringstream sskey, ssvalue; skey.append("key_"); svalue.append("value_"); hashtable_t *ht = ht_create(ht_size, NULL); for(int i = 0; i < ht_size/2; i++) { sskey << skey << i; ssvalue << svalue << i; ht_set(ht, (char *) sskey.str().c_str(), (char *) ssvalue.str().c_str()); sskey.str(std::string()); ssvalue.str(std::string()); } for(int i = 0; i < ht_size/2; i++) { sskey << skey << i; ssvalue << svalue << i; char * val = ht_rem(ht, (char *) sskey.str().c_str()); if(val) {free(val);} sskey.str(std::string()); } EXPECT_EQ(ht->stored_elements, 0); EXPECT_EQ(ht_destroy(ht), 0); }
void shiki_set(struct shiki *lru, struct slice *sk, struct slice *sv) { size_t size; struct level_node *n; if (lru->buffer == 0) return; size = (sk->len + sv->len); n = (struct level_node*)ht_get(lru->ht, sk->data); if (n == NULL) { lru->level_old.used_size += size; lru->level_old.count++; n = calloc(1, sizeof(struct level_node)); n->sk.data = malloc(sk->len); n->sk.len = sk->len; memset(n->sk.data, 0, sk->len); memcpy(n->sk.data, sk->data, sk->len); n->sv.data = malloc(sv->len); n->sv.len = sv->len; memset(n->sv.data, 0, sv->len); memcpy(n->sv.data, sv->data, sv->len); n->size = size; n->hits = 1; n->pre = NULL; n->nxt = NULL; ht_set(lru->ht, n->sk.data, n); } _shiki_set_node(lru, n); }
static __do nd_pool_create (__do pool_p, char *key, uint8_t flags, int l) { __do pool = calloc (1, sizeof(_do)); pool->flags |= flags; if (nd_pool_entry_set (pool, key, l)) { print_str ( "ERROR: d_import_entry: nd_pool_entry_set failed (out of memory)\n"); abort (); } ht_set ((hashtable_t*) pool_p->d, (unsigned char*) key, strlen (key) + 1, (void*) pool, sizeof(_do)); pool->p_pool = pool_p; mutex_lock (&di_base.index_linked.mutex); di_base.index_linked.lref_ptr = (void*) pool; md_alloc (&di_base.index_linked, 0); pool->link = di_base.index_linked.pos; pthread_mutex_unlock (&di_base.index_linked.mutex); print_str ("DEBUG: created pool: %s [%d]\n", key, l); return pool; }
int test_perfomance(){ hashtable_t *ht = ht_create( 65536, destroy_gv_t ); char key[10]; char s1[10]; char s2[10]; for(int i=0;i < 50000; i++){ sprintf(key,"key-%d", i); sprintf(s1,"s1-%d", i); sprintf(s2,"s2-%d", i); ht_set( ht, key, create_gv_t(i, s1, s2) ); check( strcmp( ((gv_t *) ht_get(ht, key))->s1, s1) == 0 ); check( strcmp( ((gv_t *) ht_get(ht, key))->s2, s2) == 0 ); check(((gv_t *) ht_get(ht, key))->i1 == i ); } ht_destroy(ht); return 0; }
int main( int argc, char **argv ) { int size = 4; struct hash_cell **hashtable = ht_create(size); ht_set( hashtable, "key1", "inky" ); ht_set( hashtable, "*", "pinky" ); ht_set( hashtable, "key3", "blinky" ); ht_set( hashtable, "key4", "floyd" ); printf( "%s\n", ht_get( hashtable, "key1" ) ); printf( "%s\n", ht_get( hashtable, "*" ) ); printf( "%s\n", ht_get( hashtable, "key3" ) ); printf( "%s\n", ht_get( hashtable, "key4" ) ); return 0; }
static void vfs_vdir_attach_node(VFSNode *vdir, const char *name, VFSNode *node) { VFSNode *oldnode = ht_get(VDIR_TABLE(vdir), name, NULL); if(oldnode) { vfs_decref(oldnode); } ht_set(VDIR_TABLE(vdir), name, node); }
/*De esta funcion cambiara lo que se envia*/ int insertarElem (ambitos_t *ambitos, char *key, int categoria, int tipoDato, int clase, int tamanoVector, int numParam, int numVarL, int posDe) { if (ambitos->local != NULL) { /*Insertamos en el local*/ if (ht_set(ambitos->local, key, categoria, tipoDato, clase, tamanoVector, numParam, numVarL, posDe) == ERR) return 0; //printf("Insercion en local\n"); return 1; } else if (ambitos->global != NULL) { /*Insertamos en el global*/ if (ht_set(ambitos->global, key, categoria, tipoDato, clase, tamanoVector, numParam, numVarL, posDe) == ERR) return 0; //printf("Insercion en global\n"); return 2; } else return 0; /*No hay ambitos*/ }
//Decodes each line int Decoder(char* symboldef, char* opcode_mnemonic, char* operand, unsigned int* opcode){ char mode = StartsWith(operand); char* ptr = NULL; //Assembler Directive/Operation Code구분(opcode_mnemonic 처리) if(StartsWith(opcode_mnemonic) == '+') //Extended Address { opcode_mnemonic = strtok(opcode_mnemonic,"+"); //remove Extended Sign statusBit |= isExtended; //set extended_flag++; } //find OP_TAB whereIsOnOPTAB = FindTAB(OPTAB,opcode_mnemonic); if(whereIsOnOPTAB>=0){ //if found *opcode = OPTAB[whereIsOnOPTAB].opcode; //change mnemonic to opcode_mnemonic //printf("%X\n",*opcode); //FOR DEBUG LOCCTR[0] = PC; //increase locctr by format of instruction PC += OPTAB[whereIsOnOPTAB].format; //presentLOCCTR += OPTAB[whereIsOnOPTAB].format if(statusBit & isExtended){ PC += 4-OPTAB[whereIsOnOPTAB].format; //In the Case of Extended, the Opcode Format is 4. } //printf("%04X %04X",LOCCTR[0],PC); //FOR DEBUG : PRINT CURRENT LOCCTR AND PC } else{ //if assembler directive : START(0), RESW(1), RESB(2), BYTE(3), EQU(4), ORG(5) LOCCTR[0] = PC; whereIsOnASMTAB=FindTAB(ASMTAB,opcode_mnemonic); *opcode = ASMTAB[whereIsOnASMTAB].opcode; } //ADD SYMBOL DEFINITION AND ADDRESS TO SYMTAB(symboldef 처리) if(*symboldef != NULL){ ht_set(SYMTAB,symboldef, LOCCTR[0],LOCCTR[0]); } //symbol reference and identify addressing mode (operand 처리) //identifying addressing mode ptr = strtok(operand,"#@"); if(ptr!=NULL){ strcpy(operand,ptr); } //operand = ptr; if(mode == '@') { statusBit |= isIndirect; } else if (mode == '#') { statusBit |= isImmediate; } else if (OPTAB[whereIsOnOPTAB].format > 2){ statusBit |= isIndirect | isImmediate; } if(EndsWith(operand,",X") == 1){ strtok(operand,","); statusBit |= isIndexed; //printf("%s",operand); //FOR DEBUG } }
int test_add_custom_struct_as_value(){ hashtable_t *ht = ht_create( 65536, destroy_gv_t ); ht_set( ht, "key4", create_gv_t(10,"jamse", "fjamse") ); check( strcmp( ((gv_t *) ht_get(ht, "key4"))->s1, "jamse") == 0 ); check( strcmp( ((gv_t *) ht_get(ht, "key4"))->s2, "fjamse") == 0 ); check(((gv_t *) ht_get(ht, "key4"))->i1 == 10 ); ht_destroy(ht); return 0; }
int main( int argc, char **argv ) { hashtable_t *hashtable = ht_create( 65536 ); char* a = "a"; char* b = "b"; char* c = "c"; char* d = "d"; ht_set( hashtable, a, 1 ); ht_set( hashtable, b, 2 ); ht_set( hashtable, c, 3 ); ht_set( hashtable, d, 5 ); printf( "%d\n", ht_get( hashtable, a ) ); printf( "%d\n", ht_get( hashtable, b ) ); printf( "%d\n", ht_get( hashtable, c ) ); printf( "%d\n", ht_get( hashtable, d ) ); return 0; }
Value ht_new_set(const int64 num_args, ...) { va_list ap; va_start(ap, num_args); HashTable* ht; const Value v_set = obj_new(klass_Set, (void**) &ht); ht_init2(ht, num_args); for (int64 i = 0; i < num_args; i++){ const Value key = va_arg(ap, Value); ht_set(ht, key); } va_end(ap); return v_set; }
void *ReadFile(void *arg){ thread_block* tb = (thread_block*) arg; size_t len = 1000; char *line = (char*)malloc(sizeof(char)*len); ssize_t read; FILE *file; file = fopen(tb->filename, "r"); int line_total = 0; if(file != NULL){ char IP[50]; char *start; char *end; while((read = (getline(&line, &len, file))) != -1){ start = line; end = strchr(line, ' '); strncpy(IP, line, end - start); IP[end - start] = '\0'; /* enter critical section */ pthread_mutex_lock(&mutexhash); if(ht_get(hashtable, IP) == NULL){ ht_set(hashtable, IP, "1"); numIP++; } /* leave critical section */ pthread_mutex_unlock(&mutexhash); line_total++; } printf("tid = %-3ld file = %-28s line total = %d\n", tb->tid, tb->filename, line_total); fclose(file); } else{ printf("open %s fail\n", tb->filename); } pthread_exit((NULL)); }
void create_lookup(){ staticLookup = ht_create( 10000 ); //printf("Creating lookup\n"); FILE *fptr = fopen("regexp.in","r"); if(fptr==NULL){ printf("regexp.in not found."); return; } char name[50],regex[50]; //printf("Scanning from file\n"); while(fscanf(fptr,"%s",name)){ fscanf(fptr,"%s",regex); printf("Adding defination : %s->%s\n",name,regex); ht_set( staticLookup, name , regex); if(strcmp(name,"DEFAULT")==0)break; } fclose(fptr); }
void OperationIsMachineOperation(char* symboldef, char* opcode_mnemonic, char *operand, unsigned int* opcode){ entry_t* searchResultSYMTAB; int operandToNumber; unsigned int operationCode; char mode = StartsWith(operand); if(OPTAB[whereIsOnOPTAB].format == 1){ operandToNumber = *opcode; //printf("%02X\n", operandToNumber); PrintOpCode(operandToNumber,2); } else if(OPTAB[whereIsOnOPTAB].format == 2){ char* ptr; int whereIsOnREGTAB1 = 0; int whereIsOnREGTAB2 = 0; ptr = strpbrk(operand,","); if(ptr == NULL){//only one register whereIsOnREGTAB1 = FindTAB(REGTAB,operand); operandToNumber = ((*opcode) << 8) | (REGTAB[whereIsOnREGTAB1].opcode <<4); //printf("%04X\n", operandToNumber); PrintOpCode(operandToNumber,2); }else{ strtok(operand,","); whereIsOnREGTAB1 = FindTAB(REGTAB,operand); whereIsOnREGTAB2 = FindTAB(REGTAB,(ptr+1)); operandToNumber = ((*opcode) << 8) | (REGTAB[whereIsOnREGTAB1].opcode <<4) | (REGTAB[whereIsOnREGTAB2].opcode); //printf("%04X\n", operandToNumber); PrintOpCode(operandToNumber,2); } } else { //Operation Format 3/4 if(mode == '='){ //This is Literal // LiteralOperation(operand); OperandIsLiteral(operand); } else if(mode == '*'){ operandToNumber = ((*opcode) << 16) | LOCCTR[0]&0xFFFF; printf("%06X\n",operandToNumber); } else if(mode >= '0' && mode <= '9'){ //This is number int disp = atoi(operand); if(disp >= 4096){ operandToNumber = ((*opcode) << 24 | statusBit<<8 | (disp & 0x0FFFFF)); //printf("%08X\n", operandToNumber); PrintOpCode(operandToNumber,4); } else{ operandToNumber = ((*opcode) << 16 | statusBit | (disp & 0x0FFF)); //printf("%06X\n", operandToNumber); PrintOpCode(operandToNumber,3); } } else{ searchResultSYMTAB = ht_get(SYMTAB,operand); if(operand == NULL){ operandToNumber = ((*opcode) << 16); } else if(searchResultSYMTAB == NULL || searchResultSYMTAB->value == 0){ //Couldn't Find Definition or Address in SYMTAB ht_set(SYMTAB,operand,0,LOCCTR[0]); //Setting a SYMBOL without address and add unresolved address if(!(statusBit & isExtended)){ operandToNumber = ((*opcode) << 16 | statusBit); //printf("%06X\n", operandToNumber); PrintOpCode(operandToNumber,3); } else { operandToNumber = (((*opcode) << 16 | statusBit) << 8); //printf("%08X\n", operandToNumber); PrintOpCode(operandToNumber,4); } } else{ //already defined and founded if(!(statusBit & isExtended)){ //if not extended, we need to calculate address relatively //we need to calculate address with respect to PC int diff = 0; diff = searchResultSYMTAB->value - PC; if (diff >= -2047 && diff <= 2048){ //This is PC Relative statusBit |= isPCRelative; } else { diff = searchResultSYMTAB->value - BASE; if( diff >= 0 && diff <= 4095){ //this is Base Relative statusBit |= isBaseRelative; } else{ printf("\nERROR: OVERFLOW, THE ADDRESS CANNOT BE CALCULATED RELATIVELY\n"); return 0; } } operandToNumber = ((*opcode) << 16 | statusBit | (diff & 0x0FFF)); //printf("%06X\n", operandToNumber); PrintOpCode(operandToNumber,3); } else{ //Extended operandToNumber = ((*opcode) << 24 | statusBit<<8 | (searchResultSYMTAB->value & 0x0FFFFF)); PrintOpCode(operandToNumber,4); if(whichPass == 2) MDRTAB=NewUnresolvedNode(MDRTAB,LOCCTR[0]+1); //add this to modification record } } } } }
void OperationIsAssemblerDirective(char* symboldef, char* opcode_mnemonic, char *operand, unsigned int* opcode){ /* For the record, ASMTAB looks like this {"START",0,0xF0}, {"END",1,0xF1}, {"RESW",2,0xF2}, {"RESB",3,0xF3}, {"BYTE",4,0xF4}, {"WORD",5,0xF5}, {"EQU",6,0xF6}, {"ORG",7,0xF7}, {"LTORG",8,0xF8},*/ entry_t* searchResultSYMTAB; int operandToNumber = 0; unsigned int operationCode; char mode = StartsWith(operand); switch(*opcode & 0xF) { case 0: startAddress = (int)strtoul(operand,NULL,16); PC = startAddress; LOCCTR[0] = PC-3; if(whichPass==2) printf("H%-6s%06X%06X\n",symboldef,startAddress,endAddress-startAddress); break; case 1: isDone = 1; endAddress = LOCCTR[0]; PC = (int)strtoul(operand,NULL,16); break; case 2: operandToNumber = atoi(operand); //printf("%04X\n", LOCCTR[0]); //FOR DEBUG PC+=3*operandToNumber; LOCCTR[0] = PC; break; case 3: operandToNumber = atoi(operand); PC+=1*operandToNumber; LOCCTR[0] = PC; break; case 4: //print the value of operand to buffer operand=strtok(operand,"'CX"); if(mode == 'C'){//'Character' while(*operand != '\0') { PrintOpCode(*operand,1); PC+=1; LOCCTR[0] = PC; operand++; } } else if(mode == 'X'){//'Hex Value' PrintOpCode((int)strtoul(operand,NULL,16),1); PC+=1; } LOCCTR[0]=PC; break; case 5: PC+=3; LOCCTR[0] = PC; break; case 6: //replace symbol to operand, find symbol and set ht_set(SYMTAB,symboldef,atoi(operand),LOCCTR[0]); break; case 7: //set location counter to operand if(mode == '*'){ //current LOCCTR LOCCTR[0] = PC;//LOCCTR[0]; } else if(mode >= '0' && mode <= '9'){ //#number LOCCTR[0] = atoi(operand); } else{ //symbol searchResultSYMTAB = ht_get(SYMTAB,operand); if(operand == NULL){ } else if(searchResultSYMTAB == NULL || searchResultSYMTAB->value == 0){ ht_set(SYMTAB,operand,0,LOCCTR[0]); //Setting a SYMBOL without address and add unresolved address } else { if(!(statusBit & isExtended)){ //if not extended, we need to calculate address relatively LOCCTR[0] = searchResultSYMTAB->value; //First, calculate address relative to PC } } } break; case 8: //LTORG, print all the contents in the LITTAB which are not located somewhere. DeployLiteral(&PC); break; case 9: //Set Base to operand value if(mode == '*'){ //current LOCCTR BASE = LOCCTR[0];//LOCCTR[0]; } else if(mode >= '0' && mode <= '9'){ //#number BASE = atoi(operand); } else{ //symbol searchResultSYMTAB = ht_get(SYMTAB,operand); if(operand == NULL){ } else if(searchResultSYMTAB == NULL || searchResultSYMTAB->value == 0){ ht_set(SYMTAB,operand,0,LOCCTR[0]); //Setting a SYMBOL without address and add unresolved address } else { if(!(statusBit & isExtended)){ //if not extended, we need to calculate address relatively BASE = searchResultSYMTAB->value; //First, calculate address relative to PC } } } break; } }
int join( mpiconfig_t *mpicfg, int id ){ int i; int next_available_rank, pred, succ, instructionmsg, buff, err; int nodepoolnumel; int actioncompleted; int *nodepoolentriesk, *nodepoolentriesv; /* constraint: only coordinator process */ if (mpicfg->rank != 0){return -1;} /* check that a new MPI proc can be spared */ if ( mpicfg->num_procs <= mpicfg->nodepool->nentries ) { printf("J %d ERR No more available MPI processes to be spared! \n", id); return -1; } /* Check if id already exists */ if ( ht_get( mpicfg->nodepool, id ) >= 0 ) { printf("J %d ERR This id is already alive! ", id); return -1; } next_available_rank = find_mark_next_sleeping(mpicfg); if ( next_available_rank < 0 ){return -1;} ht_set( mpicfg->nodepool, id, next_available_rank); aliveprocs_insert(mpicfg, id); pred = aliveprocs_pred(mpicfg,id); succ = aliveprocs_succ(mpicfg,id); if ( next_available_rank == mpicfg->rank ){ mpicfg->id = id; mpicfg->predid = pred; mpicfg->succid = succ; mpicfg->predrank = ht_get( mpicfg->nodepool, mpicfg->predid ); mpicfg->succrank = ht_get( mpicfg->nodepool, mpicfg->succid ); }else{ /* update other processes for the new member */ /* get ranks of active processes */ nodepoolnumel = mpicfg->nodepool->nentries; nodepoolentriesv = (int*)calloc(nodepoolnumel, sizeof(int)); if ( ht_dumpvalues( mpicfg->nodepool, nodepoolentriesv ) < 0){ printf("error in HT dump!\n"); } /* send instruction message to all other awake processes */ strcpy( mpicfg->imsg.instruction, "update" ); mpicfg->imsg.id = id; mpicfg->imsg.senderrank = mpicfg->rank; for (i = 0 ; i < nodepoolnumel ; i++){ if (nodepoolentriesv[i] != mpicfg->rank) { MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, nodepoolentriesv[i], 0, MPI_COMM_WORLD); /* bcast other info */ MPI_Bcast(mpicfg->procstatus, mpicfg->num_procs, MPI_INT , 0, MPI_COMM_WORLD); MPI_Bcast(mpicfg->aliveprocs, mpicfg->num_procs, MPI_INT , 0, MPI_COMM_WORLD); /* send HT size */ //nodepoolnumel = mpicfg->nodepool->nentries; MPI_Send(&nodepoolnumel, 1, MPI_INT, nodepoolentriesv[i], 0, MPI_COMM_WORLD); /*Send keys (ids) */ nodepoolentriesk = (int*)calloc(nodepoolnumel, sizeof(int)); if ( ht_dumpkeys( mpicfg->nodepool, nodepoolentriesk ) < 0){ printf("error in HT dump!\n"); } MPI_Send(nodepoolentriesk, nodepoolnumel, MPI_INT, nodepoolentriesv[i], 0, MPI_COMM_WORLD); /*dump values (ranks) */ //nodepoolentriesv = (int*)calloc(nodepoolnumel, sizeof(int)); if ( ht_dumpvalues( mpicfg->nodepool, nodepoolentriesv ) < 0){ printf("error in HT dump!\n"); } MPI_Send(nodepoolentriesv, nodepoolnumel, MPI_INT, nodepoolentriesv[i], 0, MPI_COMM_WORLD); /* wait confirmation */ while (1){ if (nodepoolentriesv[i] != mpicfg->rank) { MPI_Recv(&mpicfg->imsg, 1, mpicfg->imsg_t, nodepoolentriesv[i], 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); executeinstruction(mpicfg, mpicfg->imsg); if ( strcmp( mpicfg->imsg.instruction, "completed") == 0 ){ break; } } } } } /* send instruction message */ strcpy( mpicfg->imsg.instruction, "join" ); mpicfg->imsg.id = id; MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, next_available_rank, 0, MPI_COMM_WORLD); /* wait confirmation (or every other message that might come) */ while (1){ MPI_Recv(&mpicfg->imsg, 1, mpicfg->imsg_t, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE ); executeinstruction(mpicfg, mpicfg->imsg); if ( strcmp( mpicfg->imsg.instruction, "completed") == 0 ){break;} } free(nodepoolentriesv); free(nodepoolentriesk); } //xxx 8elw ena barrier prokeimenou na min mplextoun ta locally exxecuted pred/succ twn workers. return 0; }
int executeinstruction(mpiconfig_t *mpicfg, instructionmsg_t imsg){ int i, size, err, instructionmsg, ctr; int actioncompleted; int hostnamelen, nodepoolnumel, filepoolnumel, claimedfilepoolnumel; int *nodepoolentriesk, *nodepoolentriesv; int *filepoolkeys, *claimedfilepoolkeys; MPI_Datatype instructmsg_mpi_t; int resultlen; MPI_Status status; int fid, maxpid; MPI_Datatype array_of_types[2]; int array_of_blocklengths[2]; MPI_Aint array_of_displaysments[2]; MPI_Aint intex, charex, lb; //execute instruction message: if ( strcmp( mpicfg->imsg.instruction, "join") == 0 ) { /* Process is reading the newspaper */ if(EBUG){printf("Proccess %d received JOIN. \n", mpicfg->rank );} mpicfg->id = mpicfg->imsg.id; /* Request file ids from successor */ strcpy( mpicfg->imsg.instruction, "claimfiles" ); mpicfg->imsg.id = mpicfg->id; mpicfg->imsg.senderrank = mpicfg->rank; //printf("Proccess %d is about to claim files from %d. \n", mpicfg->rank, mpicfg->succrank ); MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, mpicfg->succrank, 0, MPI_COMM_WORLD); /* receive file ids */ MPI_Recv(&claimedfilepoolnumel, 1, MPI_INT, mpicfg->succrank, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); //printf("Proccess %d had claimed files. \n", mpicfg->rank ); if (claimedfilepoolnumel > 0 ){ claimedfilepoolkeys = (int*)calloc(claimedfilepoolnumel, sizeof(int)); }else{ claimedfilepoolkeys = (int*)calloc(1, sizeof(int)); } MPI_Recv(claimedfilepoolkeys, claimedfilepoolnumel, MPI_INT, mpicfg->succrank, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); /* Update local filepool */ for (i=0;i<claimedfilepoolnumel;i++){ ht_set( mpicfg->filepool, claimedfilepoolkeys[i], 1); } /* node joining the gang is done */ filepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } printf("J %d DONE, PRED = %d, SUCC = %d, DocList = ", mpicfg->id, mpicfg->predid,mpicfg->succid); for (i=0;i<mpicfg->filepool->nentries;i++){ printf("%d, ", filepoolkeys[i]); } printf("\n"); free(filepoolkeys); free(claimedfilepoolkeys); if(EBUG){printf("Proccess %d COMPLETED JOIN. \n", mpicfg->rank );} /* Join completed; inform coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = -1; mpicfg->imsg.senderrank = mpicfg->rank; //printf("Proccess %d is about to send complete for join. \n", mpicfg->rank ); MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD); } else if ( strcmp( mpicfg->imsg.instruction, "update") == 0 ) { /* Process is reading the newspaper */ if(EBUG){printf("Proccess %d received UPDATE for id %d. \n", mpicfg->rank, mpicfg->imsg.id );} if ( mpicfg->id < 0){ mpicfg->id = mpicfg->imsg.id; } /* Get updates on helper arrays */ MPI_Bcast(mpicfg->procstatus, mpicfg->num_procs, MPI_INT , 0, MPI_COMM_WORLD); MPI_Bcast(mpicfg->aliveprocs, mpicfg->num_procs, MPI_INT , 0, MPI_COMM_WORLD); /* Get updates on ht */ MPI_Recv(&nodepoolnumel, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); nodepoolentriesk = (int*)calloc(nodepoolnumel, sizeof(int)); MPI_Recv(nodepoolentriesk, nodepoolnumel, MPI_INT, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); nodepoolentriesv = (int*)calloc(nodepoolnumel, sizeof(int)); MPI_Recv(nodepoolentriesv, nodepoolnumel, MPI_INT, 0, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); //printf("Proccess %d woken up and informed by coordinator. \n", mpicfg->rank ); /* Update local nodepool */ for (i=0;i<nodepoolnumel;i++){ ht_set( mpicfg->nodepool, nodepoolentriesk[i], nodepoolentriesv[i]); } /* Get pred/succ from local info */ mpicfg->predid = aliveprocs_pred(mpicfg,mpicfg->imsg.id); mpicfg->succid = aliveprocs_succ(mpicfg,mpicfg->imsg.id); mpicfg->predrank = ht_get( mpicfg->nodepool, mpicfg->predid ); mpicfg->succrank = ht_get( mpicfg->nodepool, mpicfg->succid ); free(nodepoolentriesv); free(nodepoolentriesk); if(EBUG){printf("Proccess %d COMPLETED UPDATE. \n", mpicfg->rank );} /* Join completed; inform coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = -1; mpicfg->imsg.senderrank = mpicfg->rank; //printf("Proccess %d is about to send complete for join. \n", mpicfg->rank ); MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD); } else if ( strcmp( mpicfg->imsg.instruction, "leave") == 0 ) { if(EBUG){printf("Proccess %d received LEAVE. \n", mpicfg->rank );} /* Return file ids to successor */ filepoolnumel = mpicfg->filepool->nentries; filepoolkeys = (int*)calloc(filepoolnumel, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } /* Send file ids to successor node */ strcpy( mpicfg->imsg.instruction, "returnfiles" ); mpicfg->imsg.id = mpicfg->id; mpicfg->imsg.senderrank = mpicfg->rank; MPI_Send(&filepoolnumel, 1, MPI_INT, mpicfg->succrank, 0, MPI_COMM_WORLD); MPI_Send(filepoolkeys, filepoolnumel, MPI_INT, mpicfg->succrank, 0, MPI_COMM_WORLD); /* Update (delete) local filepool */ for (i=0;i<filepoolnumel;i++){ ht_del( mpicfg->filepool, filepoolkeys[i]); } printf("L %d DONE, PRED = %d, SUCC = %d, DocList = ", mpicfg->id, mpicfg->predid,mpicfg->succid); printf("\n"); free(filepoolkeys); /*destroy process params since its out of the network */ mpicfg->id = -1; mpicfg->predid = -1; mpicfg->succid = -1; mpicfg->predrank = -1; mpicfg->succrank = -1; if(EBUG){printf("Proccess %d COMPLETED LEAVE. \n", mpicfg->rank );} /* Leave completed; inform coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = -1; mpicfg->imsg.senderrank = mpicfg->rank;; //printf("Proccess %d is about to send complete for join. \n", mpicfg->rank ); MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD); } else if ( strcmp( mpicfg->imsg.instruction, "claimfiles") == 0 ) { if(EBUG){printf("Proccess %d received CLAIMFILES. \n", mpicfg->rank );} /* if no files are present : */ if ( mpicfg->filepool->nentries > 0 ){ /* dump fids of this node */ filepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } claimedfilepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); /* get fids that belong to the claiming node */ ctr = 0; for (i=0;i<mpicfg->filepool->nentries;i++){ if( filepoolkeys[i] <= mpicfg->imsg.id ){ claimedfilepoolkeys[ctr] = filepoolkeys[i]; ctr++; } } claimedfilepoolnumel = ctr; }else{ claimedfilepoolkeys = NULL; claimedfilepoolnumel = 0; } //printf("Proccess %d is about to send claimed files to %d. \n", mpicfg->rank, mpicfg->imsg.senderrank ); /* Send file is to claiming node */ MPI_Send(&claimedfilepoolnumel, 1, MPI_INT, mpicfg->imsg.senderrank, 0, MPI_COMM_WORLD); /*Send file keys (ids) */ MPI_Send(claimedfilepoolkeys, claimedfilepoolnumel, MPI_INT, mpicfg->imsg.senderrank, 0, MPI_COMM_WORLD); /* remove those fids from this node */ if ( mpicfg->filepool->nentries > 0 ){ for (i=0;i<claimedfilepoolnumel;i++){ ht_del( mpicfg->filepool, claimedfilepoolkeys[i]); } free(claimedfilepoolkeys); free(filepoolkeys); } if(EBUG){printf("Proccess %d COMPLETED CLAIMFILES. \n", mpicfg->rank );} } else if ( strcmp( mpicfg->imsg.instruction, "returnfiles") == 0 ) { if(EBUG){printf("Proccess %d received RETURNFILES. \n", mpicfg->rank );} /* receive file ids from departing node */ MPI_Recv(&filepoolnumel, 1, MPI_INT, mpicfg->imsg.senderrank, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); if (filepoolnumel > 0 ){ filepoolkeys = (int*)calloc(filepoolnumel, sizeof(int)); }else{ filepoolkeys = (int*)calloc(1, sizeof(int)); } MPI_Recv(filepoolkeys, filepoolnumel, MPI_INT, mpicfg->succrank, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); /* Update local filepool */ for (i=0;i<filepoolnumel;i++){ ht_set( mpicfg->filepool, filepoolkeys[i], 1); } free(filepoolkeys); if(EBUG){printf("Proccess %d COMPLETED RETURNFILES. \n", mpicfg->rank );} } else if ( strcmp( mpicfg->imsg.instruction, "insert") == 0 ) { if(EBUG){printf("Proccess %d received INSERT. \n", mpicfg->rank );} fid = mpicfg->imsg.id; maxpid = aliveprocs_maxid( mpicfg ); /* check if fid is eligible for insertion and insert it */ if( ( mpicfg->predid < fid && fid <= mpicfg->id ) || ( fid > maxpid && mpicfg->id == mpicfg->aliveprocs[0] ) ){ ht_set( mpicfg->filepool, fid, 1 ); /* fid insert is done */ filepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } printf("I %d DONE, NODEID = %d, DOCLIST = ", fid, mpicfg->id); for (i=0;i<mpicfg->filepool->nentries;i++){ printf("%d, ", filepoolkeys[i]); } printf("\n"); free(filepoolkeys); if(EBUG){printf("Proccess %d COMPLETED INSERT. \n", mpicfg->rank );} /* send completion msg to coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = fid; mpicfg->imsg.senderrank = mpicfg->rank; MPI_Isend(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD, &mpicfg->pendingsend); } } else if ( strcmp( mpicfg->imsg.instruction, "find") == 0 ) { if(EBUG){printf("Proccess %d received FIND. \n", mpicfg->rank );} fid = mpicfg->imsg.id; maxpid = aliveprocs_maxid( mpicfg ); /* check if fid is under this node */ if( ( mpicfg->predid < fid && fid <= mpicfg->id ) || ( fid > maxpid && mpicfg->id == mpicfg->aliveprocs[0] ) ){ /* but might not be there */ err = ht_get( mpicfg->filepool, fid ); /* fid find is done */ filepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } if ( err < 0){ printf("F %d NOTFOUND, NODEID = %d, DOCLIST = ", fid, mpicfg->id); }else{ printf("F %d DONE, NODEID = %d, DOCLIST = ", fid, mpicfg->id); } for (i=0;i<mpicfg->filepool->nentries;i++){ printf("%d, ", filepoolkeys[i]); } printf("\n"); free(filepoolkeys); if(EBUG){printf("Proccess %d COMPLETED FIND. \n", mpicfg->rank );} /* send completion msg to coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = fid; mpicfg->imsg.senderrank = mpicfg->rank; //printf("Proccess %d is about to send complete for find. \n", mpicfg->rank ); //MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD); MPI_Isend(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD, &mpicfg->pendingsend); } } else if ( strcmp( mpicfg->imsg.instruction, "del") == 0 ) { if(EBUG){printf("Proccess %d received DEL. \n", mpicfg->rank );} fid = mpicfg->imsg.id; maxpid = aliveprocs_maxid( mpicfg ); /* check if fid is eligible for deletion and insert it */ if( ( mpicfg->predid < fid && fid <= mpicfg->id ) || ( fid > maxpid && mpicfg->id == mpicfg->aliveprocs[0] ) ){ err = ht_del( mpicfg->filepool, fid ); /* fid insert is done */ filepoolkeys = (int*)calloc(mpicfg->filepool->nentries, sizeof(int)); if ( ht_dumpkeys( mpicfg->filepool, filepoolkeys ) < 0){ printf("error in HT dump!\n"); } if ( err < 0){ printf("D %d NOTFOUND, NODEID = %d, DOCLIST = ", fid, mpicfg->id); }else{ printf("D %d DONE, NODEID = %d, DOCLIST = ", fid, mpicfg->id); } for (i=0;i<mpicfg->filepool->nentries;i++){ printf("%d, ", filepoolkeys[i]); } printf("\n"); free(filepoolkeys); if(EBUG){printf("Proccess %d COMPLETED DEL. \n", mpicfg->rank );} /* send completion msg to coordinator */ strcpy( mpicfg->imsg.instruction, "completed" ); mpicfg->imsg.id = fid; mpicfg->imsg.senderrank = mpicfg->rank; //printf("Proccess %d is about to send complete for insert. \n", mpicfg->rank ); //MPI_Send(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD); MPI_Isend(&mpicfg->imsg, 1, mpicfg->imsg_t, 0, 0, MPI_COMM_WORLD, &mpicfg->pendingsend); } } else if ( strcmp( mpicfg->imsg.instruction, "completed") == 0 ) { /*instruction completed successfully */ if(EBUG){printf("Proccess %d COMPLETED COMPLETED. \n", mpicfg->rank );} return 0; } else if ( strcmp( mpicfg->imsg.instruction, "stopexecution") == 0 ) { /* update process configuration in order to not listen for more messages from coordinator */ mpicfg->stopexecution = 1; } else { if(EBUG){printf("Proccess %d WTF. \n", mpicfg->rank );} } }
int main(int argc, char **argv) { //Set up connection unsigned int j; unsigned int k; redisContext *c; redisReply *reply; const char *hostname = (argc > 3) ? argv[3] : "127.0.0.1"; int port = (argc > 4) ? atoi(argv[4]) : 6379; struct timeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout(hostname, port, timeout); if (c == NULL || c->err) { if (c) { printf("Connection error: %s\n", c->errstr); redisFree(c); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } redisReply *all_keys; all_keys = redisCommand(c, "KEYS *"); //For bidirectional, need to use 2 queues, //1 for BFS from the start, and 1 for BFS //from the end Node *start_queue; Node *end_queue; //Create hashtables for the visited hashtable_t *hashtable_start = ht_create( 65536 ); //hashtable_t *hashtable_end = ht_create( 65536 ); //Create hashtable to store hashtable parents hashtable_t *hashtable_parents = ht_create( 65536 ); hashtable_t *hashtable_parents_end = ht_create( 65536 ); //Path will be found between these two char *start = "0000138"; char *end = "0000197"; //initialize the queues start_queue = make_node(start, NULL); end_queue = make_node(end, NULL); char *start_name = start; char *end_name = end; //LL for paths Node *path_to_meet_s; Node *path_to_meet_e; path_to_meet_s = make_node(start, NULL); path_to_meet_e = make_node(end, NULL); //Where do they meet char *met1 = start_name; char *met2 = end_name; //while start and end are not NULL //while (strcmp(start_name, end_name) != 4389605843690) { while ((strcmp(start_name, "NULL") != 0) && (strcmp(end_name, "NULL") != 0)) { //kick off the BFS with the start and the end start_name = peek(&start_queue); end_name = peek(&end_queue); //printf("start %s, end %s\n", start_name, end_name); //push to their BFS queues push(&path_to_meet_s, start_name); push(&path_to_meet_e, end_name); //Check if they exist in the visited hashtable, which is named //hashtable_start char *check_start = ht_get(hashtable_start, start_name); char *check_end = ht_get(hashtable_start, end_name); //char *check_end = ht_get(hashtable_end, end_name); //Same BFS if(strcmp(check_start, "NULL")==0){ if (strcmp(end_name, start_name) == 0) { met1 = start_name; met2 = end_name; break; } char word_reply1[100] = ""; strcat(word_reply1, "LRANGE "); strcat(word_reply1, start_name); strcat(word_reply1, " 0 -1"); reply = redisCommand(c,word_reply1); if (reply->type == REDIS_REPLY_ARRAY) { for (k = 0; k < reply->elements; k++) { push(&start_queue, reply->element[k]->str); char *check2_st = ht_get(hashtable_parents, reply->element[k]->str); if (strcmp(check2_st, "NULL") == 0) { ht_set(hashtable_parents, reply->element[k]->str, start_name); } } } ht_set(hashtable_start, start_name, "visited"); } //Same BFS if(strcmp(check_end, "NULL")==0){ if (strcmp(end_name, start_name) == 0) { met1 = start_name; met2 = end_name; break; } char word_reply2[100] = ""; strcat(word_reply2, "LRANGE "); strcat(word_reply2, end_name); strcat(word_reply2, " 0 -1"); reply = redisCommand(c,word_reply2); if (reply->type == REDIS_REPLY_ARRAY) { for (k = 0; k < reply->elements; k++) { push(&end_queue, reply->element[k]->str); char *check2_en = ht_get(hashtable_parents_end, reply->element[k]->str); if (strcmp(check2_en, "NULL") == 0) { ht_set(hashtable_parents_end, reply->element[k]->str, end_name); } } } ht_set(hashtable_start, end_name, "visited"); //ht_set(hashtable_end, end_name, "visited"); } //if you haven't reached the same actor from the start and the end //keep BFS traversing the front and end until you do if (strcmp(start_name, end_name) != 0) { pop(&start_queue); pop(&end_queue); } //Otherwise, you have met, and you should break out of the while //loop to build the path else { printf("met %s %s", start_name, end_name); break; } pop(&path_to_meet_s); pop(&path_to_meet_e); } //After the while loop, you have paths to meet char *start_parent = peek(&path_to_meet_s); char *end_parent = peek(&path_to_meet_e); printf("%s ", "success\n"); /*print_list(path_to_meet_s); printf("\n"); print_list(path_to_meet_e);*/ reverse(&path_to_meet_s); pop(&path_to_meet_s); reverse(&path_to_meet_e); char *parent_st = ht_get(hashtable_parents, start_parent); char *parent_en = ht_get(hashtable_parents_end, end_parent); while (strcmp(parent_st, start) != 0) { push(&path_to_meet_s, parent_st); parent_st = ht_get(hashtable_parents, parent_st); } while (strcmp(parent_en, end) != 0) { push(&path_to_meet_e, parent_en); parent_en = ht_get(hashtable_parents_end, parent_en); } push(&path_to_meet_s, parent_st); reverse(&path_to_meet_s); push(&path_to_meet_e, parent_en); //This is the final path //print_list(path_to_meet_s); print_list(path_to_meet_e); /* Disconnects and frees the context */ redisFree(c); return 0; }
char *get_token(char *lexeme , int mode){ char *token=(char*)calloc(strlen(lexeme)+50,sizeof(char)); //printf("Getting token\n"); if(is_long(lexeme)){ sprintf(token,"%d",LONG); } else if(is_static(lexeme)){ sprintf(token,"%d",STATIC); } else if(is_union(lexeme)){ sprintf(token,"%d",UNION); } else if(is_default(lexeme)){ sprintf(token,"%d",DEFAULT); } else if(is_break(lexeme)){ sprintf(token,"%d",BREAK); } else if(is_case(lexeme)){ sprintf(token,"%d",CASE); } else if(is_continue(lexeme)){ sprintf(token,"%d",CONTINUE); } else if(is_goto(lexeme)){ sprintf(token,"%d",GOTO); } else if(is_struct(lexeme)){ sprintf(token,"%d",STRUCT); } else if(is_const(lexeme)){ sprintf(token,"%d",CONST); } else if(is_void(lexeme)){ sprintf(token,"%d",VOID); } else if(is_switch(lexeme)){ sprintf(token,"%d",SWITCH); } else if(is_for(lexeme)){ sprintf(token,"%d",FOR); } else if(is_while(lexeme)){ sprintf(token,"%d",WHILE); } else if(is_do(lexeme)){ sprintf(token,"%d",DO); } else if(is_return(lexeme)){ sprintf(token,"%d",RETURN); } else if(is_bool(lexeme)){ sprintf(token,"%d",BOOL); } else if(is_char(lexeme)){ sprintf(token,"%d",CHAR); } else if(is_signed(lexeme)){ sprintf(token,"%d",SIGNED); } else if(is_unsigned(lexeme)){ sprintf(token,"%d",UNSIGNED); } else if(is_short(lexeme)){ sprintf(token,"%d",SHORT); } else if(is_int(lexeme)){ sprintf(token,"%d",INT); } else if(is_float(lexeme)){ sprintf(token,"%d",FLOAT); } else if(is_double(lexeme)){ sprintf(token,"%d",DOUBLE); } else if(is_l_square(lexeme)){ sprintf(token,"%d",L_SQUARE); } else if(is_r_square(lexeme)){ sprintf(token,"%d",R_SQUARE); } else if(is_l_paraen(lexeme)){ sprintf(token,"%d",L_PARAEN); } else if(is_r_paraen(lexeme)){ sprintf(token,"%d",R_PARAEN); } else if(is_l_cbrace(lexeme)){ sprintf(token,"%d",L_CBRACE); } else if(is_r_cbrace(lexeme)){ sprintf(token,"%d",R_CBRACE); } else if(is_comma(lexeme)){ sprintf(token,"%d",COMMA); } else if(is_semicol(lexeme)){ sprintf(token,"%d",SEMICOL); } else if(is_eq_eq(lexeme)){ sprintf(token,"%d",EQ_EQ); } else if(is_lesser(lexeme)){ sprintf(token,"%d",LESSER); } else if(is_less_eq(lexeme)){ sprintf(token,"%d",LESS_EQ); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_greater(lexeme)){ sprintf(token,"%d",GREATER); } else if(is_great_eq(lexeme)){ sprintf(token,"%d",GREAT_EQ); } else if(is_plus_eq(lexeme)){ sprintf(token,"%d",PLUS_EQ); } else if(is_minus_eq(lexeme)){ sprintf(token,"%d",MINUS_EQ); } else if(is_div_eq(lexeme)){ sprintf(token,"%d",DIV_EQ); } else if(is_mult_eq(lexeme)){ sprintf(token,"%d",MULT_EQ); } else if(is_minus_minus(lexeme)){ sprintf(token,"%d",MINUS_MINUS); } else if(is_plus_plus(lexeme)){ sprintf(token,"%d",PLUS_PLUS); } else if(is_percent(lexeme)){ sprintf(token,"%d",PERCENT); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_mult(lexeme)){ sprintf(token,"%d",MULT); } else if(is_minus(lexeme)){ sprintf(token,"%d",MINUS); } else if(is_plus(lexeme)){ sprintf(token,"%d",PLUS); } else if(is_int_const(lexeme)){ printf("int"); sprintf(token,"%d\t%s",INT_CONST,lexeme); } else if(is_flo_const(lexeme)){ printf("float"); sprintf(token,"%d\t%s",FLO_CONST,lexeme); } else if(is_comment_start(lexeme)){ sprintf(token,"$start"); } else if(is_comment_end(lexeme)){ sprintf(token,"$end"); } else if(is_identifier(lexeme)){ printf("Identifier"); if(mode==1) ht_set( symboltable, lexeme, "1"); sprintf(token,"%d\t%s",IDNTIFIER,lexeme); } else sprintf(token,"%d",NOTOK); return token; }
int main(int argc, char **argv) { unsigned int j; unsigned int k; redisContext *c; redisReply *reply; const char *hostname = (argc > 3) ? argv[3] : "127.0.0.1"; int port = (argc > 4) ? atoi(argv[4]) : 6379; struct timeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout(hostname, port, timeout); if (c == NULL || c->err) { if (c) { printf("Connection error: %s\n", c->errstr); redisFree(c); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } //SETTING UP LOOKUP TABLE: redisReply *actors_ids; actors_ids = redisCommand(c, "SELECT 2"); redisReply *start_actor; redisReply *end_actor; //Get the starting actor char *start_act = argv[1]; start_actor = redisCommand(c,"GET %s", start_act); char *start = start_actor->str; // printf("%s\n", start); // freeReplyObject(start_actor); //Get the ending actor char *end_act = argv[2]; end_actor = redisCommand(c,"GET %s", end_act); char *end = end_actor->str; // printf("%s\n", end); // freeReplyObject(end_actor); /* BREADTH FIRST SEARCH IMPLEMENTATION */ //Switch back to DB0 for breadth-first search redisReply *switch_back; switch_back = redisCommand(c, "SELECT 0"); //initialize LLs and hashtables Node *actors; //used in BFS as the Queue Node *path; //used later for building the path //visited hashtable hashtable_t *hashtable = ht_create( 65536 ); //hashtable of parents to build path later hashtable_t *hashtable_parents = ht_create( 65536 ); //initialize the start node for the actors Queue actors = make_node(start, NULL); char *name = start; //while the start and end are not equal while (strcmp(name, end) != 0) { name = peek(&actors); //check-- has name been visited? char *check = ht_get(hashtable, name); //if name is unvisited if(strcmp(check, "NULL")==0){ //break out if you have reached //finishing condition if (strcmp(name, end) == 0) { break; } //continue BFS //set up the query for the list of actors //in the adjacency list (neighbors) char word_reply[100] = ""; strcat(word_reply, "LRANGE "); strcat(word_reply, name); strcat(word_reply, " 0 -1"); //store the query response reply = redisCommand(c,word_reply); if (reply->type == REDIS_REPLY_ARRAY) { for (k = 0; k < reply->elements; k++) { //push each neighbor onto the queue (as you //do in BFS) push(&actors, reply->element[k]->str); //check if each neighbor is in the hashtable //of parents char *check2 = ht_get(hashtable_parents, reply->element[k]->str); if (strcmp(check2, "NULL") == 0) { //if not, then put that neighbor and it's parent, name //in the hashtable. It is keeping it traceable //by the path later. ht_set(hashtable_parents, reply->element[k]->str, name); } } } //once done, set name to visited //recall that name is just the current node //we are visiting. ht_set(hashtable, name, "visited"); } //traversing graph by popping from actors //then the new current node, name, will be set to //the beginning of actors again at the beginning of the //while loop. pop(&actors); } //Switch to DB2 for actor lookup redisReply *switch_actor_name; switch_actor_name = redisCommand(c, "SELECT 1"); //GET FIRST ACTOR NAME redisReply *first_actor_name; first_actor_name = redisCommand(c,"GET %s", end); char *first_parent_name = first_actor_name->str; Node *final_path = make_node(first_parent_name, NULL); /* BUILDING UP THE PATH FROM THE PARENT HASHTABLE */ //Gets first parent to make linked list //To trace back and print the path //parent is an id in hashtable_parents char *parent = ht_get(hashtable_parents, end); //Use parent hashtable to build up path //all the way up to but not including the final parent while (strcmp(parent, start) != 0) { //Need to store in redis reply after looking //up the actual name from id stored in parent redisReply *actor_name; //Get the actor name actor_name = redisCommand(c,"GET %s", parent); char *parent_name = actor_name->str; //push parent name to build up final path push(&final_path, parent_name); //get new parent parent = ht_get(hashtable_parents, parent); } //Get the final parent, the last name that needs //to get pushed onto the path redisReply *actor_name; actor_name = redisCommand(c,"GET %s", parent); char *parent_name = actor_name->str; push(&final_path, parent_name); reverse(&final_path); print_list(final_path); /* Disconnects and frees the context */ redisFree(c); return 0; }
static int net_dis_process_inbound_update_msgl (__sock pso, __do_updex updex) { int ret = 0; mutex_lock (&di_base.msg_log.mutex); mutex_lock (&di_base.msg_log.links.mutex); if (di_base.msg_log.links.offset >= DIS_RMSGL_MAX) { if (ht_remove (di_base.msg_log.ht, (unsigned char*) di_base.msg_log.links.first->ptr, sizeof(_pid_sha1))) { print_str ( "ERROR: net_dis_process_inbound_update_msgl: [%d] could not clean hashtable\n", pso->sock); abort (); } free (di_base.msg_log.links.first->ptr); md_unlink_le (&di_base.msg_log.links, di_base.msg_log.links.first); print_str ( "D6: net_dis_process_inbound_update_msgl: removed oldest update\n"); } void * h_dig = ht_get (di_base.msg_log.ht, (unsigned char *) &updex->digest, sizeof(updex->digest)); char buffer[128]; if ( NULL != h_dig) { print_str ( "WARNING: net_dis_process_inbound_update_msgl: [%d] [%s] duplicate update recieved\n", pso->sock, bb_to_ascii (updex->digest.data, sizeof(updex->digest.data), buffer)); ret = -2; goto exit; } __pid_sha1 dc = malloc (sizeof(_pid_sha1)); *dc = updex->digest; char d = 1; ht_set (di_base.msg_log.ht, dc->data, sizeof(_pid_sha1), &d, 1); md_alloc_le (&di_base.msg_log.links, 0, 0, dc); print_str ( "D3: net_dis_process_inbound_update_msgl: [%d] [%s] cached update\n", pso->sock, bb_to_ascii (dc->data, sizeof(_pid_sha1), buffer)); exit: ; pthread_mutex_unlock (&di_base.msg_log.links.mutex); pthread_mutex_unlock (&di_base.msg_log.mutex); return ret; }
void edge_add_attr(edge_t* e, char* key, char* val){ ht_set(e->attr, key, val); }
void pre_populate_symboltable(){ symboltable = ht_create( 10000 ); ht_set( symboltable, "short", "0"); ht_set( symboltable, "sizeofint", "0"); ht_set( symboltable, "float", "0"); ht_set( symboltable, "double", "0"); ht_set( symboltable, "bool", "0"); ht_set( symboltable, "char", "0"); ht_set( symboltable, "signed", "0"); ht_set( symboltable, "unsigned", "0"); ht_set( symboltable, "for", "0"); ht_set( symboltable, "while", "0"); ht_set( symboltable, "do", "0"); ht_set( symboltable, "return", "0"); ht_set( symboltable, "structconst", "0"); ht_set( symboltable, "void", "0"); ht_set( symboltable, "switch", "0"); ht_set( symboltable, "break", "0"); ht_set( symboltable, "case", "0"); ht_set( symboltable, "continue", "0"); ht_set( symboltable, "goto", "0"); ht_set( symboltable, "long", "0"); ht_set( symboltable, "static", "0"); ht_set( symboltable, "union", "0"); ht_set( symboltable, "default", "0"); }
int d_update_file (char *path, __do pool, uint8_t flags, __do_xfd fdata, __ipr hosts_in, size_t ipr_count, uint32_t ufe_flags, _d_ufe upd_call) { mda path_b = { 0 }; md_init_le (&path_b, 256); int nd = split_string_l_le (path, 0x2F, &path_b, (size_t) path_b.count); if (nd < 1) { md_g_free_l (&path_b); return 1; } int r = 2; mutex_lock (&di_base.nd_pool.mutex); p_md_obj ptr = path_b.first; int depth = 0; while (ptr) { char *p = (char*) ptr->ptr; depth++; __do pool_p = pool; pool = (__do) ht_get((hashtable_t*)pool->d, (unsigned char*) p, strlen(p) + 1); if ( NULL == pool) { if (ufe_flags & F_DUUF_UPDATE_DESTROY) { print_str ("D2: d_update_file: no such path exists: %s\n", path); break; } pool = calloc (1, sizeof(_do)); int l; if ( NULL == ptr->next) { l = 1; } else { pool->flags |= F_DO_DIR; l = 0; } if (nd_pool_entry_set (pool, p, l)) { print_str ( "ERROR: d_update_file: nd_pool_entry_set failed (out of memory)\n"); abort (); } ht_set ((hashtable_t*) pool_p->d, (unsigned char*) p, strlen (p) + 1, (void*) pool, sizeof(_do)); print_str ("DEBUG: d_update_file: created pool: %s\n", p); } if (NULL == ptr->next) { //mutex_lock (&pool->mutex); //print_str ("DEBUG: d_import_entry: hit last: %s\n", p); pool->flags |= flags; int i, r_t; for (i = 0, r = 127; i < ipr_count; i++) { if ((r_t = upd_call (p, pool, fdata, &hosts_in[i], ufe_flags))) { if (r_t != 127) { r = r_t; break; } if (r_t == 71) { r = 0; break; } } else { r = 0; } } //pthread_mutex_unlock (&pool->mutex); break; } ptr = ptr->next; } pthread_mutex_unlock (&di_base.nd_pool.mutex); md_g_free_l (&path_b); return r; }