int main() { Tree* tree; printf("välkommen till skagettikåd\n\n"); printf("No.items, Sekvential, BinaryST, HashTable\n"); //loop for (int numberOfitems = 50000; numberOfitems <= 5000000; numberOfitems = numberOfitems * 10) { int* arr = (int*)calloc(numberOfitems, sizeof(int)); FILE* fp; fp = fopen("5 000 000 unika slumpade tal.txt", "r"); for (int i = 0; i < numberOfitems; i++) { fscanf(fp, "%d", &arr[i]); } fclose(fp); //stuff makeHashTable(arr, numberOfitems); tree = makeBST(arr, numberOfitems); //time variables double timeSekventiall = 0; double timeBST = 0; double timeHash = 0; //Genomför sköningen 100 gånger for (int i = 0; i < 100; i++) { int searchfor = 1; timeSekventiall += sekventiell(arr, searchfor, numberOfitems); timeBST += searchBST(tree, searchfor, numberOfitems); timeHash += searchHashTable(arr, searchfor, numberOfitems); } //dela på 100 för att få genomsnitt timeSekventiall = timeSekventiall / 100; timeBST = timeBST / 100; timeHash = timeHash / 100; //Print it! printf("%8d%10Lf%12Lf%10Lf\n", numberOfitems, timeSekventiall, timeBST, timeHash); //free free(arr); emptyTree(tree, tree->root); } system("pause"); return 0; }
int getValFromHashTable(char *key) { int value = 0; HashTable *search = searchHashTable(key); if (search != NULL) { value = search->value; } else { printf("Cannnot find!! %s\n", key); } return value; }
void insertToAnotherTable(HashTable* hashtableFrom,HashTable* hashtableTo){ int i; for (i=0; i<HASHSIZE; i++) { hashNode* hashcontent= hashtableFrom->content[i]; while(hashcontent){ hashNode* n =searchHashTable(hashtableTo, hashcontent->key); if(n) n->value=n->value+hashcontent->value; else insertHashTable(hashtableTo, hashcontent->key, hashcontent->value); hashcontent=hashcontent->next; } } }
void setToHashTable(char *key, int value) { HashTable *h_table = NULL; HashTable *search_end = searchHashTable(key); if (search_end == NULL) { h_table = new_HashTable(); h_table->key = (char *)gmalloc(strlen(key) + 1); sprintf(h_table->key, "%s", key); h_table->value = value; hash_table->next = h_table; hash_table = hash_table->next; } else { search_end->value = value; } }
void g_readFile(char* filename, HashTable* hashtable){ FILE* fb; fb=fopen(filename,"r"); while (1) { char tag[100]; int ret = fscanf(fb,"%s",tag); if(ret == EOF) break; int i; for (i=0; i<strlen(tag); i++) { if(!(tag[i]>='a'&&tag[i]<='z')&&!(tag[i]>='A'&&tag[i]<='Z')) tag[i]=';'; } char* ch=tag; char* val; while ((val=strsep(&ch,";"))) { if(strlen(val)==0) continue; hashNode* n = searchHashTable(hashtable, val); if (n) n->value=n->value+1; else insertHashTable(hashtable, val, 1); } } fclose(fb); }
int insertHashTable(HashTable* hashtable,char* key,int value){ unsigned int index = hashCode(key)%HASHSIZE; hashNode* newNode = (hashNode*)malloc(sizeof(hashNode)); newNode->key = (char*)malloc(sizeof(char) * (strlen(key)+1)); memcpy(newNode->key,key,strlen(key)+1); newNode->key[strlen(key)]='\0'; newNode->value = value; hashNode* n=searchHashTable(hashtable, key); if (n){ n->value = value; return 0; } else{ newNode->next = hashtable->content[index]; hashtable->content[index] = newNode; hashtable->size=hashtable->size+1; nodeIndex* insertIndex=(nodeIndex*)malloc(sizeof(nodeIndex)); insertIndex->whichNode=newNode; insertIndex->next=NULL; nodeIndex* p=hashtable->indexHead; if(!p) hashtable->indexHead=insertIndex; else if(compareString(p->whichNode->key, key)==1){ hashtable->indexHead=insertIndex; insertIndex->next=p; } else{ while (p) { if(!p->next||(compareString(p->whichNode->key, key)==-1&&compareString(p->next->whichNode->key, key)==1)){ nodeIndex* mid = p->next; p->next=insertIndex; insertIndex->next=mid; break; } p=p->next; } } return 1; } }