Node *BubbleSort(Node *pNode){ int count = SizeList(pNode);//用来控制次数 Node *pMove; pMove = pNode->next; while (count > 1) { while (pMove->next != NULL) { if (pMove->element > pMove->next->element) { int temp; temp = pMove->element; pMove->element = pMove->next->element; pMove->next->element = temp; } pMove = pMove->next; } count--; pMove = pNode->next; } printf("%s函数执行,带头结点的单链表冒泡排序成功\n",__FUNCTION__); PrintList(pNode); return pNode; }
int PNNRange(LSH* lsh, value query, int P,value* array) { int i=0; int hashSize=0; hashTable neighbors; double* distArr=NULL; for (i = 0; i < lsh->L; i++) { //gets bucket where the value is List bucketList = GetListHashTable(lsh->tables[i].tables, &query, lsh->tables[i].GConfiguration); hashSize+=SizeList(bucketList); } InitHashTable(&neighbors,hashSize,sizeof(value),NULL, data.distance,hashFunc,NULL); for (i = 0; i < lsh->L; i++) { //gets bucket where the value is List bucketList = GetListHashTable(lsh->tables[i].tables, &query, lsh->tables[i].GConfiguration); value* lValue = GetFirst(bucketList); //get all neighbours in range an place them in a list while (lValue != NULL) { InsertHashTable(neighbors, lValue,NULL); lValue = GetNext(bucketList); } } value** hashArr=malloc( GetHashSize(neighbors) * sizeof(value*)); // WARNING // HashToArr(neighbors,(void**)hashArr); distArr=malloc(GetHashSize(neighbors)*sizeof(double)); for(i=0; i<GetHashSize(neighbors); i++) { distArr[i]=data.distance(hashArr[i],&query); } QuickSortValue(distArr, hashArr, GetHashSize(neighbors)); for(i=0; (i<P && i<GetHashSize(neighbors)-1 ); i++) { array[i]=*hashArr[i+1]; } DestroyHashTable(&neighbors); free(hashArr); free(distArr); return i; }
void ReadDataEuclidean(FILE* inputFile, int k) { size_t lineSize = 0; size_t lineLen = 0; value* temp = NULL; char* lineBuff = NULL; char* token=NULL; char* secBuff= NULL; char delims[3]="\t "; int i = 0; int j=0; euc = malloc(sizeof(dataEuclidean)); euc->k = k; euc->dim=0; lineLen = getline(&lineBuff, &lineSize, inputFile); // lineLen = getline(&lineBuff, &lineSize, inputFile); secBuff=malloc((strlen(lineBuff)+1)* sizeof(char)); memcpy(secBuff,lineBuff,strlen(lineBuff)); secBuff[strlen(lineBuff)]='\0'; InitList(&(euc->data), sizeof(value), data.print, data.distance, data.destroyValue); token = strtok(secBuff, delims); while (token != NULL) { token = strtok(NULL, delims); if (token == NULL) break; euc->dim++; } do{ temp = malloc(sizeof(value)); token = strtok(lineBuff, delims); temp->name = malloc((strlen(token) + 1) * sizeof(char)); strcpy(temp->name, token); temp->content = malloc((euc->dim)*sizeof(double)); temp->extra = NULL; //printf("ID: %s Value:", temp->name); if (token == NULL) break; for (j = 0; j < euc->dim; j++) { token = strtok(NULL, delims); double* content=temp->content; content[j] = atof(token); //printf("%f ", content[i]); } InsertValueList((euc->data), temp); free(temp); i++; //printf("\n"); } while ((lineLen = getline(&lineBuff, &lineSize, inputFile)) != -1); data.tableSize = SizeList(euc->data); free(lineBuff); free(secBuff); }
void ReadDataEuclidean(value** points, FILE* inputFile, int k) { List tempList; size_t lineSize = 0; size_t lineLen = 0; value* temp = NULL; char* lineBuff = NULL; char* token=NULL; char* secBuff= NULL; char delims[4]="\t \n"; int i = 0; int j=0; euc = malloc(sizeof(dataEuclidean)); euc->k = k; euc->dim=0; lineLen = getline(&lineBuff, &lineSize, inputFile); secBuff=malloc((strlen(lineBuff)+1)* sizeof(char)); memcpy(secBuff,lineBuff,strlen(lineBuff)); secBuff[strlen(lineBuff)]='\0'; InitList(&tempList, sizeof(value), data.print, data.distance, NULL); token = strtok(secBuff, delims); while (token != NULL) { token = strtok(NULL, delims); if (token == NULL) break; euc->dim++; } do{ temp = malloc(sizeof(value)); token = strtok(lineBuff, delims); if (token == NULL) break; temp->name = malloc((strlen(token) + 1) * sizeof(char)); strcpy(temp->name, token); temp->content = malloc((euc->dim)*sizeof(double)); // temp->extra = NULL; //printf("ID: %s Value:", temp->name); for (j = 0; j < euc->dim; j++) { token = strtok(NULL, delims); double* content=temp->content; content[j] = atof(token); //printf("%f ", content[i]); } InsertValueList(tempList, temp); free(temp); i++; //printf("\n"); } while ((lineLen = getline(&lineBuff, &lineSize, inputFile)) != -1); SetDataSize(i); *points = malloc(i*sizeof(value)); //value* newData = *points; temp = GetFirst(tempList); for(i = 0; i< GetDataSize(); i++){ //(*points)[i].name = malloc((strlen(temp->name)+1)*sizeof(char)); //strcpy((*points)[i].name,temp->name); (*points)[i].name = temp->name; //(*points)[i].content = malloc((euc->dim)*sizeof(double)); (*points)[i].content = temp->content; //memcpy(data[i].content, temp->content, sizeof(bitStr)); temp = GetNext(tempList); } data.tableSize = SizeList(tempList)/8; DestroyList(&tempList); //data.tableSize = 10; free(lineBuff); free(secBuff); }
void ReadDataCosine(value** val,FILE* input, int k) { List tempList; co.k = k; size_t lineSize = 0; size_t lineLen = 0; char* lineBuff = NULL; char* token=NULL; char delims[4]="\t \n"; int nn; InitList(&tempList, sizeof(List*), NULL, NULL, NULL); lineLen = getline(&lineBuff, &lineSize, input); if (sscanf(lineBuff, "%d", &nn) < 2) { //perror("Malformed file - Exiting...\n"); //exit(-1); nn = 20; } else{ lineLen = getline(&lineBuff, &lineSize, input); } data.Pnn = nn; //secBuff=malloc((strlen(lineBuff)+1)* sizeof(char)); //memcpy(secBuff,lineBuff,strlen(lineBuff)); //secBuff[strlen(lineBuff)]='\0'; int startUser; int prevUser = -1; int maxDim = -1; do{ List tempList2; //temp = malloc(sizeof(value)); token = strtok(lineBuff, delims); if (token == NULL) break; int currUser = atoi(token); if( prevUser == -1) startUser = currUser; if(currUser != prevUser ){ prevUser = currUser; InitList(&tempList2, sizeof(item*), NULL, NULL, NULL); InsertValueList(tempList, &tempList2); } item *it = malloc(sizeof(item)); token = strtok(NULL, delims); it->id = atoi(token); if(it->id > maxDim ) maxDim = it->id; token = strtok(NULL, delims); it->score = atoi(token); InsertValueList(tempList2, it); free(it); } while ((lineLen = getline(&lineBuff, &lineSize, input)) != -1); co.dim = maxDim; int NoOfUser = SizeList(tempList); co.users = malloc(sizeof(user)* NoOfUser); co.NoOfUser = NoOfUser; int i; List *tmptempList2 = GetFirst(tempList); List tempList2 = *tmptempList2; if( tempList2 == NULL ) printf("NULLLLL\n"); for (i = 0; i < NoOfUser; ++i) { int NoOfItems = SizeList(tempList2); co.users[i].myitems = malloc(sizeof(item)* NoOfItems); co.users[i].id = i + 1; co.users[i].NoOfItems = NoOfItems; int j; item *it = GetFirst(tempList2); for (j = 0; j < NoOfItems; ++j) { co.users[i].myitems[j] = *it; it = GetNext(tempList2); } DestroyList(&tempList2); tmptempList2 = GetNext(tempList); if(tmptempList2 == NULL) break; tempList2 = *tmptempList2; } /**/ SetDataSize(NoOfUser); *val = malloc(NoOfUser*sizeof(value)); for(i = 0; i< GetDataSize(); i++){ //(*val)[i] = malloc(sizeof(value)); (*val)[i].name = NULL; int *content = malloc(sizeof(int)); *content = i; (*val)[i].content = content; } /**/ data.tableSize = (1 << co.k)/2; printf("%lld\n",data.tableSize ); DestroyList(&tempList); //data.tableSize = 10; free(lineBuff); /* for (i = 0; i < co.NoOfUser; ++i) { int j; printf("User %d\n", i); for (j = 0; j < co.users[i].NoOfItems; ++j){ printf("Item: id=%d score=%d\n",co.users[i].myitems[j].id, co.users[i].myitems[j].score ); } //free(co.users[i].myitems); } */ //free(co.users); //free(secBuff); }