int main(void) { hashTable ht; InitHashTable(&ht, 100, print, equal, hashFun); int a1 =2; int a2 =4; InsertHashTable(ht, &a1); //print(SearchHashTable(ht, &a1)); int *r =(int*)SearchHashTable(ht, &a1); assert( *r == 2 ); r = SearchHashTable(ht, &a2); assert( r == NULL ); InsertHashTable(ht, &a2); r = SearchHashTable(ht, &a2); assert( *r == 4); DestroyHashTable(&ht); printf("hashTable OK\n"); return 0; }
/* Read network from the training file */ void ReadData() { FILE *fin; char name_v1[MAX_STRING], name_v2[MAX_STRING], str[2 * MAX_STRING + 10000]; int vid; double weight; fin = fopen(network_file, "rb"); if (fin == NULL) { printf("ERROR: network file not found!\n"); exit(1); } num_edges = 0; while (fgets(str, sizeof(str), fin)) num_edges++; fclose(fin); printf("Number of edges: %lld \n", num_edges); edge_source_id = (int *)malloc(num_edges*sizeof(int)); edge_target_id = (int *)malloc(num_edges*sizeof(int)); edge_weight = (double *)malloc(num_edges*sizeof(double)); if (edge_source_id == NULL || edge_target_id == NULL || edge_weight == NULL) { printf("Error: memory allocation failed!\n"); exit(1); } fin = fopen(network_file, "rb"); num_vertices = 0; for (int k = 0; k != num_edges; k++) { fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight); if (k % 10000 == 0) { printf("Reading edges: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13); fflush(stdout); } vid = SearchHashTable(name_v1); if (vid == -1) vid = AddVertex(name_v1); vertex[vid].degree += weight; edge_source_id[k] = vid; vid = SearchHashTable(name_v2); if (vid == -1) vid = AddVertex(name_v2); vertex[vid].degree += weight; edge_target_id[k] = vid; edge_weight[k] = weight; } fclose(fin); printf("Number of vertices: %d \n", num_vertices); }
void ReadVector() { char ch, name[MAX_STRING]; real f_num; long long l; FILE *fi = fopen(vector_file1, "rb"); if (fi == NULL) { printf("Vector file 1 not found\n"); exit(1); } fscanf(fi, "%lld %lld", &num_vertices, &vector_dim1); vertex = (struct ClassVertex *)calloc(num_vertices, sizeof(struct ClassVertex)); vec1 = (real *)calloc(num_vertices * vector_dim1, sizeof(real)); for (long long k = 0; k != num_vertices; k++) { fscanf(fi, "%s", name); ch = fgetc(fi); AddVertex(name, k); l = k * vector_dim1; for (int c = 0; c != vector_dim1; c++) { fread(&f_num, sizeof(real), 1, fi); vec1[c + l] = (real)f_num; } } fclose(fi); fi = fopen(vector_file2, "rb"); if (fi == NULL) { printf("Vector file 2 not found\n"); exit(1); } fscanf(fi, "%lld %lld", &l, &vector_dim2); vec2 = (real *)calloc((num_vertices + 1) * vector_dim2, sizeof(real)); for (long long k = 0; k != num_vertices; k++) { fscanf(fi, "%s", name); ch = fgetc(fi); int i = SearchHashTable(name); if (i == -1) l = num_vertices * vector_dim2; else l = i * vector_dim2; for (int c = 0; c != vector_dim2; c++) { fread(&f_num, sizeof(float), 1, fi); vec2[c + l] = (real)f_num; } } fclose(fi); printf("Vocab size: %lld\n", num_vertices); printf("Vector size 1: %lld\n", vector_dim1); printf("Vector size 2: %lld\n", vector_dim2); }
int isDeclared(ASTnode* leaf){ SymbolTable *tmp; tmp=topSymbolStack(symbolStack); while(tmp!=NULL){ if(SearchHashTable(tmp, leaf->t.lexeme)!=-1) return 1; else tmp=tmp->parentTable; } if(tmp==NULL){ return 0; } return 1; }
int findType(ASTnode* ID, int isLHS){//tells type of id if(ID==NULL) return -1; SymbolTable *tmp; int z; tmp=topSymbolStack(symbolStack); while(tmp!=NULL){ z=SearchHashTable(tmp,ID->t.lexeme); if(z!=-1) break; else tmp=tmp->parentTable; } if(tmp==NULL){ semanticError(1,ID->t); return -1; } SymbolTableEntryNode *t=findSymbolTableNode(tmp->table[z].next,ID->t.lexeme); if(isLHS) return t->type.id.type;//do not check for initialization switch(t->type.id.type){ case 57: if(t->type.id.initialized==1) return t->type.id.type; else{ semanticError(9,ID->t); return -1; } case 58:if(t->type.id.initialized==1){ if(t->type.id.length[0]==-1&&t->type.id.length[1]==-1){ return t->type.id.type;//size can not be pre-determined } if(firstMatrix){ firstMatrix=0; matrixRow=t->type.id.length[0]; matrixCol=t->type.id.length[1]; return t->type.id.type; } else if(t->type.id.length[0]==matrixRow&&t->type.id.length[1]==matrixCol) return t->type.id.type; else{ semanticError(12,ID->t); return -1; } } else semanticError(11,ID->t); default: return t->type.id.type; } }
SymbolTable* getSymbolTable(ASTnode* ID){ if(ID==NULL) return NULL; SymbolTable *tmp; int z; tmp=topSymbolStack(symbolStack); while(tmp!=NULL){ z=SearchHashTable(tmp,ID->t.lexeme); if(z!=-1) break; else tmp=tmp->parentTable; } if(tmp==NULL){ semanticError(1,ID->t); return NULL; } return tmp; }
SymbolTableEntryNode* getSymbolTableNode(ASTnode* ID){ if(ID==NULL) return -1; SymbolTable *tmp; int z; tmp=topSymbolStack(symbolStack); while(tmp!=NULL){ z=SearchHashTable(tmp,ID->t.lexeme); if(z!=-1) break; else tmp=tmp->parentTable; } if(tmp==NULL){ semanticError(1,ID->t); return -1; } SymbolTableEntryNode *t=findSymbolTableNode(tmp->table[z].next,ID->t.lexeme); return t; }
/* Read network from the training file */ void ReadData() { FILE *fin; char name_v1[MAX_STRING], name_v2[MAX_STRING], str[2 * MAX_STRING + 10000]; int vid, u, v; double weight; Neighbor nb; fin = fopen(train_file, "rb"); if (fin == NULL) { printf("ERROR: network file not found!\n"); exit(1); } num_edges = 0; while (fgets(str, sizeof(str), fin)) num_edges++; fclose(fin); printf("Number of edges: %lld \n", num_edges); fin = fopen(train_file, "rb"); num_vertices = 0; for (int k = 0; k != num_edges; k++) { fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight); if (k % 10000 == 0) { printf("Reading edges: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13); fflush(stdout); } vid = SearchHashTable(name_v1); if (vid == -1) vid = AddVertex(name_v1); vertex[vid].degree += weight; vid = SearchHashTable(name_v2); if (vid == -1) vid = AddVertex(name_v2); vertex[vid].degree += weight; } fclose(fin); printf("Number of vertices: %d \n", num_vertices); neighbor = new std::vector<Neighbor>[num_vertices]; rank_list = (Neighbor *)calloc(num_vertices, sizeof(Neighbor)); fin = fopen(train_file, "rb"); for (long long k = 0; k != num_edges; k++) { fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight); if (k % 10000 == 0) { printf("Reading neighbors: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13); fflush(stdout); } u = SearchHashTable(name_v1); v = SearchHashTable(name_v2); nb.vid = v; nb.weight = weight; neighbor[u].push_back(nb); } fclose(fin); printf("\n"); for (int k = 0; k != num_vertices; k++) { vertex[k].sum_weight = 0; int len = neighbor[k].size(); for (int i = 0; i != len; i++) vertex[k].sum_weight += neighbor[k][i].weight; } }
SymbolTableEntryNode* outputParameterInitCheck(SymbolTable* h,char* lexeme){ int z=SearchHashTable(h,lexeme); if(z==-1) return NULL;//should never be null anyway SymbolTableEntryNode *t=findSymbolTableNode(h->table[z].next,lexeme); return t; }
void semantics(ASTnode* ASTroot){ firstMatrix=1; if(ASTroot==NULL){ return; } int z,noTraverse=0; ASTnode *rows,*l; token bufToken; SymbolTable *tmp; SymbolTableEntryNode *t; int p=0; if(sflag==0){ //first time, main function so create a symbol table for main function sflag=1; S[0]=createSymbolTable(size, NULL, "MAIN");//parentTable of Main is NULL symbolStack=createSymbolStack(); pushSymbolStack(S[0],symbolStack); p=1; counter++; } switch(ASTroot->label){ //Symbol Table creates only in 1, 61, 64 //Symbol table populates in 1:functionDef,31:declarationStmt case 1://make a new symbol table, this would be the scope unless an END is encountered, functionDef InsertSymbolTable(topSymbolStack(symbolStack), ASTroot); S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),ASTroot->array[1]->t.lexeme); pushSymbolStack(S[counter],symbolStack); InsertSymbolTableFun(topSymbolStack(symbolStack), ASTroot);//for input and output arguments of function p=1; counter++; break; case 2://ifstmt S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),"IF"); pushSymbolStack(S[counter],symbolStack); p=1; counter++; break; case 3: noTraverse=1; if(strcmp(topSymbolStack(symbolStack)->symbolTableName,ASTroot->array[0]->t.lexeme)==0){//checking for Recursion semanticError(3,ASTroot->array[0]->t); return; } //check for input parameter list and function signature- input and output tmp=topSymbolStack(symbolStack); while(tmp!=NULL){ z=SearchHashTable(tmp, ASTroot->array[0]->t.lexeme); if(z!=-1) break; else tmp=tmp->parentTable; } if(tmp==NULL){ semanticError(1,ASTroot->t); break; }//declaration of FunId is checked here itself t=findSymbolTableNode(tmp->table[z].next,ASTroot->array[0]->t.lexeme); if(t->type.fid.outputSize!=typeCounter){ semanticError(5,ASTroot->array[0]->t); break; } else{ for(z=0; z<=t->type.fid.outputSize; z++){ if(t->type.fid.output[z]!=type[z]){ semanticError(5,ASTroot->array[0]->t); noTraverse=1; break;//from for } } typeCounter=-1;//successfully implemented. } l=ASTroot->array[1]; for(z=0; z<=t->type.fid.inputSize; z++){ if(l==NULL){ semanticError(5,ASTroot->array[0]->t);//number of output parameters noTraverse=1; break; } if(t->type.fid.input[z]!=findTypeVar(l->array[0])){ semanticError(14,ASTroot->array[0]->t);//type Mismatch noTraverse=1; break; } l=l->array[1]; } break; case 11://else stmt S[counter]=createSymbolTable(size, topSymbolStack(symbolStack),"ELSE"); pushSymbolStack(S[counter],symbolStack); p=1; counter++; break; case 26:if(ASTroot->array[0]->label==67){ t=getSymbolTableNode(ASTroot->array[1]); t->type.id.initialized=1; } case 27: break; case 28: break; //it should not come case 29: break; case 30: break; case 31://declaration stmt InsertSymbolTable(topSymbolStack(symbolStack), ASTroot); return; break; case 51://Assignment noTraverse=1; typeCounter=-1; if(ASTroot->array[1]->label==3){//function call statement if(ASTroot->array[0]->label==54){//single list if(outputCheck1(ASTroot->array[0])==0)//send leaf directly return; //1- it should already have been declared, 2-if so, then it's type should be recorded } else{ //send l if(outputCheck(ASTroot->array[0])==0) return; } semantics(ASTroot->array[1]); } if(ASTroot->array[1]->label==60){//size stmt //1- check if ID is declared, 2- What is the type of ID, 3- Compare with the return type if(!isDeclared(ASTroot->array[1]->array[0])){ semanticError(1,ASTroot->array[1]->array[0]->t); return; } z=findType(ASTroot->array[1]->array[0],0); if(z==57){ if(outputCheck(ASTroot->array[0])==0){//it will populate type of LHS if declared, else returns 0 return; } else{//declared if(!(typeCounter==0&&type[0]==57)) semanticError(6,ASTroot->array[0]->t); return; } } else if(z==58){ if(outputCheck(ASTroot->array[0])==0){//it will populate type of LHS if declared, else returns 0 return; } else{//declared if(!(typeCounter==1&&type[0]==55&&type[1]==55)) semanticError(6,ASTroot->array[0]->t); return; } } else { semanticError(8,ASTroot->array[1]->array[0]->t);//Size contains other that String and Matrix } } if(ASTroot->array[1]->label==37){//Arithmetic Expression l=ASTroot->array[0]; z=findType(l,1); if(l->label==54){ if(z==57){ if(ASTroot->array[1]->array[1]==NULL){ if(ASTroot->array[1]->array[0]->array[1]==NULL){ if(findTypeVar(ASTroot->array[1]->array[0]->array[0])==57){//initialization StringInit(ASTroot->array[0],ASTroot->array[1]->array[0]->array[0]->t.lexeme); return; } } } } else if(z==58){//lhs is matrix firstMatrix=1; if(ASTroot->array[1]->array[1]==NULL){ if(ASTroot->array[1]->array[0]->array[1]==NULL){ if((ASTroot->array[1]->array[0]->array[0]->label)==44){//initialization MatrixInit(ASTroot->array[0],ASTroot->array[1]->array[0]->array[0]); return; } } } } } if(z==-1) break; typeCounter++; type[typeCounter]=z; if((z=findTypeAE(ASTroot->array[1]))!=type[typeCounter]){ bufToken.lineNumber=l->t.lineNumber; semanticError(10,bufToken); break; } //valid Arithmetic expression //debug(); t= getSymbolTableNode(ASTroot->array[0]); t->type.id.initialized=1; typeCounter=-1; } break; case 52://go to case 54 case 54: if(!isDeclared(ASTroot)) semanticError(1,ASTroot->t); break; case 75:// AND case 76:// OR case 77:// LT case 78:// LE case 79:// EQ case 80:// GT case 81:// GE case 82:// NE case 83:// NOTbooleanExpressionSemantics(ASTnode* BE) noTraverse=1; if(booleanExpressionSemantics(ASTroot)==0){ semanticError(10,bufToken); } default: break; }//end of switch int i; if(noTraverse==0){ for( i=0; i<ASTroot->arraySize; i++){ if(ASTroot->array[i]!=NULL){ semantics(ASTroot->array[i]); } } } if(p){ //if popping SymbolTable is a function, then check if it's output parameter are accurately initialised or not tmp=popSymbolStack(symbolStack); if(strcmp(tmp->symbolTableName,"MAIN")!=0&&strcmp(tmp->symbolTableName,"IF")!=0&&strcmp(tmp->symbolTableName,"ELSE")!=0){//it is a function int i; for(i=0; i<tmp->outputParameter; i++){ t=outputParameterInitCheck(tmp,tmp->outputParameterLexeme[i]); if(t->type.id.initialized!=1) { semanticError(13,ASTroot->array[1]->t); break; } } } } }//end of function
void RateItemsCosine(value* lvalue, value** nn, int size, List Rresult){ int i,j,k,pos; double R; int *x = lvalue->content; hashTable rated; int hashSize = co.users[*x].NoOfItems*size; int** array = malloc(co.users[*x].NoOfItems*sizeof(int*)); Rating rating; InitHashTable(&rated,hashSize,sizeof(int),NULL, distanceCos,IntHashFuncCos,NULL); //InitList(Rresult,sizeof(int),NULL,distance,NULL); for(i=0; i<co.users[*x].NoOfItems; ++i){ InsertHashTable(rated, &co.users[*x].myitems[i].id,NULL); } double z = 0.0; for ( i = 0; i < size; ++i) { //z+=abs((-data.distance(lvalue,nn[i])-1)); double tmp = (-data.distance(lvalue,nn[i])-1); z+= ( tmp <0 ? -tmp : tmp); } z = 1/z; double Ru = 0.0; for (k = 0; k < co.users[*x].NoOfItems; ++k) { Ru+=co.users[*x].myitems[k].score; } Ru/=co.users[*x].NoOfItems; for(i=0; i<size; ++i){ int* curr = nn[i]->content; for(j=0; j<co.users[*curr].NoOfItems; ++j ){ if(SearchHashTable(rated, &co.users[*curr].myitems[j].id)!=NULL){ continue; } InsertHashTable(rated, &co.users[*curr].myitems[j].id,NULL); double sum = 0.0; int avg = 0; for (k = 0; k < co.users[*curr].NoOfItems; ++k) { avg+=co.users[*curr].myitems[k].score; } avg/=co.users[*curr].NoOfItems; for(k=0; k<size; ++k){ int l; int *index = nn[k]->content; // for(l=0; l<co.users[*index].NoOfItems; l++){ // printf("%d ",co.users[*index].myitems[l].id ); // } // printf("\n"); pos=BinarySearchCosine(co.users[*index].myitems,co.users[*index].NoOfItems,co.users[*curr].myitems[j].id); //printf("%d %d\n", co.users[*curr].myitems[j].id,pos); if(pos == -1) continue; sum+= (-data.distance(lvalue,nn[k])+1)*(co.users[k].myitems[pos].score-avg); //printf("dist %f score %d \n", (-data.distance(lvalue,nn[k])+1),co.users[k].myitems[pos].score); } R = Ru+z*sum; rating.rate = R; rating.id = co.users[*curr].myitems[j].id; InsertValueList(Rresult, &rating); //printf("%f , %d\n",R, co.users[*curr].myitems[j].id); } } }
/* Check Insert */ void Check_thenInsert(int *hdTable, int key){ int *search_result = SearchHashTable(hdTable, key); if(search_result == NULL){ InsertNodeHashTable(hdTable, key); } }