void insert(string s){ int index = hashFunc1(s); int indexH = hashFunc2(s); while(hashTable[index] != "") index = (index + indexH)%hashTableSize; hashTable[index] = s; }
void search(string s){ int index = hashFunc1(s); int indexH = hashFunc2(s); while(hashTable[index] != s && hashTable[index] != "") index = (index + indexH)%hashTableSize; if(hashTable[index] == s) return true; return false; }
// find an empty page frame int findPageFrame(int pageNum, BM_HashTable *hTable){ // (assumes table not full) int counter = 0, i; int size = hTable->arraySize; int hashVal = hashFunc1(pageNum, size); // hash the key int stepSize = 1; // get step size // int stepSize = hashFunc2(pageNum); // get step size // until empty cell with value -1 while((counter < hTable->arraySize) && (getKey(hTable->hashArray[hashVal]) != -1)) { hashVal += stepSize; // add the step hashVal %= hTable->arraySize; // for wraparound counter++; } return hashVal; }
// find item with key int find(int key, BM_HashTable *hTable){ int counter = 0, i; // (assumes table not full) int size = hTable->arraySize; int hashVal = hashFunc1(key, size); // hash the key int stepSize = 1; // get step size for Linear probing way of hashing // int stepSize = hashFunc2(key); // get step size for separate chaining way of hashing // while((counter < hTable->arraySize) && (getKey(hTable->hashArray[hashVal]) != -1)) // until empty cell, while((counter < hTable->arraySize) && (hTable->hashArray[hashVal]->pageHandle->pageNum != -1)) { // is correct hashVal? if(getKey(hTable->hashArray[hashVal]) == key) { //return hTable.hashArray[hashVal]; // yes, return item return hashVal; } counter ++; hashVal += stepSize; // add the step hashVal %= hTable->arraySize; // for wraparound } return -1; // can’t find item }
int main() { hashTable hash1; hashTable hash2; hashTable hash3; hashTable hash4; hashTable hash5; hashTableConstructor(&hash1); hashTableConstructor(&hash2); hashTableConstructor(&hash3); hashTableConstructor(&hash4); hashTableConstructor(&hash5); char* book = NULL; long bookLen = -1; const char* filename = "book.txt"; read_file(filename, &book, &bookLen); char* stringsInBook = NULL; int quantityString = -1; stringsInBook = searchAllStr(book, bookLen, &quantityString); for(long i = 0; i < quantityString; i++ ) { addNewInHash(hashFunc1(&stringsInBook[i], &hash1), &stringsInBook[i], &hash1); addNewInHash(hashFunc2(&stringsInBook[i], &hash2), &stringsInBook[i], &hash2); addNewInHash(hashFunc3(&stringsInBook[i], &hash3), &stringsInBook[i], &hash3); addNewInHash(hashFunc4(&stringsInBook[i], &hash4), &stringsInBook[i], &hash4); addNewInHash(hashFunc5(&stringsInBook[i], &hash5), &stringsInBook[i], &hash5); } //hashDump(&hash5); const char* outputfile = "exel.csv"; FILE* output = fopen(outputfile, "wr"); for(long i = 0; i < hash1.quantityElements; i++) { fprintf(output, "%d,", hash1.table[i].quantityElements); } fprintf(output, "\n"); for(long i = 0; i < hash2.quantityElements; i++) { fprintf(output, "%d,", hash2.table[i].quantityElements); } fprintf(output, "\n"); for(long i = 0; i < hash3.quantityElements; i++) { fprintf(output, "%d,", hash3.table[i].quantityElements); } fprintf(output, "\n"); for(long i = 0; i < hash4.quantityElements; i++) { fprintf(output, "%d,", hash4.table[i].quantityElements); } fprintf(output, "\n"); for(long i = 0; i < hash5.quantityElements; i++) { fprintf(output, "%d,", hash5.table[i].quantityElements); } fclose(output); hashDump(&hash5); hashTableDestruktor(&hash1); hashTableDestruktor(&hash2); hashTableDestruktor(&hash3); hashTableDestruktor(&hash4); hashTableDestruktor(&hash5); return 0; }