//################################################################## // PROGRAMMERINGSUPPGIFT 3 // DATASTRUKTURER OCH ALGORITMER // HASH TABELL //================================================================== // HASH.HPP // Filen innehåller hashfunktionerna // Christian Ohlsson // Ingela Johansson // Anna-Maria Haglund // Karlstad 981007 //================================================================== #include "header.hpp" //################################################################## //Function: hashClass() //Task: constuctorn //Calls: nothing //================================================================== hashClass::hashClass() { prime(); PRIMENUMBER=0; ARRAYSIZE = DEFAULTSIZE; hashPtr = new hashNode[ARRAYSIZE]; for(int i=0; i ARRAYSIZE) location = location - ARRAYSIZE; if(strcmp(hashPtr[location].name, name) == 0) return TRUE; } return FALSE; } //################################################################## //Function: getNode() //Task: hämtar en nod ur tabellen, d.v.s. namn telefonnummer och // "deleted-flaggan" //Calls: hash1() och hash2() //================================================================== hashNode hashClass::getNode(nameType &oneName) { int location = hash1(oneName); if(strcmp(hashPtr[location].name, oneName) == 0) return (hashPtr[location]); else do { location = location + hash2(hash1(oneName)); if(location>ARRAYSIZE) { location = (location-ARRAYSIZE); if(strcmp(hashPtr[location].name, oneName) == 0) return hashPtr[location]; } }while(hashPtr[location].exist == FALSE); } //################################################################## //Function: biggerHash() //Task: kollar om en större tabell behövs (om 80% av positionerna // är upptagna) //Calls: checkOccupied() //================================================================== bool hashClass::biggerHash() { float siz = INSERTFACTOR*getSize(); int occ = checkOccupied(); if(occ>=siz) return TRUE; return FALSE; } //################################################################## //Function: newHashArray() //Task: //Calls: reHash() //================================================================== void hashClass::newHashArray() { int oldSize = ARRAYSIZE; ARRAYSIZE = nextPrime(); reHash(oldSize); } //################################################################## //Function: //Task: //Calls: //================================================================== bool hashClass::anotherHash() { int del=0; float siz = DELETEFACTOR*getSize(); for(int i=0; i=siz) return TRUE; return FALSE; } //################################################################## //Function: //Task: //Calls: //================================================================== void hashClass::reHash(int oldSize) { int location; hashNode *tempPtr = new hashNode[ARRAYSIZE]; for(int j=0; j ARRAYSIZE) location = location - ARRAYSIZE; if(hashPtr[location].exist == FALSE) insert(location, tempPtr[i]); } } delete tempPtr; } //################################################################## //Function: //Task: //Calls: //================================================================== int hashClass::hash1(nameType name) { int sum = 0; for(int i=0 ; i